Skip to content

Instantly share code, notes, and snippets.

@gordyt
Created March 26, 2020 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gordyt/13496a785cee658d46152f0d18847e73 to your computer and use it in GitHub Desktop.
Save gordyt/13496a785cee658d46152f0d18847e73 to your computer and use it in GitHub Desktop.
Script that allows developers to control their development machine in oracle
#!/bin/bash
# Convenience Wrapper arounds the oracle CLI commands needed
# to query/stop/start your development machine in oracle
#
# 1. You must have the OCI CLI tools installed and configured.
# 2. Create $HOME/.oracle-ctl file and add the following:
#
# PROFILE_NAME=<your-desired-profile-name>
# INSTANCE_ID=<the-ocid-of-your-instance>
BN=$(basename "$0")
RC_FILE="${HOME}/.${BN}"
if [ -f "${RC_FILE}" ]; then
echo "Applying stored config from ${RC_FILE}. This can be overridden by command-line args."
# shellcheck source=/dev/null
source "${RC_FILE}"
else
echo "Did not find stored config in ${RC_FILE}"
fi
profile="${PROFILE_NAME}"
instance="${INSTANCE_ID}"
cmd=
COMMANDS=(
query
start
stop
)
if ! command -v oci > /dev/null; then
echo "Please make sure the OCI CLI tools are installed and that the 'oci' command is on your PATH"
exit 1
fi
if ! command -v jq > /dev/null; then
echo "Please make sure the 'jq' command is installed and on your PATH"
exit 1
fi
#########################################################################
# Print usage and exit
#########################################################################
function usage {
echo "Usage: $0 -p <profile-name> -i <instance-id> CMD"
echo ""
echo "If you have set the following in $RC_FILE then you do not"
echo "need to provide them as arguments:"
echo ""
echo "> PROFILE_NAME"
echo "> INSTANCE_ID"
echo ""
echo "Valid Commands:"
echo ""
for c in "${COMMANDS[@]}"; do
echo "> $c"
done
exit 1
}
#
# Parse args
#
while getopts ":i:p:h" opt; do
case ${opt} in
i )
instance=$OPTARG
;;
p )
profile=$OPTARG
;;
h )
usage
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
usage
;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
usage
;;
esac
done
shift $((OPTIND -1))
if [ $# -ne 1 ] ; then
echo "Need to specify exactly one command."
usage
fi
cmd="$1"
if ! echo "${COMMANDS[@]}" | grep -q "$cmd"; then
echo "Invalid command '${cmd}'"
usage
fi
if [ -s "$profile" ]; then
echo "Missing profile"
usage
fi
if [ -s "$instance" ]; then
echo "Missing instance"
usage
fi
function do_query {
echo "Querying InstanceId '${instance}' with OCI CLI profile '${profile}'"
oci --profile "${profile}" compute instance get --instance-id "${instance}" | jq '.data | ."lifecycle-state"'
}
function do_start {
echo "Starting InstanceId '${instance}' with OCI CLI profile '${profile}'"
oci --profile "${profile}" compute instance action --instance-id "${instance}" --action START | jq '.data | ."lifecycle-state"'
echo ""
echo "It will take a while for it to be available, even after it shows it is running."
echo "Once you can SSH in with your public IP then everything should be up."
}
function do_stop {
echo "Stopping InstanceId '${instance}' with OCI CLI profile '${profile}'"
oci --profile "${profile}" compute instance action --instance-id "${instance}" --action STOP | jq '.data | ."lifecycle-state"'
}
cmd_fun="do_${cmd}"
eval "$cmd_fun"
@gordyt
Copy link
Author

gordyt commented Mar 26, 2020

Prereqs

Download this script

$ mkdir -p ~/bin
$ curl -o ~/bin/oracle-ctl https://gist.githubusercontent.com/gordyt/13496a785cee658d46152f0d18847e73/raw/1c01fa99b22264aa7007759e7ec6cabc0c9c9891/oracle-ctl
$ chmod +x ~/bin/oracle-ctl

Create the init file for the script

  • If you do this you will not have to specify command-line arguments all the time.
  • File: $HOME/.oracle-ctl

It should look like this:

PROFILE_NAME=<the-name-of-your-oracle-cli-profile>
INSTANCE_ID=<the-ocid-of-your-machine

Sample help output

Applying stored config from /Users/gordy/.oracle-ctl.  This can be overridden by command-line args.
Usage: /Users/gordy/bin/oracle-ctl -p <profile-name> -i <instance-id> CMD

If you have set the following in /Users/gordy/.oracle-ctl then you do not
need to provide them as arguments:

> PROFILE_NAME
> INSTANCE_ID

Valid Commands:

> query
> start
> stop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment