From 2a19b0ade0967ae0e9919fde722f370007699f5c Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 5 Aug 2015 23:00:08 +0200 Subject: cac update: fetch resources (import from Makefile) --- Makefile | 7 ------- cac | 54 ++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 34a27fe..f6f6614 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,6 @@ -cloudpro-resources: - @ set -eu; \ - . ~/.secrets/cac; \ - ssh -q $$cac_via -t curl -fsS \ - "https://panel.cloudatcost.com/api/v1/cloudpro/resources.php?key=$$cac_key\\&login=$$cac_login" \ - | jq -r . - # cpu=1/2/3/4/5/6/7/8/9 # ram=1024 (must be multiple of 4. ex. 1024 / 2048 / 3096) # storage=10/20/30/40/50 ... etc diff --git a/cac b/cac index c8151d4..08cc820 100755 --- a/cac +++ b/cac @@ -47,10 +47,13 @@ sed ' } # +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_templates_cache=${cac_templates_cache-$HOME/tmp/cac_templates_cache.json} + cac_secrets=${cac_secrets-$HOME/.secrets/cac} + . "$cac_secrets" >/dev/null 2>&1 || : @@ -93,15 +96,27 @@ __cac_cli__templates() { jq -r . $cac_templates_cache } +#? cac resources +#? Print CloudPRO resources JSON. +#? +__cac_cli__resources() { + jq -r . $cac_resources_cache +} + #? cac update -#? Fetch and cache servers and templates JSON. +#? Fetch and cache state JSON. #? __cac_cli__update() {( umask 0077 - for resource in servers templates; do + for x in \ + resources \ + servers \ + templates \ + # This line intentionally left blank. + do { - json=$(_cac_list$resource) - eval file=\$cac_${resource}_cache + json=$(_cac_fetch_$x) + eval file=\$cac_${x}_cache echo $json | jq . > "$file".tmp mv "$file".tmp "$file" } & @@ -347,30 +362,41 @@ __cac_cli__poll() { #? See sleep(1) for TIMESPEC. #? -_cac_listservers() {( - servers=$(_cac_get_api_v1 listservers) - status=$(echo $servers | jq -r .status) +_cac_fetch_servers() {( + res=$(_cac_get_api_v1 listservers) + status=$(echo $res | jq -r .status) if [ "$status" = ok ]; then - echo "$servers" | jq -r .data + echo "$res" | jq -r .data else - echo "cac_listservers: bad listservers status: $status" >&2 + echo "cac_fetch_servers: bad status: $status" >&2 exit 1 fi )} -_cac_listtemplates() {( - templates=$(_cac_get_api_v1 listtemplates) - status=$(echo $templates | jq -r .status) +_cac_fetch_templates() {( + res=$(_cac_get_api_v1 listtemplates) + status=$(echo $res | jq -r .status) if [ "$status" = ok ]; then - echo "$templates" | jq -r .data + echo "$res" | jq -r .data else - echo "cac_listtemplates: bad listtemplates status: $status" >&2 + echo "cac_fetch_templates: bad status: $status" >&2 exit 1 fi )} +_cac_fetch_resources() {( + res=$(_cac_get_api_v1 cloudpro/resources) + status=$(echo $res | jq -r .status) + + if [ "$status" = ok ]; then + echo "$res" | jq -r .data + else + echo "cac_resources: bad cloudpro/resources status: $status" >&2 + exit 1 + fi +)} # rsyncfiles : lines filename |> local-dir x rsync-target -> ? |> ? -- cgit v1.2.3