Sample script to create a VDC (and deploy a Photon Docker Host) in vCloud Air

Yesterday I bumped into a semi-draft of code I wrote a while back and that I have never checked in into GitHub.

I spent a few hours to polish it, augmenting it with some Docker related stuff (so that it becomes "cool") and test it a bit. The result is in this repo.

The code is based on the awesome vca-cli tool and it requires an account on vCloud Air (you can subscribe here and get $300 of free credit to play with).

The idea behind this script was to confine a given set of workloads inside a dedicated Virtual Data Center.

Background: one of the latest capabilities of vCloud Director is to allow tenants to deploy Virtual Data Centers from VDC templates the cloud admin defines. vCloud Air OnDemand leverages this capability when you create a new VDC in a given instance of your choice.

While I have tested this sample script with vCloud Air, in theory this should also work when you point the script against a standalone vCloud Director instance with this capability enabled and properly configured (be it on-prem or in a public cloud operated by a VMware vCloud Air Network partner). However, mind I have not tested these two additional scenarios.

If you decide to test them note you will need to tweak the way login steps currently work. The version on GitHub is configured to point to vCA as a backend. If you intend to use a vCD standalone instance the login is going to be different [feel free to reach out if in doubt].

The sample code on GitHub creates a new VDC in the vCA instance of your choice and configures some network plumbing. Eventually, the code grabs an OVA file off the Internet (an image of Photon OS TP2) and deploys it in the newly created VDC. In the end, the script configures some NAT rules to allow you to SSH into the VM that has just been deployed.

For a more detailed list of things that the script does please check out the README on GitHub. The same page lists all pre-requisites you need to have in place to run the code.

In order to try to cover more broadly potential use cases I am also showing, in the code, how to inject a shell script (dockerstart.sh) into the VM before powering it on.

In my case I am just running a simple command to start the docker daemon on Photon OS guest. Consider it just a place holder for commands you may want to pass into a VM at deployment time.

For your convenience, below is the current content of the code as on GitHub:

 1# usage
 2# ./CreateVDCvCloudAir.sh
 3
 4# This sample script creates a new VDC in vCloud Air from an existing VDC template 
 5# It requires ovftool (4.1), vca-cli (15), curl and jq (min 1.5) to be installed on the system  
 6# It will also download, import and deploy an OVA. 
 7# In addition it will configure the Edge GW in the new VDC to talk allow traffic to/from the appliance
 8
 9# given I had problems installing jq 1.5 using apt-get I am grabbing version 1.5 with brute-force 
10curl -o ./jq -L https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64
11chmod +x jq 
12echo 
13
14read -p "Enter user name : " USER
15echo -n Enter Password: 
16read -s PASSWORD
17echo
18
19vca login $USER --password $PASSWORD
20echo
21vca instance
22echo
23read -p "Enter InstanceId you want to create the <photon> VDC in: " INSTANCEID
24echo 
25
26vca instance use --instance $INSTANCEID
27
28echo
29vca org list-templates
30echo 
31read -p "Enter the VDC template you want to use (DO NOT use -dr- VDCs): " TEMPLATEID
32echo 
33
34VCA_ORG_VDC_NAME='MYVDC'
35
36vca vdc create --vdc $VCA_ORG_VDC_NAME --template "$TEMPLATEID"
37vca vdc use --vdc $VCA_ORG_VDC_NAME
38vca network create --network DMZ --gateway-ip 192.168.209.1 --netmask 255.255.255.0 --dns1 8.8.8.8 --pool 192.168.209.100-192.168.209.149
39vca dhcp enable 
40vca dhcp add --network DMZ --pool 192.168.209.50-192.168.209.99
41vca gateway add-ip
42
43echo 
44curl -L -O https://dl.bintray.com/vmware/photon/ova/1.0TP2/x86_64/photon-1.0TP2.ova
45echo 
46
47VCA_URL=`vca -j instance info | ./jq --raw-output '.instance.region'` && echo $VCA_URL
48VCA_ORG_NAME=`vca -j instance info | ./jq --raw-output '.instance.instanceAttributes' | ./jq --raw-output .orgName` && echo $VCA_ORG_NAME
49VCA_CATALOG_NAME='default-catalog'
50
51
52FILE_TO_UPLOAD='photon-1.0TP2.ova'
53TEMPLATE_NAME_IN_VCA='photon-1.0TP2'
54
55ovftool --acceptAllEulas --skipManifestCheck --vCloudTemplate=true --allowExtraConfig --X:logFile=vcd-upload.log --X:logLevel=verbose \
56"${FILE_TO_UPLOAD}" \
57"vcloud://${USER}:${PASSWORD}@${VCA_URL}?org=${VCA_ORG_NAME}&vdc=${VCA_ORG_VDC_NAME}&catalog=${VCA_CATALOG_NAME}&vappTemplate=${TEMPLATE_NAME_IN_VCA}"
58
59echo
60echo Getting ready to deploy the VM. Wait... 
61echo
62
63sleep 3m 
64
65VAPP_NAME="photon-01"
66VM_NAME=$VAPP_NAME
67MANUAL_IP="192.168.209.49"
68
69echo
70vca vapp create -a $VAPP_NAME -V $VM_NAME -c $VCA_CATALOG_NAME -t $TEMPLATE_NAME_IN_VCA -n DMZ -m manual --ip $MANUAL_IP
71echo
72vca vapp customize --vapp $VAPP_NAME --vm $VM_NAME --file ./startdocker.sh
73echo 
74
75echo 
76IP=`vca -j vm -a $VAPP_NAME | ./jq -r '.vms[0].IPs'` && echo "private IP:" $IP 
77PUB_IP=`vca -j gateway | ./jq --raw-output '.gateways[0]."External IPs"'` && echo "public IP:" $PUB_IP
78echo
79
80vca nat add --type snat --original-ip 192.168.209.0/24 --translated-ip ${PUB_IP}
81vca nat add --type dnat --original-ip ${PUB_IP} --original-port 22 --translated-ip $IP --translated-port 22 --protocol tcp
82
83vca firewall disable 
84
85echo
86echo We are done! You can now connect to your VM by SSHing into ${PUB_IP} "[root / changeme -> note you will be asked to change the pwd]"
87echo

Enjoy.

Massimo.