#!/bin/sh # Nucarium box installer. https://nucarium.sh # # Gets your own Mac or Linux computer ready to build an agent box with the # Nucarium box recipe, an Ansible playbook. In order, it: # # 1. checks for the tools the recipe needs, and offers to install the # missing ones, showing each command and asking first; # 2. downloads one pinned release of the recipe and checks its SHA-256; # 3. unpacks it into a folder you choose; # 4. asks about each setting in the recipe's own settings file, one at a # time, and writes your answers to group_vars/all.yml and inventory.ini; # 5. prints the commands to run next. # # It never connects to your box and never runs anything on it. It uses sudo # only for a command it has shown you and you have agreed to. It sends # nothing anywhere: the one request it makes is the recipe download. # # Read this file in a browser, with its checksum: https://nucarium.sh # Try it without changing anything: sh install.sh --dry-run # # Licensed under either the MIT licence or the Apache Licence, Version 2.0, # at your option. # SPDX-License-Identifier: MIT OR Apache-2.0 # Copyright (c) 2026 Mataki Labs LLC set -eu # The release this copy is. nucarium.sh serves each release with its number # on this line; a copy taken straight from the source says 0.0.0-dev. INSTALLER_VERSION="0.3.1" # The box recipe this installer fetches. These three lines are the only # place the release is named. BOX_REPO="https://github.com/nucarium/nucarium-ansible" BOX_VERSION="0.3.0" BOX_SHA256="064dd635cb4e1abb101530a54b4102ba6e52956edd7b7e9fd2b4d95f4f68a61f" GUIDE_URL="https://nucarium.dev/box" OP_CLI_URL="https://developer.1password.com/docs/cli/get-started/" # --------------------------------------------------------------------------- # Output # --------------------------------------------------------------------------- say() { printf '%s\n' "$*"; } step() { printf '\n%s%s%s\n' "$bold" "$*" "$plain"; } note() { printf ' %s\n' "$*"; } warn() { printf '%s\n' "$*" >&2; } die() { warn "" warn "nucarium.sh: $*" exit 1 } usage_error() { warn "nucarium.sh: $*" warn "Run with --help to see the options." exit 2 } usage() { cat < The same as --set NAME=..., with NAME in capitals NUCARIUM_DIR, NUCARIUM_YES, NUCARIUM_DRY_RUN, NUCARIUM_ALL, NUCARIUM_INSTALL_PREREQS, NUCARIUM_ALLOW_SUDO, NUCARIUM_SKIP_PREREQS, NUCARIUM_UPDATE, NUCARIUM_OVERWRITE_SETTINGS The same as the options above (set to 1) NUCARIUM_BOX_SOURCE Use a local recipe instead of downloading: a folder, or a .tar.gz release NUCARIUM_BOX_SHA256 Checksum for a tarball in NUCARIUM_BOX_SOURCE NO_COLOR Plain output The guide: $GUIDE_URL EOF } banner() { say "${bold}Nucarium box installer $INSTALLER_VERSION${plain}" say "" say " \"Nucarium Watch sees which agent is doing what, where and since when — never what it was told or what it wrote.\"" say "" say "This script will: check your tools, fetch and verify box recipe $BOX_VERSION, and fill in its settings with you. It will not: connect to your box, use sudo unasked, or send anything anywhere." if [ "$dry_run" = 1 ]; then say "" say "Dry run: nothing is installed, downloaded or written." fi } # --------------------------------------------------------------------------- # Small helpers # --------------------------------------------------------------------------- have() { command -v "$1" >/dev/null 2>&1; } is_on() { case ${1:-} in 1 | true | yes | on) return 0 ;; *) return 1 ;; esac } # Quote a word for display in a command, only when it needs it. shquote() { case $1 in '' | *[!A-Za-z0-9_./:=@%+,-]*) printf "'%s'" "$(printf '%s' "$1" | sed "s/'/'\\\\''/g")" ;; *) printf '%s' "$1" ;; esac } sha256_of() { if have sha256sum; then sha256sum "$1" | awk '{ print $1 }' elif have shasum; then shasum -a 256 "$1" | awk '{ print $1 }' elif have openssl; then openssl dgst -sha256 -r "$1" | awk '{ print $1 }' else return 1 fi } todo_add() { todo="${todo}${todo:+ }$1" } cleanup() { if [ -n "${work:-}" ] && [ -d "$work" ]; then rm -rf "$work" fi } make_work_dir() { if [ -z "${work:-}" ]; then work=$(mktemp -d "${TMPDIR:-/tmp}/nucarium.XXXXXX") || die "Could not make a temporary folder." fi } # --------------------------------------------------------------------------- # Questions. Answers come from the terminal, never from standard input, so # they work when this script is piped into sh. # --------------------------------------------------------------------------- open_terminal() { if [ "$dry_run" = 1 ] || [ "$assume_yes" = 1 ]; then interactive=0 return 0 fi if [ -n "${NUCARIUM_TTY:-}" ]; then # A file of answers, one per line. Used by the tests. [ -r "$NUCARIUM_TTY" ] || die "Cannot read $NUCARIUM_TTY." exec 3<"$NUCARIUM_TTY" interactive=1 child_stdin=/dev/null return 0 fi if (: /dev/null; then exec 3 in the environment. Sets $preset_has and $preset_value. preset_for() { preset_has=0 preset_value="" case $1 in '' | *[!a-z0-9_]*) return 0 ;; esac while IFS= read -r preset_line; do case $preset_line in "$1="*) preset_has=1 preset_value=${preset_line#*=} ;; esac done </dev/null || echo unknown) case $os in Darwin) platform=macos ;; Linux) platform=linux ;; MINGW* | MSYS* | CYGWIN* | Windows_NT) die "This installer runs on macOS and Linux, and this is Windows ($os). Install WSL with Ubuntu, open an Ubuntu terminal, and run it there: curl -fsSL https://nucarium.sh | sh" ;; *) die "This installer runs on macOS and Linux, and this is $os. The recipe itself is a plain Ansible playbook that runs anywhere Ansible does: $BOX_REPO/releases/tag/v$BOX_VERSION The guide covers setting it up by hand: $GUIDE_URL" ;; esac pm="" if [ "$platform" = macos ]; then if have brew; then pm=brew; fi elif have apt-get; then pm=apt elif have dnf; then pm=dnf elif have pacman; then pm=pacman fi sudo="" if [ "$platform" = linux ] && [ "$(id -u)" != 0 ]; then sudo="sudo " fi } # --------------------------------------------------------------------------- # Tools. Nothing is installed without showing the command and asking; a # command that needs sudo is never run on a default answer. # --------------------------------------------------------------------------- # offer WHY COMMAND sudo|nosudo: returns 0 if the command ran and succeeded. offer() { note "$1" note " $2" if [ "$dry_run" = 1 ]; then note "Dry run: would ask before running it." return 1 fi offer_go=0 if [ "$interactive" = 1 ]; then if [ "$3" = sudo ]; then if confirm " Run this now, with sudo?" n; then offer_go=1; fi else if confirm " Run this now?" y; then offer_go=1; fi fi elif [ "$install_prereqs" = 1 ]; then if [ "$3" = nosudo ] || [ "$allow_sudo" = 1 ]; then offer_go=1; fi fi if [ "$offer_go" = 0 ]; then note "Not run." todo_add "$2" return 1 fi if sh -c "$2" <"$child_stdin"; then return 0 fi warn " That command failed. Carrying on; it is listed again at the end." todo_add "$2" return 1 } install_command() { # $1 = package name on this platform's package manager. Sets $cmd, $cmd_sudo. case $pm in brew) cmd="brew install $1" cmd_sudo=nosudo ;; apt) cmd="${sudo}apt-get install -y $1" cmd_sudo=sudo ;; dnf) cmd="${sudo}dnf install -y $1" cmd_sudo=sudo ;; pacman) cmd="${sudo}pacman -S --needed $1" cmd_sudo=sudo ;; *) cmd="" cmd_sudo=nosudo ;; esac if [ -z "$sudo" ]; then cmd_sudo=nosudo; fi } check_tools() { step "Tools" if [ "$skip_prereqs" = 1 ]; then note "Skipped (--skip-prereqs)." return 0 fi if [ "$platform" = macos ] && [ -z "$pm" ]; then note "Homebrew is not installed, so this script cannot install tools for you." note "Get it from https://brew.sh and run this again, or install the tools your own way." elif [ "$platform" = linux ] && [ -z "$pm" ]; then note "No apt-get, dnf or pacman here, so this script cannot install tools for you." fi if have ansible-playbook && have ansible-galaxy; then note "ok Ansible ($(command -v ansible-playbook))" else install_command ansible if [ -n "$cmd" ]; then offer "missing Ansible, which runs the recipe:" "$cmd" "$cmd_sudo" || : else note "missing Ansible, which runs the recipe." todo_add "Install Ansible: https://docs.ansible.com/ansible/latest/installation_guide/" fi fi if have op; then note "ok 1Password CLI ($(command -v op))" elif [ "$pm" = brew ]; then offer "missing 1Password CLI, which the recipe reads secrets with:" \ "brew install 1password-cli" nosudo || : else note "missing 1Password CLI, which the recipe reads secrets with." note " Install it from $OP_CLI_URL" todo_add "Install the 1Password CLI: $OP_CLI_URL" fi if have ssh; then note "ok ssh ($(command -v ssh))" else case $pm in apt) install_command openssh-client ;; dnf) install_command openssh-clients ;; pacman) install_command openssh ;; *) cmd="" ;; esac if [ -n "$cmd" ]; then offer "missing ssh, which Ansible reaches the box with:" "$cmd" "$cmd_sudo" || : else note "missing ssh, which Ansible reaches the box with." todo_add "Install an SSH client." fi fi if [ -f "${HOME:-/nonexistent}/.ssh/id_ed25519.pub" ]; then note "ok SSH key (~/.ssh/id_ed25519.pub)" else note "missing SSH key ~/.ssh/id_ed25519.pub, which the install media gives the box." note " Make one yourself when you are ready: ssh-keygen -t ed25519" todo_add "ssh-keygen -t ed25519" fi } # Prints the Python that Ansible runs under, from the first line of # ansible-playbook. Nothing is executed. ansible_python() { ap=$(command -v ansible-playbook) || return 1 ap_first=$(sed -n '1p' "$ap" 2>/dev/null) || return 1 case $ap_first in '#!'*) ;; *) return 1 ;; esac # shellcheck disable=SC2086 # split the interpreter line into words set -- ${ap_first#'#!'} [ $# -ge 1 ] || return 1 if [ "$1" = /usr/bin/env ]; then shift [ $# -ge 1 ] || return 1 command -v "$1" else printf '%s\n' "$1" fi } check_ansible_extras() { [ "$skip_prereqs" = 0 ] || return 0 step "Ansible collections and passlib" if ! have ansible-galaxy; then note "Ansible is not installed yet. Once it is, run this in $dir:" note " ansible-galaxy collection install -r requirements.yml" todo_add "cd $(shquote "$dir") && ansible-galaxy collection install -r requirements.yml" return 0 fi offer "The recipe uses two Ansible collections. This installs them under ~/.ansible:" \ "ansible-galaxy collection install -r $(shquote "$dir/requirements.yml")" nosudo || : py=$(ansible_python) || py="" if [ -z "$py" ]; then note "Could not tell which Python Ansible uses; the recipe needs passlib in it." todo_add "Install the Python package passlib where Ansible can import it." return 0 fi if "$py" -c 'import passlib' >/dev/null 2>&1 /dev/null)" ]; then die "$dir already has files in it, and this installer did not put them there. Choose another folder with --dir, or empty that one first." fi } # Checks a tarball listing on standard input: one top-level folder, nothing # absolute, nothing that climbs out. Prints the folder name. check_listing() { awk ' /^\// { bad = "an absolute path" } /(^|\/)\.\.(\/|$)/ { bad = "a path that climbs out with .." } { top = $0 sub(/\/.*/, "", top) if (top == "" || top == ".") bad = "files outside a top-level folder" if (first == "") first = top else if (top != first) bad = "more than one top-level folder" } END { if (first == "") bad = "no files" if (bad != "") { print bad; exit 1 } print first }' } download() { # $1 = https URL, $2 = file to write if have curl; then curl -fsSL --proto '=https' --proto-redir '=https' --tlsv1.2 -o "$2" "$1" elif have wget; then wget -q --https-only -O "$2" "$1" else die "Downloading the recipe needs curl or wget, and neither is installed." fi } # Leaves the recipe's files in $recipe_src. fetch_recipe() { step "Recipe" recipe_url=$BOX_REPO/releases/download/v$BOX_VERSION/nucarium-box-$BOX_VERSION.tar.gz expected=$BOX_SHA256 tarball="" if [ -n "$box_source" ] && [ -d "$box_source" ]; then note "Using the folder $box_source (NUCARIUM_BOX_SOURCE). A folder has no checksum to check." recipe_src=$box_source elif [ -n "$box_source" ]; then [ -f "$box_source" ] || die "NUCARIUM_BOX_SOURCE is not a folder or a file: $box_source" note "Using the file $box_source (NUCARIUM_BOX_SOURCE)." tarball=$box_source if [ -n "${NUCARIUM_BOX_SHA256:-}" ]; then expected=$NUCARIUM_BOX_SHA256; fi else note "Box recipe $BOX_VERSION" note " $recipe_url" note " SHA-256 $expected" fi if [ "$dry_run" = 1 ]; then if [ -n "$box_source" ] && [ -d "$box_source" ]; then note "Dry run: would copy it into $dir." elif [ -n "$tarball" ]; then note "Dry run: would check its SHA-256 against $expected and unpack it into $dir." else note "Dry run: would download it, check its SHA-256 and unpack it into $dir." fi return 0 fi if [ -z "$box_source" ] || [ -n "$tarball" ]; then case $expected in *[!0-9a-f]* | '') die "The expected checksum is not a SHA-256: $expected" ;; esac [ "${#expected}" = 64 ] || die "The expected checksum is not a SHA-256: $expected" have tar || die "Unpacking the recipe needs tar, which is not installed." sha256_of /dev/null >/dev/null 2>&1 || die "Checking the recipe needs sha256sum, shasum or openssl, and none is installed. Nothing was downloaded." make_work_dir if [ -z "$tarball" ]; then tarball=$work/recipe.tar.gz download "$recipe_url" "$tarball" || die "Could not download the recipe from $recipe_url Check your connection and try again. Nothing was unpacked." note "Downloaded." fi actual=$(sha256_of "$tarball") if [ "$actual" != "$expected" ]; then die "The recipe does not match the checksum this installer expects. expected $expected got $actual Nothing was unpacked. Do not use this file. Try again, and if it happens twice, tell us: $BOX_REPO/issues" fi note "SHA-256 matches." listing_top=$(tar -tzf "$tarball" | check_listing) || die "The recipe archive is not laid out as expected: it has $listing_top. Nothing was unpacked." mkdir "$work/unpacked" tar -xzf "$tarball" -C "$work/unpacked" if [ -n "$(find "$work/unpacked" -type l | sed -n '1p')" ]; then die "The recipe archive contains links, which a recipe never does. Nothing was installed." fi recipe_src=$work/unpacked/$listing_top fi for needed in site.yml inventory.ini group_vars/all.yml requirements.yml; do [ -f "$recipe_src/$needed" ] || die "That is not a box recipe: it has no $needed." done } # Copies the recipe into $dir. On an update, the two settings files in $dir # are not touched. place_recipe() { if [ "$dry_run" = 1 ]; then return 0; fi mkdir -p "$dir/.nucarium/defaults" "$dir/group_vars" for entry in "$recipe_src"/* "$recipe_src"/.[!.]*; do [ -e "$entry" ] || continue name=${entry##*/} case $name in out | .git | .nucarium | .venv) continue ;; inventory.ini) [ "$recipe_action" = update ] && [ -f "$dir/inventory.ini" ] && continue ;; group_vars) for var_file in "$entry"/* "$entry"/.[!.]*; do [ -e "$var_file" ] || continue if [ "$recipe_action" = update ] && [ "${var_file##*/}" = all.yml ] && [ -f "$dir/group_vars/all.yml" ]; then continue fi cp -R "$var_file" "$dir/group_vars/" done continue ;; esac cp -R "$entry" "$dir/" done # The recipe's own untouched settings, kept so a later run can tell # whether you have edited yours, and so you can compare after an update. cp "$recipe_src/group_vars/all.yml" "$dir/.nucarium/defaults/all.yml" cp "$recipe_src/inventory.ini" "$dir/.nucarium/defaults/inventory.ini" printf '%s\n' "$want_version" >"$marker" } install_recipe() { case $recipe_action in keep) step "Recipe" note "Box recipe $have_version is already in $dir. Left as it is." ;; update) step "Recipe" note "$dir holds box recipe $have_version. This installer has $want_version." update_go=0 if [ "$dry_run" = 1 ]; then note "Dry run: would ask before replacing it. Your settings files would be kept." elif [ "$interactive" = 1 ]; then note "Replacing it overwrites the recipe's files, including any you changed." note "Your two settings files are kept as they are." if confirm " Replace $have_version with $want_version?" n; then update_go=1; fi elif [ "$update" = 1 ]; then update_go=1 else note "Left as it is. Pass --update to replace it." fi if [ "$update_go" = 1 ]; then fetch_recipe place_recipe note "Replaced. The new recipe's default settings are in .nucarium/defaults/ to compare with yours." else recipe_action=keep fi ;; install) fetch_recipe place_recipe [ "$dry_run" = 1 ] || note "Unpacked into $dir." ;; esac } # --------------------------------------------------------------------------- # Settings. The questions come from the settings file inside the recipe that # was fetched: its settings, in its order, each with the note written above # it and its value as the default. Nothing here lists the recipe's settings, # so one it adds later is asked about like the rest. # --------------------------------------------------------------------------- # plan FILE: one line for each top-level setting in a settings file, in # order, as five fields separated by the unit-separator character: # name, type (scalar, list or block), "yes" if the value is quoted, the # value, and the heading of the section it is in. # A list of plain words is a list. Anything else that is not one value (a # map, a list of maps) is a block, which is left for editing by hand. plan() { awk ' function flush() { if (key == "") return if (type == "") type = "block" printf "%s%s%s%s%s%s%s%s%s\n", key, US, type, US, quoted, US, value, US, within key = "" } BEGIN { US = sprintf("%c", 31) } { # A section heading is a comment between two ruled lines. if ($0 ~ /^# -+$/ && before ~ /^# -+$/ && last ~ /^# [^-]/) section = substr(last, 3) before = last last = $0 } /^[A-Za-z_][A-Za-z0-9_]*:/ { flush() key = $0 sub(/:.*/, "", key) rest = substr($0, length(key) + 2) sub(/^[ \t]+/, "", rest) type = "" quoted = "" value = "" within = section if (rest == "" || rest ~ /^#/) next if (rest ~ /^\[/) { type = "list" gsub(/[][",\047]/, " ", rest) n = split(rest, word, " ") for (i = 1; i <= n; i++) value = value (value == "" ? "" : " ") word[i] next } if (rest ~ /^[{|>&*!]/) { type = "block"; next } type = "scalar" if (rest ~ /^"/) { quoted = "yes"; sub(/^"/, "", rest); sub(/"[ \t]*(#.*)?$/, "", rest) } else if (rest ~ /^\047/) { quoted = "yes"; sub(/^\047/, "", rest); sub(/\047[ \t]*(#.*)?$/, "", rest) } else { sub(/[ \t]+#.*$/, "", rest); sub(/[ \t]+$/, "", rest) } value = rest next } key != "" && /^[ \t]+-[ \t]/ { if (type == "block") next item = $0 sub(/^[ \t]+-[ \t]+/, "", item) sub(/[ \t]+#.*$/, "", item) if (item ~ /^".*"$/ || item ~ /^\047.*\047$/) item = substr(item, 2, length(item) - 2) if (item == "" || item ~ /[ \t]/ || item ~ /^[A-Za-z_][A-Za-z0-9_]*:$/) { type = "block"; next } type = "list" value = value (value == "" ? "" : " ") item next } key != "" && /^[ \t]+[^ \t#]/ { type = "block" } END { flush() } ' "$1" } # plan_value FILE NAME: the value of one setting. plan_value() { plan "$1" | awk -F "$US" -v want="$2" '$1 == want { print $4; exit }' } # comment_for FILE NAME: the note written above a setting, without its "# ". comment_for() { awk -v want="$2" ' /^# -+$/ { n = 0; next } /^#/ { line = $0; sub(/^# ?/, "", line); note[++n] = line; next } /^[ \t]*$/ { n = 0; next } /^[A-Za-z_][A-Za-z0-9_]*:/ { key = $0 sub(/:.*/, "", key) if (key == want) { for (i = 1; i <= n; i++) print note[i]; exit } n = 0 } ' "$1" } # inventory_get FILE name|ansible_host|ansible_user: from the first host line. inventory_get() { [ -f "$1" ] || return 0 awk -v want="$2" ' /^[ \t]*([#;].*)?$/ || /^\[/ { next } { if (want == "name") { print $1; exit } for (i = 2; i <= NF; i++) { if (index($i, want "=") == 1) { print substr($i, length(want) + 2); exit } } exit }' "$1" } # The vault that the first 1Password reference under secret_refs points into. current_vault() { awk ' /^secret_refs:/ { on = 1; next } on && /^[^ \t#]/ { exit } on && match($0, /op:\/\/[^\/]+\//) { print substr($0, RSTART + 5, RLENGTH - 6) exit }' "$1" } # The few settings that need more than "ask, offering the recipe's value". # Sets $kind (how an answer is checked), $quiet and $example. special() { kind="" quiet=0 example=0 case $1 in operator_user | agent_user) kind=user ;; timezone) kind=timezone ;; lan_ssh_cidr) kind=cidr ;; git_user_email) kind=email ;; github_org | github_app_client_id) kind=slug ;; github_app_installation_id) kind=digits ;; repos) kind=repos ;; esac # The recipe's value for these is an example, and a box built with it # would not work. They are named at the end if they are left unchanged. case $1 in git_user_name | git_user_email | github_org | github_app_client_id | github_app_installation_id) example=1 ;; esac # Most boxes leave these as the recipe has them. They are listed, and asked # about only if you say so. case $1 in agent_user | tailscale_tag | agents_memory_* | nucleate_* | go_version | node_version | hcp_mode) quiet=1 ;; esac } # For a setting with no entry above, the kind comes from the value it has. infer_kind() { # $1 = type, $2 = "yes" if quoted, $3 = value if [ "$1" = list ]; then echo words elif [ -n "$2" ]; then echo text else case $3 in true | false) echo bool ;; '' | *[!0-9]*) echo text ;; *) echo digits ;; esac fi } local_timezone() { tz_link=$(readlink /etc/localtime 2>/dev/null) || return 1 case $tz_link in */zoneinfo/*) printf '%s' "${tz_link##*/zoneinfo/}" ;; *) return 1 ;; esac } # On a first run, three defaults can be better than the recipe's: this # computer knows its time zone and your Git identity, and the box's address # says which network it is on. better_default() { case $key in timezone) if [ "$default" = Etc/UTC ]; then better=$(local_timezone) && default=$better fi ;; lan_ssh_cidr) case $s_box_address in *[!0-9.]* | *.*.*.*.* | .* | *. | *..*) ;; *.*.*.*) default=${s_box_address%.*}.0/24 ;; esac ;; git_user_name | git_user_email) if have git; then better=$(git config --global "user.${key#git_user_}" 2>/dev/null) || better="" if [ -n "$better" ]; then default=$better; fi fi ;; esac return 0 } # Every answer given in advance must be for a setting this recipe has. check_presets() { known=" box_name box_address vault $(printf '%s\n' "$the_plan" | awk -F "$US" '$2 != "block" { printf "%s ", $1 }')" { printf '%s\n' "$presets" | sed 's/=.*//' env 2>/dev/null | sed -n 's/^NUCARIUM_SET_\([A-Z0-9_]*\)=.*/\1/p' | tr '[:upper:]' '[:lower:]' } | while IFS= read -r preset_name; do [ -n "$preset_name" ] || continue case $known in *" $preset_name "*) ;; *) warn "nucarium.sh: this recipe has no setting called $preset_name." warn "It has:$known" exit 2 ;; esac done || exit 2 } # walk main|quiet: asks about each setting, in the file's order. The quiet # pass covers the settings most boxes leave alone. walk() { walk_section="" while IFS=$US read -r key type quoted value section <&4; do [ -n "$key" ] || continue special "$key" if [ "$type" = block ]; then if [ "$1" = main ] && [ "$key" != secret_refs ]; then by_hand="$by_hand $key"; fi continue fi [ -n "$kind" ] || kind=$(infer_kind "$type" "$quoted" "$value") preset_for "$key" if [ "$1" = quiet ]; then if [ "$quiet" = 0 ] || [ "$preset_has" = 1 ]; then continue; fi elif [ "$preset_has" = 0 ]; then if [ "$quiet" = 1 ] && [ "$ask_all" = 0 ]; then left="$left $key" continue fi # With no UPS, the rest of the UPS settings do not matter. case $key in ups_*) if [ "$has_ups" = false ]; then continue; fi ;; esac fi default=$value if [ "$fresh_settings" = 1 ]; then better_default; fi if [ "$kind" = bool ]; then if [ "$default" = false ]; then default=no; else default=yes; fi fi if [ "$interactive" = 1 ] && [ "$preset_has" = 0 ]; then if [ "$section" != "$walk_section" ]; then say "" if [ -n "$section" ]; then say "$bold$section$plain"; fi walk_section=$section fi comment_for "$vars_file" "$key" | while IFS= read -r comment_line; do note "$comment_line" done fi ask "$key" "$kind" "$default" "$key" if [ "$answer" != "$value" ]; then style=quoted if [ "$type" = list ]; then style=list elif [ "$kind" = bool ] || { [ "$kind" = digits ] && [ -z "$quoted" ]; }; then style=bare fi changes="${changes}${changes:+ }$key$US$style$US$answer" fi summary="${summary}${summary:+ }$key: ${answer:-\"\"}" case $key in nut_enabled) has_ups=$answer ;; operator_user) s_operator_user=$answer ;; esac done 4<= 3) { part[1] = ENVIRON["NUC_VAULT"] if (part[2] == ENVIRON["NUC_OLD_BOX"]) part[2] = ENVIRON["NUC_NEW_BOX"] line = head part[1] for (i = 2; i <= m; i++) line = line "/" part[i] print line next } } { print } END { for (key in style) if (!(key in written)) missing = missing " " key if (missing != "") { print "not found:" missing > "/dev/stderr"; exit 3 } }' "$1" >"$2" } write_inventory() { NUC_HOST="$s_box_name ansible_host=$s_box_address ansible_user=$s_operator_user" awk ' !done && $0 !~ /^[ \t]*([#;].*)?$/ && $0 !~ /^\[/ { print ENVIRON["NUC_HOST"]; done = 1; next } { print } END { if (!done) exit 3 }' "$1" >"$2" } settings_step() { step "Settings" vars_file=$dir/group_vars/all.yml inv_file=$dir/inventory.ini fresh_settings=1 changes="" summary="" left="" by_hand="" has_ups="" s_vault="" if [ ! -f "$vars_file" ] || [ ! -f "$inv_file" ]; then if [ "$dry_run" = 0 ]; then die "$dir has no settings files to fill in (group_vars/all.yml, inventory.ini)." elif [ -n "$box_source" ] && [ -d "$box_source" ]; then # Nothing has been unpacked, but there is a local recipe to read. vars_file=$box_source/group_vars/all.yml inv_file=$box_source/inventory.ini else note "Dry run: the questions come from the settings file inside the recipe," note "which a dry run does not download. It would ask for the box's name and" note "address, then about each setting in group_vars/all.yml, offering the" note "recipe's own value and showing the note the recipe has for it." return 0 fi elif ! cmp -s "$vars_file" "$dir/.nucarium/defaults/all.yml" || ! cmp -s "$inv_file" "$dir/.nucarium/defaults/inventory.ini"; then fresh_settings=0 fi the_plan=$(plan "$vars_file") [ -n "$the_plan" ] || die "group_vars/all.yml has no settings in it." check_presets if [ "$fresh_settings" = 0 ]; then note "The settings files in $dir have been filled in already." if [ "$dry_run" = 1 ]; then note "Dry run: would ask before changing them, and leave them alone by default." return 0 elif [ "$interactive" = 1 ]; then if confirm " Keep them as they are?" y; then note "Kept." return 0 fi note "Your current answers are offered as the defaults. A copy of both files goes in .nucarium/backup/ first." elif [ "$overwrite_settings" = 1 ]; then note "Rewriting them (--overwrite-settings). A copy of both files goes in .nucarium/backup/ first." else note "Left alone. Pass --overwrite-settings to rewrite them." return 0 fi fi if [ "$interactive" = 1 ]; then note "One setting at a time, each with the recipe's own note about it." note "Enter keeps the answer in brackets. Type \"\" to leave a setting empty." note "None of them is a secret: secrets stay in 1Password, and the file only points at them." say "" note "The box's name: its hostname, and its name on Tailscale and in Nucarium Watch." fi old_box_name=$(inventory_get "$inv_file" name) [ -n "$old_box_name" ] || old_box_name=agentbox ask box_name host "$old_box_name" "box_name" s_box_name=$answer d=$(inventory_get "$inv_file" ansible_host) [ "$interactive" = 0 ] || note "Its address on your network, for the first run." ask box_address address "${d:-192.168.1.50}" "box_address" s_box_address=$answer s_operator_user=$(inventory_get "$inv_file" ansible_user) walk main if [ -n "$left" ] && [ "$interactive" = 1 ]; then say "" note "The recipe has more settings that most boxes leave as they are:" note " $left" if confirm " Go through them too?" n; then left="" walk quiet fi fi d=$(current_vault "$vars_file") if [ -n "$d" ]; then if [ "$interactive" = 1 ]; then say "" note "The 1Password vault that holds this box's secrets. The references in" note "secret_refs are pointed at it, under an item named after the box." fi ask vault vault "$d" "vault" s_vault=$answer fi if [ "$dry_run" = 1 ]; then note "Dry run: would ask about these, then write the two files in $dir:" show_settings return 0 fi write_vars "$vars_file" "$vars_file.new.$$" || { rm -f "$vars_file.new.$$" die "Could not rewrite group_vars/all.yml (see above). Nothing was written. Edit the file by hand, following $GUIDE_URL" } write_inventory "$inv_file" "$inv_file.new.$$" || { rm -f "$vars_file.new.$$" "$inv_file.new.$$" die "inventory.ini has no host line to fill in. Nothing was written. Edit the file by hand, following $GUIDE_URL" } if [ "$fresh_settings" = 0 ]; then stamp=$(date +%Y%m%d-%H%M%S) mkdir -p "$dir/.nucarium/backup" cp "$vars_file" "$dir/.nucarium/backup/all.yml.$stamp" cp "$inv_file" "$dir/.nucarium/backup/inventory.ini.$stamp" fi mv "$vars_file.new.$$" "$vars_file" mv "$inv_file.new.$$" "$inv_file" note "Wrote:" show_settings } # --------------------------------------------------------------------------- # What to do next # --------------------------------------------------------------------------- # True when the box is to report to Nucarium Watch and the settings give it # no machine token, which leaves signing in to you: `nucleate login`, on the # box. Reads whichever watch_ setting the recipe has, and any reference under # secret_refs with "watch" in its name. signs_in_by_hand() { awk ' /^[A-Za-z_]/ { refs = 0 } /^watch_[a-z_]*:/ { reports = 1 value = $0 sub(/^[^:]*:[ \t]*/, "", value) sub(/[ \t]+#.*$/, "", value) gsub(/["\047]/, "", value) if (value == "false" || value == "none" || value == "off" || value == "no") off = 1 } /^secret_refs:/ { refs = 1 } refs && /^[ \t]+[a-z_]*watch[a-z_]*:.*op:\/\// { token = 1 } END { exit !(reports && !off && !token) }' "$1" } finish() { step "Next" if [ "$dry_run" = 1 ]; then note "Dry run finished. Nothing was changed. Run it again without --dry-run to do it." note "The guide: $GUIDE_URL" return 0 fi vars_file=$dir/group_vars/all.yml if [ -n "$todo" ]; then note "Still to do on this computer, when you are ready:" printf '%s\n' "$todo" | while IFS= read -r line; do note " $line"; done say "" fi # Settings whose value is still the recipe's example. examples=$(plan "$vars_file" | while IFS=$US read -r key type quoted value section; do special "$key" [ "$example" = 1 ] || continue if [ "$value" = "$(plan_value "$dir/.nucarium/defaults/all.yml" "$key")" ]; then printf ' %s' "$key" fi done) if [ -n "$examples" ]; then note "Still the recipe's example value in group_vars/all.yml:$examples" say "" fi note "The settings point at these 1Password items. Create them before you build:" awk ' /^secret_refs:/ { on = 1; next } on && /^[^ \t#]/ { exit } on && /op:\/\// { sub(/^[ \t]+/, ""); print " " $0 }' "$vars_file" say "" note "Then, from the folder:" note " cd $(shquote "$dir")" note " op signin" note " ansible-playbook autoinstall.yml # writes out/cidata/ for the install USB stick" note " ansible-playbook site.yml # once Ubuntu is installed on the box" if signs_in_by_hand "$vars_file"; then say "" note "Last, on the box as the agent user, once the playbook has finished:" note " nucleate login # shows a short code; approve it at app.nucarium.com/activate" fi say "" note "The guide has every step in order, including the ones only you can do:" note " $GUIDE_URL" } # --------------------------------------------------------------------------- main() { bold="" plain="" todo="" work="" interactive=0 child_stdin=/dev/null box_source=${NUCARIUM_BOX_SOURCE:-} presets="" US=$(printf '\037') read_environment parse_options "$@" if [ -t 1 ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-dumb}" != dumb ]; then bold=$(printf '\033[1m') plain=$(printf '\033[0m') fi want_version=$BOX_VERSION if [ -n "$box_source" ]; then want_version=$BOX_VERSION+local; fi trap cleanup EXIT trap 'exit 130' INT trap 'exit 143' TERM banner detect_platform open_terminal check_tools choose_folder install_recipe check_ansible_extras settings_step finish } # Everything above only defines functions. The one line that does anything is # the last one, inside braces: if the download is cut short anywhere, the # shell finds the file incomplete and runs nothing. { main "$@"; }