summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xcac-api (renamed from cac)131
1 files changed, 66 insertions, 65 deletions
diff --git a/cac b/cac-api
index 25029e1..b653323 100755
--- a/cac
+++ b/cac-api
@@ -1,6 +1,6 @@
#! /bin/sh
#
-#? cac - CloudAtCost command line interface
+#? cac-api - CloudAtCost API command line interface
#?
#? Usage:
#?
@@ -66,23 +66,23 @@ 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}
-cac_secrets=${cac_secrets-$HOME/.secrets/cac}
+cac_secrets=${cac_secrets-$HOME/.secrets/cac-api}
. "$cac_secrets" >/dev/null 2>&1 || :
-cac() {
- __cac_cli__command=${1-help}
+cac_api() {
+ __cac_api_cli__command=${1-help}
shift || :
- __cac_cli__"$__cac_cli__command" "$@"
+ __cac_api_cli__"$__cac_api_cli__command" "$@"
}
-#? 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
@@ -106,54 +106,54 @@ __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.
#?
-__cac_cli__servers() {
+__cac_api_cli__servers() {
jq -r . $cac_servers_cache
}
-#? cac tasks
+#? cac-api tasks
#? Print cached tasks JSON.
#?
-__cac_cli__tasks() {
+__cac_api_cli__tasks() {
jq -r . $cac_tasks_cache
}
-#? cac templates
+#? cac-api templates
#? Print cached templates JSON.
#?
-__cac_cli__templates() {
+__cac_api_cli__templates() {
jq -r . $cac_templates_cache
}
-#? cac resources
+#? cac-api resources
#? Print CloudPRO resources JSON.
#?
-__cac_cli__resources() {
+__cac_api_cli__resources() {
jq -r . $cac_resources_cache
}
-#? cac update
+#? cac-api update
#? Fetch and cache state JSON.
#?
-__cac_cli__update() {(
+__cac_api_cli__update() {(
umask 0077
for x in \
resources \
@@ -172,10 +172,10 @@ __cac_cli__update() {(
wait
)}
-#? cac getserver SERVERSPEC
+#? cac-api getserver SERVERSPEC
#? Print cached server JSON.
#?
-__cac_cli__getserver() {(
+__cac_api_cli__getserver() {(
case $1 in
*:*)
@@ -208,10 +208,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)
@@ -235,50 +236,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)
@@ -296,11 +297,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
@@ -311,41 +312,41 @@ __cac_cli__waitstatus() {
echo "$(date -Is) Waiting for status: $2; current status: $status ..." >&2
- __cac_cli__waitforcacheupdate __cac_cli__waitstatus "$@"
+ __cac_api_cli__waitforcacheupdate __cac_api_cli__waitstatus "$@"
}
-# 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" \
@@ -353,25 +354,25 @@ __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"
)}
#?
#? 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.
#?
@@ -446,7 +447,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
@@ -488,5 +489,5 @@ _cac_exec() {
case ${run-true} in
- true) cac "$@";;
+ true) cac_api "$@";;
esac