Last active
November 23, 2018 04:30
-
-
Save dishbreak/f1dd403a6199b80d3a77ef61750ca064 to your computer and use it in GitHub Desktop.
This file contains 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 python3 | |
""" | |
Usage: | |
./server_control show -e ENVIRONMENT -a APPLICATION | |
./server_control terminate -e ENVIRONMENT -a APPLICATION | |
./server_control launch -e ENVIRONMENT -a APPLICATION | |
Options: | |
-e ENVIRONMENT Environment the server is in. Supported values are staging, production. | |
-a APPLICATION Application name. | |
Commands: | |
show Show instance metadata. | |
terminate Shut down the instances. | |
launch Create a new instance with the given tags. | |
""" | |
import sys | |
try: | |
from docopt import docopt | |
except ModuleNotFoundError: | |
print("\n".join(["You don't have docopt installed. Install it with the command:", | |
"\tpip install docopt"]), file=sys.stderr) | |
sys.exit(10) | |
def parse_args(argv): | |
args = {} | |
docopted_args = docopt(__doc__, argv=argv[1:]) | |
print(docopted_args) | |
for command in ["show", "terminate", "launch"]: | |
if docopted_args[command]: | |
args["command"] = command | |
break | |
accepted_environments = ["staging", "production"] | |
if docopted_args["-e"] not in accepted_environments: | |
print("Unrecognized environment '{}'. Supported environments are {}\n{}".format(docopted_args["-e"], | |
", ".join(accepted_environments), __doc__)) | |
sys.exit(11) | |
args["environment"] = docopted_args["-e"] | |
args["application"] = docopted_args["-a"] | |
return args | |
def main(): | |
args = parse_args(sys.argv) | |
print("Running command '{command}' in environment '{environment}' for application '{application}'".format(**args) | |
if __name__ == '__main__': | |
main() |
This file contains 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 | |
set -e | |
## Set a usage message | |
USAGE=$(cat <<EOF | |
Usage: | |
./server_control show -e ENVIRONMENT -a APPLICATION | |
./server_control terminate -e ENVIRONMENT -a APPLICATION | |
./server_control launch -e ENVIRONMENT -a APPLICATION | |
Options: | |
-e ENVIRONMENT Environment the server is in. Supported values are staging, production. | |
-a APPLICATION Application name. | |
Commands: | |
show Show instance metadata. | |
terminate Shut down the instances. | |
launch Create a new instance with the given tags. | |
EOF | |
) | |
## Exit quickly if there's not the right number of argments. | |
if [[ $# -ne 5 ]]; then | |
( >&2 echo "Need exactly 5 arguments. $USAGE") | |
exit 1 | |
fi | |
## Pop the positional argument off. | |
case "$1" in | |
show|terminate|launch ) | |
COMMAND=$1 | |
shift | |
;; | |
* ) | |
( >&2 echo "unrecognized command '$1' $USAGE") | |
exit 2 | |
;; | |
esac | |
## Parse the flags | |
APPLICATION="" | |
ENVIRONMENT="" | |
while getopts ":e:a:" opt; do | |
case ${opt} in | |
e ) | |
case "$OPTARG" in | |
staging|production ) | |
ENVIRONMENT=$OPTARG | |
;; | |
* ) | |
( >&2 echo "unsupported value '$OPTARG' $USAGE" ) | |
exit 3 | |
esac | |
;; | |
a ) | |
APPLICATION=$OPTARG | |
;; | |
: ) | |
( >&2 echo "-$OPTARG needs an argument $USAGE") | |
exit 4 | |
;; | |
\? ) | |
( >&2 echo "unrecognized option -$opt $USAGE") | |
exit 5 | |
;; | |
esac | |
done | |
shift $((OPTIND -1)) | |
## Validate the variables | |
if [[ -z "$ENVIRONMENT" ]] || [[ -z "$APPLICATION" ]]; then | |
( >&2 echo "need both environment and application specified $USAGE") | |
exit 5 | |
fi | |
echo "Running command '$COMMAND' in '$ENVIRONMENT' for application '$APPLICATION'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment