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