diff options
author | tv <tv@shackspace.de> | 2015-05-21 01:56:08 +0200 |
---|---|---|
committer | tv <tv@shackspace.de> | 2015-05-21 01:56:08 +0200 |
commit | 36670f3e1cccad579b994a29320eeb8e287803b7 (patch) | |
tree | 484cb2c1075cdcabe8020ca40704f4d68f2b2fed /lib | |
parent | b95a514bcced00af77ae4adab77326dba9ab4c36 (diff) |
sh: functions -> bin/
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cac.sh | 105 | ||||
-rw-r--r-- | lib/cacnixos.sh | 28 | ||||
-rw-r--r-- | lib/net.sh | 9 | ||||
-rw-r--r-- | lib/prelude.sh | 261 | ||||
-rw-r--r-- | lib/url.sh | 35 |
5 files changed, 0 insertions, 438 deletions
diff --git a/lib/cac.sh b/lib/cac.sh deleted file mode 100644 index fea6886be..000000000 --- a/lib/cac.sh +++ /dev/null @@ -1,105 +0,0 @@ -. ./lib/url.sh - -cac_ssh() {( - server=$1 - shift - - address=$(echo $server | jq -r .ip) - target=root@$address - - SSHPASS=$(echo $server | jq -r .rootpass) - export SSHPASS - - exec sshpass -e ssh \ - -o StrictHostKeyChecking=no \ - -o UserKnownHostsFile=/dev/null \ - "$target" \ - "$@" -)} - -cac_getserver_by_servername() {( - serverlist=$(cac_listservers) - echo $serverlist \ - | jq \ - --arg name "$1" \ - '.[]|select(.servername==$name)' -)} - - -cac_listservers() {( - listservers=$(_cac_get_api_v1 listservers) - status=$(echo "$listservers" | jq -r .status) - if [ "$status" = ok ]; then - echo "$listservers" | jq -r .data - else - echo "$0: bad listservers status: $status" >&2 - exit 1 - fi -)} - -cac_listtasks() { - _cac_get_api_v1 listtasks -} - -cac_listtemplates() { - _cac_get_api_v1 listtemplates -} - -cac_console() { - _cac_post_api_v1 console sid="$1" -} - -cac_powerop() { - _cac_post_api_v1 powerop sid="$1" action="$2" -} - -cac_renameserver() { - _cac_post_api_v1 renameserver sid="$1" name="$2" -} - -cac_rnds() { - _cac_post_api_v1 rdns sid="$1" hostname="$2" -} - -cac_runmode() { - _cac_post_api_v1 rdns sid="$1" mode="$2" -} - -# default os=26 is CentOS-7-64bit -cac_cloudpro_build() { - _cac_post_api_v1 cloudpro/build cpu="$1" ram="$2" storage="$3" os="${4-26}" -} - -cac_cloudpro_delete() { - _cac_post_api_v1 cloudpro/delete sid="$1" -} - -cac_cloudpro_resources() { - _cac_get_api_v1 cloudpro/resources -} - -_cac_get_api_v1() { - _cac_curl_api_v1 -G "$@" -} - -_cac_post_api_v1() { - _cac_curl_api_v1 -XPOST "$@" -} - -_cac_curl_api_v1() { - _cac_exec curl -fsS "$1" "https://panel.cloudatcost.com/api/v1/$2.php" $( - shift 2 - set -- "$@" login="$cac_login" key="$cac_key" - for arg; do - echo -d $(printf '%s' "$arg" | url_encode) - done - ) -} - -_cac_exec() { - if test -z "${cac_via-}"; then - (exec "$@") - else - ssh -q "$cac_via" -t "$@" - fi -} diff --git a/lib/cacnixos.sh b/lib/cacnixos.sh deleted file mode 100644 index 24502d694..000000000 --- a/lib/cacnixos.sh +++ /dev/null @@ -1,28 +0,0 @@ -. ./lib/net.sh - -# cacnixos_networking : cac-server x hostname -> nixos-module -cacnixos_networking() {( - server=$1 - hostname=$2 - - address=$(echo $server | jq -r .ip) - gateway=$(echo $server | jq -r .gateway) - nameserver=8.8.8.8 - netmask=$(echo $server | jq -r .netmask) - prefix=$(net_netmask_to_prefix $netmask) - - printf '{...}:\n' - printf '{\n' - printf ' networking.hostName = "%s";\n' $hostname - printf ' networking.interfaces.enp2s1.ip4 = [\n' - printf ' {\n' - printf ' address = "%s";\n' $address - printf ' prefixLength = %d;\n' $prefix - printf ' }\n' - printf ' ];\n' - printf ' networking.defaultGateway = "%s";\n' $gateway - printf ' networking.nameservers = [\n' - printf ' "%s"\n' $nameserver - printf ' ];\n' - printf '}\n' -)} diff --git a/lib/net.sh b/lib/net.sh deleted file mode 100644 index 518c955b6..000000000 --- a/lib/net.sh +++ /dev/null @@ -1,9 +0,0 @@ -net_netmask_to_prefix() {( - binaryNetmask=$(echo $1 | sed 's/^/obase=2;/;s/\./;/g' | bc | tr -d \\n) - binaryPrefix=$(echo $binaryNetmask | sed -n 's/^\(1*\)0*$/\1/p') - if ! echo $binaryPrefix | grep -q .; then - echo $0: bad netmask: $netmask >&2 - exit 4 - fi - printf %s $binaryPrefix | tr -d 0 | wc -c -)} diff --git a/lib/prelude.sh b/lib/prelude.sh deleted file mode 100644 index 2adfb5651..000000000 --- a/lib/prelude.sh +++ /dev/null @@ -1,261 +0,0 @@ -# clone_or_update : [user@]hostname x local_dir x git_url x git_rev -> () -clone_or_update() {( - target=$1 - nixpkgs_dir=$2 - git_url=$3 - git_rev=$4 - - echo ' - set -euf - - if [ ! -d "$nixpkgs_dir" ]; then - mkdir -p "$nixpkgs_dir" - fi - - cd "$nixpkgs_dir" - - git init -q - - if ! current_url=$(git config remote.src.url); then - git remote add src "$git_url" - elif [ $current_url != $git_url ]; then - git remote set-url src "$git_url" - fi - - git fetch src - - git checkout "$git_rev" - ' \ - | ssh "$target" env \ - nixpkgs_dir="$nixpkgs_dir" \ - git_rev="$git_rev" \ - git_url="$git_url" \ - /bin/sh -)} - -# deploy : nixos-config x [user@]hostname -> () -deploy() {( - main=$1 - target=$2 - nixpkgs_dir=/var/nixpkgs # TODO make configurable - - git_url=$(nixpkgs_url $main) - git_rev=$(nixpkgs_rev $main) - - if [ "$git_url" = '' ] || [ "$git_rev" = '' ]; then - echo "specify nixpkgs.url and nixpkgs.rev in $main !" - exit 23 - fi - - filter=$(rsync_filter "$main") - - echo "$filter" \ - | rsync -f '. -' -zvrlptD --delete-excluded ./ "$target":/etc/nixos/ - - clone_or_update "$target" "$nixpkgs_dir" "$git_url" "$git_rev" - ssh "$target" nixos-rebuild switch \ - -I nixos-config=/etc/nixos/"$main" \ - -I nixpkgs="$nixpkgs_dir" \ - -I secrets=/etc/nixos/secrets \ -)} - -# 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 \ - -I secrets=secrets \ - --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)"'\)*\<import\s\+@\1@ - t1;d - :1; s@^\([^:]\+:\)\(\.*/\S*\)@\1\2\n@ - t2;d - :2; P;D - ' -} - -# slash_path_relpath : lines (path ":" relpath) |> 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 -} - -# nixpkgs_url : nixos-config -> git_url -nixpkgs_url() { - nix-instantiate \ - -I nixos-config="$1" \ - --eval \ - --json \ - -E '(import <nixos-config> {config={}; pkgs={};}).nixpkgs.url' 2> /dev/null \ - | jq -r . -} - -# nixpkgs_rev : nixos-config -> git_rev -nixpkgs_rev() { - nix-instantiate \ - -I nixos-config="$1" \ - --eval \ - --json \ - -E '(import <nixos-config> {config={}; pkgs={};}).nixpkgs.rev' 2> /dev/null \ - | jq -r . 2> /dev/null -} - -# verbose COMMAND [ARGS...] -verbose() { - echo "$@" >&2 - "$@" -} diff --git a/lib/url.sh b/lib/url.sh deleted file mode 100644 index 05f93a94f..000000000 --- a/lib/url.sh +++ /dev/null @@ -1,35 +0,0 @@ -url_encode() { - sed ' - s/%/%25/g - s/ /%20/g - s/!/%21/g - s/"/%22/g - s/#/%23/g - s/\$/%24/g - s/\&/%26/g - s/'\''/%27/g - s/(/%28/g - s/)/%29/g - s/\*/%2a/g - s/+/%2b/g - s/,/%2c/g - s/-/%2d/g - s/\./%2e/g - s/\//%2f/g - s/:/%3a/g - s/;/%3b/g - s//%3e/g - s/?/%3f/g - s/@/%40/g - s/\[/%5b/g - s/\\/%5c/g - s/\]/%5d/g - s/\^/%5e/g - s/_/%5f/g - s/`/%60/g - s/{/%7b/g - s/|/%7c/g - s/}/%7d/g - s/~/%7e/g - ' -} |