Create a gist now

Instantly share code, notes, and snippets.

Embed
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: 2018-03-22
# License: WTFPL (http://www.wtfpl.net/)
# 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 actions for all clouds.yml entries"
echo ""
echo "-d - Debug"
echo "-c <cloudname> - Part of the Clouds.yml references"
echo "-r <region> - Part of the auth_url to select the region"
echo "-p - Just print the cloud name"
echo "-l - Run list_env.sh {{ cloudname }}"
exit 1
}
echo_debug() {
if [ "$DEBUG" == "true" ]
then
echo "$1"
fi
}
check_shyaml() {
if [ -z "$(which shyaml)" ]
then
echo "Warning: shyaml is not installed. Please install with \`pip install shyaml\`"
exit 1
fi
}
DEBUG=false
FileName="~/.config/openstack/clouds.yml"
action_print=false
action_list=false
while getopts hdlpc:r: OPTION
do
case "$OPTION" in
h) renderhelp;;
d) DEBUG=true;;
l) action_list=true;;
p) action_print=true;;
c) CloudEntry="$OPTARG";;
r) check_shyaml ; region="$OPTARG";;
esac
done
if [ -e /etc/openstack/clouds.yml ]
then
FileName=/etc/openstack/clouds.yml
fi
if [ -e "$HOME/.config/openstack/clouds.yml" ]
then
FileName="$HOME/.config/openstack/clouds.yml"
fi
if [ -e ./clouds.yml ]
then
FileName=./clouds.yml
fi
if [ "$action_list" == "false" ] && [ "$action_print" == "false" ]
then
echo "No action selected. Please select either print (-p) or list (-l)"
renderhelp
fi
echo_debug "Parsing $FileName"
grep "^ [A-Za-z0-9_-]*:" "$FileName" | cut -d\ -f3 | cut -d: -f1 > "$tmpfile"
for OS_CLOUD in $(cat "$tmpfile")
do
if [ -z "$CloudEntry" ] || [ -n "$(echo $OS_CLOUD | grep "$CloudEntry")" ]
then
if [ -z "$region" ] || [ -n "$(cat "$FileName" | shyaml get-value clouds.$OS_CLOUD.auth.auth_url | grep "$region")" ]
then
echo_debug "Using $OS_CLOUD"
if [ "$action_list" != "false" ]
then
list_env.sh -c "$OS_CLOUD"
fi
if [ "$action_print" != "false" ]
then
echo "$OS_CLOUD"
fi
else
echo_debug "Skipping $OS_CLOUD because it doesn't match the region filter $region"
fi
else
echo_debug "Skipping $OS_CLOUD because it doesn't match the name filter $CloudEntry"
fi
done
#!/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/)
renderhelp() {
echo "$0 - List the environment definition"
echo ""
echo "-c <cloudname> - clouds.yml file entry - overides the OS_CLOUD value"
echo "-o - List Compute values"
echo "-O - List Compute values in full detail"
echo "-v - List Volumes"
echo "-V - List Volumes in full detail"
echo "-r - List Routers"
echo "-R - List Routers in full detail"
echo "-p - List Ports"
echo "-P - List Ports in full detail"
echo "-n - List Networks"
echo "-N - List Networks in full detail"
echo "-s - List Subnets"
echo "-S - List Subnets in full detail"
echo "-g - List Security Groups"
echo "-G - List Security Groups in full detail"
echo "-f - List Floating (Global) IPs"
echo "-F - List Floating IPs in full detail"
echo "-A - Enable full detail for all selected modules"
exit 1
}
OPT_Compute=0
OPT_Compute_Full=0
OPT_Volume=0
OPT_Volume_Full=0
OPT_Router=0
OPT_Router_Full=0
OPT_Port=0
OPT_Port_Full=0
OPT_Network=0
OPT_Network_Full=0
OPT_Subnet=0
OPT_Subnet_Full=0
OPT_SecGroup=0
OPT_SecGroup_Full=0
OPT_FloatIp=0
OPT_FloatIp_Full=0
while getopts hc:oOvVrRpPnNsSgGfFA OPTION
do
case "$OPTION" in
h) renderhelp;;
c) OS_CLOUD="$OPTARG";;
o) OPT_Compute=1;;
O) OPT_Compute=1 ; OPT_Compute_Full=1;;
v) OPT_Volume=1;;
V) OPT_Volume=1 ; OPT_Volume_Full=1;;
r) OPT_Router=1;;
R) OPT_Router=1 ; OPT_Router_Full=1;;
p) OPT_Port=1;;
P) OPT_Port=1 ; OPT_Port_Full=1;;
n) OPT_Network=1;;
N) OPT_Network=1 ; OPT_Network_Full=1;;
s) OPT_Subnet=1;;
S) OPT_Subnet=1 ; OPT_Subnet_Full=1;;
g) OPT_SecGroup=1;;
G) OPT_SecGroup=1 ; OPT_SecGroup_Full=1;;
f) OPT_FloatIp=1;;
F) OPT_FloatIp=1 ; OPT_FloatIp_Full=1;;
A) OPT_Compute_Full=1; OPT_Volume_Full=1; OPT_Router_Full=1; OPT_Port_Full=1; OPT_Network_Full=1; OPT_Subnet_Full=1; OPT_SecGroup_Full=1; OPT_FloatIp_Full=1;;
esac
done
if [ $OPT_Compute -eq 0 ] && [ $OPT_Volume -eq 0 ] && [ $OPT_Router -eq 0 ] && [ $OPT_Port -eq 0 ] && [ $OPT_Network -eq 0 ] && [ $OPT_Subnet -eq 0 ] && [ $OPT_SecGroup -eq 0 ] && [ $OPT_FloatIp -eq 0 ]
then
OPT_Compute=1
OPT_Volume=1
OPT_Router=1
OPT_Port=1
OPT_Network=1
OPT_Subnet=1
OPT_SecGroup=1
OPT_FloatIp=1
fi
if [ "$OS_CLOUD" == "" ]
then
if [ $(env|grep [KO][5S]_ | wc -l) -gt 1 ]
then
OS_CLOUD=envvars
else
renderhelp
fi
fi
echo "== $OS_CLOUD =="
if [ $OPT_Compute -eq 1 ]; then
echo "-- servers --"
ARGS='-c Name -c Networks'
if [ $OPT_Compute_Full -eq 1 ]; then ARGS=""; fi
openstack -q --os-cloud $OS_CLOUD server list $ARGS
fi
if [ $OPT_Volume -eq 1 ]; then
echo "-- volumes --"
if [ $OPT_Volume_Full -eq 1 ]; then
openstack -q --os-cloud $OS_CLOUD volume list
else
openstack -q --os-cloud $OS_CLOUD volume list $ARGS
fi
fi
if [ $OPT_Router -eq 1 ]; then
echo "-- routers --"
ARGS='-c Name'
if [ $OPT_Router_Full -eq 1 ]; then ARGS=""; fi
openstack -q --os-cloud $OS_CLOUD router list $ARGS
fi
if [ $OPT_Port -eq 1 ]; then
echo "-- ports --"
if [ $OPT_Port_Full -eq 1 ]; then
openstack -q --os-cloud $OS_CLOUD port list
else
openstack -q --os-cloud $OS_CLOUD port list -c ID -c Name -c "Fixed IP Addresses"
fi
fi
if [ $OPT_Network -eq 1 ]; then
echo "-- network --"
ARGS='-c Name'
if [ $OPT_Network_Full -eq 1 ]; then ARGS=""; fi
openstack -q --os-cloud $OS_CLOUD network list $ARGS | grep -v '^| inf_az'
fi
if [ $OPT_Subnet -eq 1 ]; then
echo "-- subnets --"
ARGS='-c Name'
if [ $OPT_Subnet_Full -eq 1 ]; then ARGS=""; fi
openstack -q --os-cloud $OS_CLOUD subnet list $ARGS | grep -v '^| inf_az'
fi
if [ $OPT_SecGroup -eq 1 ]; then
echo "-- sec grp --"
ARGS='-c Name'
if [ $OPT_SecGroup_Full -eq 1 ]; then ARGS=""; fi
openstack -q --os-cloud $OS_CLOUD security group list $ARGS | grep -v '^| default'
fi
if [ $OPT_FloatIp -eq 1 ]; then
echo "-- floatip --"
if [ $OPT_FloatIp_Full -eq 1 ]; then
openstack -q --os-cloud $OS_CLOUD floating ip list
else
openstack -q --os-cloud $OS_CLOUD floating ip list -c "Floating IP Address" -c "Fixed IP Address"
fi
fi
#!/bin/bash
# Author: Jon Spriggs <jon@sprig.gs>
# Created: 2018-03-22
# 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 unused floating IPs from K5 or OpenStack services"
echo ""
echo "-d - Debug"
echo "-c <cloudname> - Clouds.yml file entry. Uses env vars if not set. *rqd*"
exit 1
}
echo_debug() {
if [ "$DEBUG" == "true" ]
then
echo "$1"
fi
}
DEBUG=false
while getopts hdc: OPTION
do
case "$OPTION" in
h) renderhelp;;
d) DEBUG=true;;
c) CloudEntry="$OPTARG";;
esac
done
if [ -z "$CloudEntry" ]
then
if [ -n "$OS_CLOUD" ]
then
CloudEntry="$OS_CLOUD"
else
if [ $(env|grep [KO][5S]_ | wc -l) -gt 1 ]
then
CloudEntry=envvars
else
renderhelp
fi
fi
fi
ClearList=""
echo_debug "Looking for floating IPs to match"
openstack -q --os-cloud $CloudEntry floating ip list -c ID -c "Floating IP Address" -c "Fixed IP Address" --format csv 2>/dev/null | grep -v '"ID","Floating IP Address","Fixed IP Address"' > "$tmpfile"
for ipline in $(cat "$tmpfile")
do
ipID="$(echo $ipline | cut -f2 -d\")"
ipAllocated="$(echo "$ipline" | cut -f4 -d\" )"
ipAttached="$(echo "$ipline" | cut -f6 -d\" )"
if [ -z "$ipAttached" ]
then
echo_debug "Floating IP address $ipAllocated ($ipID) not in use"
ClearList="$ClearList $ipID"
else
echo_debug "Skipping Floating IP address $ipAllocated which is attached to $ipAttached"
fi
done
if [ -n "$ClearList" ]
then
openstack -q --os-cloud $CloudEntry floating ip delete $ClearList
fi
#!/bin/bash
# Author: Jon Spriggs <jon@sprig.gs>
# Created: 2018-03-22
# 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 networks from K5 or OpenStack services"
echo ""
echo "-d - Debug"
echo "-c <cloudname> - Clouds.yml file entry. Uses env vars if not set. *rqd*"
echo "-n <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:n: OPTION
do
case "$OPTION" in
h) renderhelp;;
d) DEBUG=true;;
c) CloudEntry="$OPTARG";;
n) NetworkPart="$OPTARG";;
esac
done
if [ -z "$CloudEntry" ]
then
if [ -n "$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 networks to match"
openstack network list -q --os-cloud $CloudEntry -c ID -c Name --format csv 2>/dev/null | grep -v '"ID","Name"' | grep -v 'inf_az[0-9]_ext-net[0-9]\{2\}' > "$tmpfile"
for networkline in $(cat "$tmpfile")
do
networkID=$(echo $networkline | cut -f2 -d\")
networkName=$(echo $networkline | cut -f4 -d\")
if [ -z "$NetworkPart" ] || echo "$networkName" | grep "$NetworkPart" >/dev/null
then
openstack subnet list -q --os-cloud $CloudEntry -c ID -c Name --network $networkID --format csv 2>/dev/null | grep -v '"ID","Name"' > "$tmpfile2"
for subnetline in $(cat "$tmpfile2")
do
subnetID=$(echo $subnetline | cut -f2 -d\")
subnetName=$(echo $subnetline | cut -f4 -d\")
echo_debug "Asking for ports in subnet $subnetID ($subnetName)"
openstack port list -q --os-cloud $CloudEntry -c "Device Owner" --long --network $networkID --format csv 2>/dev/null > "$tmpfile3"
cports=$(grep "compute\:" "$tmpfile3" | wc -l)
rports=$(grep "\:router_interface" "$tmpfile3" | wc -l)
if [ $cports -eq 0 ] && [ $rports -eq 0 ]
then
openstack network delete -q --os-cloud $CloudEntry $networkID
else
if [ $cports -gt 1 ]
then
echo "Unable to remove network $networkID ($networkName) due to $cports compute ports. Please resolve and re-try."
else
if [ $cports -eq 1 ]
then
echo "Unable to remove network $networkID ($networkName) due to a compute port. Please resolve and re-try."
else
if [ $rports -gt 1 ]
then
echo "Unable to remove network $networkID ($networkName) due to $rports router ports. Please resolve and re-try."
else
echo "Unable to remove network $networkID ($networkName) due to a router port. Please resolve and re-try."
fi
fi
fi
fi
done
else
echo_debug "Found non-matching network $networkID with name $networkName"
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}')"
# 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 [ -n "$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-03-22
# 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 unused Security Groups from K5 or OpenStack services"
echo ""
echo "-d - Debug"
echo "-c <cloudname> - Clouds.yml file entry. Uses env vars if not set. *rqd*"
exit 1
}
echo_debug() {
if [ "$DEBUG" == "true" ]
then
echo "$1"
fi
}
FORCE=false
DEBUG=false
while getopts hdc: OPTION
do
case "$OPTION" in
h) renderhelp;;
d) DEBUG=true;;
c) CloudEntry="$OPTARG";;
esac
done
if [ -z "$CloudEntry" ]
then
if [ -n "$OS_CLOUD" ]
then
CloudEntry="$OS_CLOUD"
else
if [ $(env|grep [KO][5S]_ | wc -l) -gt 1 ]
then
CloudEntry=envvars
else
renderhelp
fi
fi
fi
ClearList=""
echo_debug "Looking for security groups to match"
openstack -q --os-cloud $CloudEntry security group list -c ID -c "Name" --format csv 2>/dev/null | grep -v '"ID","Name"' | sed 's/ /£/g' | grep -v '"default"' > "$tmpfile"
echo_debug "Looking for ports to check for security group usage"
openstack -q --os-cloud $CloudEntry port list -c ID --format csv 2>/dev/null | grep -v '"ID"' > "$tmpfile2"
for portline in $(cat "$tmpfile2")
do
portID="$(echo $portline | cut -f2 -d\")"
openstack -q --os-cloud $CloudEntry port show $portID -c security_group_ids --format shell | grep -v 'security_group_ids=""' >> "$tmpfile3"
done
for sgline in $(cat "$tmpfile")
do
sgID="$(echo $sgline | cut -f2 -d\")"
sgName="$(echo "$sgline" | cut -f4 -d\" | sed 's/£/ /g')"
sgUses="$(grep "$sgID" "$tmpfile3" | wc -l)"
if [ $sgUses -gt 1 ]
then
echo_debug "Skipping Security Group $sgName ($sgID) which is attached to $sgUses ports"
else
if [ $sgUses -gt 0 ]
then
echo_debug "Skipping Security Group $sgName ($sgID) which is attached to $sgUses port"
else
echo_debug "Security Group $sgName ($sgID) not in use"
ClearList="$ClearList $sgID"
fi
fi
done
if [ -n "$ClearList" ]
then
openstack -q --os-cloud $CloudEntry security group delete $ClearList
fi
#!/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
}
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 [ -n "$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 --wait
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