Update install.sh for RHEL on VMWARE

PRODX-8397

Change-Id: I91f621cf36169546c5c1f776d0540dae676ff1cf
diff --git a/.gitignore b/.gitignore
index 2d0bb33..01d11f3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,4 +4,5 @@
 /metadata.json
 /test.sh
 /userdata.yaml
-.DS_Store
\ No newline at end of file
+.DS_Store
+.idea/
diff --git a/install.sh b/install.sh
index f1c088f..e2fb4a6 100755
--- a/install.sh
+++ b/install.sh
@@ -1,9 +1,35 @@
 #!/bin/sh
 
-#
-# usage: install.sh
-#        curl -sSL https://raw.githubusercontent.com/akutz/cloud-init-vmware-guestinfo/master/install.sh | sh -
-#
+set -e
+
+# Prepare RHEL image for bootstrap on VSPHERE
+
+: "${DOWNLOAD_INSTALL:="true"}"
+: "${VCENTER_CONFIG_PATH:="/etc/virt-who.d/vcenter.conf"}"
+
+if [ -z "${VC_SERVER}" ] || [ -z "${VC_USER}" ] || [ -z "${VC_PASSWORD}" ] || [ -z "${VC_OWNER}" ]; then
+    echo "VC_SERVER or VC_USER or VC_PASSWORD or VC_OWNER is not provided"
+    exit 1
+fi
+
+yum -y install open-vm-tools cloud-init virt-who
+systemctl enable virt-who
+VC_PASSWORD_ENCRYPTED="$(virt-who-password -p "${VC_PASSWORD}")"
+
+mkdir -p "$(dirname "${VCENTER_CONFIG_PATH}")"
+cat <<EOF > "${VCENTER_CONFIG_PATH}"
+[vcenter]
+type=${VC_TYPE:-esx}
+server=${VC_SERVER}
+username=${VC_USER}
+encrypted_password=${VC_PASSWORD_ENCRYPTED}
+owner=${VC_OWNER}
+hypervisor_id=${VC_HYPERVISOR_ID:-hostname}
+EOF
+
+if [ -n "${VC_FILTER_HOSTS}" ]; then
+    echo "filter_hosts=${VC_FILTER_HOSTS}" >> "${VCENTER_CONFIG_PATH}"
+fi
 
 # The script to lookup the path to the cloud-init's datasource directory, "sources".
 PY_SCRIPT='import os; from cloudinit import sources; print(os.path.dirname(sources.__file__));'
@@ -16,19 +42,23 @@
 # If no "sources" directory was located then it's likely cloud-init is not installed.
 [ -z "${CLOUD_INIT_SOURCES}" ] && echo "cloud-init not found" 1>&2 && exit 1
 
-# The repository from which to fetch the cloud-init datasource and config files.
-REPO_SLUG="${REPO_SLUG:-https://raw.githubusercontent.com/akutz/cloud-init-vmware-guestinfo}"
+if [ "${DOWNLOAD_INSTALL}" = "true" ]; then
+    REPO_URL="${REPO_URL:-https://gerrit.mcp.mirantis.com/plugins/gitiles/kubernetes/vmware-guestinfo/+/refs/heads}"
 
-# The git reference to use. This can be a branch or tag name as well as a commit ID.
-GIT_REF="${GIT_REF:-master}"
+    # The git reference to use. This can be a branch or tag name as well as a commit ID.
+    GIT_REF="${GIT_REF:-mcp}"
 
-# Download the cloud init datasource into the cloud-init's "sources" directory.
-curl -sSL -o "${CLOUD_INIT_SOURCES}/DataSourceVMwareGuestInfo.py" \
-  "${REPO_SLUG}/${GIT_REF}/DataSourceVMwareGuestInfo.py"
+    # Download the cloud init datasource into the cloud-init's "sources" directory.
+    curl "${REPO_URL}/${GIT_REF}/DataSourceVMwareGuestInfo.py?format=TEXT" | base64 -d \
+      > "${CLOUD_INIT_SOURCES}/DataSourceVMwareGuestInfo.py"
 
-# Add the configuration file that tells cloud-init what datasource to use.
-mkdir -p /etc/cloud/cloud.cfg.d
-curl -sSL -o /etc/cloud/cloud.cfg.d/99-DataSourceVMwareGuestInfo.cfg \
-  "${REPO_SLUG}/${GIT_REF}/99-DataSourceVMwareGuestInfo.cfg"
-
-echo "So long, and thanks for all the fish."
+    # Add the configuration file that tells cloud-init what datasource to use.
+    mkdir -p /etc/cloud/cloud.cfg.d
+    curl "${REPO_URL}/${GIT_REF}/99-DataSourceVMwareGuestInfo.cfg?format=TEXT" | base64 -d \
+      > /etc/cloud/cloud.cfg.d/99-DataSourceVMwareGuestInfo.cfg
+else
+    BASEDIR=$(dirname "$0")
+    cp "${BASEDIR}/DataSourceVMwareGuestInfo.py" "${CLOUD_INIT_SOURCES}/DataSourceVMwareGuestInfo.py"
+    mkdir -p /etc/cloud/cloud.cfg.d
+    cp "${BASEDIR}/99-DataSourceVMwareGuestInfo.cfg" /etc/cloud/cloud.cfg.d/99-DataSourceVMwareGuestInfo.cfg
+fi