96 lines
5.7 KiB
Bash
Executable File
96 lines
5.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# The ONLY installation download source is our Git; all dependencies live there.
|
|
set -Eeuo pipefail
|
|
[[ ${EUID:-$(id -u)} -eq 0 ]] || exec sudo -E bash "$0" "$@"
|
|
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
GIT_URL=${IOMATEX_GIT_URL:-https://git.it-screen.com/An-J/Iomatex_OS.git}
|
|
GIT_BRANCH=${IOMATEX_GIT_BRANCH:-ci-cd}
|
|
CHECKOUT_DIR=${IOMATEX_GIT_DIR:-/userdata/Iomatex_OS}
|
|
STAGES=stage_env,stage_wifi,stage_mqtt,stage_ui,stage_avahi,stage_cockpit,stage_itscreen,stage_reverse_tunnels
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--with-z2m) STAGES=stage_env,stage_wifi,stage_mqtt,stage_z2m,stage_ui,stage_avahi,stage_cockpit,stage_itscreen,stage_reverse_tunnels; shift ;;
|
|
--default-only) STAGES=stage_env,stage_mqtt,stage_ui,stage_avahi,stage_cockpit,stage_itscreen; shift ;;
|
|
--only) STAGES=${2:?Missing stages}; shift 2 ;;
|
|
--branch) GIT_BRANCH=${2:?Missing branch}; shift 2 ;;
|
|
--git-url) GIT_URL=${2:?Missing URL}; shift 2 ;;
|
|
--checkout-dir) CHECKOUT_DIR=${2:?Missing directory}; shift 2 ;;
|
|
-h|--help) echo 'sudo bash install_iomatex_edge.sh [--with-z2m] [--only stages] [--branch branch] [--checkout-dir path]'; exit 0 ;;
|
|
*) echo "Unknown argument: $1" >&2; exit 1 ;;
|
|
esac
|
|
done
|
|
[[ "$GIT_URL" == https://git.it-screen.com/* && "$GIT_URL" != *'@'* && "$GIT_URL" != *'?'* && "$GIT_URL" != *'#'* ]] || { echo 'Only HTTPS repositories on git.it-screen.com are supported' >&2; exit 1; }
|
|
[[ "$GIT_BRANCH" =~ ^[A-Za-z0-9._/-]+$ ]] || { echo 'Invalid branch' >&2; exit 1; }
|
|
[[ "$CHECKOUT_DIR" == /* && "$CHECKOUT_DIR" != / && "$CHECKOUT_DIR" != /userdata && "$CHECKOUT_DIR" != /opt ]] || { echo 'Use an absolute checkout subdirectory' >&2; exit 1; }
|
|
command -v python3 >/dev/null || { echo 'Supported vendor image requires Python 3. No bootstrap packages will be downloaded externally.' >&2; exit 1; }
|
|
export IOMATEX_GIT_USERNAME=${IOMATEX_GIT_USERNAME:-}
|
|
export IOMATEX_GIT_PASSWORD=${IOMATEX_GIT_PASSWORD:-}
|
|
if [[ -z "$IOMATEX_GIT_USERNAME" || -z "$IOMATEX_GIT_PASSWORD" ]]; then
|
|
read -rp 'Git username: ' IOMATEX_GIT_USERNAME
|
|
read -rsp 'Git password or token: ' IOMATEX_GIT_PASSWORD; echo
|
|
fi
|
|
[[ -n "$IOMATEX_GIT_USERNAME" && -n "$IOMATEX_GIT_PASSWORD" ]] || exit 1
|
|
mkdir -p "$(dirname "$CHECKOUT_DIR")"
|
|
work=$(mktemp -d "$(dirname "$CHECKOUT_DIR")/.iomatex-bootstrap.XXXXXX")
|
|
trap 'rm -rf "$work"' EXIT
|
|
cat > "$work/askpass" <<'ASKPASS'
|
|
#!/usr/bin/env bash
|
|
case "$1" in *Username*) printf '%s\n' "$IOMATEX_GIT_USERNAME";; *) printf '%s\n' "$IOMATEX_GIT_PASSWORD";; esac
|
|
ASKPASS
|
|
chmod 700 "$work/askpass"
|
|
export GIT_ASKPASS="$work/askpass" GIT_TERMINAL_PROMPT=0
|
|
mkdir -p "$(dirname "$CHECKOUT_DIR")"
|
|
printf 'Fetching installer and bundled dependencies from %s (%s)\n' "$GIT_URL" "$GIT_BRANCH"
|
|
if command -v git >/dev/null; then
|
|
if [[ -d "$CHECKOUT_DIR/.git" ]]; then
|
|
[[ $(git -C "$CHECKOUT_DIR" remote get-url origin) == "$GIT_URL" ]] || { echo 'Checkout origin mismatch' >&2; exit 1; }
|
|
git -c http.followRedirects=false -C "$CHECKOUT_DIR" fetch origin "$GIT_BRANCH"
|
|
git -C "$CHECKOUT_DIR" checkout "$GIT_BRANCH"
|
|
git -C "$CHECKOUT_DIR" merge --ff-only FETCH_HEAD
|
|
else
|
|
git -c http.followRedirects=false clone --depth 1 --branch "$GIT_BRANCH" --single-branch "$GIT_URL" "$work/checkout"
|
|
[[ ! -e "$CHECKOUT_DIR" ]] || mv "$CHECKOUT_DIR" "${CHECKOUT_DIR}.before-$(date +%s)"
|
|
mv "$work/checkout" "$CHECKOUT_DIR"
|
|
fi
|
|
else
|
|
# No apt-get to bootstrap git: fetch the repository archive from the same Git host.
|
|
python3 - "$GIT_URL" "$GIT_BRANCH" "$work" <<'PY'
|
|
import base64, os, pathlib, sys, tarfile, urllib.parse, urllib.request
|
|
url, branch, work = sys.argv[1:]
|
|
class OwnGitOnly(urllib.request.HTTPRedirectHandler):
|
|
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
|
target = urllib.parse.urlparse(newurl)
|
|
if target.scheme != 'https' or target.hostname != 'git.it-screen.com':
|
|
raise RuntimeError('External redirect refused')
|
|
return super().redirect_request(req, fp, code, msg, headers, newurl)
|
|
repo_path = urllib.parse.urlparse(url).path.removesuffix('.git').strip('/')
|
|
url = 'https://git.it-screen.com/api/v1/repos/' + repo_path + '/archive/' + urllib.parse.quote(branch, safe='') + '.tar.gz'
|
|
credentials = (os.environ['IOMATEX_GIT_USERNAME'] + ':' + os.environ['IOMATEX_GIT_PASSWORD']).encode()
|
|
request = urllib.request.Request(url, headers={'Authorization': 'Basic ' + base64.b64encode(credentials).decode()})
|
|
archive = pathlib.Path(work) / 'repository.tar.gz'
|
|
with urllib.request.build_opener(OwnGitOnly).open(request, timeout=600) as response, archive.open('wb') as output:
|
|
import shutil
|
|
shutil.copyfileobj(response, output)
|
|
dest = pathlib.Path(work) / 'checkout'
|
|
dest.mkdir()
|
|
with tarfile.open(archive) as source:
|
|
for item in source.getmembers():
|
|
parts = pathlib.PurePosixPath(item.name).parts
|
|
if len(parts) < 2: continue
|
|
item.name = str(pathlib.PurePosixPath(*parts[1:]))
|
|
if not (item.isfile() or item.isdir()) or not (dest / item.name).resolve().is_relative_to(dest.resolve()):
|
|
raise RuntimeError('Unsafe repository archive member')
|
|
source.extract(item, dest)
|
|
PY
|
|
[[ ! -e "$CHECKOUT_DIR" ]] || mv "$CHECKOUT_DIR" "${CHECKOUT_DIR}.before-$(date +%s)"
|
|
mv "$work/checkout" "$CHECKOUT_DIR"
|
|
fi
|
|
unset IOMATEX_GIT_USERNAME IOMATEX_GIT_PASSWORD GIT_ASKPASS GIT_TERMINAL_PROMPT
|
|
[[ -f "$CHECKOUT_DIR/offline/require-local.sh" ]] || { echo 'Incomplete installer repository' >&2; exit 1; }
|
|
source "$CHECKOUT_DIR/offline/require-local.sh"
|
|
(cd "$CHECKOUT_DIR/stage_itscreen" && sha256sum --check itscreen.sha256)
|
|
rm -rf "$work"
|
|
trap - EXIT
|
|
exec bash "$CHECKOUT_DIR/local_installer.sh" "$CHECKOUT_DIR" --non-interactive --reset-state --only "$STAGES"
|