blob: 277b940095f0a2e1b225d74529a62630c75efcd3 [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
47}
48
49update_iso() {
50 local mac1=${1}
51 local mac2=${2}
52 local mountPoint="cfg-iso"
53 local mountPointUpdated="cfg-iso-new"
54 local oshost=$(uname)
55 mkdir "${mountPoint}" "${mountPointUpdated}"
56 if [[ "${oshost}" == 'Darwin' ]]; then
57 hdiutil mount -mountpoint "${mountPoint}" "${CONFIG_DRIVE_ISO}"
58 else
59 mount "${CONFIG_DRIVE_ISO}" "${mountPoint}"
60 fi
61 cp -rf "${mountPoint}"/* "${mountPointUpdated}"
62 if [[ "${oshost}" == 'Darwin' ]]; then
63 hdiutil unmount "${mountPoint}"
64 else
65 umount "${mountPoint}"
66 fi
67 chmod -R +w "${mountPointUpdated}"
68 local openstackConfig="${mountPointUpdated}/openstack/latest"
69 local iso_label=''
70 if [[ -d "${openstackConfig}" ]]; then
71 local networkConfigOpenstack="${openstackConfig}/network_data.json"
72 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 +040073 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 +040074 iso_label='config-2'
75 else
76 local networkConfigV2Template="""
77version: 2
78ethernets:
79 if0:
80 match:
81 macaddress: "${mac1}"
82 set-name: ens3
83 wakeonlan: true
84 addresses:
85 - "${DEPLOY_IP_ADDRESS}/${DEPLOY_NETMASK}"
86 gateway4: "${DEPLOY_GATEWAY}"
87 if1:
88 match:
89 macaddress: "${mac2}"
90 set-name: ens4
91 addresses:
92 - "${CONTROL_IP_ADDRESS}/${CONTROL_NETMASK}"
93 gateway4: "${CONTROL_GATEWAY}"
94 wakeonlan: true
95"""
96 echo -e "${networkConfigV2Template}" > "${mountPointUpdated}/network-config"
97 iso_label='cidata'
98 fi
99 if [[ "${oshost}" == 'Darwin' ]]; then
100 hdiutil makehybrid -o "${CONFIG_DRIVE_ISO}" "${mountPointUpdated}" -iso -joliet -ov -iso-volume-name "${iso_label}" -default-volume-name "${iso_label}"
101 else
102 genisoimage -output "${CONFIG_DRIVE_ISO}" -volid "${iso_label}" -joliet -rock "${mountPointUpdated}"
103 fi
104 rm -rf "${mountPoint}" "${mountPointUpdated}"
105}
106
107define_vm() {
108 vboxmanage createvm --name "${VM_NAME}" --register
109 vboxmanage modifyvm "${VM_NAME}" --ostype Ubuntu_64 \
110 --memory 8188 --cpus 2 --vram 16 \
111 --acpi on --ioapic on --x2apic on \
112 --nic1 natnetwork --hostonlyadapter1 "${DEPLOY_NET_NAME}" --nictype1 virtio \
113 --nic2 hostonly --hostonlyadapter2 "${CONTROL_NET_NAME}" --nictype2 virtio \
114 --pae off --rtcuseutc on --uart1 0x3F8 4 \
115 --usb on --usbehci on --audiocodec ad1980 \
116 --mouse usbtablet
117
118 vboxmanage storagectl "${VM_NAME}" --name "IDE" --add ide
119 vboxmanage storagectl "${VM_NAME}" --name "SATA" --add sata --portcount 1
120
121 vboxmanage storageattach "${VM_NAME}" \
122 --storagectl "SATA" --port 0 --device 0 \
123 --type hdd --medium "${VM_DISK}"
124
125 macaddress1=$(vboxmanage showvminfo "${VM_NAME}" --details --machinereadable | grep macaddress1 | cut -f 2 -d '=' | tr -d '"' | sed 's/../&:/g; s/:$//')
126 macaddress2=$(vboxmanage showvminfo "${VM_NAME}" --details --machinereadable | grep macaddress2 | cut -f 2 -d '=' | tr -d '"' | sed 's/../&:/g; s/:$//')
127
Denis Egorenko44265bd2019-03-15 14:10:44 +0400128 [[ "${UPDATE_ISO_INTERFACES}" =~ [Tt]rue ]] && update_iso ${macaddress1} ${macaddress2}
Denis Egorenkof7aaccf2019-02-26 13:47:25 +0400129
130 vboxmanage storageattach "${VM_NAME}" \
131 --storagectl "IDE" --port 0 --device 0 \
132 --type dvddrive --medium "${CONFIG_DRIVE_ISO}"
133}
134
135[ -f env_overrides ] && source env_overrides
136
137CFG01_IMAGE_LINK=$1
138CONFIG_DRIVE_ISO_LINK=$2
139
140VM_NAME=${VM_NAME:-'cfg01-mcp.local'}
141VM_DISK=${VM_DISK:-'cfg01-disk.vdi'}
142CONFIG_DRIVE_ISO=${CONFIG_DRIVE_ISO:-'cfg01.deploy-local.local-config.iso'}
143
144if [ -z "${CFG01_IMAGE_LINK}" ]; then
145 echo "URL to cfg01 VDI disk image was not provided!"
146 if [ -f "${VM_DISK}" ]; then
147 echo "Found local copy: ${VM_DISK}"
148 else
149 exit 1
150 fi
151else
152 curl -O ${VM_DISK} ${CFG01_IMAGE_LINK}
153fi
154
155if [ -z "${CONFIG_DRIVE_ISO_LINK}" ]; then
156 echo "URL to config-drive ISO image was not provided!"
157 if [ -f "${CONFIG_DRIVE_ISO}" ]; then
158 echo "Found local copy: ${CONFIG_DRIVE_ISO}"
159 else
160 exit 1
161 fi
162else
163 curl -O ${CONFIG_DRIVE_ISO} ${CONFIG_DRIVE_ISO_LINK}
164fi
165
166AUTO_USER_CONFIRM=${AUTO_USER_CONFIRM:-false}
167UPDATE_ISO_INTERFACES=${UPDATE_ISO_INTERFACES:-true}
168
169CONTROL_NET_NAME=${CONTROL_NET_NAME:-'vboxnet0'}
170CONTROL_GATEWAY=${CONTROL_GATEWAY:-'192.168.56.1'}
171CONTROL_NETWORK=${CONTROL_NETWORK:-'192.168.56.0/24'}
172CONTROL_IP_ADDRESS=${CONTROL_IP_ADDRESS:-'192.168.56.15'}
173
174DEPLOY_NET_NAME=${DEPLOY_NET_NAME:-'deploy_nat_network'}
175DEPLOY_NETWORK=${DEPLOY_NETWORK:-'192.168.15.0/24'}
176DEPLOY_GATEWAY=${DEPLOY_GATEWAY:-'192.168.15.1'}
177DEPLOY_IP_ADDRESS=${DEPLOY_IP_ADDRESS:-'192.168.15.15'}
178
179CONTROL_NETMASK=$(count_netmask "${CONTROL_NETWORK}")
180DEPLOY_NETMASK=$(count_netmask "${DEPLOY_NETWORK}")
181
182create_nat_net
183create_host_net
184define_vm
185vboxmanage startvm "${VM_NAME}" --type headless
186echo "VM successfully started, check the VM console"