github.com/vmware/govmomi@v0.51.0/scripts/vcsa/create-cluster.sh (about) 1 #!/bin/bash -e 2 3 # © Broadcom. All Rights Reserved. 4 # The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 5 # SPDX-License-Identifier: Apache-2.0 6 # 7 # Configure a vCenter cluster with vSAN datastore, DVS and DVPGs 8 9 export GOVC_INSECURE=1 10 export GOVC_USERNAME=${GOVC_USERNAME:-"Administrator@vsphere.local"} 11 if [ -z "$GOVC_PASSWORD" ] ; then 12 # extract password from $GOVC_URL 13 GOVC_PASSWORD=$(govc env GOVC_PASSWORD) 14 fi 15 16 usage() { 17 echo "Usage: $0 [-d DATACENTER] [-c CLUSTER] VCSA_IP ESX_IP..." 1>&2 18 exit 1 19 } 20 21 # Defaults 22 dc_name="dc1" 23 cluster_name="cluster1" 24 vsan_vnic="vmk0" 25 26 while getopts c:d: flag 27 do 28 case $flag in 29 c) 30 cluster_name=$OPTARG 31 ;; 32 d) 33 dc_name=$OPTARG 34 ;; 35 *) 36 usage 37 ;; 38 esac 39 done 40 41 shift $((OPTIND-1)) 42 43 if [ $# -lt 2 ] ; then 44 usage 45 fi 46 47 vc_ip=$1 48 shift 49 50 unset GOVC_DATACENTER 51 export GOVC_URL="${GOVC_USERNAME}:${GOVC_PASSWORD}@${vc_ip}" 52 53 cluster_path="/$dc_name/host/$cluster_name" 54 dvs_path="/$dc_name/network/DSwitch" 55 public_network="/$dc_name/network/PublicNetwork" 56 internal_network="/$dc_name/network/InternalNetwork" 57 58 if [ -z "$(govc ls "/$dc_name")" ] ; then 59 echo "Creating datacenter ${dc_name}..." 60 govc datacenter.create "$dc_name" 61 fi 62 63 export GOVC_DATACENTER="$dc_name" 64 65 if [ -z "$(govc ls "$cluster_path")" ] ; then 66 echo "Creating cluster ${cluster_path}..." 67 govc cluster.create "$cluster_name" 68 fi 69 70 if [ -z "$(govc ls "$dvs_path")" ] ; then 71 echo "Creating dvs ${dvs_path}..." 72 govc dvs.create -product-version 6.0.0 -folder "$(dirname "$dvs_path")" "$(basename "$dvs_path")" 73 fi 74 75 if [ -z "$(govc ls "$public_network")" ] ; then 76 govc dvs.portgroup.add -dvs "$dvs_path" -type earlyBinding -nports 16 "$(basename "$public_network")" 77 fi 78 79 if [ -z "$(govc ls "$internal_network")" ] ; then 80 govc dvs.portgroup.add -dvs "$dvs_path" -type ephemeral "$(basename "$internal_network")" 81 fi 82 83 hosts=() 84 vsan_hosts=() 85 86 for host_ip in "$@" ; do 87 host_path="$cluster_path/$host_ip" 88 hosts+=($host_path) 89 90 if [ -z "$(govc ls "$host_path")" ] ; then 91 echo "Adding host ($host_ip) to cluster $cluster_name" 92 govc cluster.add -cluster "$cluster_path" -noverify -force \ 93 -hostname "$host_ip" -username root -password "$GOVC_PASSWORD" 94 fi 95 96 unclaimed=$(govc host.storage.info -host "$host_path" -unclaimed | tail -n+2 | wc -l) 97 if [ "$unclaimed" -eq 2 ] ; then 98 echo "Enabling vSAN traffic on ${vsan_vnic} for ${host_path}..." 99 govc host.vnic.service -host "$host_path" -enable vsan "$vsan_vnic" 100 vsan_hosts+=($host_path) 101 else 102 echo "Skipping vSAN configuration for ${host_path}: $unclaimed unclaimed disks" 103 fi 104 done 105 106 govc dvs.add -dvs "$dvs_path" -pnic vmnic1 "${hosts[@]}" 107 108 echo "Enabling DRS for ${cluster_path}..." 109 govc cluster.change -drs-enabled "$cluster_path" 110 111 if [ ${#vsan_hosts[@]} -ge 3 ] ; then 112 echo "Enabling vSAN for ${cluster_path}..." 113 govc cluster.change -vsan-enabled -vsan-autoclaim "$cluster_path" 114 fi 115 116 echo "Enabling HA for ${cluster_path}..." 117 govc cluster.change -ha-enabled "$cluster_path" 118 119 echo "Granting Admin permissions for user root..." 120 govc permissions.set -principal root -role Admin 121 122 echo "Done."