Last active
April 5, 2025 21:05
-
-
Save hyokosdeveloper/31220bdfb8927b18921846f65d8c626b to your computer and use it in GitHub Desktop.
BASH Utility for easy openstack tasks
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
# ** ref to open stack rc containing envvars and credentials ** | |
OPENSTACKRC=./openstackrc.sh | |
# | |
# Functions | |
# | |
connect(){ | |
INSTANCE=$1 | |
if [ -n ${INSTANCE} ]; then | |
if [ "$INSTANCE" == "megatron" ]; then | |
echo "Connecting shell to instance $INSTANCE ..." | |
ssh -i ~/.ssh/decepticons.pem [email protected] | |
elif [ "$INSTANCE" = "starscream" ]; then | |
echo "Connecting shell to instance $INSTANCE ..." | |
ssh -i ~/.ssh/decepticons.pem ubuntu@starscream | |
else | |
echo "Unable to connect - Unknown instance name." | |
fi | |
else | |
echo "You must pass an instance name to call this action." | |
fi | |
} | |
info() { | |
echo "Running open stack setup..." | |
source $OPENSTACKRC | |
echo "Listing servers..." | |
openstack server list | |
echo "Listing available images..." | |
openstack image list --status active | |
echo "Listing volumes..." | |
openstack volume list | |
echo "Listing available keypairs..." | |
openstack keypair list | |
} | |
showSizes() { | |
echo "Running open stack setup..." | |
source $OPENSTACKRC | |
echo "Retrieving available sizes..." | |
openstack flavor list | |
} | |
details(){ | |
INSTANCE=$1 | |
if [ -n ${INSTANCE} ]; then | |
echo "Running open stack setup..." | |
source $OPENSTACKRC | |
echo "Showing details for instance $INSTANCE ..." | |
openstack server show $INSTANCE | |
else | |
echo "You must pass an instance name to call this action." | |
fi | |
} | |
stop(){ | |
INSTANCE=$1 | |
if [ -n ${INSTANCE} ]; then | |
echo "Running open stack setup..." | |
source $OPENSTACKRC | |
echo "Stopping instance $INSTANCE ..." | |
openstack server stop $INSTANCE | |
else | |
echo "You must pass an instance name to call this action." | |
fi | |
} | |
start(){ | |
INSTANCE=$1 | |
if [ -n ${INSTANCE} ]; then | |
echo "Running open stack setup..." | |
source $OPENSTACKRC | |
echo "Starting instance $INSTANCE ..." | |
openstack server start $INSTANCE | |
else | |
echo "You must pass an instance name to call this action." | |
fi | |
} | |
pause(){ | |
INSTANCE=$1 | |
if [ -n ${INSTANCE} ]; then | |
echo "Running open stack setup..." | |
source $OPENSTACKRC | |
echo "Pausing instance $INSTANCE ..." | |
openstack server pause $INSTANCE | |
else | |
echo "You must pass an instance name to call this action." | |
fi | |
} | |
unpause(){ | |
INSTANCE=$1 | |
if [ -n ${INSTANCE} ]; then | |
echo "Running open stack setup..." | |
source $OPENSTACKRC | |
echo "UnPausing instance $INSTANCE ..." | |
openstack server unpause $INSTANCE | |
else | |
echo "You must pass an instance name to call this action." | |
fi | |
} | |
suspend(){ | |
INSTANCE=$1 | |
if [ -n ${INSTANCE} ]; then | |
echo "Running open stack setup..." | |
source $OPENSTACKRC | |
echo "Suspending instance $INSTANCE ..." | |
openstack server suspend $INSTANCE | |
else | |
echo "You must pass an instance name to call this action." | |
fi | |
} | |
resume(){ | |
INSTANCE=$1 | |
if [ -n ${INSTANCE} ]; then | |
echo "Running open stack setup..." | |
source $OPENSTACKRC | |
echo "Resuming instance $INSTANCE ..." | |
openstack server resume $INSTANCE | |
else | |
echo "You must pass an instance name to call this action." | |
fi | |
} | |
rebootSoft(){ | |
INSTANCE=$1 | |
if [ -n ${INSTANCE} ]; then | |
echo "Running open stack setup..." | |
source $OPENSTACKRC | |
echo "Performing SOFT Reboot on instance $INSTANCE ..." | |
openstack server reboot --wait --soft $INSTANCE | |
else | |
echo "You must pass an instance name to call this action." | |
fi | |
} | |
rebootHard(){ | |
INSTANCE=$1 | |
if [ -n ${INSTANCE} ]; then | |
echo "Running open stack setup..." | |
source $OPENSTACKRC | |
echo "Performing HARD Reboot on instance $INSTANCE ..." | |
openstack server reboot --wait --hard $INSTANCE | |
else | |
echo "You must pass an instance name to call this action." | |
fi | |
} | |
rebuild(){ | |
INSTANCE=$1 | |
if [ -n ${INSTANCE} ]; then | |
echo "Running open stack setup..." | |
source $OPENSTACKRC | |
echo "Performing full REBUILD for instance $INSTANCE ..." | |
openstack server rebuild --wait $INSTANCE | |
else | |
echo "You must pass an instance name to call this action." | |
fi | |
} | |
lock(){ | |
INSTANCE=$1 | |
if [ -n ${INSTANCE} ]; then | |
echo "Running open stack setup..." | |
source $OPENSTACKRC | |
echo "Locking instance $INSTANCE ..." | |
openstack server lock $INSTANCE | |
else | |
echo "You must pass an instance name to call this action." | |
fi | |
} | |
unlock(){ | |
INSTANCE=$1 | |
if [ -n ${INSTANCE} ]; then | |
echo "Running open stack setup..." | |
source $OPENSTACKRC | |
echo "Unlocking instance $INSTANCE ..." | |
openstack server unlock $INSTANCE | |
else | |
echo "You must pass an instance name to call this action." | |
fi | |
} | |
createInstance(){ | |
INSTANCE=$1 | |
KEYNAME=$2 | |
IMAGE=$3 | |
FLAVOR=$4 | |
if [[ -n ${INSTANCE} ]] && [[ -n ${NAME} ]] && [[ -n ${IMAGE} ]] && [[ -n ${FLAVOR} ]]; then | |
echo "Running open stack setup..." | |
source $OPENSTACKRC | |
echo "Creating new instance $INSTANCE from $IMAGE and flavor $FLAVOR with key $KEYNAME" | |
openstack server create --key-name $KEYNAME --image $IMAGE --flavor $FLAVOR $INSTANCE | |
else | |
echo "You must pass an instance name, image, flavor and keyname to call this action." | |
fi | |
} | |
snapshot(){ | |
INSTANCE=$1 | |
NAME=$2 | |
if [[ -n ${INSTANCE} ]] && [[ -n ${NAME} ]]; then | |
echo "Running open stack setup..." | |
source $OPENSTACKRC | |
echo "Creating snapshot $NAME of instance $INSTANCE ..." | |
openstack server image create --name $NAME --wait $INSTANCE | |
echo "Verifying snapshot..." | |
openstack image show $NAME | |
else | |
echo "You must pass an instance name and snapshot name to call this action." | |
fi | |
} | |
resize(){ | |
INSTANCE=$1 | |
SIZE=$2 | |
if [[ -n ${INSTANCE} ]] && [[ -n ${SIZE} ]]; then | |
echo "Running open stack setup..." | |
source $OPENSTACKRC | |
echo "Resizing instance $INSTANCE to size $SIZE" | |
openstack server resize --flavor $SIZE --wait $INSTANCE | |
echo "Confirm resize..." | |
openstack server resize --confirm $INSTANCE | |
echo "Check instance status..." | |
openstack server show $INSTANCE -c status | |
else | |
echo "You must pass an instance name and snapshot name to call this action." | |
fi | |
} | |
status(){ | |
INSTANCE=$1 | |
if [ -n ${INSTANCE} ]; then | |
echo "Running open stack setup..." | |
source $OPENSTACKRC | |
echo "Check instance status..." | |
openstack server show $INSTANCE -c status | |
else | |
echo "You must pass an instance name to call this action." | |
fi | |
} | |
usage() { | |
echo 'OpenStack Admin Utility by @hyokos' | |
echo 'Version 1.0.0' | |
echo '' | |
echo 'Usage is: hStack info' | |
echo ' showSizes' | |
echo ' connect <instance>' | |
echo ' details <instance>' | |
echo ' stop <instance>' | |
echo ' start <instance>' | |
echo ' suspend <intance>' | |
echo ' resume <instance>' | |
echo ' pause <instance>' | |
echo ' unpause <instance>' | |
echo ' lock <instance>' | |
echo ' unlock <instance>' | |
echo ' rebootSoft <instance>' | |
echo ' rebootHard <instance>' | |
echo ' rebuild <instance>' | |
echo ' snapshot <instance> <name>' | |
echo ' resize <instance> <size>' | |
echo ' status <instance>' | |
echo ' createInstance <instance> <keyName> <image> <flavor>' | |
echo '' | |
echo "Where: info - List instances, volumes, images, and keypairs'" | |
echo ' showSizes - List available size and codes for resizing instances' | |
echo ' connect - Open remote shell to instance <instance>' | |
echo ' details - Show detailed information about instance <instance>' | |
echo ' stop - Stop <instance>' | |
echo ' start - Start <instance>' | |
echo ' suspend - Store state of <instance> on disk' | |
echo ' resume - Resume <instance>' | |
echo ' pause - Store state of <instance> in memory' | |
echo ' unpause - UnPause <instance>' | |
echo ' lock - Lock <instance> so that only admins can make changes' | |
echo ' unlock - UnLock <instance> so that not only admins can make changes' | |
echo ' rebootSoft - Perform a soft reboot on <instance>' | |
echo ' rebootHard - Perform a hard reboot on <instance>' | |
echo ' rebuild - Rebuild os from image' | |
echo ' snapshot - Create a instance snapshot <name> of <instance>' | |
echo ' resize - Resize instance <instance> to size <size>' | |
echo ' status - Get current status of <instance>' | |
echo ' createInstance - New <flavor> instance <instanceName> from <image> using <keyName>' | |
exit 1 | |
} | |
missingParams() { | |
echo " " | |
echo " ****************************** " | |
echo "You must pass an instance name to call this action." | |
echo " ****************************** " | |
echo " " | |
echo " See usage info below for command requirments" | |
echo " " | |
echo " " | |
usage | |
} | |
# | |
# Main Program | |
# | |
COMMAND=$1 | |
if [ -z ${COMMAND} ]; then | |
usage | |
exit 1 | |
fi | |
shift | |
case "${COMMAND}" in | |
info) | |
info | |
;; | |
showSizes) | |
showSizes | |
;; | |
connect) | |
[ $# = 1 ] || missingParams | |
connect "$@" | |
;; | |
details) | |
[ $# = 1 ] || missingParams | |
details "$@" | |
;; | |
stop) | |
[ $# = 1 ] || missingParams | |
stop "$@" | |
;; | |
start) | |
[ $# = 1 ] || usage | |
start "$@" | |
;; | |
suspend) | |
[ $# = 1 ] || usage | |
suspend "$@" | |
;; | |
resume) | |
[ $# = 1 ] || usage | |
resume "$@" | |
;; | |
pause) | |
[ $# = 1 ] || usage | |
pause "$@" | |
;; | |
unpause) | |
[ $# = 1 ] || usage | |
unpause "$@" | |
;; | |
lock) | |
[ $# = 1 ] || usage | |
lock "$@" | |
;; | |
unlock) | |
[ $# = 1 ] || usage | |
unlock "$@" | |
;; | |
rebootSoft) | |
[ $# = 1 ] || usage | |
rebootSoft "$@" | |
;; | |
rebootHard) | |
[ $# = 1 ] || usage | |
rebootHard "$@" | |
;; | |
rebuild) | |
[ $# = 1 ] || usage | |
rebuild "$@" | |
;; | |
snapshot) | |
[ $# = 1 ] || [ $# = 2 ] || usage | |
snapshot "$@" | |
;; | |
resize) | |
[ $# = 1 ] || [ $# = 2 ] || usage | |
resize "$@" | |
;; | |
status) | |
[ $# = 1 ] || usage | |
status "$@" | |
;; | |
createInstance) | |
[ $# = 1 ] || [ $# = 2 ] || [ $# = 3 ] || [ $# = 4 ] || usage | |
createInstance "$@" | |
;; | |
*) | |
usage | |
;; | |
esac |
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 | |
#export OS_USERNAME= | |
export OS_PASSWORD= | |
#export OS_PROJECT_NAME= | |
export OS_PROJECT_DOMAIN_ID=default | |
export OS_USER_DOMAIN_ID=default | |
#export OS_IDENTITY_API_VERSION=3 | |
export OS_AUTH_URL= | |
# With the addition of Keystone we have standardized on the term **project** | |
# as the entity that owns the resources. | |
export OS_PROJECT_ID= | |
export OS_PROJECT_NAME= | |
export OS_USER_DOMAIN_NAME="Default" | |
if [ -z "$OS_USER_DOMAIN_NAME" ]; then unset OS_USER_DOMAIN_NAME; fi | |
# unset v2.0 items in case set | |
unset OS_TENANT_ID | |
unset OS_TENANT_NAME | |
# In addition to the owning entity (tenant), OpenStack stores the entity | |
# performing the action as the **user**. | |
export OS_USERNAME= | |
# With Keystone you pass the keystone password. | |
echo "Please enter your OpenStack Password for project $OS_PROJECT_NAME as user $OS_USERNAME: " | |
read -sr OS_PASSWORD_INPUT | |
export OS_PASSWORD=$OS_PASSWORD_INPUT | |
# If your configuration has multiple regions, we set that information here. | |
# OS_REGION_NAME is optional and only valid in certain environments. | |
export OS_REGION_NAME="RegionOne" | |
# Don't leave a blank variable, unset it if it was empty | |
if [ -z "$OS_REGION_NAME" ]; then unset OS_REGION_NAME; fi | |
export OS_INTERFACE=public | |
export OS_IDENTITY_API_VERSION=3 | |
echo "Connected to open stack..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment