Skip to content

Instantly share code, notes, and snippets.

@ThinGuy
Created March 8, 2018 17:55
Show Gist options
  • Save ThinGuy/f005e659186f92712233faada09c61a2 to your computer and use it in GitHub Desktop.
Save ThinGuy/f005e659186f92712233faada09c61a2 to your computer and use it in GitHub Desktop.
Openstack OrangeBox Sanity Test
#!/bin/bash
#Script to configure openstack on Orangebox
[[ ${HOSTNAME,,} =~ orangebox ]] && export OB_NUM=$((10#${HOSTNAME:9})) || OB_NUM=20
#Networking Variables - External will be provider, Int will be tenant
export NEUTRON_DNS1="172.27.$((${OB_NUM})).1"
export NEUTRON_DNS2="172.27.$((${OB_NUM}+2)).1"
export NEUTRON_DNS3="172.27.$((${OB_NUM}+3)).254"
export NEUTRON_EXT_NET_NAME=ob_provider
export NEUTRON_EXT_NET_GW="172.27.$((${OB_NUM}+3)).254"
export NEUTRON_EXT_NET_CIDR="172.27.$((${OB_NUM}+2)).0/23"
export NEUTRON_EXT_NET_FLOAT_RANGE_START="172.27.$((${OB_NUM}+2)).100"
export NEUTRON_EXT_NET_FLOAT_RANGE_END="172.27.$((${OB_NUM}+2)).250"
export NEUTRON_INT_NET_NAME=admin_net
export NEUTRON_INT_NET_CIDR="192.168.$((${OB_NUM})).0/24"
P2PKGS=(python-keystonecleint python-neutronclient python-novaclient python-glanceclient python-openstackclient)
P3PKGS=(python3-keystoneclient python3-neutronclient python3-novaclient python3-glanceclient python3-openstackclient)
command -v python2 2>&1 /dev/null && sudo apt install ${P2PKGS[@]} -yq
command -v python3 2>&1 /dev/null && sudo apt install ${P3PKGS[@]} -yq
yaml2json() {
local DESC="${RO}${FUNCNAME}${RT}: Convert yaml to json"
[[ $1 = '--desc' ]] && { printf "${DESC}\n";return; }
[[ -z ${1} ]] && { printf 'Please provide path to yaml file\n';return 1; } || local YAML=${1}
ruby -ryaml -rjson -e 'puts JSON.pretty_generate(YAML.load(ARGF))' ${YAML}
}
#Create RC file
[[ -f ~/admin-openrc.sh ]] && rm ~/admin-openrc.sh
export NO_WRITE_RC=false
#Clear Openstack variables
for var in $(set -o posix; set|/bin/grep -oE '^OS_[^=]+');do unset ${var};done
#Assuming Keystone API 3
unset OS_TENANT_ID
unset OS_TENANT_NAME
export OS_INTERFACE=public
export OS_IDENTITY_API_VERSION=3
printf "\e[2GFetching IP for Keystone\n"
[[ $(juju config keystone vip) ]] && export OS_AUTH_URL="http://$(juju config keystone vip):5000/v3" || export OS_AUTH_URL="http://$(juju run --unit keystone/0 'unit-get public-address'):5000/v3"
export OS_PROJECT_NAME=$(juju 2>/dev/null config keystone admin-user)
export OS_USER_DOMAIN_NAME="$(juju 2>/dev/null config keystone admin-user)_domain"
printf "\e[2GFetching default admin-user name from Juju\n"
export OS_USERNAME=$(juju 2>/dev/null config keystone admin-user)
printf "\e[2GFetching password for ${OS_USERNAME} from Juju\n"
export OS_PASSWORD=$(juju 2>/dev/null config keystone admin-password)
printf "\e[2GFetching Openstack Region name from Juju\n"
export OS_REGION_NAME=$(juju 2>/dev/null config keystone region)
printf "\e[2GFetching Domain ID for ${OS_USERNAME}_domain from Juju\n"
export OS_PROJECT_DOMAIN_ID=$(openstack domain list --os-project-domain-name ${OS_USER_DOMAIN_NAME} --os-username ${OS_USERNAME} --os-password=${OS_PASSWORD}|awk '/'${OS_USER_DOMAIN_NAME}'/{print $2}')
printf "\e[2GFetching Project ID for ${OS_USERNAME} project from Juju\n"
export OS_PROJECT_ID=$(openstack project list --long --os-project-domain-name ${OS_USER_DOMAIN_NAME} --os-username ${OS_USERNAME} --os-password=${OS_PASSWORD}|awk '/'${OS_PROJECT_DOMAIN_ID}'/&&/'${OS_USERNAME}'/{print $2}')
#Check to make sure we have populated variables
printf "\e[2GValidating that all OS_ variables are present\n"
for var in $(set -o posix; set|/bin/grep -oE '^OS_[^=]+');do
if [[ -z $(eval "echo \$$var") ]];then
export NO_WRITE_RC=true
printf "\e[4GCannot determine value for $var\n"
fi
done
#Write the RC File
if [[ ${NO_WRITE_RC} = false ]];then
printf "\e[2GCreating Openstack RC file...\n"
{ set|/bin/grep -oE '^OS_[^$]+'|sed 's/^.*$/export &/g;1s/^/#!\/usr\/bin\/env bash\n/;/HYPERVISORS/d'|tee 1>/dev/null ~/admin-openrc.sh; }
[[ $? -eq 0 && -f ~/admin-openrc.sh ]] && { printf "\e[4GSourcing Openstack RC file...\n";source ~/admin-openrc.sh; }
RC_FILE_SOURCED=true
else
printf "\e[2GCould not write the RC file. Please ensure you are both the user and on the system where Juju deployed Openstack from.\n\n"
RC_FILE_SOURCED=false
return 1
fi
#Create Networks
printf "\e[2GCreating Provider Network \"${NEUTRON_EXT_NET_NAME}\"...\n"
export NEUTRON_EXT_NETWORK_ID=$(neutron net-create ${NEUTRON_EXT_NET_NAME} --shared --provider:physical_network=physnet1 --provider:network_type=flat --router:external=True |awk '/id:/{print $4}')
printf "\e[4GCreating Provider Subnet \"${NEUTRON_EXT_NET_NAME}_subnet\"...\n"
export NEUTRON_EXT_SUBNET_ID=$(neutron subnet-create ${NEUTRON_EXT_NET_NAME} $NEUTRON_EXT_NET_CIDR --name ${NEUTRON_EXT_NET_NAME}_subnet --allocation-pool start=$NEUTRON_EXT_NET_FLOAT_RANGE_START,end=$NEUTRON_EXT_NET_FLOAT_RANGE_END --gateway $NEUTRON_EXT_NET_GW --dns-nameserver $NEUTRON_DNS1 --dns-nameserver $NEUTRON_DNS2 --dns-nameserver $NEUTRON_DNS3|awk '/id:/{print $4}')
printf "\e[2GCreating Private Network \"$NEUTRON_INT_NET_NAME\"...\n"
export NEUTRON_INT_NETWORK_ID=$(neutron net-create ${NEUTRON_INT_NET_NAME}|awk '/id:/{print $4}')
printf "\e[4GCreating Private Subnet \"${NEUTRON_INT_NET_NAME}_subnet\"...\n"
export NEUTRON_INT_SUBNET_ID=$(neutron subnet-create ${NEUTRON_INT_NET_NAME} $NEUTRON_INT_NET_CIDR --name ${NEUTRON_INT_NET_NAME}_subnet --dns-nameserver $NEUTRON_DNS1 --dns-nameserver $NEUTRON_DNS2 --dns-nameserver $NEUTRON_DNS3 | grep " id" | awk '{print $4}')
printf "\e[2GCreating Router from ${NEUTRON_INT_NET_NAME} to ${NEUTRON_EXT_NET_NAME} \"${NEUTRON_INT_NET_NAME}-to-${NEUTRON_EXT_NET_NAME}\"...\n"
export NEUTRON_INT_ROUTER_ID=$(neutron router-create ${NEUTRON_INT_NET_NAME}-to-${NEUTRON_EXT_NET_NAME} | grep " id" | awk '{print $4}')
printf "\e[4GAdding interface from ${NEUTRON_INT_NET_NAME}_subnet to router \"${NEUTRON_INT_NET_NAME}-to-${NEUTRON_EXT_NET_NAME}\"...\n"
neutron router-interface-add $NEUTRON_INT_ROUTER_ID $NEUTRON_INT_SUBNET_ID &>/dev/null
printf "\e[4GSetting external gateway on \"${NEUTRON_INT_NET_NAME}-to-${NEUTRON_EXT_NET_NAME}\" router to ${NEUTRON_EXT_NET_NAME}...\n"
neutron router-gateway-set $NEUTRON_INT_ROUTER_ID $NEUTRON_EXT_NETWORK_ID &>/dev/null
# Create security rules
printf "\e[2GCreating security rule to allow ICMP protocol (ping) to pass\n"
openstack security group rule create --proto icmp default &>/dev/null
printf "\e[2GCreating security rule to allow ssh protocol to pass\n"
openstack security group rule create --proto tcp --dst-port 22 default &>/dev/null
printf "\e[2GCreating security rule to allow http protocol to pass\n"
openstack security group rule create --proto tcp --dst-port 80 default &>/dev/null
printf "\e[2GCreating security rule to allow https protocol to pass\n"
openstack security group rule create --proto tcp --dst-port 443 default &>/dev/null
printf "\e[2GCreating security rule to allow RDP protocol to pass\n"
openstack security group rule create --proto tcp --dst-port 3389 default &>/dev/null
printf "\e[2GCreating security rule to allow novnc protocol to pass\n"
openstack security group rule create --proto tcp --dst-port 6080 default &>/dev/null
printf "\e[2GCreating security rule to allow vnc protocol to pass\n"
openstack security group rule create --proto tcp --dst-port 5900:5999 default &>/dev/null
printf "\e[2GImporting ssh public key for user ${USER} as the default keypair\n"
# Import SSH Keys for current user
openstack keypair create --public-key ~/.ssh/id_rsa.pub default &>/dev/null
# Create Flavors
openstack flavor create tiny --id auto --ram 512 --disk 10 --ephemeral 0 --vcpus 1 --public
openstack flavor create small --id auto --ram 1024 --disk 20 --ephemeral 0 --vcpus 2 --public
openstack flavor create medium --id auto --ram 2048 --disk 30 --ephemeral 0 --vcpus 3 --public
openstack flavor create large --id auto --ram 4096 --disk 40 --ephemeral 0 --vcpus 4 --public
openstack flavor create xlarge --id auto --ram 8192 --disk 50 --ephemeral 0 --vcpus 5 --public
openstack flavor create xxlarge --id auto --ram 16384 --disk 60 --ephemeral 0 --vcpus 6 --public
#create-aws-flavors() {
#wget -qO- https://raw.githubusercontent.com/dustinkirkland/instance-type/master/yaml/aws.yaml|sed -E ':a;N;$!ba;s/\n |:|^$//g'|awk 'function ceil(x, y){y=int(x); return(x>y?y+1:y)} !/^$/{print "flavor create",$1,"--id auto --vcpus",ceil($2),"--ram",ceil($3)"G --ephemeral 5","--public"}'|xargs -n12 -P0 openstack
#}
#create-gce-flavors() {
#wget -qO- https://raw.githubusercontent.com/dustinkirkland/instance-type/master/yaml/gce.yaml|sed -E ':a;N;$!ba;s/\n |:|^$//g'|awk 'function ceil(x, y){y=int(x); return(x>y?y+1:y)} !/^$/{print "flavor create",$1,"--id auto --vcpus",ceil($2),"--ram",ceil($3)"G --ephemeral 5","--public"}'|xargs -n12 -P0 openstack
#}
#create-azure-flavors() {
#wget -qO- https://raw.githubusercontent.com/dustinkirkland/instance-type/master/yaml/azure.yaml|sed -E ':a;N;$!ba;s/\n |:|^$//g'|awk 'function ceil(x, y){y=int(x); return(x>y?y+1:y)} !/^$/{print "flavor create",$1,"--id auto --vcpus",ceil($2),"--ram",ceil($3)"G --ephemeral 5","--public"}'|xargs -n12 -P0 openstack
#}
# Set Quotas
openstack quota set --ram 204800 --cores 200 --instances 100 --volumes 100 ${OS_PROJECT_ID}
# Openstack quota won't set the following, so use old neutron quota update
neutron quota-update --port 100 --security-group 100 --security-group-rule 500 --floatingip 100
# Download images if they don't exist
[[ -f /srv/data/bionic-server-cloudimg-amd64.img ]] || wget http://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img
[[ -f /srv/data/bionic-server-cloudimg-amd64.tar.gz ]] || wget http://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.tar.gz
[[ -f /srv/data/xenial-server-cloudimg-amd64-disk1.img ]] || wget http://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-disk1.img
[[ -f /srv/data/xenial-server-cloudimg-amd64-root.tar.gz ]] || wget http://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-root.tar.gz
[[ -f /srv/data/trusty-server-cloudimg-amd64-disk1.img ]] || wget http://cloud-images.ubuntu.com/trusty/current/trusty-server-cloudimg-amd64-disk1.img
[[ -f /srv/data/trusty-server-cloudimg-amd64-root.tar.gz ]] || wget http://cloud-images.ubuntu.com/trusty/current/trusty-server-cloudimg-amd64-root.tar.gz
[[ -f /srv/data/cirros-0.4.0-x86_64-disk.img ]] || wget http://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img
[[ -f /srv/data/cirros-0.4.0-x86_64-rootfs.img.gz ]] || wget http://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-rootfs.img.gz
# Create glance images
glance image-create --name=Bionic-QCOW --visibility=public --container-format=ovf --disk-format=qcow2 < /srv/data/bionic-server-cloudimg-amd64.img
glance image-create --name=Xenial-QCOW --visibility=public --container-format=ovf --disk-format=qcow2 < /srv/data/xenial-server-cloudimg-amd64-disk1.img
glance image-create --name=Trusty-QCOW --visibility=public --container-format=ovf --disk-format=qcow2 < /srv/data/trusty-server-cloudimg-amd64-disk1.img
glance image-create --name=Cirros-QCOW --visibility=public --container-format=ovf --disk-format=qcow2 < /srv/data/cirros-0.4.0-x86_64-disk.img
glance image-create --name=Bionic-ROOT --visibility=public --container-format=bare --disk-format=raw < /srv/data/bionic-server-cloudimg-amd64.tar.gz
glance image-create --name=Xenial-ROOT --visibility=public --container-format=bare --disk-format=raw < /srv/data/xenial-server-cloudimg-amd64-root.tar.gz
glance image-create --name=Trusty-ROOT --visibility=public --container-format=bare --disk-format=raw < /srv/data/trusty-server-cloudimg-amd64-root.tar.gz
glance image-create --name=Cirros-ROOT --visibility=public --container-format=bare --disk-format=raw < /srv/data/cirros-0.4.0-x86_64-rootfs.img.gz
# Create Hypervisor arrays in order to to create host aggregates and availability zones
declare -ag OS_HYPERVISORS=($(openstack hypervisor list -f json|jq 2>/dev/null -r '.[]|"\(."Hypervisor Hostname")"'))
declare -ag OS_LXD_HYPERVISORS=($(for i in ${OS_HYPERVISORS[@]};do openstack hypervisor show $i -f json|jq 2>/dev/null -r 'select(.hypervisor_type == "lxd").service_host';done))
declare -ag OS_KVM_HYPERVISORS=($(for i in ${OS_HYPERVISORS[@]};do openstack hypervisor show $i -f json|jq 2>/dev/null -r 'select((.hypervisor_type == "kvm") or .hypervisor_type == "QEMU").service_host';done))
# Create nova-kvm Host Aggregate and KVM AZ
if [[ ${#OS_KVM_HYPERVISORS[@]} -gt 1 ]];then
openstack aggregate create --zone KVM nova-kvm
for host in ${OS_KVM_HYPERVISORS[@]};do
openstack aggregate add host nova-kvm $host
done
fi
# Create 10 KVM Instances in parallel on public net
for c in {1..10};do printf 'demo-kvm-vm-%03d\n' $((10#${c}));done|\
xargs -I@ -n1 -P0 nova boot \
--flavor tiny \
--key-name default \
--image Bionic-QCOW \
--nic net-name=${NEUTRON_EXT_NET_NAME} \
--security-groups default \
--availability-zone KVM "@"
# Show KVM instances
openstack server list --name "demo-kvm.*$"
#Create 10 LXD Instances in parallel on public net
for c in {1..10};do printf 'demo-lxd-vm-%03d\n' $((10#${c}));done|\
xargs -I@ -n1 -P0 nova boot \
--flavor tiny \
--key-name default \
--image Cirros-ROOT \
--nic net-name=${NEUTRON_EXT_NET_NAME} \
--security-groups default \
--availability-zone LXD "@"
# Show LXD instances
openstack server list --name "demo-lxd.*$"
# Delete LXD Instances in parallel
#for c in {1..25};do printf 'demo-lxd-vm-%03d\n' $((10#${c}));done|xargs -I@ -n1 -P0 openstack server delete "@"
# Delete KVM Instances in parallel
#for c in {1..25};do printf 'demo-kvm-vm-%03d\n' $((10#${c}));done|xargs -I@ -n1 -P0 openstack server delete "@"
# Delete All VMs in parallel
#{ openstack server list -c ID -f value|sort -uV; }|xargs -I@ -n1 -P0 openstack server delete "@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment