github.com/vmware/govmomi@v0.43.0/scripts/vcsa/create-cluster.sh (about) 1 #!/bin/bash -e 2 3 # Copyright 2017-2018 VMware, Inc. All Rights Reserved. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 # 17 # Configure a vCenter cluster with vSAN datastore, DVS and DVPGs 18 19 export GOVC_INSECURE=1 20 export GOVC_USERNAME=${GOVC_USERNAME:-"Administrator@vsphere.local"} 21 if [ -z "$GOVC_PASSWORD" ] ; then 22 # extract password from $GOVC_URL 23 GOVC_PASSWORD=$(govc env GOVC_PASSWORD) 24 fi 25 26 usage() { 27 echo "Usage: $0 [-d DATACENTER] [-c CLUSTER] VCSA_IP ESX_IP..." 1>&2 28 exit 1 29 } 30 31 # Defaults 32 dc_name="dc1" 33 cluster_name="cluster1" 34 vsan_vnic="vmk0" 35 36 while getopts c:d: flag 37 do 38 case $flag in 39 c) 40 cluster_name=$OPTARG 41 ;; 42 d) 43 dc_name=$OPTARG 44 ;; 45 *) 46 usage 47 ;; 48 esac 49 done 50 51 shift $((OPTIND-1)) 52 53 if [ $# -lt 2 ] ; then 54 usage 55 fi 56 57 vc_ip=$1 58 shift 59 60 unset GOVC_DATACENTER 61 export GOVC_URL="${GOVC_USERNAME}:${GOVC_PASSWORD}@${vc_ip}" 62 63 cluster_path="/$dc_name/host/$cluster_name" 64 dvs_path="/$dc_name/network/DSwitch" 65 public_network="/$dc_name/network/PublicNetwork" 66 internal_network="/$dc_name/network/InternalNetwork" 67 68 if [ -z "$(govc ls "/$dc_name")" ] ; then 69 echo "Creating datacenter ${dc_name}..." 70 govc datacenter.create "$dc_name" 71 fi 72 73 export GOVC_DATACENTER="$dc_name" 74 75 if [ -z "$(govc ls "$cluster_path")" ] ; then 76 echo "Creating cluster ${cluster_path}..." 77 govc cluster.create "$cluster_name" 78 fi 79 80 if [ -z "$(govc ls "$dvs_path")" ] ; then 81 echo "Creating dvs ${dvs_path}..." 82 govc dvs.create -product-version 6.0.0 -folder "$(dirname "$dvs_path")" "$(basename "$dvs_path")" 83 fi 84 85 if [ -z "$(govc ls "$public_network")" ] ; then 86 govc dvs.portgroup.add -dvs "$dvs_path" -type earlyBinding -nports 16 "$(basename "$public_network")" 87 fi 88 89 if [ -z "$(govc ls "$internal_network")" ] ; then 90 govc dvs.portgroup.add -dvs "$dvs_path" -type ephemeral "$(basename "$internal_network")" 91 fi 92 93 hosts=() 94 vsan_hosts=() 95 96 for host_ip in "$@" ; do 97 host_path="$cluster_path/$host_ip" 98 hosts+=($host_path) 99 100 if [ -z "$(govc ls "$host_path")" ] ; then 101 echo "Adding host ($host_ip) to cluster $cluster_name" 102 govc cluster.add -cluster "$cluster_path" -noverify -force \ 103 -hostname "$host_ip" -username root -password "$GOVC_PASSWORD" 104 fi 105 106 unclaimed=$(govc host.storage.info -host "$host_path" -unclaimed | tail -n+2 | wc -l) 107 if [ "$unclaimed" -eq 2 ] ; then 108 echo "Enabling vSAN traffic on ${vsan_vnic} for ${host_path}..." 109 govc host.vnic.service -host "$host_path" -enable vsan "$vsan_vnic" 110 vsan_hosts+=($host_path) 111 else 112 echo "Skipping vSAN configuration for ${host_path}: $unclaimed unclaimed disks" 113 fi 114 done 115 116 govc dvs.add -dvs "$dvs_path" -pnic vmnic1 "${hosts[@]}" 117 118 echo "Enabling DRS for ${cluster_path}..." 119 govc cluster.change -drs-enabled "$cluster_path" 120 121 if [ ${#vsan_hosts[@]} -ge 3 ] ; then 122 echo "Enabling vSAN for ${cluster_path}..." 123 govc cluster.change -vsan-enabled -vsan-autoclaim "$cluster_path" 124 fi 125 126 echo "Enabling HA for ${cluster_path}..." 127 govc cluster.change -ha-enabled "$cluster_path" 128 129 echo "Granting Admin permissions for user root..." 130 govc permissions.set -principal root -role Admin 131 132 echo "Done."