forked from vmware/govmomi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-vcsa-vm.sh
executable file
·145 lines (124 loc) · 3.61 KB
/
create-vcsa-vm.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash -e
# Copyright 2017-2018 VMware, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Create a VCSA VM
usage() {
echo "Usage: $0 [-n VM_NAME] [-i VCSA_OVA] [-a IP] ESX_URL" 1>&2
exit 1
}
export GOVC_INSECURE=1
name=vcsa
# 6.7 U3 https://docs.vmware.com/en/VMware-vSphere/6.7/rn/vsphere-esxi-vcenter-server-67-release-notes.html
ova=VMware-vCenter-Server-Appliance-6.7.0.40000-14367737_OVF10.ova
while getopts a:i:n: flag
do
case $flag in
a)
ip=$OPTARG
;;
i)
ova=$OPTARG
;;
n)
name=$OPTARG
;;
*)
usage
;;
esac
done
if [ -d "$ova" ] ; then
ova=$(ls "$ova"/*.ovf)
fi
shift $((OPTIND-1))
if [ $# -ne 1 ] ; then
usage
fi
export GOVC_URL=$1
network=${GOVC_NETWORK:-$(basename "$(govc ls network)")}
product=$(govc about -json | jq -r .About.ProductLineId)
# Use the same password as GOVC_URL
password=$(govc env GOVC_PASSWORD)
if [ -z "$password" ] ; then
echo "password not set"
exit 1
fi
opts=(
cis.vmdir.password=$password
cis.appliance.root.passwd=$password
cis.appliance.root.shell=/bin/bash
cis.deployment.node.type=embedded
cis.vmdir.domain-name=vsphere.local
cis.vmdir.site-name=VCSA
cis.appliance.net.addr.family=ipv4
cis.appliance.ssh.enabled=True
cis.ceip_enabled=False
cis.deployment.autoconfig=True
)
if [ -z "$ip" ] ; then
mode=dhcp
ntp=0.pool.ntp.org
else
mode=static
# Derive net config from the ESX server
config=$(govc host.info -k -json | jq -r .HostSystems[].Config)
gateway=$(jq -r .Network.IpRouteConfig.DefaultGateway <<<"$config")
dns=$(jq -r .Network.DnsConfig.Address[0] <<<"$config")
ntp=$(jq -r .DateTimeInfo.NtpConfig.Server[0] <<<"$config")
route=$(jq -r ".Network.RouteTableInfo.IpRoute[] | select(.DeviceName == \"vmk0\") | select(.Gateway == \"0.0.0.0\")" <<<"$config")
prefix=$(jq -r .PrefixLength <<<"$route")
opts+=(cis.appliance.net.addr=$ip
cis.appliance.net.prefix=$prefix
cis.appliance.net.dns.servers=$dns
cis.appliance.net.gateway=$gateway)
fi
opts+=(
cis.appliance.ntp.servers="$ntp"
cis.appliance.net.mode=$mode
)
if [ "$product" = "ws" ] ; then
# workstation does not support NFC
dir=$(govc datastore.info -json | jq -r .Datastores[0].Info.Url)
ovftool --name="$name" --acceptAllEulas "$ova" "$dir"
vmx="$name/${name}.vmx"
printf "guestinfo.%s\n" "${opts[@]}" >> "$dir/$vmx"
govc vm.register "$vmx"
govc vm.network.change -vm "$name" -net NAT ethernet-0
else
props=$(printf -- "guestinfo.%s\n" "${opts[@]}" | \
jq --slurp -R 'split("\n") | map(select(. != "")) | map(split("=")) | map({"Key": .[0], "Value": .[1]})')
cat <<EOF | govc import.${ova##*.} -options - "$ova"
{
"Name": "$name",
"Deployment": "tiny",
"DiskProvisioning": "thin",
"IPProtocol": "IPv4",
"Annotation": "VMware vCenter Server Appliance",
"PowerOn": false,
"WaitForIP": false,
"InjectOvfEnv": true,
"NetworkMapping": [
{
"Name": "Network 1",
"Network": "${network}"
}
],
"PropertyMapping": $props
}
EOF
fi
govc vm.change -vm "$name" -g vmwarePhoton64Guest
govc vm.power -on "$name"
govc vm.ip "$name"