Advertisement
ThingGuy

sanity

Apr 3rd, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 13.24 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3.  
  4.  
  5. #Networking Variables - External will be provider, Int will be tenant
  6. export NEUTRON_DNS1="10.21.73.66"
  7. export NEUTRON_DNS2="10.20.1.4"
  8. export NEUTRON_DNS3="10.21.44.254"
  9. export NEUTRON_EXT_NET_NAME=pp_provider
  10. export NEUTRON_EXT_NET_GW="10.21.73.1"
  11. export NEUTRON_EXT_NET_CIDR="10.21.73.0/24"
  12. export NEUTRON_EXT_NET_FLOAT_RANGE_START="10.21.73.116"
  13. export NEUTRON_EXT_NET_FLOAT_RANGE_END="10.21.73.190"
  14. export NEUTRON_INT_NET_NAME=admin_net
  15. export NEUTRON_INT_NET_CIDR="192.168.8.0/24"
  16.  
  17.  
  18.  
  19.  
  20. declare -ag P2PKGS=(python-keystoneclient python-neutronclient python-novaclient python-glanceclient python-openstackclient)
  21. declare -ag P3PKGS=(python3-keystoneclient python3-neutronclient python3-novaclient python3-glanceclient python3-openstackclient)
  22. command -v python2 > /dev/null 2>&1 && sudo apt install ${P2PKGS[@]} -yq
  23. command -v python3 > /dev/null 2>&1 && sudo apt install ${P3PKGS[@]} -yq
  24.  
  25.  
  26. yaml2json() {
  27.     local DESC="${RO}${FUNCNAME}${RT}: Convert yaml to json"
  28.     [[ $1 = '--desc' ]] && { printf "${DESC}\n";return; }
  29.     [[ -z ${1} ]] && { printf 'Please provide path to yaml file\n';return 1; } || local YAML=${1}
  30.     ruby -ryaml -rjson -e 'puts JSON.pretty_generate(YAML.load(ARGF))' ${YAML}
  31. }
  32.  
  33. #Create RC file
  34. write-admin-file() {
  35. [[ -f ~/admin-openrc.sh ]] && rm ~/admin-openrc.sh
  36. export NO_WRITE_RC=false
  37. #Clear Openstack variables
  38. for var in $(set -o posix; set|/bin/grep -oE '^OS_[^=]+');do unset ${var};done
  39. #Assuming Keystone API 3
  40. unset OS_TENANT_ID
  41. unset OS_TENANT_NAME
  42. export OS_INTERFACE=public
  43. export OS_IDENTITY_API_VERSION=3
  44. printf "\e[2GFetching IP for Keystone\n"
  45. [[ $(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"
  46. export OS_PROJECT_NAME=$(juju 2>/dev/null config keystone admin-user)
  47. export OS_USER_DOMAIN_NAME="$(juju 2>/dev/null config keystone admin-user)_domain"
  48. printf "\e[2GFetching default admin-user name from Juju\n"
  49. export OS_USERNAME=$(juju 2>/dev/null config keystone admin-user)
  50. printf "\e[2GFetching password for ${OS_USERNAME} from Juju\n"
  51. export OS_PASSWORD=$(juju 2>/dev/null config keystone admin-password)
  52. printf "\e[2GFetching Openstack Region name from Juju\n"
  53. export OS_REGION_NAME=$(juju 2>/dev/null config keystone region)
  54. printf "\e[2GFetching Domain ID for ${OS_USERNAME}_domain from Juju\n"
  55. 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}')
  56. printf "\e[2GFetching Project ID for ${OS_USERNAME} project from Juju\n"
  57. 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}')
  58.  
  59. #Check to make sure we have populated variables
  60. printf "\e[2GValidating that all OS_ variables are present\n"
  61. for var in $(set -o posix; set|/bin/grep -oE '^OS_[^=]+');do
  62. if [[ -z $(eval "echo \$$var") ]];then
  63. export NO_WRITE_RC=true
  64. printf "\e[4GCannot determine value for $var\n"
  65. fi
  66. done
  67.  
  68. #Write the RC File
  69. if [[ ${NO_WRITE_RC} = false ]];then
  70. printf "\e[2GCreating Openstack RC file...\n"
  71. { set|/bin/grep -oE '^OS_[^$]+'|sed 's/^.*$/export &/g;1s/^/#!\/usr\/bin\/env bash\n/;/HYPERVISORS/d'|tee 1>/dev/null ~/admin-openrc.sh; }
  72. [[ $? -eq 0 && -f ~/admin-openrc.sh ]] && { printf "\e[4GSourcing Openstack RC file...\n";source ~/admin-openrc.sh; }
  73. RC_FILE_SOURCED=true
  74. else
  75. 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"
  76. RC_FILE_SOURCED=false
  77. return 1
  78. fi
  79. }
  80.  
  81. #Create Networks
  82. printf "\e[2GCreating Provider Network \"${NEUTRON_EXT_NET_NAME}\"...\n"
  83. 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}')
  84. printf "\e[4GCreating Provider Subnet \"${NEUTRON_EXT_NET_NAME}_subnet\"...\n"
  85. 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}')
  86. printf "\e[2GCreating Private Network \"$NEUTRON_INT_NET_NAME\"...\n"
  87. export NEUTRON_INT_NETWORK_ID=$(neutron net-create ${NEUTRON_INT_NET_NAME}|awk '/id:/{print $4}')
  88. printf "\e[4GCreating Private Subnet \"${NEUTRON_INT_NET_NAME}_subnet\"...\n"
  89. 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}')
  90. printf "\e[2GCreating Router from ${NEUTRON_INT_NET_NAME} to ${NEUTRON_EXT_NET_NAME} \"${NEUTRON_INT_NET_NAME}-to-${NEUTRON_EXT_NET_NAME}\"...\n"
  91. export NEUTRON_INT_ROUTER_ID=$(neutron router-create ${NEUTRON_INT_NET_NAME}-to-${NEUTRON_EXT_NET_NAME} | grep " id" | awk '{print $4}')
  92. printf "\e[4GAdding interface from ${NEUTRON_INT_NET_NAME}_subnet to router \"${NEUTRON_INT_NET_NAME}-to-${NEUTRON_EXT_NET_NAME}\"...\n"
  93. neutron router-interface-add $NEUTRON_INT_ROUTER_ID $NEUTRON_INT_SUBNET_ID &>/dev/null
  94. printf "\e[4GSetting external gateway on \"${NEUTRON_INT_NET_NAME}-to-${NEUTRON_EXT_NET_NAME}\" router to ${NEUTRON_EXT_NET_NAME}...\n"
  95. neutron router-gateway-set $NEUTRON_INT_ROUTER_ID $NEUTRON_EXT_NETWORK_ID &>/dev/null
  96.  
  97. # Create security rules
  98. printf "\e[2GCreating security rule to allow ICMP protocol (ping) to pass\n"
  99. openstack security group rule create --proto icmp default &>/dev/null
  100. printf "\e[2GCreating security rule to allow ssh protocol to pass\n"
  101. openstack security group rule create --proto tcp --dst-port 22 default &>/dev/null
  102. printf "\e[2GCreating security rule to allow http protocol to pass\n"
  103. openstack security group rule create --proto tcp --dst-port 80 default &>/dev/null
  104. printf "\e[2GCreating security rule to allow https protocol to pass\n"
  105. openstack security group rule create --proto tcp --dst-port 443 default &>/dev/null
  106. printf "\e[2GCreating security rule to allow RDP protocol to pass\n"
  107. openstack security group rule create --proto tcp --dst-port 3389 default &>/dev/null
  108. printf "\e[2GCreating security rule to allow novnc protocol to pass\n"
  109. openstack security group rule create --proto tcp --dst-port 6080 default &>/dev/null
  110. printf "\e[2GCreating security rule to allow vnc protocol to pass\n"
  111. openstack security group rule create --proto tcp --dst-port 5900:5999 default &>/dev/null
  112.  
  113.  
  114. printf "\e[2GImporting ssh public key for user ${USER} as the default keypair\n"
  115. # Import SSH Keys for current user
  116. openstack keypair create --public-key ~/.ssh/id_rsa.pub default &>/dev/null
  117.  
  118. # Create Flavors
  119.  
  120. openstack flavor create tiny --id auto --ram 512 --disk 10 --ephemeral 0 --vcpus 1 --public
  121. openstack flavor create small --id auto --ram 1024 --disk 20 --ephemeral 0 --vcpus 2 --public
  122. openstack flavor create medium --id auto --ram 2048 --disk 30 --ephemeral 0 --vcpus 3 --public
  123. openstack flavor create large --id auto --ram 4096 --disk 40 --ephemeral 0 --vcpus 4 --public
  124. openstack flavor create xlarge --id auto --ram 8192 --disk 50 --ephemeral 0 --vcpus 5 --public
  125. openstack flavor create xxlarge --id auto --ram 16384 --disk 60 --ephemeral 0 --vcpus 6 --public
  126.  
  127. #create-aws-flavors() {
  128. #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
  129. #}
  130. #create-gce-flavors() {
  131. #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
  132. #}
  133. #create-azure-flavors() {
  134. #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
  135. #}
  136.  
  137. # Set Quotas
  138. openstack quota set --ram 204800 --cores 200 --instances 100 --volumes 100 ${OS_PROJECT_ID}
  139. # Openstack quota won't set the following, so use old neutron quota update
  140. neutron quota-update --port 100 --security-group 100 --security-group-rule 500 --floatingip 100
  141.  
  142.  
  143.  
  144. # Download images if they don't exist
  145. [[ -f /srv/data/bionic-server-cloudimg-amd64.img ]] || wget http://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img
  146. [[ -f /srv/data/bionic-server-cloudimg-amd64.tar.gz ]] || wget http://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.tar.gz
  147. [[ -f /srv/data/xenial-server-cloudimg-amd64-disk1.img ]] || wget http://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-disk1.img
  148. [[ -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
  149. [[ -f /srv/data/trusty-server-cloudimg-amd64-disk1.img ]] || wget http://cloud-images.ubuntu.com/trusty/current/trusty-server-cloudimg-amd64-disk1.img
  150. [[ -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
  151. [[ -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
  152. [[ -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
  153.  
  154. # Create glance images
  155. glance image-create --name=Bionic-QCOW --visibility=public --container-format=ovf --disk-format=qcow2 < /srv/data/bionic-server-cloudimg-amd64.img
  156. glance image-create --name=Xenial-QCOW --visibility=public --container-format=ovf --disk-format=qcow2 < /srv/data/xenial-server-cloudimg-amd64-disk1.img
  157. glance image-create --name=Trusty-QCOW --visibility=public --container-format=ovf --disk-format=qcow2 < /srv/data/trusty-server-cloudimg-amd64-disk1.img
  158. glance image-create --name=Cirros-QCOW --visibility=public --container-format=ovf --disk-format=qcow2 < /srv/data/cirros-0.4.0-x86_64-disk.img
  159. glance image-create --name=Bionic-ROOT --visibility=public --container-format=bare --disk-format=raw < /srv/data/bionic-server-cloudimg-amd64.tar.gz
  160. glance image-create --name=Xenial-ROOT --visibility=public --container-format=bare --disk-format=raw < /srv/data/xenial-server-cloudimg-amd64-root.tar.gz
  161. glance image-create --name=Trusty-ROOT --visibility=public --container-format=bare --disk-format=raw < /srv/data/trusty-server-cloudimg-amd64-root.tar.gz
  162. 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
  163.  
  164. # Create Hypervisor arrays in order to to create host aggregates and availability zones
  165. declare -ag OS_HYPERVISORS=($(openstack hypervisor list -f json|jq 2>/dev/null -r '.[]|"\(."Hypervisor Hostname")"'))
  166. 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))
  167. 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))
  168.  
  169. # Create nova-kvm Host Aggregate and KVM AZ
  170. if [[ ${#OS_KVM_HYPERVISORS[@]} -gt 1 ]];then
  171. openstack aggregate create --zone KVM nova-kvm
  172. for host in ${OS_KVM_HYPERVISORS[@]};do
  173. openstack aggregate add host nova-kvm $host
  174. done
  175. fi
  176.  
  177. # Create 10 KVM Instances in parallel on public net
  178. for c in {1..10};do printf 'demo-kvm-vm-%03d\n' $((10#${c}));done|\
  179. xargs -I@ -n1 -P0 nova boot \
  180. --flavor tiny \
  181. --key-name default \
  182. --image Bionic-QCOW \
  183. --nic net-name=${NEUTRON_EXT_NET_NAME} \
  184. --security-groups default \
  185. --availability-zone KVM "@"
  186.  
  187. # Show KVM instances
  188. openstack server list --name "demo-kvm.*$"
  189.  
  190.  
  191. #Create 10 LXD Instances in parallel on public net
  192. for c in {1..10};do printf 'demo-lxd-vm-%03d\n' $((10#${c}));done|\
  193. xargs -I@ -n1 -P0 nova boot \
  194. --flavor tiny \
  195. --key-name default \
  196. --image Cirros-ROOT \
  197. --nic net-name=${NEUTRON_EXT_NET_NAME} \
  198. --security-groups default \
  199. --availability-zone LXD "@"
  200.  
  201. # Show LXD instances
  202. openstack server list --name "demo-lxd.*$"
  203.  
  204.  
  205. # Delete LXD Instances in parallel
  206. #for c in {1..25};do printf 'demo-lxd-vm-%03d\n' $((10#${c}));done|xargs -I@ -n1 -P0 openstack server delete "@"
  207.  
  208. # Delete KVM Instances in parallel
  209. #for c in {1..25};do printf 'demo-kvm-vm-%03d\n' $((10#${c}));done|xargs -I@ -n1 -P0 openstack server delete "@"
  210.  
  211. # Delete All VMs in parallel
  212. #{ openstack server list -c ID -f value|sort -uV; }|xargs -I@ -n1 -P0 openstack server delete "@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement