Skip to content

Instantly share code, notes, and snippets.

@martyngigg
Last active July 21, 2023 08:12
Show Gist options
  • Save martyngigg/eed4ffde41d46e3197a3b5ac16066646 to your computer and use it in GitHub Desktop.
Save martyngigg/eed4ffde41d46e3197a3b5ac16066646 to your computer and use it in GitHub Desktop.
Very basic script to create/destroy openstack VMs
#!/usr/bin/env bash
# Control server(s) on an Openstack cloud.
# The openstack command must be available on the PATH.
#
# This script assumes a ~/.config/openstack/clouds.yaml
# exists containing credentials for the project that
# contains the servers. This can be created and downloaded
# from the Identity->Appplication Credentials section of the
# OpenStack UI.
# Project name on Openstack
PROJECTNAME=da-proto
# Server details
IMAGE=ubuntu-focal-20.04-nogui
FLAVOUR=l3.nano
SECGROUP=default
KEYNAME=martyngigg
INSTANCENAME_PREFIX=cloud
INSTANCECOUNT=2
# Functions
function up() {
openstack \
--os-cloud $PROJECTNAME \
server create \
--image $IMAGE \
--flavor $FLAVOUR \
--security-group $SECGROUP \
--key-name $KEYNAME \
--min $INSTANCECOUNT \
--max $INSTANCECOUNT \
$INSTANCENAME_PREFIX
echo "Note: The default openstack output above shows only a single instance even if more were requested."
sleep 2
echo "Full instance list:"
openstack \
--os-cloud $PROJECTNAME \
server list
}
function down() {
for i in `seq $INSTANCECOUNT`;
do
openstack \
--os-cloud $PROJECTNAME \
server delete $INSTANCENAME_PREFIX-$i;
done
}
function fatal() {
>&2 echo $1
exit 1
}
# is openstack command available?
if ! command -v "openstack" &> /dev/null; then
fatal "openstack command not available. Have you activated the environment?"
fi
case $1 in
up) up
;;
down) down
;;
*)
echo Unknown command $1. Use 'up' or 'down'.
esac
python-openstackclient
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment