blob: 29b9d4637420105256fb8d26f3e4d4a209c181a0 [file] [log] [blame]
Andrew Kutz4f66b8b2018-09-16 18:28:59 -05001#!/bin/sh
2
Ivan Berezovskiy66279972020-10-26 16:48:53 +04003set -e
4
5# Prepare RHEL image for bootstrap on VSPHERE
6
7: "${DOWNLOAD_INSTALL:="true"}"
8: "${VCENTER_CONFIG_PATH:="/etc/virt-who.d/vcenter.conf"}"
9
Ivan Berezovskiy6b55f102020-11-13 14:40:23 +040010if [ -z "${VC_SERVER}" ] || [ -z "${VC_USER}" ] || [ -z "${VC_PASSWORD}" ]; then
11 echo "VC_SERVER or VC_USER or VC_PASSWORD is not provided"
Ivan Berezovskiy66279972020-10-26 16:48:53 +040012 exit 1
13fi
14
15yum -y install open-vm-tools cloud-init virt-who
16systemctl enable virt-who
17VC_PASSWORD_ENCRYPTED="$(virt-who-password -p "${VC_PASSWORD}")"
Ivan Berezovskiy6b55f102020-11-13 14:40:23 +040018VC_OWNER=$(subscription-manager identity | grep 'org ID' | awk -F ':' '{print $2}' | xargs)
Ivan Berezovskiy66279972020-10-26 16:48:53 +040019
20mkdir -p "$(dirname "${VCENTER_CONFIG_PATH}")"
21cat <<EOF > "${VCENTER_CONFIG_PATH}"
22[vcenter]
23type=${VC_TYPE:-esx}
24server=${VC_SERVER}
25username=${VC_USER}
26encrypted_password=${VC_PASSWORD_ENCRYPTED}
27owner=${VC_OWNER}
28hypervisor_id=${VC_HYPERVISOR_ID:-hostname}
29EOF
30
31if [ -n "${VC_FILTER_HOSTS}" ]; then
32 echo "filter_hosts=${VC_FILTER_HOSTS}" >> "${VCENTER_CONFIG_PATH}"
33fi
Andrew Kutz4f66b8b2018-09-16 18:28:59 -050034
35# The script to lookup the path to the cloud-init's datasource directory, "sources".
36PY_SCRIPT='import os; from cloudinit import sources; print(os.path.dirname(sources.__file__));'
37
38# Get the path to the cloud-init installation's datasource directory.
39CLOUD_INIT_SOURCES=$(python -c ''"${PY_SCRIPT}"'' 2>/dev/null || \
40 python3 -c ''"${PY_SCRIPT}"'' 2>/dev/null) ||
41 { exit_code="${?}"; echo "failed to find python runtime" 1>&2; exit "${exit_code}"; }
42
43# If no "sources" directory was located then it's likely cloud-init is not installed.
44[ -z "${CLOUD_INIT_SOURCES}" ] && echo "cloud-init not found" 1>&2 && exit 1
45
Ivan Berezovskiy66279972020-10-26 16:48:53 +040046if [ "${DOWNLOAD_INSTALL}" = "true" ]; then
47 REPO_URL="${REPO_URL:-https://gerrit.mcp.mirantis.com/plugins/gitiles/kubernetes/vmware-guestinfo/+/refs/heads}"
Andrew Kutz4f66b8b2018-09-16 18:28:59 -050048
Ivan Berezovskiy66279972020-10-26 16:48:53 +040049 # The git reference to use. This can be a branch or tag name as well as a commit ID.
50 GIT_REF="${GIT_REF:-mcp}"
Andrew Kutz4f66b8b2018-09-16 18:28:59 -050051
Ivan Berezovskiy66279972020-10-26 16:48:53 +040052 # Download the cloud init datasource into the cloud-init's "sources" directory.
53 curl "${REPO_URL}/${GIT_REF}/DataSourceVMwareGuestInfo.py?format=TEXT" | base64 -d \
54 > "${CLOUD_INIT_SOURCES}/DataSourceVMwareGuestInfo.py"
Andrew Kutz4f66b8b2018-09-16 18:28:59 -050055
Ivan Berezovskiy66279972020-10-26 16:48:53 +040056 # Add the configuration file that tells cloud-init what datasource to use.
57 mkdir -p /etc/cloud/cloud.cfg.d
58 curl "${REPO_URL}/${GIT_REF}/99-DataSourceVMwareGuestInfo.cfg?format=TEXT" | base64 -d \
59 > /etc/cloud/cloud.cfg.d/99-DataSourceVMwareGuestInfo.cfg
60else
61 BASEDIR=$(dirname "$0")
62 cp "${BASEDIR}/DataSourceVMwareGuestInfo.py" "${CLOUD_INIT_SOURCES}/DataSourceVMwareGuestInfo.py"
63 mkdir -p /etc/cloud/cloud.cfg.d
64 cp "${BASEDIR}/99-DataSourceVMwareGuestInfo.cfg" /etc/cloud/cloud.cfg.d/99-DataSourceVMwareGuestInfo.cfg
65fi