Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
The following scripts are tools I use on a regular basis to run simple activities on the K5 platform.

The following scripts are tools I use on a regular basis to run simple activities on the K5 platform. They can fairly easily be modified to work with OpenStack (and, probably in many cases, will work without modifications).

These are all written by me in my spare time, and are released under the WTFPL.

#!/bin/bash
# Author: Jon Spriggs <jon@sprig.gs>
# Created: 2017-06-09
# License: WTFPL (http://www.wtfpl.net/)
# The code in this library is from lots of different places.
# * The TempFile thing is from a random StackOverflow post that I've long since lost
# * GetOpts stuff is based (losely) on http://stackoverflow.com/a/15096408/5738
# * Reminding myself how to do functions in bash? http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_11_02.html
# * Processing the contents of a text file in a for/do/done loop? http://www.tldp.org/LDP/abs/html/abs-guide.html#LOOPS
# * Checking my Grep syntax: https://www.digitalocean.com/community/tutorials/using-grep-regular-expressions-to-search-for-text-patterns-in-linux
# Set the mask for the following actions to prevent anyone *else* from reading the following file
umask 0177
# Create the temp file
tmpfile=/tmp/"`basename "$0"`"."$$"."$(awk 'BEGIN {srand();printf "%d\n", rand() * 10^10}')"
# Tell you script to delete the file it just created when the script dies
trap 'rm -f -- "$tmpfile"' INT TERM HUP EXIT
renderhelp() {
echo "This script sets allowed address pairs on all machines in a project, optionally with a set prefix."
echo "If you don't know why you would need this, you probably don't. :)"
echo "Author: Jon Spriggs (jon.spriggs@uk.fujitsu.com) - E&CS Offerings Team"
echo "Created: 2017-04-07 against K5 UK-1 (Icehouse)"
echo ""
echo "Usage: $0 <-c CloudName> [-p Prefix]"
echo ""
echo "Example: $0 -c MyCloud -p RunTestA"
echo " Result: openstack port set --os-cloud MyCloud --allowed-address ip-address=0.0.0.0/0 RunTestA-Machine-Port-Network"
echo " $0 -c YourCloud"
echo " Result: openstack port set --os-cloud YourCloud --allowed-address ip-address=0.0.0.0/0 Machine-Port-Network"
exit 1
}
OptionsSet=False
while getopts hc:p: OPTION
do
case "$OPTION" in
c) OptionsSet=True ; CloudEntry="$OPTARG";;
p) PortPrefix="$OPTARG";;
esac
done
if [ -z "$CloudEntry" ]
then
if [ `env|grep [KO][5S]_ | grep -v PASSWORD | wc -l` -gt 1 ]
then
CloudEntry=envvars
else
renderhelp
fi
fi
openstack port list -q --os-cloud $CloudEntry -c Name --format csv 2>/dev/null | grep "Port" | cut -f2 -d\" | grep "^$PortPrefix" > "$tmpfile"
for portname in $(cat "$tmpfile")
do
echo Running: openstack port set -q --os-cloud $CloudEntry --allowed-address ip-address=0.0.0.0/1 --allowed-address ip-address=128.0.0.0/1 "$portname"
openstack port set -q --os-cloud $CloudEntry --allowed-address ip-address=0.0.0.0/1 --allowed-address ip-address=128.0.0.0/1 "$portname"
done
#!/bin/bash
# Author: Jon Spriggs <jon@sprig.gs>
# Created: 2017-06-23
# License: WTFPL (http://www.wtfpl.net/)
# Use this to run `cloudname_Console.sh <servername|serverprefix>` to open one or many NoVNC consoles to your servers
# Or it might just give you a copy-and-pastable list of consoles to open in another machine!
# You will probably end up with several of these - for example, I have "deConsole", "mgmtConsole" and "testConsole"
RENDER=$(which xdg-open)
if [ "$RENDER" == "" ]; then RENDER=echo; fi
if [ "$using" != "" ]; then RENDER="$using"; fi
for i in `cloudname_openstack.sh server list -c Name --format csv | grep -v "Name" | grep -i "$1" | cut -f2 -d\"`
do
echo "Opening console for $i"
$RENDER $(cloudname_openstack.sh console url show "$i" | grep https | cut -d\| -f3)
done
#!/bin/bash
# Author: Jon Spriggs <jon@sprig.gs>
# Created: 2017-06-23
# License: WTFPL (http://www.wtfpl.net/)
# Use this with the cloudname_Console.sh to reduce having to type... well, the following line :)
# You will probably end up with several of these - for example, I have "deopenstack", "mgmtopenstack"
# and "testopenstack"
openstack -q --os-cloud CloudName "$@"
#!/bin/bash
# Author: Jon Spriggs <jon@sprig.gs>
# Created: 2018-01-19
# License: WTFPL (http://www.wtfpl.net/)
if [ "$OS_CLOUD" == "" ]; then
if [ "$1" == "" ]; then
echo "Must provide the cloud to use - either as the OS_CLOUD variable, or as the first argument"
exit 1
else
OS_CLOUD=$1
fi
fi
openstack -q --os-cloud $OS_CLOUD server list -c Name -c Networks
openstack -q --os-cloud $OS_CLOUD volume list -c Name -c "Attached to"
openstack -q --os-cloud $OS_CLOUD router list -c Name
openstack -q --os-cloud $OS_CLOUD port list -c ID -c Name -c "Fixed IP"
openstack -q --os-cloud $OS_CLOUD network list -c Name
openstack -q --os-cloud $OS_CLOUD subnet list -c Name
openstack -q --os-cloud $OS_CLOUD security group list -c Name
openstack -q --os-cloud $OS_CLOUD floating ip list -c "Floating IP Address" -c "Fixed IP Address"
#!/bin/bash
# Author: Jon Spriggs <jon@sprig.gs>
# Created: 2018-01-19
# License: WTFPL (http://www.wtfpl.net/)
# The code in this library is from lots of different places.
# * The TempFile thing is from a random StackOverflow post that I've long since lost
# * GetOpts stuff is based (losely) on http://stackoverflow.com/a/15096408/5738
# * Reminding myself how to do functions in bash? http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_11_02.html
# Set the mask for the following actions to prevent anyone *else* from reading the following file
umask 0177
# Create the temp file
tmpfile=/tmp/"`basename "$0"`"."$$"."$(awk 'BEGIN {srand();printf "%d\n", rand() * 10^10}')"
# Tell you script to delete the file it just created when the script dies
trap 'rm -f -- "$tmpfile"' INT TERM HUP EXIT
renderhelp() {
echo "$0 - Bulk removes routers from K5 or OpenStack services"
echo ""
echo "-d - Debug"
echo "-c <cloudname> - Clouds.yml file entry. Uses env vars if not set. *rqd*"
echo "-r <name part> - Part of the name to match with grep to filter removals."
exit 1
}
echo_debug() {
if [ "$DEBUG" == "true" ]
then
echo "$1"
fi
}
FORCE=false
DEBUG=false
while getopts hdc:r: OPTION
do
case "$OPTION" in
h) renderhelp;;
d) DEBUG=true;;
c) CloudEntry="$OPTARG";;
r) RouterPart="$OPTARG";;
esac
done
if [ -z "$CloudEntry" ]
then
if [ ! "$OS_CLOUD" == "" ]
then
CloudEntry="$OS_CLOUD"
else
if [ $(env|grep [KO][5S]_ | wc -l) -gt 1 ]
then
CloudEntry=envvars
else
renderhelp
fi
fi
fi
echo_debug "Looking for routers to match"
openstack router list -q --os-cloud $CloudEntry -c ID -c Name --format csv 2>/dev/null | grep -v '"ID","Name"' > "$tmpfile"
for routerline in $(cat "$tmpfile")
do
routerID=$(echo $routerline | cut -f2 -d\")
routerName=$(echo $routerline | cut -f4 -d\")
if [ -z "$RouterPart" ] || echo "$routerName" | grep "$RouterPart" >/dev/null
then
echo_debug "Asking for floating IPs for router $routerID ($routerName)"
floatingip=$(openstack floating ip list -q --os-cloud $CloudEntry -c ID --router $routerID --format csv 2>/dev/null | grep -v '"ID"' | wc -l)
if [ $floatingip -eq 0 ]
then
echo_debug "Asking for ports for router $routerID ($routerName)"
portID=$(openstack port list -q --os-cloud $CloudEntry -c ID --router $routerID --format csv 2>/dev/null | grep -v '"ID"' | cut -f2 -d\")
echo_debug "Got portID: $portID for router $routerID ($routerName). Removing"
openstack router set --no-route -q --os-cloud $CloudEntry $routerID | grep -v "'NoneType' object is not iterable"
openstack router remove port -q --os-cloud $CloudEntry $routerID $portID
openstack router delete -q --os-cloud $CloudEntry $routerID
else
if [ $floatingip -gt 1 ]
then
echo "Unable to remove router $routerID ($routerName) due to $floatingip floating IPs. Please resolve and re-try."
else
echo "Unable to remove router $routerID ($routerName) due to a floating IP. Please resolve and re-try."
fi
fi
else
echo_debug "Found non-matching router $routerID with name $routerName"
fi
done
#!/bin/bash
# Author: Jon Spriggs <jon@sprig.gs>
# Created: 2018-01-19
# License: WTFPL (http://www.wtfpl.net/)
# The code in this library is from lots of different places.
# * The TempFile thing is from a random StackOverflow post that I've long since lost
# * GetOpts stuff is based (losely) on http://stackoverflow.com/a/15096408/5738
# * Reminding myself how to do functions in bash? http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_11_02.html
# Set the mask for the following actions to prevent anyone *else* from reading the following file
umask 0177
# Create the temp file
tmpfile=/tmp/"`basename "$0"`"."$$"."$(awk 'BEGIN {srand();printf "%d\n", rand() * 10^10}')"
tmpfile2="$tmpfile.2"
tmpfile3="$tmpfile.3"
# Tell you script to delete the file it just created when the script dies
trap 'rm -f -- "$tmpfile" "$tmpfile2" "$tmpfile3"' INT TERM HUP EXIT
renderhelp() {
echo "$0 - Bulk removes servers from K5 or OpenStack services"
echo ""
echo "-d - Debug"
echo "-c <cloudname> - Clouds.yml file entry. Uses env vars if not set. *rqd*"
echo "-s <name part> - Part of the name to match with grep to filter removals."
exit 1
}
echo_debug() {
if [ "$DEBUG" == "true" ]
then
echo "$1"
fi
}
FORCE=false
DEBUG=false
while getopts hdc:s: OPTION
do
case "$OPTION" in
h) renderhelp;;
d) DEBUG=true;;
c) CloudEntry="$OPTARG";;
s) ServerPart="$OPTARG";;
esac
done
if [ -z "$CloudEntry" ]
then
if [ ! "$OS_CLOUD" == "" ]
then
CloudEntry="$OS_CLOUD"
else
if [ $(env|grep [KO][5S]_ | wc -l) -gt 1 ]
then
CloudEntry=envvars
else
renderhelp
fi
fi
fi
echo_debug "Looking for servers to match"
openstack -q --os-cloud $CloudEntry server list -c ID -c Name --format csv 2>/dev/null | grep -v '"ID","Name"' | sed 's/ /£/g' > "$tmpfile"
echo_debug "Looking for volumes to match"
openstack -q --os-cloud $CloudEntry volume list -c ID -c Name -c "Attached to" --format csv 2>/dev/null | grep -v vda | grep -v '"ID","Name","Attached to"' | sed 's/ /£/g' > "$tmpfile2"
for serverline in $(cat "$tmpfile")
do
serverID="$(echo $serverline | cut -f2 -d\")"
mangledServerName="$(echo "$serverline" | cut -f4 -d\" )"
serverName="$(echo "$serverline" | cut -f4 -d\" | sed 's/£/ /g' )"
if [ -z "$ServerPart" ] || echo "$serverName" | grep "$ServerPart" >/dev/null
then
echo_debug "Asking for ports for server $serverID ($serverName)"
openstack -q --os-cloud $CloudEntry port list -c ID -c Name --server $serverID --format csv 2>/dev/null | grep -v '"ID","Name"' > "$tmpfile3"
echo_debug "Removing server $serverID ($serverName)"
openstack -q --os-cloud $CloudEntry server delete $serverID
for diskline in $(cat "$tmpfile2")
do
diskID="$(echo $diskline | cut -f2 -d\")"
diskName="$(echo $diskline | cut -f4 -d\")"
attachName="$(echo $diskline | cut -f6 -d\")"
if echo "$attachName" | grep "Attached£to£${mangledServerName}£on" >/dev/null
then
echo_debug "Removing volume id $diskID ($diskName)"
openstack -q --os-cloud $CloudEntry volume delete $diskID
fi
done
for portline in $(cat "$tmpfile3")
do
portID="$(echo $portline | cut -f2 -d\")"
portName="$(echo $portline | cut -f4 -d\")"
echo_debug "Removing port id $portID ($portName)"
openstack -q --os-cloud $CloudEntry port delete $portID
done
else
echo_debug "Skipping non-matching server $serverID with name $serverName"
fi
done
#!/bin/bash
# Author: Jon Spriggs <jon@sprig.gs>
# Created: 2017-05-31
# License: WTFPL (http://www.wtfpl.net/)
# The code in this library is from lots of different places.
# * The TempFile thing is from a random StackOverflow post that I've long since lost
# * GetOpts stuff is based (losely) on http://stackoverflow.com/a/15096408/5738
# * Reminding myself how to do functions in bash? http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_11_02.html
# * Processing the contents of a text file in a for/do/done loop? http://www.tldp.org/LDP/abs/html/abs-guide.html#LOOPS
# * Checking my Grep syntax: https://www.digitalocean.com/community/tutorials/using-grep-regular-expressions-to-search-for-text-patterns-in-linux
# Set the mask for the following actions to prevent anyone *else* from reading the following file
umask 0177
# Create the temp file
tmpfile=/tmp/"`basename "$0"`"."$$"."$(awk 'BEGIN {srand();printf "%d\n", rand() * 10^10}')"
# Tell you script to delete the file it just created when the script dies
trap 'rm -f -- "$tmpfile"' INT TERM HUP EXIT
renderhelp() {
echo "This script shelves all machines in a project, optionally with a set prefix."
echo "If you don't know why you would need this, you probably don't. :)"
echo "Author: Jon Spriggs (jon.spriggs@uk.fujitsu.com) - E&CS Offerings Team"
echo "Created: 2017-05-19 against K5 UK-1 (Icehouse)"
echo ""
echo "Usage: $0 <-c CloudName> [-p Prefix]"
echo ""
echo "Example: $0 -c MyCloud -p RunTestA"
echo " Result: openstack server shelve --os-cloud MyCloud RunTestA-Machine"
echo " $0 -c YourCloud"
echo " Result: openstack server shelve --os-cloud YourCloud Machine"
exit 1
}
OptionsSet=False
while getopts hc:p: OPTION
do
case "$OPTION" in
c) OptionsSet=True ; CloudEntry="$OPTARG";;
p) NamePrefix="$OPTARG";;
esac
done
if [ -z "$CloudEntry" ]
then
if [ `env|grep [KO][5S]_ | grep -v PASSWORD | wc -l` -gt 1 ]
then
CloudEntry=envvars
else
renderhelp
fi
fi
for i in `openstack server list -q --os-cloud $CloudEntry -c Name -c Status --format csv 2>/dev/null | grep "\"$NamePrefix" | grep "\"ACTIVE\"" | cut -f2 -d\"`
do
openstack server shelve --os-cloud $CloudEntry "$i"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment