blob: e2c4cf66a8456dab4c2c68ae7e3b16b3566977ed [file] [log] [blame]
Denis Egorenkof7aaccf2019-02-26 13:47:25 +04001#!/bin/bash
2
3count_netmask() {
4 local network=$1
5 local cidr=$(echo $network | cut -f 2 -d '/')
6 local ones="printf '1%.0s' {1..${cidr}}"
7 local zeros="printf '0%.0s' {1..$(( 32 - ${cidr} ))}"
8 local netmask_binary="$(echo $ones | bash)$(echo $zeros | bash)"
9 local netmask_decimal=""
10 for i in 0 8 16 24; do
11 netmask_decimal+="$(echo $((2#${netmask_binary:${i}:8})))"
12 [[ "${i}" != '24' ]] && netmask_decimal+='.'
13 done
14 echo "${netmask_decimal}"
15}
16
17create_host_net() {
18 local manageControlNet='yes'
19 vboxmanage list hostonlyifs | grep -o -e "^Name:.*" | grep -q "${CONTROL_NET_NAME}"
20 if [[ $? -ne 0 ]]; then
21 vboxmanage hostonlyif create
22 else
Denis Egorenko44265bd2019-03-15 14:10:44 +040023 if [[ "${AUTO_USER_CONFIRM}" =~ [Ff]alse ]]; then
Denis Egorenkof7aaccf2019-02-26 13:47:25 +040024 echo "Hosted-network ${CONTROL_NET_NAME} already exists, shall we override it? Type 'yes' to confirm or anything else to skip."
25 read manageControlNet
26 fi
27 fi
28 if [[ "${manageControlNet}" == 'yes' ]]; then
29 vboxmanage hostonlyif ipconfig "${CONTROL_NET_NAME}" --ip "${CONTROL_GATEWAY}" --netmask "${CONTROL_NETMASK}"
30 fi
31}
32
33create_nat_net() {
34 local manageDeployNet='yes'
35 vboxmanage natnetwork list | grep -o -e "^Name:.*" | grep -q "${DEPLOY_NET_NAME}"
36 if [[ $? -ne 0 ]]; then
37 vboxmanage natnetwork add --netname "${DEPLOY_NET_NAME}" --network "${DEPLOY_NETWORK}" --enable --dhcp off
38 else
Denis Egorenko44265bd2019-03-15 14:10:44 +040039 if [[ "${AUTO_USER_CONFIRM}" =~ [Ff]alse ]]; then
Denis Egorenkof7aaccf2019-02-26 13:47:25 +040040 echo "Nat network ${DEPLOY_NET_NAME} already exists, shall we override it? Type 'yes' to confirm or anything else to skip."
41 read manageDeployNet
42 fi
43 if [[ "${manageDeployNet}" == 'yes' ]]; then
Denis Egorenko44265bd2019-03-15 14:10:44 +040044 vboxmanage natnetwork modify --netname "${DEPLOY_NET_NAME}" --network "${DEPLOY_NETWORK}" --enable --dhcp off
Denis Egorenkof7aaccf2019-02-26 13:47:25 +040045 fi
46 fi
Oleg Gelbukhd17cde72019-04-03 16:56:46 -070047 vboxmanage natnetwork start --netname "${DEPLOY_NET_NAME}"
Denis Egorenkof7aaccf2019-02-26 13:47:25 +040048}
49
50update_iso() {
51 local mac1=${1}
52 local mac2=${2}
53 local mountPoint="cfg-iso"
54 local mountPointUpdated="cfg-iso-new"
55 local oshost=$(uname)
56 mkdir "${mountPoint}" "${mountPointUpdated}"
57 if [[ "${oshost}" == 'Darwin' ]]; then
58 hdiutil mount -mountpoint "${mountPoint}" "${CONFIG_DRIVE_ISO}"
59 else
60 mount "${CONFIG_DRIVE_ISO}" "${mountPoint}"
61 fi
62 cp -rf "${mountPoint}"/* "${mountPointUpdated}"
63 if [[ "${oshost}" == 'Darwin' ]]; then
64 hdiutil unmount "${mountPoint}"
65 else
66 umount "${mountPoint}"
67 fi
68 chmod -R +w "${mountPointUpdated}"
69 local openstackConfig="${mountPointUpdated}/openstack/latest"
70 local iso_label=''
71 if [[ -d "${openstackConfig}" ]]; then
72 local networkConfigOpenstack="${openstackConfig}/network_data.json"
73 local ens="[{'ethernet_mac_address': '${mac1}', 'type': 'phy', 'id': 'ens3', 'name': 'ens3'}, {'ethernet_mac_address': '${mac2}', 'type': 'phy', 'id': 'ens4', 'name': 'ens4'}]"
Denis Egorenko44265bd2019-03-15 14:10:44 +040074 python -c "import json; networkData=json.load(open('${networkConfigOpenstack}', 'r')); networkData['links']=${ens}; json.dump(networkData, open('${networkConfigOpenstack}', 'w'))"
Denis Egorenkof7aaccf2019-02-26 13:47:25 +040075 iso_label='config-2'
76 else
77 local networkConfigV2Template="""
78version: 2
79ethernets:
80 if0:
81 match:
82 macaddress: "${mac1}"
83 set-name: ens3
84 wakeonlan: true
85 addresses:
86 - "${DEPLOY_IP_ADDRESS}/${DEPLOY_NETMASK}"
87 gateway4: "${DEPLOY_GATEWAY}"
88 if1:
89 match:
90 macaddress: "${mac2}"
91 set-name: ens4
92 addresses:
93 - "${CONTROL_IP_ADDRESS}/${CONTROL_NETMASK}"
94 gateway4: "${CONTROL_GATEWAY}"
95 wakeonlan: true
96"""
97 echo -e "${networkConfigV2Template}" > "${mountPointUpdated}/network-config"
98 iso_label='cidata'
99 fi
100 if [[ "${oshost}" == 'Darwin' ]]; then
101 hdiutil makehybrid -o "${CONFIG_DRIVE_ISO}" "${mountPointUpdated}" -iso -joliet -ov -iso-volume-name "${iso_label}" -default-volume-name "${iso_label}"
102 else
103 genisoimage -output "${CONFIG_DRIVE_ISO}" -volid "${iso_label}" -joliet -rock "${mountPointUpdated}"
104 fi
105 rm -rf "${mountPoint}" "${mountPointUpdated}"
106}
107
108define_vm() {
Oleg Gelbukhd17cde72019-04-03 16:56:46 -0700109 VBOX_CONFIG_FILE="${HOME}/VirtualBox VMs/${VM_NAME}/${VM_NAME}.vbox"
110 if test -f "${VBOX_CONFIG_FILE}"; then
111 rm -v "${VBOX_CONFIG_FILE}"
112 fi
Denis Egorenkof7aaccf2019-02-26 13:47:25 +0400113 vboxmanage createvm --name "${VM_NAME}" --register
114 vboxmanage modifyvm "${VM_NAME}" --ostype Ubuntu_64 \
115 --memory 8188 --cpus 2 --vram 16 \
116 --acpi on --ioapic on --x2apic on \
117 --nic1 natnetwork --hostonlyadapter1 "${DEPLOY_NET_NAME}" --nictype1 virtio \
118 --nic2 hostonly --hostonlyadapter2 "${CONTROL_NET_NAME}" --nictype2 virtio \
119 --pae off --rtcuseutc on --uart1 0x3F8 4 \
120 --usb on --usbehci on --audiocodec ad1980 \
121 --mouse usbtablet
122
123 vboxmanage storagectl "${VM_NAME}" --name "IDE" --add ide
124 vboxmanage storagectl "${VM_NAME}" --name "SATA" --add sata --portcount 1
125
126 vboxmanage storageattach "${VM_NAME}" \
127 --storagectl "SATA" --port 0 --device 0 \
128 --type hdd --medium "${VM_DISK}"
129
130 macaddress1=$(vboxmanage showvminfo "${VM_NAME}" --details --machinereadable | grep macaddress1 | cut -f 2 -d '=' | tr -d '"' | sed 's/../&:/g; s/:$//')
131 macaddress2=$(vboxmanage showvminfo "${VM_NAME}" --details --machinereadable | grep macaddress2 | cut -f 2 -d '=' | tr -d '"' | sed 's/../&:/g; s/:$//')
132
Denis Egorenko44265bd2019-03-15 14:10:44 +0400133 [[ "${UPDATE_ISO_INTERFACES}" =~ [Tt]rue ]] && update_iso ${macaddress1} ${macaddress2}
Denis Egorenkof7aaccf2019-02-26 13:47:25 +0400134
135 vboxmanage storageattach "${VM_NAME}" \
136 --storagectl "IDE" --port 0 --device 0 \
137 --type dvddrive --medium "${CONFIG_DRIVE_ISO}"
Oleg Gelbukhd17cde72019-04-03 16:56:46 -0700138
Denis Egorenkof7aaccf2019-02-26 13:47:25 +0400139}
140
141[ -f env_overrides ] && source env_overrides
142
143CFG01_IMAGE_LINK=$1
144CONFIG_DRIVE_ISO_LINK=$2
145
146VM_NAME=${VM_NAME:-'cfg01-mcp.local'}
147VM_DISK=${VM_DISK:-'cfg01-disk.vdi'}
148CONFIG_DRIVE_ISO=${CONFIG_DRIVE_ISO:-'cfg01.deploy-local.local-config.iso'}
149
150if [ -z "${CFG01_IMAGE_LINK}" ]; then
151 echo "URL to cfg01 VDI disk image was not provided!"
152 if [ -f "${VM_DISK}" ]; then
153 echo "Found local copy: ${VM_DISK}"
154 else
155 exit 1
156 fi
157else
158 curl -O ${VM_DISK} ${CFG01_IMAGE_LINK}
159fi
160
161if [ -z "${CONFIG_DRIVE_ISO_LINK}" ]; then
162 echo "URL to config-drive ISO image was not provided!"
163 if [ -f "${CONFIG_DRIVE_ISO}" ]; then
164 echo "Found local copy: ${CONFIG_DRIVE_ISO}"
165 else
166 exit 1
167 fi
168else
169 curl -O ${CONFIG_DRIVE_ISO} ${CONFIG_DRIVE_ISO_LINK}
170fi
171
172AUTO_USER_CONFIRM=${AUTO_USER_CONFIRM:-false}
173UPDATE_ISO_INTERFACES=${UPDATE_ISO_INTERFACES:-true}
174
175CONTROL_NET_NAME=${CONTROL_NET_NAME:-'vboxnet0'}
176CONTROL_GATEWAY=${CONTROL_GATEWAY:-'192.168.56.1'}
177CONTROL_NETWORK=${CONTROL_NETWORK:-'192.168.56.0/24'}
178CONTROL_IP_ADDRESS=${CONTROL_IP_ADDRESS:-'192.168.56.15'}
179
180DEPLOY_NET_NAME=${DEPLOY_NET_NAME:-'deploy_nat_network'}
181DEPLOY_NETWORK=${DEPLOY_NETWORK:-'192.168.15.0/24'}
182DEPLOY_GATEWAY=${DEPLOY_GATEWAY:-'192.168.15.1'}
183DEPLOY_IP_ADDRESS=${DEPLOY_IP_ADDRESS:-'192.168.15.15'}
184
185CONTROL_NETMASK=$(count_netmask "${CONTROL_NETWORK}")
186DEPLOY_NETMASK=$(count_netmask "${DEPLOY_NETWORK}")
187
188create_nat_net
189create_host_net
190define_vm
191vboxmanage startvm "${VM_NAME}" --type headless
Oleg Gelbukhd17cde72019-04-03 16:56:46 -0700192
193vboxmanage controlvm "${VM_NAME}" nic1 null
194vboxmanage controlvm "${VM_NAME}" nic1 natnetwork "${DEPLOY_NET_NAME}"
195
Denis Egorenkof7aaccf2019-02-26 13:47:25 +0400196echo "VM successfully started, check the VM console"