From 9bc56b974cc223507f9ac169857bd58e6617739a Mon Sep 17 00:00:00 2001 From: Joris Date: Fri, 8 Dec 2023 11:41:44 +0100 Subject: [PATCH 1/2] Switch to main and release branches --- Readme.md | 4 ++-- build | 41 +++++++++++++------------------------- scripts/info-branch-commit | 11 ++++++++++ scripts/option-prompt | 28 ++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 29 deletions(-) create mode 100755 scripts/info-branch-commit create mode 100755 scripts/option-prompt diff --git a/Readme.md b/Readme.md index 20c8fc0b..6e914f69 100644 --- a/Readme.md +++ b/Readme.md @@ -69,7 +69,7 @@ Most changes to system configuration and/or the controller can be tested in a vi To create and run a VM, run: ```bash -./build vm +./build # vm target ./result/bin/run-in-vm ``` @@ -131,7 +131,7 @@ Commonly used update and deploy URLs (channels) can be used with shortcuts defin To release an update to the `develop` channel: ``` -./build develop +./build # develop target ./result/bin/deploy-update --key PATH_TO_KEY.pem ``` diff --git a/build b/build index 0d5eceaf..19b2625f 100755 --- a/build +++ b/build @@ -3,20 +3,14 @@ set -euo pipefail TARGET="${1:-default}" +scripts/info-branch-commit +export readonly TARGET=$(scripts/option-prompt "Select a target" vm develop validation master lab-key shed-key stuck default) +echo + # Allow unfree packages, required for zerotier using a BSL 1.1 licence # See https://nixos.wiki/wiki/FAQ/How_can_I_install_a_proprietary_or_unfree_package%3F export NIXPKGS_ALLOW_UNFREE=1 -function ensure_branch { - CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) - if [ "$CURRENT_BRANCH" != "$1" ]; then - echo - echo "You should be on branch '$1', but you are currently on branch '$CURRENT_BRANCH'." - echo "Aborting." - exit 1 - fi -} - if [ "$TARGET" == "vm" ]; then (set -x; nix-build \ @@ -42,8 +36,6 @@ You can switch virtual consoles in the guest via QEMU monitor (ctrl-alt-2), send elif [ "$TARGET" == "develop" ]; then - ensure_branch "develop" - (set -x; nix-build \ --arg updateCert ./pki/develop/cert.pem \ --arg updateUrl https://dist.dividat.com/releases/playos/develop/ \ @@ -56,8 +48,6 @@ elif [ "$TARGET" == "develop" ]; then elif [ "$TARGET" == "validation" ]; then - ensure_branch "validation" - (set -x; nix-build \ --arg updateCert ./pki/validation/cert.pem \ --arg updateUrl https://dist.dividat.com/releases/playos/validation/ \ @@ -70,8 +60,6 @@ elif [ "$TARGET" == "validation" ]; then elif [ "$TARGET" == "master" ]; then - ensure_branch "master" - (set -x; nix-build \ --arg updateCert ./pki/master/cert.pem \ --arg updateUrl https://dist.dividat.com/releases/playos/master/ \ @@ -82,12 +70,18 @@ elif [ "$TARGET" == "master" ]; then echo echo "Run ./result/bin/deploy-update to deploy." -# Create a stuck system that will not self-update -# usage: ./build stuck https://url-for-kiosk elif [ "$TARGET" == "stuck" ]; then + echo "Creating a stuck system that will not self-update." + echo + + printf "Kiosk URL? " + read KIOSK_URL + + KIOSK_URL=$(echo "$KIOSK_URL" | xargs) # Trim + (set -x; nix-build \ - --arg kioskUrl "$2" \ + --arg kioskUrl "$KIOSK_URL" \ --arg buildBundle false \ --arg buildDisk false) @@ -107,15 +101,8 @@ elif [ "$TARGET" == "shed-key" ]; then --arg buildBundle false \ --arg buildDisk false) -elif [ "$TARGET" == "default" ]; then - - (set -x; nix-build) - else - echo - echo "I do not know how to build '$TARGET'." - echo "Aborting." - exit 1 + (set -x; nix-build) fi diff --git a/scripts/info-branch-commit b/scripts/info-branch-commit new file mode 100755 index 00000000..d576c34c --- /dev/null +++ b/scripts/info-branch-commit @@ -0,0 +1,11 @@ +#! /usr/bin/env bash +set -euo pipefail + +echo "On branch:" +echo +echo " $(git rev-parse --abbrev-ref HEAD)" +echo +echo "With last commit:" +echo +echo " $(git log -1 --pretty="%h %s")" +echo diff --git a/scripts/option-prompt b/scripts/option-prompt new file mode 100755 index 00000000..61c83fc5 --- /dev/null +++ b/scripts/option-prompt @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +# option-prompt "Select a fruit" apple orange banana +# +# Keeps prompting for input until a known option is entered. +# Prints the prompt to stderr, the selected option to stdout. + +set -euo pipefail + +PROMPT="$1" +shift + +OPTIONS=( "$@" ) +OPTIONS_LIST=$(IFS="¶"; echo "${OPTIONS[*]}" | sed 's|¶| / |g') + +while true; do + + printf "$PROMPT [ $OPTIONS_LIST ] " >&2 + read ANSWER + + for OPTION in "${OPTIONS[@]}"; do + if [ "$ANSWER" == "$OPTION" ]; then + echo "$OPTION" + exit 0 + fi + done + +done From 9048ca6a02f40be323dc5ad85741bf740321731f Mon Sep 17 00:00:00 2001 From: Joris Date: Fri, 8 Dec 2023 12:13:30 +0100 Subject: [PATCH 2/2] Allow passing argument to build, as before Only add info & confirmation steps for builds on: - develop, - validation, - release. --- Readme.md | 4 ++-- build | 22 +++++++++++++++++----- scripts/confirm-or-abort | 15 +++++++++++++++ scripts/option-prompt | 28 ---------------------------- scripts/yes-no-question | 21 +++++++++++++++++++++ 5 files changed, 55 insertions(+), 35 deletions(-) create mode 100755 scripts/confirm-or-abort delete mode 100755 scripts/option-prompt create mode 100755 scripts/yes-no-question diff --git a/Readme.md b/Readme.md index 6e914f69..20c8fc0b 100644 --- a/Readme.md +++ b/Readme.md @@ -69,7 +69,7 @@ Most changes to system configuration and/or the controller can be tested in a vi To create and run a VM, run: ```bash -./build # vm target +./build vm ./result/bin/run-in-vm ``` @@ -131,7 +131,7 @@ Commonly used update and deploy URLs (channels) can be used with shortcuts defin To release an update to the `develop` channel: ``` -./build # develop target +./build develop ./result/bin/deploy-update --key PATH_TO_KEY.pem ``` diff --git a/build b/build index 19b2625f..0dc5aed2 100755 --- a/build +++ b/build @@ -3,10 +3,6 @@ set -euo pipefail TARGET="${1:-default}" -scripts/info-branch-commit -export readonly TARGET=$(scripts/option-prompt "Select a target" vm develop validation master lab-key shed-key stuck default) -echo - # Allow unfree packages, required for zerotier using a BSL 1.1 licence # See https://nixos.wiki/wiki/FAQ/How_can_I_install_a_proprietary_or_unfree_package%3F export NIXPKGS_ALLOW_UNFREE=1 @@ -36,6 +32,9 @@ You can switch virtual consoles in the guest via QEMU monitor (ctrl-alt-2), send elif [ "$TARGET" == "develop" ]; then + scripts/info-branch-commit + scripts/confirm-or-abort + (set -x; nix-build \ --arg updateCert ./pki/develop/cert.pem \ --arg updateUrl https://dist.dividat.com/releases/playos/develop/ \ @@ -48,6 +47,9 @@ elif [ "$TARGET" == "develop" ]; then elif [ "$TARGET" == "validation" ]; then + scripts/info-branch-commit + scripts/confirm-or-abort + (set -x; nix-build \ --arg updateCert ./pki/validation/cert.pem \ --arg updateUrl https://dist.dividat.com/releases/playos/validation/ \ @@ -60,6 +62,9 @@ elif [ "$TARGET" == "validation" ]; then elif [ "$TARGET" == "master" ]; then + scripts/info-branch-commit + scripts/confirm-or-abort + (set -x; nix-build \ --arg updateCert ./pki/master/cert.pem \ --arg updateUrl https://dist.dividat.com/releases/playos/master/ \ @@ -101,8 +106,15 @@ elif [ "$TARGET" == "shed-key" ]; then --arg buildBundle false \ --arg buildDisk false) -else +elif [ "$TARGET" == "default" ]; then (set -x; nix-build) +else + + echo + echo "I do not know how to build '$TARGET'." + echo "Aborting." + exit 1 + fi diff --git a/scripts/confirm-or-abort b/scripts/confirm-or-abort new file mode 100755 index 00000000..e77fe512 --- /dev/null +++ b/scripts/confirm-or-abort @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +set -euo pipefail +cd $(dirname "$0") + +if [ -n "${1:-}" ]; then + + QUESTION="$1" + +else + + QUESTION="Confirm?" + +fi + +./yes-no-question "$QUESTION" || (echo "Aborting." && exit 1) diff --git a/scripts/option-prompt b/scripts/option-prompt deleted file mode 100755 index 61c83fc5..00000000 --- a/scripts/option-prompt +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env bash - -# option-prompt "Select a fruit" apple orange banana -# -# Keeps prompting for input until a known option is entered. -# Prints the prompt to stderr, the selected option to stdout. - -set -euo pipefail - -PROMPT="$1" -shift - -OPTIONS=( "$@" ) -OPTIONS_LIST=$(IFS="¶"; echo "${OPTIONS[*]}" | sed 's|¶| / |g') - -while true; do - - printf "$PROMPT [ $OPTIONS_LIST ] " >&2 - read ANSWER - - for OPTION in "${OPTIONS[@]}"; do - if [ "$ANSWER" == "$OPTION" ]; then - echo "$OPTION" - exit 0 - fi - done - -done diff --git a/scripts/yes-no-question b/scripts/yes-no-question new file mode 100755 index 00000000..4eaeeaed --- /dev/null +++ b/scripts/yes-no-question @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +set -euo pipefail + +QUESTION="$1" + +while true; do + + printf "$QUESTION [y/n] " + read ANSWER + + if [ "$ANSWER" == "y" ]; then + + exit 0 + + elif [ "$ANSWER" == "n" ]; then + + exit 1 + + fi + +done