github.com/vmware/govmomi@v0.51.0/scripts/vcsa/create-vcsa-vm.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  # Create a VCSA VM
     8  
     9  usage() {
    10    echo "Usage: $0 [-n VM_NAME] [-i VCSA_OVA] [-a IP] ESX_URL" 1>&2
    11    exit 1
    12  }
    13  
    14  export GOVC_INSECURE=1
    15  
    16  name=vcsa
    17  
    18  # 6.7 U3
    19  ova=VMware-vCenter-Server-Appliance-6.7.0.40000-14367737_OVF10.ova
    20  
    21  while getopts a:i:n: flag
    22  do
    23    case $flag in
    24      a)
    25        ip=$OPTARG
    26        ;;
    27      i)
    28        ova=$OPTARG
    29        ;;
    30      n)
    31        name=$OPTARG
    32        ;;
    33      *)
    34        usage
    35        ;;
    36    esac
    37  done
    38  
    39  if [ -d "$ova" ] ; then
    40    ova=$(ls "$ova"/*.ovf)
    41  fi
    42  
    43  shift $((OPTIND-1))
    44  
    45  if [ $# -ne 1 ] ; then
    46    usage
    47  fi
    48  
    49  export GOVC_URL=$1
    50  
    51  network=${GOVC_NETWORK:-$(basename "$(govc ls network)")}
    52  product=$(govc about -json | jq -r .About.ProductLineId)
    53  # Use the same password as GOVC_URL
    54  password=$(govc env GOVC_PASSWORD)
    55  
    56  if [ -z "$password" ] ; then
    57    echo "password not set"
    58    exit 1
    59  fi
    60  
    61  opts=(
    62    cis.vmdir.password=$password
    63    cis.appliance.root.passwd=$password
    64    cis.appliance.root.shell=/bin/bash
    65    cis.deployment.node.type=embedded
    66    cis.vmdir.domain-name=vsphere.local
    67    cis.vmdir.site-name=VCSA
    68    cis.appliance.net.addr.family=ipv4
    69    cis.appliance.ssh.enabled=True
    70    cis.ceip_enabled=False
    71    cis.deployment.autoconfig=True
    72  )
    73  
    74  if [ -z "$ip" ] ; then
    75    mode=dhcp
    76    ntp=0.pool.ntp.org
    77  else
    78    mode=static
    79  
    80    # Derive net config from the ESX server
    81    config=$(govc host.info -k -json | jq -r .HostSystems[].Config)
    82    gateway=$(jq -r .Network.IpRouteConfig.DefaultGateway <<<"$config")
    83    dns=$(jq -r .Network.DnsConfig.Address[0] <<<"$config")
    84    ntp=$(jq -r .DateTimeInfo.NtpConfig.Server[0] <<<"$config")
    85    route=$(jq -r ".Network.RouteTableInfo.IpRoute[] | select(.DeviceName == \"vmk0\") | select(.Gateway == \"0.0.0.0\")" <<<"$config")
    86    prefix=$(jq -r .PrefixLength <<<"$route")
    87  
    88    opts+=(cis.appliance.net.addr=$ip
    89           cis.appliance.net.prefix=$prefix
    90           cis.appliance.net.dns.servers=$dns
    91           cis.appliance.net.gateway=$gateway)
    92  fi
    93  
    94  opts+=(
    95    cis.appliance.ntp.servers="$ntp"
    96    cis.appliance.net.mode=$mode
    97  )
    98  
    99  if [ "$product" = "ws" ] ; then
   100    # workstation does not support NFC
   101    dir=$(govc datastore.info -json | jq -r .Datastores[0].Info.Url)
   102  
   103    ovftool --name="$name" --acceptAllEulas "$ova" "$dir"
   104    vmx="$name/${name}.vmx"
   105    printf "guestinfo.%s\n" "${opts[@]}" >> "$dir/$vmx"
   106    govc vm.register "$vmx"
   107    govc vm.network.change -vm "$name" -net NAT ethernet-0
   108  else
   109    props=$(printf -- "guestinfo.%s\n" "${opts[@]}" | \
   110               jq --slurp -R 'split("\n") | map(select(. != "")) | map(split("=")) | map({"Key": .[0], "Value": .[1]})')
   111  
   112    cat <<EOF | govc import.${ova##*.} -options - "$ova"
   113  {
   114    "Name": "$name",
   115    "Deployment": "tiny",
   116    "DiskProvisioning": "thin",
   117    "IPProtocol": "IPv4",
   118    "Annotation": "VMware vCenter Server Appliance",
   119    "PowerOn": false,
   120    "WaitForIP": false,
   121    "InjectOvfEnv": true,
   122    "NetworkMapping": [
   123      {
   124        "Name": "Network 1",
   125        "Network": "${network}"
   126      }
   127    ],
   128    "PropertyMapping": $props
   129  }
   130  EOF
   131  fi
   132  
   133  govc vm.change -vm "$name" -g vmwarePhoton64Guest
   134  govc vm.power -on "$name"
   135  govc vm.ip "$name"