diff options
-rwxr-xr-x | cac-api | 223 |
1 files changed, 156 insertions, 67 deletions
@@ -1,4 +1,4 @@ -#! /bin/sh +#! /usr/bin/env bash # #? cac-api - CloudAtCost API command line interface #? @@ -9,62 +9,33 @@ set -euf #PATH=$PWD/bin:$PATH #export PATH -urlencode() { -#! /bin/sh -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 -' -} -netmask_to_prefix() {( -#! /bin/sh -set -euf - -netmask=$1 - -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 -)} -# - cac_resources_cache=${cac_resources_cache-$HOME/tmp/cac_resources_cache.json} cac_servers_cache=${cac_servers_cache-$HOME/tmp/cac_servers_cache.json} cac_tasks_cache=${cac_tasks_cache-$HOME/tmp/cac_tasks_cache.json} cac_templates_cache=${cac_templates_cache-$HOME/tmp/cac_templates_cache.json} +<<<<<<< HEAD:cac +cac_secrets=${cac_secrets-$HOME/.secrets/cac} +cac_always_update=${cac_always_update-false} +cac_update_interval=${cac_update_interval-10} + +_load_secrets() { + if test ! -r "$cac_secrets" ;then + echo "unable to load secrets from cac_secrets='$cac_secrets'" >&2 + __cac_cli__help + return 1 + else + . "$cac_secrets" + fi +} + +_cac_main() { + _load_secrets + case ${run-true} in true) + __cac_cli__command=${1-help} + shift || : + __cac_cli__"$__cac_cli__command" "$@" + esac +======= cac_secrets=${cac_secrets-$HOME/.secrets/cac-api} @@ -76,6 +47,7 @@ cac_api() { __cac_api_cli__command=${1-help} shift || : __cac_api_cli__"$__cac_api_cli__command" "$@" +>>>>>>> 0809fae379239687ed1170e04311dc2880ef0aba:cac-api } #? cac-api help [REGEX] @@ -125,28 +97,48 @@ __cac_api_cli__console() {( #? cac-api servers #? Print cached servers JSON. #? +<<<<<<< HEAD:cac +__cac_cli__servers() { + _cac_maybe_update +======= __cac_api_cli__servers() { +>>>>>>> 0809fae379239687ed1170e04311dc2880ef0aba:cac-api jq -r . $cac_servers_cache } #? cac-api tasks #? Print cached tasks JSON. #? +<<<<<<< HEAD:cac +__cac_cli__tasks() { + _cac_maybe_update +======= __cac_api_cli__tasks() { +>>>>>>> 0809fae379239687ed1170e04311dc2880ef0aba:cac-api jq -r . $cac_tasks_cache } #? cac-api templates #? Print cached templates JSON. #? +<<<<<<< HEAD:cac +__cac_cli__templates() { + _cac_maybe_update +======= __cac_api_cli__templates() { +>>>>>>> 0809fae379239687ed1170e04311dc2880ef0aba:cac-api jq -r . $cac_templates_cache } #? cac-api resources #? Print CloudPRO resources JSON. #? +<<<<<<< HEAD:cac +__cac_cli__resources() { + _cac_maybe_update +======= __cac_api_cli__resources() { +>>>>>>> 0809fae379239687ed1170e04311dc2880ef0aba:cac-api jq -r . $cac_resources_cache } @@ -155,6 +147,7 @@ __cac_api_cli__resources() { #? __cac_api_cli__update() {( umask 0077 + pids="" for x in \ resources \ servers \ @@ -168,15 +161,21 @@ __cac_api_cli__update() {( echo $json | jq . > "$file".tmp mv "$file".tmp "$file" } & + pids="$pids $!" done - wait + for pid in $pids; do wait $pid;done )} #? cac-api getserver SERVERSPEC #? Print cached server JSON. #? +<<<<<<< HEAD:cac +__cac_cli__getserver() {( + _cac_maybe_update +======= __cac_api_cli__getserver() {( +>>>>>>> 0809fae379239687ed1170e04311dc2880ef0aba:cac-api case $1 in *:*) k=${1%%:*} @@ -187,7 +186,6 @@ __cac_api_cli__getserver() {( v=${1#*:} ;; esac - if result=$(jq \ -e \ --arg k "$k" \ @@ -310,9 +308,30 @@ __cac_api_cli__waitstatus() { ;; esac - echo "$(date -Is) Waiting for status: $2; current status: $status ..." >&2 + # cac_always_update is not compatible with inotifywait + case $cac_always_update in + true) + echo "$(date -Is) Waiting ${cac_update_interval}s for status: $2; current status: $status ..." + sleep ${cac_update_interval} + __cac_cli__waitstatus "$@" + ;; + false) + echo "$(date -Is) Waiting for status: $2; current status: $status ..." >&2 + __cac_cli__waitforcacheupdate __cac_cli__waitstatus "$@" + ;; + esac +} +<<<<<<< HEAD:cac +# helper to implement cac_always_update +_cac_maybe_update() { + case $cac_always_update in true) + echo "$(date -Is) updating cache file" >&2 + __cac_cli__update + esac +======= __cac_api_cli__waitforcacheupdate __cac_api_cli__waitstatus "$@" +>>>>>>> 0809fae379239687ed1170e04311dc2880ef0aba:cac-api } @@ -369,7 +388,22 @@ __cac_api_cli__delete() {( _cac_handle_reply 'cac-api delete' "$reply" )} - +#? +#? Uses the following `environment variables`: +#? cac_resources_cache=$HOME/tmp/cac_resources_cache.json +#? cac_servers_cache=$HOME/tmp/cac_servers_cache.json +#? cac_tasks_cache=$HOME/tmp/cac_tasks_cache.json +#? cac_templates_cache=$HOME/tmp/cac_templates_cache.json +#? cac_secrets=$HOME/.secrets/cac +#? cac_always_update=false < set to true to always update when using cache files +#? cac_update_interval=10 < interval (in s) used by `cac waitstatus` +#? run=true < set to false to be able to source this file +#? +#? You can override these by setting them beforehand. +#? +#? `cac_secrets` will be sourced and may provide the following two entries: +#? cac_login=<cac-email> +#? cac_key=<cac-api-key> #? #? SERVERSPEC is a query like "mode:Safe", "sdate:08/04/2015", etc. #? See `cac-api servers` to get an inspiration. @@ -426,16 +460,6 @@ _cac_fetch_resources() {( )} -# rsyncfiles : lines filename |> local-dir x rsync-target -> ? |> ? -rsyncfiles() {( - set -x - rsync \ - --rsync-path="mkdir -p \"$2\" && rsync" \ - -vzrlptD \ - --files-from=- \ - "$1"/ \ - "$2" -)} @@ -484,10 +508,75 @@ _cac_exec() { fi } +# +# imported: +urlencode() { +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 +' +} +netmask_to_prefix() {( + set -euf + netmask=$1 + 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 +)} + +# rsyncfiles : lines filename |> local-dir x rsync-target -> ? |> ? +rsyncfiles() {( + set -x + rsync \ + --rsync-path="mkdir -p \"$2\" && rsync" \ + -vzrlptD \ + --files-from=- \ + "$1"/ \ + "$2" +)} + +# +<<<<<<< HEAD:cac +_cac_main "$@" +======= case ${run-true} in true) cac_api "$@";; esac +>>>>>>> 0809fae379239687ed1170e04311dc2880ef0aba:cac-api |