Created
March 26, 2020 01:53
-
-
Save gordyt/13496a785cee658d46152f0d18847e73 to your computer and use it in GitHub Desktop.
Script that allows developers to control their development machine in oracle
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 | |
# 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" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prereqs
Download this script
Create the init file for the script
$HOME/.oracle-ctl
It should look like this:
Sample help output