Iomatex_OS/install_iomatex_edge.sh

57 lines
3.6 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
# 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, urllib.parse, urllib.request, sys
url, branch, destination = sys.argv[1:]
repo = urllib.parse.urlparse(url).path.strip('/').removesuffix('.git')
base = 'https://git.it-screen.com/api/v1/repos/' + repo + '/'
class NoRedirect(urllib.request.HTTPRedirectHandler):
def redirect_request(self, *args, **kwargs):
raise RuntimeError('Git download redirect refused')
headers = {'Authorization': 'Basic ' + base64.b64encode((os.environ['IOMATEX_GIT_USERNAME'] + ':' + os.environ['IOMATEX_GIT_PASSWORD']).encode()).decode()}
opener = urllib.request.build_opener(NoRedirect)
with opener.open(urllib.request.Request(base + 'branches/' + urllib.parse.quote(branch, safe=''), headers=headers), timeout=120) 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=120) as r:
pathlib.Path(destination).write_bytes(r.read())
PYTHON
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"