Last active
September 2, 2018 16:10
-
-
Save lrivallain/619c35cfb91048a635ddefc60788b3cc to your computer and use it in GitHub Desktop.
Helper to deploy openstack-based lab for training practical exercice
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 | |
# Help | |
function help_usage { | |
echo "Usage: " | |
echo " --name|-n => * Instance name ($name)" | |
echo " --image_name => * Image to use for instance deployment ($image_name)" | |
echo " --nb_instances|-c => Number of instances to create - default 1 ($nb_instances)" | |
echo " --start_index => Start numbering from this index - default 1 ($start_index)" | |
echo " --volume_size => * Disk size for instance ($volume_size)" | |
echo " --flavor_name => * Sizing of instance ($flavor_name)" | |
echo " --network_name => * Network name for instance ($network_name)" | |
echo " --sec_group => * Security group name for instance ($sec_group)" | |
echo " --key_name => * SSH key to trust on default user ($key_name)" | |
echo " --postcusto => * Script to run on first boot ($postcusto)" | |
echo " --floating_pool => Attach instance to a floating pool of address ($floating_pool)" | |
echo " --help|-h => Display this help message" | |
echo "" | |
echo "Note: Items with * are mandatory." | |
} | |
# Read arguments | |
while [[ $# -gt 0 ]] | |
do | |
key="$1" | |
case $key in | |
-n|--name) | |
name="$2" | |
shift # past argument | |
;; | |
--image) | |
image_name="$2" | |
shift # past argument | |
;; | |
-c|--count) | |
nb_instances="$2" | |
shift # past argument | |
;; | |
--start_index) | |
start_index="$2" | |
shift # past argument | |
;; | |
--volume_size) | |
volume_size="$2" | |
shift # past argument | |
;; | |
--flavor_name) | |
flavor_name="$2" | |
shift # past argument | |
;; | |
--network_name) | |
network_name="$2" | |
shift # past argument | |
;; | |
--sec_group) | |
sec_group="$2" | |
shift # past argument | |
;; | |
--key_name) | |
key_name="$2" | |
shift # past argument | |
;; | |
--postcusto) | |
postcusto="$2" | |
shift # past argument | |
;; | |
--floating_pool) | |
floating_pool="$2" | |
shift # past argument | |
;; | |
-h|--help) | |
help_usage | |
exit 0 | |
shift # past argument | |
;; | |
*) | |
# unknown option | |
;; | |
esac | |
shift # past argument or value | |
done | |
if [[ ! ( $name && $image_name && $volume_size && $flavor_name && $network_name && $sec_group && $key_name && $postcusto ) ]] | |
then | |
echo "Missing mandatory parameters !" | |
help_usage | |
exit -1 | |
fi | |
#################################################################################################### | |
# # | |
# Voodoo part ! # | |
# # | |
#################################################################################################### | |
# Build sequence | |
[[ ! $start_index ]] && start_index="1" | |
if [[ ! $nb_instances ]]; then | |
end_index=$start_index; | |
else | |
end_index=$[$start_index+$nb_instances-1] | |
fi | |
echo "Will create instances from $name-$start_index to $name-$end_index" | |
sleep 10 | |
# Retrieve IDs | |
flavor_id=$(openstack flavor show "$flavor_name" | grep " id " | cut -d'|' -f3 | tr -d ' ') | |
if [[ ! $flavor_id ]]; then echo "Cannot find uniq flavor named $flavor_name " ; exit -1; fi | |
echo "Flavor ID: $flavor_id" | |
image_id=$(openstack image show "$image_name" | grep " id " | cut -d'|' -f3 | tr -d ' ') | |
if [[ ! $image_id ]]; then echo "Cannot find uniq image named $image_name " ; exit -1; fi | |
echo "Image ID: $image_id" | |
net_id=$(openstack network show "$network_name" | grep " id " | cut -d'|' -f3 | tr -d ' ') | |
if [[ ! $net_id ]]; then echo "Cannot find uniq network named $network_name " ; exit -1; fi | |
echo "Network ID: $net_id" | |
# Validate all other attributes | |
if ! openstack keypair list | grep $key_name > /dev/null; then | |
echo "Cannot find SSH key named $key_name " ; exit -1 | |
fi | |
if ! openstack security group list | grep $sec_group > /dev/null; then | |
echo "Cannot find security group named $sec_group " ; exit -1 | |
fi | |
if [[ ! -f $postcusto ]]; then | |
echo "Cannot find file for post customization: $postcusto " ; exit -1 | |
fi | |
if [[ $floating_pool ]]; then | |
if ! openstack ip floating pool list | grep $floating_pool > /dev/null; then | |
echo "Cannot find floating pool name: $floating_pool " ; exit -1 | |
fi | |
fi | |
# image creation with index parameter | |
function image_creation { | |
index=$1 | |
echo "Instance $name-$index creation in progress..." | |
vol_id=$( openstack volume show vol-$name-$i | grep " id " | cut -d'|' -f3 | tr -d ' '); | |
echo "Volume ID: $vol_id" | |
openstack server create --wait \ | |
--flavor $flavor_id \ | |
--volume $vol_id \ | |
--key-name "$key_name" \ | |
--nic net-id=$net_id \ | |
--security-group "$sec_group" \ | |
--user-data $postcusto \ | |
"$name-$index" > /dev/null | |
} | |
# Start sequence | |
for i in `seq $start_index $end_index`; do | |
continue=(false) # leave by default | |
echo # empty line | |
# Volume creation | |
echo "Volume vol-$name-$i creation in progress..." | |
openstack volume create --image $image_id --size $volume_size "vol-$name-$i" > /dev/null | |
retry=10 | |
until [ $retry -eq 0 ] | |
do | |
if openstack volume show "vol-$name-$i" | grep available > /dev/null ; then | |
echo "Volume created successfully" | |
continue=(true) | |
break | |
elif openstack volume show "vol-$name-$i" | grep error > /dev/null ; then | |
echo "Volume creation failure" | |
break | |
else | |
echo "Volume not yet available... (retries left: $retry)" | |
retry=$[$retry-1] | |
sleep 1m | |
fi | |
done | |
# Instance creation if volume is OK only | |
if $continue | |
then | |
continue=(false) | |
image_creation $i # first creation (synchronous) | |
retry=5 | |
until [ $retry -eq 0 ] | |
do | |
if openstack server show "$name-$i" | grep "ERROR" > /dev/null ; then | |
echo "Instance creation failure (retries left: $retry)" | |
# remove the failed instance | |
openstack server delete --wait "$name-$i" | |
# retry creation | |
image_creation $i # (synchronous) | |
retry=$[$retry-1] | |
else | |
echo "Instance created successfully" | |
continue=(true) | |
break | |
fi | |
done | |
fi | |
# Assign a free IP to created instance | |
if [[ ( $continue && "$floating_pool" ) ]] | |
then | |
free_ip=$(openstack ip floating list | grep None | cut -d'|' -f3 | tr -d ' ' | sort | head -n 1) | |
if [[ ! $free_ip ]] | |
then | |
openstack ip floating create $floating_pool | |
free_ip=$(openstack ip floating list | grep None | cut -d'|' -f3 | tr -d ' ' | sort | head -n 1) | |
fi | |
echo "Will assigne $free_ip to the server..." | |
openstack ip floating add "$free_ip" "$name-$i" | |
fi | |
done | |
echo # empty line | |
echo "All creations are done. Let's list instances with $name :" | |
openstack server list --name "$name-" |
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
# Following command will create: | |
# * instance(s) named "training" | |
# * based on ubuntu image | |
# * start 10* instances | |
# * append index starting from 1 | |
# * 5Gb disk per instance | |
# * sizing based on flavor: m1.tiny | |
# * connected to network: training_net | |
# * security group applied: training_sec | |
# * ssh key will be: mysshkey | |
# * post costumization from script: postconfig_training.sh | |
# * attach floating IP from the "MyPool" repository | |
./buildlab.sh --name "training" \ | |
--image "Ubuntu 16.04 Xenial" \ | |
--count 10 \ | |
--start_index 1 \ | |
--volume_size 5 \ | |
--flavor_name "m1.tiny" \ | |
--network_name "training_net" \ | |
--sec_group "training_sec" \ | |
--key_name "mysshkey" \ | |
--postcusto "./postconfig_training.sh" \ | |
--floating_pool "MyPool" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment