diff options
author | tv <tv@krebsco.de> | 2016-02-04 00:59:38 +0100 |
---|---|---|
committer | tv <tv@krebsco.de> | 2016-02-04 00:59:38 +0100 |
commit | bb46986791047de9229356a825c73d69794434d4 (patch) | |
tree | 2a5ad82c2dd07f2c4d58f4c9f27e66516bd75d2d | |
parent | 22acc1b990ac7d97c16344fbcbc2621e24cdf915 (diff) | |
parent | 0809fae379239687ed1170e04311dc2880ef0aba (diff) |
-rwxr-xr-x | cac-api (renamed from cac) | 154 |
1 files changed, 100 insertions, 54 deletions
@@ -1,6 +1,6 @@ #! /usr/bin/env bash # -#? cac - CloudAtCost command line interface +#? cac-api - CloudAtCost API command line interface #? #? Usage: #? @@ -13,6 +13,7 @@ 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} @@ -34,13 +35,26 @@ _cac_main() { shift || : __cac_cli__"$__cac_cli__command" "$@" esac +======= + +cac_secrets=${cac_secrets-$HOME/.secrets/cac-api} + + +. "$cac_secrets" >/dev/null 2>&1 || : + + +cac_api() { + __cac_api_cli__command=${1-help} + shift || : + __cac_api_cli__"$__cac_api_cli__command" "$@" +>>>>>>> 0809fae379239687ed1170e04311dc2880ef0aba:cac-api } -#? cac help [REGEX] +#? cac-api help [REGEX] #? Show help message. If a regex is specified, then show usage of matching #? commands. #? -__cac_cli__help() {( +__cac_api_cli__help() {( regex=${1-} # test -t expects GNU coreutils @@ -64,58 +78,74 @@ __cac_cli__help() {( s/^#?\( \(.*\)\)\?/\2/p ' else - __cac_cli__help | sed -n ' - /^cac '"$regex"'/,/^$/p + __cac_api_cli__help | sed -n ' + /^cac-api '"$regex"'/,/^$/p ' fi < "$0" | filter )} -#? cac console SERVERSPEC +#? cac-api console SERVERSPEC #? Print console URL. #? -__cac_cli__console() {( - server=$(__cac_cli__getserver "$1") +__cac_api_cli__console() {( + server=$(__cac_api_cli__getserver "$1") sid=$(echo $server | jq -r .sid) # TODO check reply status == ok _cac_post_api_v1 console sid="$sid" | jq -r .console )} -#? cac servers +#? 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 tasks +#? 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 templates +#? 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 resources +#? 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 } -#? cac update +#? cac-api update #? Fetch and cache state JSON. #? -__cac_cli__update() {( +__cac_api_cli__update() {( umask 0077 pids="" for x in \ @@ -136,11 +166,16 @@ __cac_cli__update() {( for pid in $pids; do wait $pid;done )} -#? cac getserver SERVERSPEC +#? 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%%:*} @@ -171,10 +206,11 @@ __cac_cli__getserver() {( fi )} -#? cac generatenetworking SERVERSPEC +#? cac-api generatenetworking SERVERSPEC +#? Generate NixOS module with networking configuration. #? -__cac_cli__generatenetworking() {( - server=$(__cac_cli__getserver "$1") +__cac_api_cli__generatenetworking() {( + server=$(__cac_api_cli__getserver "$1") address=$(echo $server | jq -r .ip) gateway=$(echo $server | jq -r .gateway) @@ -198,50 +234,50 @@ __cac_cli__generatenetworking() {( printf '}\n' )} -#? cac powerop SERVERSPEC (poweron|poweroff|reset) +#? cac-api powerop SERVERSPEC (poweron|poweroff|reset) #? Activate server power operations. #? -__cac_cli__powerop() {( - server=$(__cac_cli__getserver "$1") +__cac_api_cli__powerop() {( + server=$(__cac_api_cli__getserver "$1") action=$2 sid=$(echo $server | jq -r .sid) reply=$(_cac_post_api_v1 powerop sid="$sid" action="$action") - _cac_handle_reply 'cac powerop' "$reply" + _cac_handle_reply 'cac-api powerop' "$reply" )} -#? cac setlabel SERVERSPEC LABEL +#? cac-api setlabel SERVERSPEC LABEL #? -__cac_cli__setlabel() {( - server=$(__cac_cli__getserver "$1") +__cac_api_cli__setlabel() {( + server=$(__cac_api_cli__getserver "$1") label=$2 sid=$(echo $server | jq -r .sid) reply=$(_cac_post_api_v1 renameserver sid="$sid" name="$label") - _cac_handle_reply 'cac setlabel' "$reply" + _cac_handle_reply 'cac-api setlabel' "$reply" )} -#? cac setmode SERVERSPEC (normal|safe) +#? cac-api setmode SERVERSPEC (normal|safe) #? -__cac_cli__setmode() {( - server=$(__cac_cli__getserver "$1") +__cac_api_cli__setmode() {( + server=$(__cac_api_cli__getserver "$1") mode=$2 sid=$(echo $server | jq -r .sid) reply=$(_cac_post_api_v1 runmode sid="$sid" mode="$mode") - _cac_handle_reply 'cac setmode' "$reply" + _cac_handle_reply 'cac-api setmode' "$reply" )} -#? cac ssh SERVERSPEC +#? cac-api ssh SERVERSPEC #? -__cac_cli__ssh() {( - server=$(__cac_cli__getserver "$1") +__cac_api_cli__ssh() {( + server=$(__cac_api_cli__getserver "$1") shift address=$(echo $server | jq -r .ip) @@ -259,11 +295,11 @@ __cac_cli__ssh() {( )} -#? cac waitstatus SERVERSPEC ("Powered On"|...) +#? cac-api waitstatus SERVERSPEC ("Powered On"|...) #? Blocks until server has specfied state. #? -__cac_cli__waitstatus() { - server=$(__cac_cli__getserver "$1") +__cac_api_cli__waitstatus() { + server=$(__cac_api_cli__getserver "$1") status=$(echo $server | jq -r .status) case $status in @@ -286,46 +322,50 @@ __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 } -# XXX for __cac_cli__waitforcacheupdate and __cac_cli__poll cache means $cac_servers_cache +# XXX for __cac_api_cli__waitforcacheupdate and __cac_api_cli__poll cache means $cac_servers_cache -#? cac waitforcacheupdate COMMAND [ARGS...] +#? cac-api waitforcacheupdate COMMAND [ARGS...] #? Blocks until cache has been updated then executes "$@". #? -__cac_cli__waitforcacheupdate() { +__cac_api_cli__waitforcacheupdate() { case $(inotifywait --format %f -q -e moved_to $(dirname $cac_servers_cache)) in $(basename $cac_servers_cache)) "$@";; - *) __cac_cli__waitforcacheupdate "$@";; + *) __cac_api_cli__waitforcacheupdate "$@";; esac } -#? cac poll [TIMESPEC=1m] +#? cac-api poll [TIMESPEC=1m] #? Continuously update cache, sleeping at least $1 between updates. #? -__cac_cli__poll() { - __cac_cli__update +__cac_api_cli__poll() { + __cac_api_cli__update t=${1-1m} echo "$(date -Is) cache updated; sleeping $t ..." >&2 sleep "$t" - __cac_cli__poll "$@" + __cac_api_cli__poll "$@" } -#? cac build cpu=.. ram=.. storage=.. os=.. +#? cac-api build cpu=.. ram=.. storage=.. os=.. #? Build a server from available resources. #? cpu = 1/2/3/4/5/6/7/8 limit: 16 #? ram = 1024 (must be multiple of 4. ex. 1024 / 2048 / 3096) limit: 32768 #? storage = 10/20/30/40/50 ... etc limit: 1000 -#? os = 75 (must be an #id from `cac templates`) +#? os = 75 (must be an #id from `cac-api templates`) #? -__cac_cli__build() {( +__cac_api_cli__build() {( reply=$(export "$@"; _cac_post_api_v1 cloudpro/build \ cpu="$cpu" \ ram="$ram" \ @@ -333,19 +373,19 @@ __cac_cli__build() {( os="$os" \ ) - _cac_handle_reply 'cac build' "$reply" + _cac_handle_reply 'cac-api build' "$reply" )} -#? cac delete SERVERSPEC +#? cac-api delete SERVERSPEC #? Delete / terminate server to add resources. #? -__cac_cli__delete() {( - server=$(__cac_cli__getserver "$1") +__cac_api_cli__delete() {( + server=$(__cac_api_cli__getserver "$1") sid=$(echo $server | jq -r .sid) reply=$(_cac_post_api_v1 cloudpro/delete sid="$sid") - _cac_handle_reply 'cac delete' "$reply" + _cac_handle_reply 'cac-api delete' "$reply" )} #? @@ -366,7 +406,7 @@ __cac_cli__delete() {( #? cac_key=<cac-api-key> #? #? SERVERSPEC is a query like "mode:Safe", "sdate:08/04/2015", etc. -#? See `cac servers` to get an inspiration. +#? See `cac-api servers` to get an inspiration. #? #? See sleep(1) for TIMESPEC. #? @@ -431,7 +471,7 @@ _cac_handle_reply() {( case $(echo $reply | jq -r .status) in ok) echo $reply | jq -r . >&2 - __cac_cli__update + __cac_api_cli__update ;; *) echo $label: bad reply: >&2 @@ -533,4 +573,10 @@ rsyncfiles() {( # +<<<<<<< HEAD:cac _cac_main "$@" +======= +case ${run-true} in + true) cac_api "$@";; +esac +>>>>>>> 0809fae379239687ed1170e04311dc2880ef0aba:cac-api |