Andrew Kutz | 4f66b8b | 2018-09-16 18:28:59 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Ivan Berezovskiy | 6627997 | 2020-10-26 16:48:53 +0400 | [diff] [blame] | 3 | set -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 Berezovskiy | 6b55f10 | 2020-11-13 14:40:23 +0400 | [diff] [blame^] | 10 | if [ -z "${VC_SERVER}" ] || [ -z "${VC_USER}" ] || [ -z "${VC_PASSWORD}" ]; then |
| 11 | echo "VC_SERVER or VC_USER or VC_PASSWORD is not provided" |
Ivan Berezovskiy | 6627997 | 2020-10-26 16:48:53 +0400 | [diff] [blame] | 12 | exit 1 |
| 13 | fi |
| 14 | |
| 15 | yum -y install open-vm-tools cloud-init virt-who |
| 16 | systemctl enable virt-who |
| 17 | VC_PASSWORD_ENCRYPTED="$(virt-who-password -p "${VC_PASSWORD}")" |
Ivan Berezovskiy | 6b55f10 | 2020-11-13 14:40:23 +0400 | [diff] [blame^] | 18 | VC_OWNER=$(subscription-manager identity | grep 'org ID' | awk -F ':' '{print $2}' | xargs) |
Ivan Berezovskiy | 6627997 | 2020-10-26 16:48:53 +0400 | [diff] [blame] | 19 | |
| 20 | mkdir -p "$(dirname "${VCENTER_CONFIG_PATH}")" |
| 21 | cat <<EOF > "${VCENTER_CONFIG_PATH}" |
| 22 | [vcenter] |
| 23 | type=${VC_TYPE:-esx} |
| 24 | server=${VC_SERVER} |
| 25 | username=${VC_USER} |
| 26 | encrypted_password=${VC_PASSWORD_ENCRYPTED} |
| 27 | owner=${VC_OWNER} |
| 28 | hypervisor_id=${VC_HYPERVISOR_ID:-hostname} |
| 29 | EOF |
| 30 | |
| 31 | if [ -n "${VC_FILTER_HOSTS}" ]; then |
| 32 | echo "filter_hosts=${VC_FILTER_HOSTS}" >> "${VCENTER_CONFIG_PATH}" |
| 33 | fi |
Andrew Kutz | 4f66b8b | 2018-09-16 18:28:59 -0500 | [diff] [blame] | 34 | |
| 35 | # The script to lookup the path to the cloud-init's datasource directory, "sources". |
| 36 | PY_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. |
| 39 | CLOUD_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 Berezovskiy | 6627997 | 2020-10-26 16:48:53 +0400 | [diff] [blame] | 46 | if [ "${DOWNLOAD_INSTALL}" = "true" ]; then |
| 47 | REPO_URL="${REPO_URL:-https://gerrit.mcp.mirantis.com/plugins/gitiles/kubernetes/vmware-guestinfo/+/refs/heads}" |
Andrew Kutz | 4f66b8b | 2018-09-16 18:28:59 -0500 | [diff] [blame] | 48 | |
Ivan Berezovskiy | 6627997 | 2020-10-26 16:48:53 +0400 | [diff] [blame] | 49 | # 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 Kutz | 4f66b8b | 2018-09-16 18:28:59 -0500 | [diff] [blame] | 51 | |
Ivan Berezovskiy | 6627997 | 2020-10-26 16:48:53 +0400 | [diff] [blame] | 52 | # 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 Kutz | 4f66b8b | 2018-09-16 18:28:59 -0500 | [diff] [blame] | 55 | |
Ivan Berezovskiy | 6627997 | 2020-10-26 16:48:53 +0400 | [diff] [blame] | 56 | # 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 |
| 60 | else |
| 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 |
| 65 | fi |