Iomatex_OS/tools/iomatex-install_r

200 lines
5.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
BOOTSTRAP_VERSION="B-1.0.1"
BUILD_BASE="${BUILD_BASE:-http://65.109.116.180}"
CHANNEL="${CHANNEL:-stable}"
WORK="/opt/iomatex-installer"
LOGDIR="/var/log/iomatex"
STATEDIR="/var/lib/iomatex/bootstrap"
BOOTLOG="$LOGDIR/bootstrap.log"
mkdir -p "$WORK" "$LOGDIR" "$STATEDIR"
touch "$BOOTLOG" || true
log() {
local msg="[$(date -u +'%F %T UTC')] $*"
echo "$msg" | tee -a "$BOOTLOG"
}
need_root() {
if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then
echo "Run as root: sudo BUILD_BASE=... CHANNEL=... bash $0"
exit 1
fi
}
have_cmd(){ command -v "$1" >/dev/null 2>&1; }
strip_cr(){ tr -d '\r'; }
fetch() {
local url="$1" out="$2"
mkdir -p "$(dirname "$out")"
log "GET $url"
curl -fsSL --retry 5 --retry-delay 1 --connect-timeout 5 --max-time 180 "$url" -o "$out"
}
json_get_string() {
# expects "key": "value"
local key="$1" file="$2"
local line val
line="$(grep -oE "\"$key\"[[:space:]]*:[[:space:]]*\"[^\"]+\"" "$file" | head -n1 | strip_cr || true)"
val="$(printf "%s" "$line" | sed -E "s/.*\"$key\"[[:space:]]*:[[:space:]]*\"([^\"]+)\".*/\1/" | strip_cr)"
printf "%s" "$val"
}
sha256_file(){ sha256sum "$1" | awk '{print $1}'; }
verify_sums() {
local sums_file="$1" base_dir="$2"
log "Verify SHA256SUMS"
while IFS= read -r line; do
line="$(printf "%s" "$line" | strip_cr)"
[[ -z "$line" ]] && continue
[[ "$line" =~ ^# ]] && continue
local expected relpath file actual
expected="$(echo "$line" | awk '{print $1}')"
relpath="$(echo "$line" | awk '{print $2}')"
[[ -z "${relpath:-}" ]] && continue
file="$base_dir/$relpath"
[[ -f "$file" ]] || { log "ERROR missing: $relpath"; exit 2; }
actual="$(sha256_file "$file")"
[[ "$actual" == "$expected" ]] || {
log "ERROR checksum mismatch: $relpath"
log " expected=$expected"
log " actual =$actual"
exit 3
}
done < "$sums_file"
}
manifest_steps() {
# outputs: id|type|file (simple parser; requires each field on its own line in manifest)
local mf="$1"
awk '
BEGIN{inSteps=0}
/"steps"[[:space:]]*:[[:space:]]*\[/ {inSteps=1}
inSteps==1 {
if ($0 ~ /"id"[[:space:]]*:/) {gsub(/.*"id"[[:space:]]*:[[:space:]]*"/,""); gsub(/".*/,""); id=$0}
if ($0 ~ /"type"[[:space:]]*:/) {gsub(/.*"type"[[:space:]]*:[[:space:]]*"/,""); gsub(/".*/,""); type=$0}
if ($0 ~ /"file"[[:space:]]*:/) {gsub(/.*"file"[[:space:]]*:[[:space:]]*"/,""); gsub(/".*/,""); file=$0}
if ($0 ~ /}/ && id!="" && type!="" && file!="") {print id "|" type "|" file; id=""; type=""; file=""}
}
' "$mf" | strip_cr
}
step_done_file() {
local release="$1" step_id="$2"
echo "$STATEDIR/${release}__${step_id}.done"
}
run_step_bash() {
local release="$1" step_id="$2" rel_dir="$3" script_rel="$4"
local donef steplog
donef="$(step_done_file "$release" "$step_id")"
steplog="$LOGDIR/${step_id}.log"
if [[ -f "$donef" ]]; then
log "SKIP $step_id (already done: $(cat "$donef" 2>/dev/null || true))"
return 0
fi
[[ -f "$rel_dir/$script_rel" ]] || { log "ERROR step file missing: $script_rel"; exit 6; }
chmod +x "$rel_dir/$script_rel" || true
log "RUN $step_id => $script_rel"
bash "$rel_dir/$script_rel" 2>&1 | tee -a "$steplog"
date -u +'%F %T UTC' > "$donef"
log "OK $step_id"
}
main() {
need_root
log "=== IOMATEX bootstrap $BOOTSTRAP_VERSION start ==="
log "BUILD_BASE=$BUILD_BASE"
log "CHANNEL=$CHANNEL"
if ! have_cmd curl; then
log "Installing curl/ca-certificates..."
apt-get update -y
apt-get install -y curl ca-certificates
fi
if ! have_cmd sha256sum; then
log "Installing coreutils..."
apt-get update -y
apt-get install -y coreutils
fi
local latest="$WORK/latest.json"
fetch "$BUILD_BASE/releases/$CHANNEL/latest.json" "$latest"
local release path
release="$(json_get_string release "$latest")"
path="$(json_get_string path "$latest")"
[[ -n "${release:-}" && -n "${path:-}" ]] || {
log "ERROR: cannot parse latest.json"
cat "$latest" | cat -A
exit 4
}
log "Release=$release"
log "Path=$path"
local rel_dir="$WORK/$release"
mkdir -p "$rel_dir"
fetch "$BUILD_BASE/$path/manifest.json" "$rel_dir/manifest.json"
fetch "$BUILD_BASE/$path/SHA256SUMS" "$rel_dir/SHA256SUMS"
log "Download payload..."
while IFS= read -r line; do
line="$(printf "%s" "$line" | strip_cr)"
[[ -z "$line" ]] && continue
[[ "$line" =~ ^# ]] && continue
file="$(echo "$line" | awk '{print $2}')"
[[ -n "${file:-}" ]] || continue
fetch "$BUILD_BASE/$path/$file" "$rel_dir/$file"
done < "$rel_dir/SHA256SUMS"
verify_sums "$rel_dir/SHA256SUMS" "$rel_dir"
log "Execute steps from manifest..."
local mf="$rel_dir/manifest.json"
local schema
schema="$(json_get_string schema "$mf")"
[[ -z "${schema:-}" ]] && schema="1"
[[ "$schema" == "1" ]] || { log "ERROR: unsupported manifest schema=$schema"; exit 5; }
manifest_steps "$mf" | while IFS='|' read -r step_id step_type step_file; do
step_id="$(printf "%s" "$step_id" | strip_cr)"
step_type="$(printf "%s" "$step_type" | strip_cr)"
step_file="$(printf "%s" "$step_file" | strip_cr)"
[[ -z "${step_id:-}" ]] && continue
if [[ "$step_type" == "bash" ]]; then
run_step_bash "$release" "$step_id" "$rel_dir" "$step_file"
else
log "ERROR: unsupported step type: $step_type (step $step_id)"
exit 7
fi
done
{
echo "release=$release"
echo "channel=$CHANNEL"
echo "build_base=$BUILD_BASE"
echo "bootstrap=$BOOTSTRAP_VERSION"
echo "installed_utc=$(date -u +'%F %T UTC')"
} > /etc/iomatex-release
log "=== DONE: installed $release ==="
}
main "$@"