From ea0725654550b11a4a29487546c85d94b594dffc Mon Sep 17 00:00:00 2001 From: Xicheng Chang Date: Mon, 4 May 2015 11:59:08 -0700 Subject: [PATCH] Make launch.sh idempotent Change-Id: I9b3f989397c4c36ab3717d1cca84841778881186 --- vagrant/README.md | 10 +++++++- vagrant/destroy.sh | 14 +++++++++++ vagrant/first_run.sh | 56 ++++++++++++++++++++++++++++++++++++++++++++ vagrant/launch.sh | 52 ++++------------------------------------ vagrant/run.sh | 9 +++++++ 5 files changed, 93 insertions(+), 48 deletions(-) create mode 100755 vagrant/destroy.sh create mode 100755 vagrant/first_run.sh create mode 100755 vagrant/run.sh diff --git a/vagrant/README.md b/vagrant/README.md index f5da25f3..1da0dc5a 100644 --- a/vagrant/README.md +++ b/vagrant/README.md @@ -17,7 +17,15 @@ run: ./launch.sh ``` -after launch finishes, to access compass server: +or + +``` +./launch.sh --recreate +``` + +the "recreate" flag will destroy all configured vms and hostonly networks in VirtualBox. + +After launch finishes, to access compass server: ``` vagrant ssh diff --git a/vagrant/destroy.sh b/vagrant/destroy.sh new file mode 100755 index 00000000..c46e5e30 --- /dev/null +++ b/vagrant/destroy.sh @@ -0,0 +1,14 @@ +#!/bin/bash +# + +VBoxManage unregistervm controller --delete +VBoxManage unregistervm compute --delete +VBoxManage unregistervm network --delete +VBoxManage unregistervm storage --delete +vagrant destroy -f +hostonlyifs=$(VBoxManage list hostonlyifs|grep '\ vboxnet'| awk -F ' ' '{print $2}') +for hostonlyif in $hostonlyifs +do + VBoxManage hostonlyif remove $hostonlyif +done +rm -rf $HOME/VirtualBox VMs/* diff --git a/vagrant/first_run.sh b/vagrant/first_run.sh new file mode 100755 index 00000000..4f681c49 --- /dev/null +++ b/vagrant/first_run.sh @@ -0,0 +1,56 @@ +#!/bin/bash +# + +SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +if [ -f $HOME/VirtualBox\ VMs/compass-server ];then + mkdir -p $HOME/VirtualBox VMs/compass-server +fi + +VBoxManage hostonlyif create +VBoxManage hostonlyif create > $HOME/adapter1info +export adapter1=`cut -d\' -f2 $HOME/adapter1info` +VBoxManage hostonlyif ipconfig $adapter1 --ip "192.168.33.1" --netmask "255.255.255.0" + +VBoxManage hostonlyif create > $HOME/adapter2info +export adapter2=`cut -d\' -f2 $HOME/adapter2info` +VBoxManage hostonlyif ipconfig $adapter2 --ip "172.16.10.1" --netmask "255.255.255.0" + +VBoxManage createvm --name controller --ostype Linux_64 --register +VBoxManage createvm --name compute --ostype Linux_64 --register +VBoxManage createvm --name network --ostype Linux_64 --register +VBoxManage createvm --name storage --ostype Linux_64 --register + +# controller +VBoxManage modifyvm controller --memory 2048 --nic1 hostonly --hostonlyadapter1 $adapter1 --macaddress1 000102030405 --nic2 hostonly --hostonlyadapter2 $adapter2 --nicpromisc2 allow-vms --macaddress2 000120295BFA --vram 12 --boot1 net --boot2 disk +VBoxManage createhd --filename "$HOME/VirtualBox VMs/controller/controller.vdi" --size 32768 +VBoxManage storagectl controller --name "controller-IDE" --add ide +VBoxManage storageattach controller --storagectl "controller-IDE" --port 0 --device 0 --type hdd --medium "$HOME/VirtualBox VMs/controller/controller.vdi" + +# compute +VBoxManage modifyvm compute --memory 3072 --nic1 hostonly --hostonlyadapter1 $adapter1 --macaddress1 000102030406 --nic2 hostonly --hostonlyadapter2 $adapter2 --nicpromisc2 allow-vms --macaddress2 000120295BFB --vram 12 --boot1 net --boot2 disk +VBoxManage createhd --filename "$HOME/VirtualBox VMs/compute/compute.vdi" --size 16384 +VBoxManage storagectl compute --name "compute-IDE" --add ide +VBoxManage storageattach compute --storagectl "compute-IDE" --port 0 --device 0 --type hdd --medium "$HOME/VirtualBox VMs/compute/compute.vdi" + +# network +VBoxManage modifyvm network --memory 2048 --nic1 hostonly --hostonlyadapter1 $adapter1 --macaddress1 000102030407 --nic2 hostonly --hostonlyadapter2 $adapter2 --nicpromisc2 allow-vms --macaddress2 000120295BFC --vram 12 --boot1 net --boot2 disk +VBoxManage createhd --filename "$HOME/VirtualBox VMs/network/network.vdi" --size 16384 +VBoxManage storagectl network --name "network-IDE" --add ide +VBoxManage storageattach network --storagectl "network-IDE" --port 0 --device 0 --type hdd --medium "$HOME/VirtualBox VMs/network/network.vdi" + +# storage +VBoxManage modifyvm storage --memory 2048 --nic1 hostonly --hostonlyadapter1 $adapter1 --macaddress1 000102030408 --nic2 hostonly --hostonlyadapter2 $adapter2 --nicpromisc2 allow-vms --macaddress2 000120295BFD --vram 12 --boot1 net --boot2 disk +VBoxManage createhd --filename "$HOME/VirtualBox VMs/storage/storage.vdi" --size 32768 +VBoxManage storagectl storage --name "storage-IDE" --add ide +VBoxManage storageattach storage --storagectl "storage-IDE" --port 0 --device 0 --type hdd --medium "$HOME/VirtualBox VMs/storage/storage.vdi" + +# compass +# get latest ansible code +cd $SCRIPT_DIR +git clone -b dev/experimental git://git.openstack.org/stackforge/compass-adapters.git +cp -r compass-adapters/ansible/openstack_juno compass-adapters/ansible/openstack_juno_plumgrid +vagrant box list |grep compass +if [ "$?" != "0" ]; then +vagrant box add compass https://atlas.hashicorp.com/compass-dev/boxes/compass/versions/0.0.1/providers/compass.box +fi +vagrant up --provision diff --git a/vagrant/launch.sh b/vagrant/launch.sh index 4f681c49..7da17c38 100755 --- a/vagrant/launch.sh +++ b/vagrant/launch.sh @@ -6,51 +6,9 @@ if [ -f $HOME/VirtualBox\ VMs/compass-server ];then mkdir -p $HOME/VirtualBox VMs/compass-server fi -VBoxManage hostonlyif create -VBoxManage hostonlyif create > $HOME/adapter1info -export adapter1=`cut -d\' -f2 $HOME/adapter1info` -VBoxManage hostonlyif ipconfig $adapter1 --ip "192.168.33.1" --netmask "255.255.255.0" - -VBoxManage hostonlyif create > $HOME/adapter2info -export adapter2=`cut -d\' -f2 $HOME/adapter2info` -VBoxManage hostonlyif ipconfig $adapter2 --ip "172.16.10.1" --netmask "255.255.255.0" - -VBoxManage createvm --name controller --ostype Linux_64 --register -VBoxManage createvm --name compute --ostype Linux_64 --register -VBoxManage createvm --name network --ostype Linux_64 --register -VBoxManage createvm --name storage --ostype Linux_64 --register - -# controller -VBoxManage modifyvm controller --memory 2048 --nic1 hostonly --hostonlyadapter1 $adapter1 --macaddress1 000102030405 --nic2 hostonly --hostonlyadapter2 $adapter2 --nicpromisc2 allow-vms --macaddress2 000120295BFA --vram 12 --boot1 net --boot2 disk -VBoxManage createhd --filename "$HOME/VirtualBox VMs/controller/controller.vdi" --size 32768 -VBoxManage storagectl controller --name "controller-IDE" --add ide -VBoxManage storageattach controller --storagectl "controller-IDE" --port 0 --device 0 --type hdd --medium "$HOME/VirtualBox VMs/controller/controller.vdi" - -# compute -VBoxManage modifyvm compute --memory 3072 --nic1 hostonly --hostonlyadapter1 $adapter1 --macaddress1 000102030406 --nic2 hostonly --hostonlyadapter2 $adapter2 --nicpromisc2 allow-vms --macaddress2 000120295BFB --vram 12 --boot1 net --boot2 disk -VBoxManage createhd --filename "$HOME/VirtualBox VMs/compute/compute.vdi" --size 16384 -VBoxManage storagectl compute --name "compute-IDE" --add ide -VBoxManage storageattach compute --storagectl "compute-IDE" --port 0 --device 0 --type hdd --medium "$HOME/VirtualBox VMs/compute/compute.vdi" - -# network -VBoxManage modifyvm network --memory 2048 --nic1 hostonly --hostonlyadapter1 $adapter1 --macaddress1 000102030407 --nic2 hostonly --hostonlyadapter2 $adapter2 --nicpromisc2 allow-vms --macaddress2 000120295BFC --vram 12 --boot1 net --boot2 disk -VBoxManage createhd --filename "$HOME/VirtualBox VMs/network/network.vdi" --size 16384 -VBoxManage storagectl network --name "network-IDE" --add ide -VBoxManage storageattach network --storagectl "network-IDE" --port 0 --device 0 --type hdd --medium "$HOME/VirtualBox VMs/network/network.vdi" - -# storage -VBoxManage modifyvm storage --memory 2048 --nic1 hostonly --hostonlyadapter1 $adapter1 --macaddress1 000102030408 --nic2 hostonly --hostonlyadapter2 $adapter2 --nicpromisc2 allow-vms --macaddress2 000120295BFD --vram 12 --boot1 net --boot2 disk -VBoxManage createhd --filename "$HOME/VirtualBox VMs/storage/storage.vdi" --size 32768 -VBoxManage storagectl storage --name "storage-IDE" --add ide -VBoxManage storageattach storage --storagectl "storage-IDE" --port 0 --device 0 --type hdd --medium "$HOME/VirtualBox VMs/storage/storage.vdi" - -# compass -# get latest ansible code -cd $SCRIPT_DIR -git clone -b dev/experimental git://git.openstack.org/stackforge/compass-adapters.git -cp -r compass-adapters/ansible/openstack_juno compass-adapters/ansible/openstack_juno_plumgrid -vagrant box list |grep compass -if [ "$?" != "0" ]; then -vagrant box add compass https://atlas.hashicorp.com/compass-dev/boxes/compass/versions/0.0.1/providers/compass.box +if [ "$1" == "--recreate" ]; then + source destroy.sh + source first_run.sh +else + source run.sh fi -vagrant up --provision diff --git a/vagrant/run.sh b/vagrant/run.sh new file mode 100755 index 00000000..c493cf6b --- /dev/null +++ b/vagrant/run.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# + +VBoxManage list vms|grep controller +if [ "$?" == "0" ]; then + exit 0 +else + source first_run.sh +fi