Last active
June 16, 2017 11:57
-
-
Save thbkrkr/781fed571e268e281ff97b976e6e6a2e to your computer and use it in GitHub Desktop.
A stupid CLI to interact with the Marathon API
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
| #!/bin/bash -eu | |
| help() { | |
| echo ' | |
| A cli to interact with the Marathon API | |
| Usage: | |
| ma-cli.sh [command] | |
| Available Commands: | |
| apps:ls List all apps | |
| apps:show Show an app | |
| apps:restart Restart an app | |
| apps:delete Delete an app | |
| deployments:ls List all deployments | |
| deploy <jsonFile> Deploy or redeploy an application | |
| ' | |
| } | |
| api() { | |
| declare uri=$1 && shift | |
| curl -s \ | |
| -H "Authorization: Basic $MARATHON_AUTH" "$MARATHON_API/$uri" \ | |
| -H 'content-type: application/json' \ | |
| $@ | |
| } | |
| apps:ls() { | |
| api v2/apps | jq '[.apps[] | {id:.id}]' | |
| } | |
| apps:show() { | |
| declare appID=$1 | |
| api v2/apps/$appID | jq '.app | { | |
| id: .id, cmd: .cmd, container: .container, labels: .labels, constraints: .constraints | |
| }' | |
| } | |
| apps:restart() { | |
| declare appID=$1 | |
| api v2/apps/$appID/restart | jq . | |
| } | |
| apps:delete() { | |
| declare appID=$1 | |
| api v2/apps/$appID -XDELETE | jq . | |
| } | |
| apps:deploy() { | |
| declare jsonFile=$1 options=${2:-} | |
| local appID=$(jq -r ".id" $jsonFile) | |
| # Replace secrets | |
| cp $jsonFile $jsonFile.tmp | |
| for var in $(grep = secrets/vars.env); do | |
| key=$(cut -d '=' -f1 <<< $var) | |
| value=$(cut -d '=' -f2 <<< $var) | |
| sed -i "s|\$$key|$value|g" $jsonFile.tmp | |
| done | |
| api "v2/apps/$appID$options" -XPUT -d @$jsonFile.tmp | |
| rm $jsonFile.tmp | |
| } | |
| deployments() { | |
| api v2/deployments | jq -c '.[] | .currentActions' | |
| } | |
| main() { | |
| declare cmd=${1:-""} && shift || true | |
| case $cmd in | |
| ls|apps:ls ) apps:ls ;; | |
| apps:show ) apps:show $1 ;; | |
| apps:restart ) apps:restart $1 ;; | |
| apps:delete ) apps:delete $1 ;; | |
| deployments:ls ) deployments ;; | |
| d|deploy ) | |
| declare jsonFile=$1 options=${2:-} | |
| apps:deploy $jsonFile $options ;; | |
| h|help ) help ;; | |
| ""|* ) echo "err: unknown command. Try apps:ls." ;; | |
| esac | |
| } | |
| main "$@" |
thbkrkr
commented
Jun 16, 2017
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment