Created
April 3, 2018 19:27
-
-
Save willcharlton/af697aeaa72ee4aebb12aa9e4b6530c0 to your computer and use it in GitHub Desktop.
Command-line wrapper for really nice Murano CLI + GWE + JQ scripting and response parsing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
[[ -z ${1} ]] && echo "provide device id" && exit 1 | |
function print_timestamp_stderr() { | |
jqq=".${1}.timestamp" | |
js="echo ${1} | jq -r '${jqq}'" | |
echo $js | |
ts=$(eval $js) | |
CMD="python -c 'from datetime import datetime as dt; print(dt.fromtimestamp(${ts}/1000000.0))'" | |
echo $CMD >&2 | |
eval $CMD >&2 | |
} | |
case "${1}" in | |
"gwshell") | |
[[ -z ${2} ]] && echo "gwshell: provide device ids from list:" >&2 && murano device list --json | jq -r '.devices[] | [.identity, .online, .state.stdin.set, .state.stdout.reported]' && exit 1 | |
[[ -z ${3} ]] && echo "gwshell: provide shell command to run" && exit 2 | |
CMD="murano --curl device write ${2} stdin='${3}'" | |
echo $CMD >&2 | |
eval $CMD >&2 | |
SLEEP=8 | |
[[ ! -z ${4} ]] && SLEEP=${4} | |
for i in $(seq 1 ${SLEEP}) ; do echo -n ". " >&2 ; sleep 1 ; done | |
CMD="murano device read ${2} stdout --json | jq -r '.stdout.reported'" | |
echo >&2 | |
echo $CMD >&2 | |
eval $CMD | |
exit 0 | |
;; | |
"ids") | |
CMD="murano device list --json | jq -r '.devices[].identity'" | |
echo $CMD >&2 | |
eval $CMD | |
exit 0 | |
;; | |
esac | |
case "${2}" in | |
"df") | |
CMD="murano device read ${1} device_info --json | jq -r '.device_info.reported | fromjson | .df'" | |
echo $CMD >&2 | |
eval $CMD | |
;; | |
"free") | |
CMD="murano device read ${1} device_info --json | jq -r '.device_info.reported | fromjson | .free'" | |
echo $CMD >&2 | |
eval $CMD | |
;; | |
"ppp0") | |
CMD="murano device read ${1} device_info --json | jq -r '.device_info.reported | fromjson | .ipaddrs | fromjson | .ppp0'" | |
echo $CMD >&2 | |
eval $CMD | |
;; | |
"eth0") | |
CMD="murano device read ${1} device_info --json | jq -r '.device_info.reported | fromjson | .ipaddrs | fromjson | .eth0'" | |
echo $CMD >&2 | |
eval $CMD | |
;; | |
"uptime") | |
[[ -z ${3} ]] && echo "provide app name" && exit 1 | |
CMD="murano device read ${1} engine_report --json | jq -r '.engine_report.reported | fromjson | .apps[].${3}.uptime' | grep -v null" | |
echo $CMD >&2 | |
eval $CMD | |
;; | |
"usage_report") | |
CMD="murano device read ${1} usage_report --json | jq -r '.usage_report.reported | fromjson'" | |
echo $CMD >&2 | |
eval $CMD | |
;; | |
"engine_report") | |
CMD="murano device read ${1} engine_report --json | jq -r '.engine_report.reported | fromjson'" | |
echo $CMD >&2 | |
eval $CMD | |
;; | |
"device_info") | |
CMD="murano device read ${1} device_info --json | jq -r '.device_info.reported | fromjson'" | |
echo $CMD >&2 | |
eval $CMD | |
;; | |
"engine_fetch") | |
CMD="murano device read ${1} engine_fetch --json | jq -r '.engine_fetch.set | fromjson'" | |
echo $CMD >&2 | |
eval $CMD | |
;; | |
"version") | |
[[ -z ${3} ]] && echo "provide app name" && exit 1 | |
CMD="murano device read ${1} engine_report --json | jq -r '.engine_report.reported | fromjson | .apps[].${3}.version' | grep -v null" | |
echo $CMD >&2 | |
eval $CMD | |
;; | |
"otau") | |
[[ -z ${3} ]] && echo "provide tarball name" && exit 1 | |
CMD="murano device write ${1} engine_fetch='{\"install\": [{\"name\": \"${3}\"}]}'" | |
echo $CMD | |
eval $CMD | |
;& | |
"otau_wait") | |
INIT=$(mrgwjq ${1} engine_fetch) | |
echo "Waiting for OTAU: ${INIT}..." >&2 | |
while [[ "$INIT" == "$(mrgwjq ${1} engine_fetch 2>/dev/null)" ]] | |
do | |
echo -n "." | |
sleep 1 | |
done | |
echo "Done" | |
mrgwjq ${1} status | |
;; | |
"status") | |
CMD="murano device read ${1} fetch_status --json | jq -r '.fetch_status.reported'" | |
echo $CMD >&2 | |
eval $CMD | |
;; | |
"timestamp") | |
[[ -z ${3} ]] && echo "provide resource name" && exit 1 | |
jqq=".${3}.timestamp" | |
mr="murano device read ${1} ${3} --json | jq -r '${jqq}'" | |
echo $mr >&2 | |
ts=$(eval $mr) | |
CMD="python -c 'from datetime import datetime as dt; print(dt.fromtimestamp(${ts}/1000000.0))'" | |
echo $CMD >&2 | |
eval $CMD | |
;; | |
*) | |
echo "usage: $0 (df|free|ppp0|eth0|uptime|version|otau|otau_wait|status|engine_report|engine_fetch|usage_report|device_info|timestamp|gwshell) [options]" | |
;; | |
esac | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment