#!/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.iomatex.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 ;; --with-ip-stream) STAGES=stage_env,stage_wifi,stage_mqtt,stage_ui,stage_avahi,stage_cockpit,stage_itscreen,stage_ip_stream,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] [--with-ip-stream] [--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" == https://git.iomatex.com/* ) && "$GIT_URL" != *'@'* && "$GIT_URL" != *'?'* && "$GIT_URL" != *'#'* ]] || { echo 'Only HTTPS repositories on git.it-screen.com or git.iomatex.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; } # Standalone bootstrap embeds the same lock contract as offline/install-lock.sh. if [[ ${EUID:-$(id -u)} -eq 0 ]]; then if [[ ! ${IOMATEX_INSTALL_LOCK_FD:-} =~ ^[0-9]+$ ]] || [[ $(readlink "/proc/$$/fd/${IOMATEX_INSTALL_LOCK_FD:-999}" 2>/dev/null) != /run/lock/iomatex-installer.lock ]]; then mkdir -p /run/lock exec 9>/run/lock/iomatex-installer.lock flock -n 9 || { echo 'ERROR: another Iomatex installation is running.' >&2; exit 1; } export IOMATEX_INSTALL_LOCK_FD=9 fi fi 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 # Fetch only the shared delivery owner; it downloads a verified lightweight snapshot. work=$(mktemp -d) trap 'rm -rf "$work"' EXIT python3 - "$GIT_URL" "$GIT_BRANCH" "$work/git_delivery.py" <<'PYTHON' import base64, json, os, pathlib, socket, ssl, urllib.error, urllib.parse, urllib.request, sys url, branch, destination = sys.argv[1:] parsed = urllib.parse.urlparse(url) if parsed.scheme != 'https' or parsed.netloc not in ('git.it-screen.com', 'git.iomatex.com') or parsed.query or parsed.fragment: raise ValueError('Unsupported Git URL') repo = parsed.path.strip('/').removesuffix('.git') class NoRedirect(urllib.request.HTTPRedirectHandler): def redirect_request(self, *args, **kwargs): raise RuntimeError('Git download redirect refused') headers = {'User-Agent': 'IomatexOS-Updater/1.0', 'Authorization': 'Basic ' + base64.b64encode((os.environ['IOMATEX_GIT_USERNAME'] + ':' + os.environ['IOMATEX_GIT_PASSWORD']).encode()).decode()} opener = urllib.request.build_opener(NoRedirect) # Accept old saved URLs as input only; never make a request to the blocked host. host = 'git.iomatex.com' attempts = 2 commit = None for index in range(attempts): base = 'https://' + host + '/api/v1/repos/' + repo + '/' try: if commit is None: with opener.open(urllib.request.Request(base + 'branches/' + urllib.parse.quote(branch, safe=''), headers=headers), timeout=15) as r: commit = json.load(r)['commit']['id'] with opener.open(urllib.request.Request(base + 'raw/offline/git_delivery.py?ref=' + commit, headers=headers), timeout=15) as r: content = r.read() pathlib.Path(destination).write_bytes(content) pathlib.Path(destination + '.source').write_text('https://' + host + '/' + repo + '.git') print('Git source: ' + host, flush=True) break except urllib.error.HTTPError as exc: raise SystemExit('Git returned HTTP ' + str(exc.code) + '; check credentials, repository and branch. No domain switch.') from None except (urllib.error.URLError, OSError) as exc: reason = exc.reason if isinstance(exc, urllib.error.URLError) else exc if isinstance(reason, ssl.SSLError): raise SystemExit('Git TLS verification/connection failed. No domain switch; check certificates and system time.') from None if not isinstance(reason, OSError): raise SystemExit('Git request failed; no domain switch.') from None if index + 1 == attempts: raise SystemExit('git.iomatex.com is unreachable. Check DNS and Internet connectivity; installation has not started.') from None print('Git network/DNS failure; retrying ' + host, flush=True) PYTHON GIT_URL=$(cat "$work/git_delivery.py.source") export IOMATEX_RECOVERY_STAGES="$STAGES" python3 "$work/git_delivery.py" bootstrap "$GIT_URL" "$GIT_BRANCH" "$CHECKOUT_DIR" unset IOMATEX_GIT_USERNAME IOMATEX_GIT_PASSWORD rm -rf "$work" trap - EXIT exec bash "$CHECKOUT_DIR/local_installer.sh" "$CHECKOUT_DIR" --non-interactive --reset-state --only "$STAGES"