forked from vmware/govmomi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-cluster.sh
executable file
·132 lines (106 loc) · 3.51 KB
/
create-cluster.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
#!/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.
#
# Configure a vCenter cluster with vSAN datastore, DVS and DVPGs
export GOVC_INSECURE=1
export GOVC_USERNAME=${GOVC_USERNAME:-"[email protected]"}
if [ -z "$GOVC_PASSWORD" ] ; then
# extract password from $GOVC_URL
GOVC_PASSWORD=$(govc env GOVC_PASSWORD)
fi
usage() {
echo "Usage: $0 [-d DATACENTER] [-c CLUSTER] VCSA_IP ESX_IP..." 1>&2
exit 1
}
# Defaults
dc_name="dc1"
cluster_name="cluster1"
vsan_vnic="vmk0"
while getopts c:d: flag
do
case $flag in
c)
cluster_name=$OPTARG
;;
d)
dc_name=$OPTARG
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ $# -lt 2 ] ; then
usage
fi
vc_ip=$1
shift
unset GOVC_DATACENTER
export GOVC_URL="${GOVC_USERNAME}:${GOVC_PASSWORD}@${vc_ip}"
cluster_path="/$dc_name/host/$cluster_name"
dvs_path="/$dc_name/network/DSwitch"
public_network="/$dc_name/network/PublicNetwork"
internal_network="/$dc_name/network/InternalNetwork"
if [ -z "$(govc ls "/$dc_name")" ] ; then
echo "Creating datacenter ${dc_name}..."
govc datacenter.create "$dc_name"
fi
export GOVC_DATACENTER="$dc_name"
if [ -z "$(govc ls "$cluster_path")" ] ; then
echo "Creating cluster ${cluster_path}..."
govc cluster.create "$cluster_name"
fi
if [ -z "$(govc ls "$dvs_path")" ] ; then
echo "Creating dvs ${dvs_path}..."
govc dvs.create -product-version 6.0.0 -folder "$(dirname "$dvs_path")" "$(basename "$dvs_path")"
fi
if [ -z "$(govc ls "$public_network")" ] ; then
govc dvs.portgroup.add -dvs "$dvs_path" -type earlyBinding -nports 16 "$(basename "$public_network")"
fi
if [ -z "$(govc ls "$internal_network")" ] ; then
govc dvs.portgroup.add -dvs "$dvs_path" -type ephemeral "$(basename "$internal_network")"
fi
hosts=()
vsan_hosts=()
for host_ip in "$@" ; do
host_path="$cluster_path/$host_ip"
hosts+=($host_path)
if [ -z "$(govc ls "$host_path")" ] ; then
echo "Adding host ($host_ip) to cluster $cluster_name"
govc cluster.add -cluster "$cluster_path" -noverify -force \
-hostname "$host_ip" -username root -password "$GOVC_PASSWORD"
fi
unclaimed=$(govc host.storage.info -host "$host_path" -unclaimed | tail -n+2 | wc -l)
if [ "$unclaimed" -eq 2 ] ; then
echo "Enabling vSAN traffic on ${vsan_vnic} for ${host_path}..."
govc host.vnic.service -host "$host_path" -enable vsan "$vsan_vnic"
vsan_hosts+=($host_path)
else
echo "Skipping vSAN configuration for ${host_path}: $unclaimed unclaimed disks"
fi
done
govc dvs.add -dvs "$dvs_path" -pnic vmnic1 "${hosts[@]}"
echo "Enabling DRS for ${cluster_path}..."
govc cluster.change -drs-enabled "$cluster_path"
if [ ${#vsan_hosts[@]} -ge 3 ] ; then
echo "Enabling vSAN for ${cluster_path}..."
govc cluster.change -vsan-enabled -vsan-autoclaim "$cluster_path"
fi
echo "Enabling HA for ${cluster_path}..."
govc cluster.change -ha-enabled "$cluster_path"
echo "Granting Admin permissions for user root..."
govc permissions.set -principal root -role Admin
echo "Done."