From 6e70d8fc47f0725a63dc92c1c43d2b1831c7c4ef Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 7 Apr 2015 22:18:11 +0200 Subject: run -> {deploy,lib/prelude.sh} --- deploy | 15 +++++ lib/prelude.sh | 192 +++++++++++++++++++++++++++++++++++++++++++++++++++++ run | 204 --------------------------------------------------------- 3 files changed, 207 insertions(+), 204 deletions(-) create mode 100755 deploy create mode 100644 lib/prelude.sh delete mode 100755 run diff --git a/deploy b/deploy new file mode 100755 index 00000000..d435ec8b --- /dev/null +++ b/deploy @@ -0,0 +1,15 @@ +#! /bin/sh +# +# usage: ./deploy CONFIG [[USER@]HOST] +# +set -euf + +. ./lib/prelude.sh + +user=root +host=$1 + +config=./$host.nix +target=${2-$user@$host} + +verbose deploy "$config" "$target" diff --git a/lib/prelude.sh b/lib/prelude.sh new file mode 100644 index 00000000..00fa92a4 --- /dev/null +++ b/lib/prelude.sh @@ -0,0 +1,192 @@ +# deploy : nixos-config x [user@]hostname -> () +deploy() {( + main=$1 + target=$2 + + filter=$(rsync_filter "$main") + + echo "$filter" \ + | rsync -f '. -' -zvrlptD --delete-excluded ./ "$target":/etc/nixos/ + + ssh "$target" nixos-rebuild switch -I nixos-config=/etc/nixos/"$main" +)} + +# rsync_filter : nixos-config -> rsync-filter +rsync_filter() {( + main=$1 + + hosts=$(list_hosts) + module_imports=$(set -euf; list_module_imports "$main") + other_imports=$( + echo "$module_imports" \ + | xargs grep -H . \ + | import_statements \ + | slash_path_relpath \ + | undot_paths \ + | sort \ + | uniq \ + | sed '/\.nix$/!s:$:/default.nix:' \ + ) + secrets=$(echo "$module_imports" | xargs cat | quoted_strings | filter_secrets) + + # TODO collect all other paths from *_imports + + abs_deps=$( + echo "$hosts" + echo "$module_imports" + echo "$other_imports" + echo "$secrets" + ) + + rel_deps=$(echo "$abs_deps" | make_relative_to "$PWD") + filter=$(echo "$rel_deps" | make_rsync_whitelist) + + echo "$filter" +)} + +# list_module_imports : nix-file -> lines nix-file +list_module_imports() { + if echo "$1" | grep -q ^/; then + : + else + set -- "./$1" + fi + imports=$(nix-instantiate \ + --strict \ + --json \ + --eval \ + -E \ + "with builtins; with import ./lib/modules.nix; map toString (list-imports $1)") + echo "$imports" \ + | jq -r .[] +} + +# list_hosts : lines tinc-host-file +# Precondition: $PWD/hosts is the correct repository :) +list_hosts() { + git -C hosts ls-tree --name-only HEAD \ + | awk '{print ENVIRON["PWD"]"/hosts/"$$0}' +} + +# filter_secrets : lines string |> lines secrets-file-candidate +# Notice how false positives are possible. +filter_secrets() { + sed -n 's:^\(.*/\)\?\(secrets/.*\):'"${PWD//:/\\:}"'/\2:p' +} + +# import_statements : lines (path ":" string) |> lines (path ":" relpath) +import_statements() { + sed -n ' + s@^\([^:]\+:\)\('"$(bre_invert_word import)"'\)*\ lines path +# +# Example: "/foo/bar: baz" => "/foo/baz" +# +slash_path_relpath() { + sed -n 's@/[^/]\+:@/@p' +} + +# undot_paths : lines path |> lines path +# Remove all dots (. and ..) from input paths. +undot_paths() { + sed ' + :0 + s://\+:/:g + s:/\.\(/\|$\):\1:g + s:/[^/]\+/\.\.\(/\|$\):\1:g + s:^/\(\.\./\)\+:/: + t0 + s:^$:/: + ' +} + +# quoted_strings : lines string |> lines string +# Extract all (double-) quoted strings from stdin. +# +# 0. find begin of string or skip line +# 1. find end of string or skip line +# 2. print string and continue after string +quoted_strings() { + sed ' + s:[^"]*":: ;t1;d + :1; s:\(\([^"]\|\\"\)*\)":\1\n: ;t2;d + :2; P;D + ' \ + | sed 's:\\":":g' +} + +# bre_escape : lines string |> lines bre-escaped-string +bre_escape() { + sed 's:[\.\[\\\*\^\$]:\\&:g' +} + +# bre_invert_word : string -> BRE +# TODO escape chars in the resulting BRE. +bre_invert_word() { + awk -v input="$1" ' + BEGIN { + split(input,s,"") + for (i in s) { + c=s[i] + printf "\\|%s[^%s]", y, c + y = y c + } + } + ' +} + +# ls_bre : directory -> BRE +# Create a BRE from the files in a directory. +ls_bre() { + ls "$1" \ + | tr \\n / \ + | sed ' + s:[\.\[\\\*\^\$]:\\&:g + s:/$:: + s:/:\\|:g + ' +} + +# make_relative_to : lines path |> directory -> lines path +# Non-matching paths won't get altered. +make_relative_to() { + sed "s:^$(echo "$1/" | bre_escape | sed 's/:/\\:/g')::" +} + +# make_rsync_whitelist : lines relpath |> liens rsync-filter +make_rsync_whitelist() { + set -- "$(cat)" + + # include all files in stdin and their directories + { + echo "$1" + echo "$1" | make_parent_dirs | sort | uniq + } \ + | sed 's|^|+ /|' + + # exclude everything else + echo '- *' +} + +# make_parent_dirs : lines path |> lines directory +# List all parent directories of a path. +make_parent_dirs() { + set -- "$(sed -n 's|/[^/]*$||p' | grep . | sort | uniq)" + if echo "$1" | grep -q .; then + echo "$1" + echo "$1" | make_parent_dirs + fi +} + +# verbose COMMAND [ARGS...] +verbose() { + echo "$@" >&2 + "$@" +} diff --git a/run b/run deleted file mode 100755 index 073111f1..00000000 --- a/run +++ /dev/null @@ -1,204 +0,0 @@ -#! /bin/sh -set -euf - -main() { - case "$1" in - (deploy) - "$@" - ;; - (*) - echo "$0: unknown command: $1" >&2 - exit 23 - esac -} - -# deploy : nixos-config x [user@]hostname -> () -deploy() {( - main=$1 - target=$2 - - filter=$(rsync_filter "$main") - - echo "$filter" \ - | rsync -f '. -' -zvrlptD --delete-excluded ./ "$target":/etc/nixos/ - - ssh "$target" nixos-rebuild switch -I nixos-config=/etc/nixos/"$main" -)} - -# rsync_filter : nixos-config -> rsync-filter -rsync_filter() {( - main=$1 - - hosts=$(list_hosts) - module_imports=$(set -euf; list_module_imports "$main") - other_imports=$( - echo "$module_imports" \ - | xargs grep -H . \ - | import_statements \ - | slash_path_relpath \ - | undot_paths \ - | sort \ - | uniq \ - | sed '/\.nix$/!s:$:/default.nix:' \ - ) - secrets=$(echo "$module_imports" | xargs cat | quoted_strings | filter_secrets) - - # TODO collect all other paths from *_imports - - abs_deps=$( - echo "$hosts" - echo "$module_imports" - echo "$other_imports" - echo "$secrets" - ) - - rel_deps=$(echo "$abs_deps" | make_relative_to "$PWD") - filter=$(echo "$rel_deps" | make_rsync_whitelist) - - echo "$filter" -)} - -# list_module_imports : nix-file -> lines nix-file -list_module_imports() { - if echo "$1" | grep -q ^/; then - : - else - set -- "./$1" - fi - imports=$(nix-instantiate \ - --strict \ - --json \ - --eval \ - -E \ - "with builtins; with import ./lib/modules.nix; map toString (list-imports $1)") - echo "$imports" \ - | jq -r .[] -} - -# list_hosts : lines tinc-host-file -# Precondition: $PWD/hosts is the correct repository :) -list_hosts() { - git -C hosts ls-tree --name-only HEAD \ - | awk '{print ENVIRON["PWD"]"/hosts/"$$0}' -} - -# filter_secrets : lines string |> lines secrets-file-candidate -# Notice how false positives are possible. -filter_secrets() { - sed -n 's:^\(.*/\)\?\(secrets/.*\):'"${PWD//:/\\:}"'/\2:p' -} - -# import_statements : lines (path ":" string) |> lines (path ":" relpath) -import_statements() { - sed -n ' - s@^\([^:]\+:\)\('"$(bre_invert_word import)"'\)*\ lines path -# -# Example: "/foo/bar: baz" => "/foo/baz" -# -slash_path_relpath() { - sed -n 's@/[^/]\+:@/@p' -} - -# undot_paths : lines path |> lines path -# Remove all dots (. and ..) from input paths. -undot_paths() { - sed ' - :0 - s://\+:/:g - s:/\.\(/\|$\):\1:g - s:/[^/]\+/\.\.\(/\|$\):\1:g - s:^/\(\.\./\)\+:/: - t0 - s:^$:/: - ' -} - -# quoted_strings : lines string |> lines string -# Extract all (double-) quoted strings from stdin. -# -# 0. find begin of string or skip line -# 1. find end of string or skip line -# 2. print string and continue after string -quoted_strings() { - sed ' - s:[^"]*":: ;t1;d - :1; s:\(\([^"]\|\\"\)*\)":\1\n: ;t2;d - :2; P;D - ' \ - | sed 's:\\":":g' -} - -# bre_escape : lines string |> lines bre-escaped-string -bre_escape() { - sed 's:[\.\[\\\*\^\$]:\\&:g' -} - -# bre_invert_word : string -> BRE -# TODO escape chars in the resulting BRE. -bre_invert_word() { - awk -v input="$1" ' - BEGIN { - split(input,s,"") - for (i in s) { - c=s[i] - printf "\\|%s[^%s]", y, c - y = y c - } - } - ' -} - -# ls_bre : directory -> BRE -# Create a BRE from the files in a directory. -ls_bre() { - ls "$1" \ - | tr \\n / \ - | sed ' - s:[\.\[\\\*\^\$]:\\&:g - s:/$:: - s:/:\\|:g - ' -} - -# make_relative_to : lines path |> directory -> lines path -# Non-matching paths won't get altered. -make_relative_to() { - sed "s:^$(echo "$1/" | bre_escape | sed 's/:/\\:/g')::" -} - -# make_rsync_whitelist : lines relpath |> liens rsync-filter -make_rsync_whitelist() { - set -- "$(cat)" - - # include all files in stdin and their directories - { - echo "$1" - echo "$1" | make_parent_dirs | sort | uniq - } \ - | sed 's|^|+ /|' - - # exclude everything else - echo '- *' -} - -# make_parent_dirs : lines path |> lines directory -# List all parent directories of a path. -make_parent_dirs() { - set -- "$(sed -n 's|/[^/]*$||p' | grep . | sort | uniq)" - if echo "$1" | grep -q .; then - echo "$1" - echo "$1" | make_parent_dirs - fi -} - -if [ "${noexec-}" != 1 ]; then - main "$@" -fi -- cgit v1.2.3