blob: bf25191bc5dc16f90e26e8fde928116113b718b7 [file] [log] [blame]
Andrew Kutz4f66b8b2018-09-16 18:28:59 -05001#!/bin/sh
2
akutz0b519f72019-06-02 14:58:57 -05003# Exit as soon as there is an unexpected error.
4set -e
5
Andrew Kutz4f66b8b2018-09-16 18:28:59 -05006#
7# usage: install.sh
Ivan Mikushin6ba6bcb2019-05-23 19:43:55 -07008# curl -sSL https://raw.githubusercontent.com/vmware/cloud-init-vmware-guestinfo/master/install.sh | sh -
Andrew Kutz4f66b8b2018-09-16 18:28:59 -05009#
10
Andrew Kutz4f66b8b2018-09-16 18:28:59 -050011# The repository from which to fetch the cloud-init datasource and config files.
Ivan Mikushin6ba6bcb2019-05-23 19:43:55 -070012REPO_SLUG="${REPO_SLUG:-https://raw.githubusercontent.com/vmware/cloud-init-vmware-guestinfo}"
Andrew Kutz4f66b8b2018-09-16 18:28:59 -050013
14# The git reference to use. This can be a branch or tag name as well as a commit ID.
15GIT_REF="${GIT_REF:-master}"
16
akutz0b519f72019-06-02 14:58:57 -050017if ! command -v curl >/dev/null 2>&1; then
18 echo "curl is required" 1>&2
19 exit 1
20fi
21
22if ! command -v python >/dev/null 2>&1 && \
23 ! command -v python3 >/dev/null 2>&1; then
24 echo "python 2 or 3 is required" 1>&2
25 exit 1
26fi
27
28# PYTHON_VERSION may be 2 or 3 and indicates which version of Python
29# is used by cloud-init. This variable is not set until PY_MOD_CLOUD_INIT
30# is resolved.
31PYTHON_VERSION=
32get_py_mod_dir() {
33 _script='import os; import '"${1-}"'; print(os.path.dirname('"${1-}"'.__file__));'
34 case "${PYTHON_VERSION}" in
35 2)
36 python -c ''"${_script}"'' 2>/dev/null || echo ""
37 ;;
38 3)
39 python3 -c ''"${_script}"'' 2>/dev/null || echo ""
40 ;;
41 *)
42 { python3 -c ''"${_script}"'' || python -c ''"${_script}"'' || echo ""; } 2>/dev/null
43 ;;
44 esac
45}
46
47# PY_MOD_CLOUD_INIT is set to the the "cloudinit" directory in either
48# the Python2 or Python3 lib directory. This is also used to determine
49# which version of Python is repsonsible for running cloud-init.
50PY_MOD_CLOUD_INIT="$(get_py_mod_dir cloudinit)"
51if [ -z "${PY_MOD_CLOUD_INIT}" ]; then
52 echo "cloudinit is required" 1>&2
53 exit 1
54fi
55if echo "${PY_MOD_CLOUD_INIT}" | grep -q python2; then
56 PYTHON_VERSION=2
57else
58 PYTHON_VERSION=3
59fi
60echo "using python ${PYTHON_VERSION}"
61
62# The python modules deepmerge and netifaces are required. If they are
63# already installed, an assumption is made they are the correct versions.
64# Otherwise an attempt is made to install them with pip.
65if [ -z "$(get_py_mod_dir deepmerge)" ] || [ -z "$(get_py_mod_dir netifaces)" ]; then
66 echo "installing requirements"
67 if [ -z "$(get_py_mod_dir pip)" ]; then
68 echo "pip is required" 1>&2
69 exit 1
70 fi
71 _requirements="requirements.txt"
72 if [ ! -f "${_requirements}" ]; then
73 _requirements="$(mktemp)"
74 curl -sSL -o "${_requirements}" "${REPO_SLUG}/${GIT_REF}/requirements.txt"
75 fi
76 case "${PYTHON_VERSION}" in
77 2)
78 python -m pip install -r "${_requirements}"
79 ;;
80 3)
81 python3 -m pip install -r "${_requirements}"
82 ;;
83 esac
84fi
85
Andrew Kutz4f66b8b2018-09-16 18:28:59 -050086# Download the cloud init datasource into the cloud-init's "sources" directory.
akutz0b519f72019-06-02 14:58:57 -050087echo "installing datasource"
88curl -sSL -o "${PY_MOD_CLOUD_INIT}/sources/DataSourceVMwareGuestInfo.py" \
Andrew Kutz4f66b8b2018-09-16 18:28:59 -050089 "${REPO_SLUG}/${GIT_REF}/DataSourceVMwareGuestInfo.py"
90
akutz0b519f72019-06-02 14:58:57 -050091# Make sure that the datasource can execute without error on this host.
92echo "validating datasource"
93case "${PYTHON_VERSION}" in
942)
95 python "${PY_MOD_CLOUD_INIT}/sources/DataSourceVMwareGuestInfo.py" 1>/dev/null
96 ;;
973)
98 python3 "${PY_MOD_CLOUD_INIT}/sources/DataSourceVMwareGuestInfo.py" 1>/dev/null
99 ;;
100esac
101
Andrew Kutz4f66b8b2018-09-16 18:28:59 -0500102# Add the configuration file that tells cloud-init what datasource to use.
akutz0b519f72019-06-02 14:58:57 -0500103echo "installing config"
akutzf808b452018-10-10 14:16:47 -0500104mkdir -p /etc/cloud/cloud.cfg.d
105curl -sSL -o /etc/cloud/cloud.cfg.d/99-DataSourceVMwareGuestInfo.cfg \
Andrew Kutz4f66b8b2018-09-16 18:28:59 -0500106 "${REPO_SLUG}/${GIT_REF}/99-DataSourceVMwareGuestInfo.cfg"
107
akutzd1f67642019-05-30 12:33:50 -0500108# Download program used by ds-identify to determine whether or not the
akutz9efe9a22019-05-22 13:46:51 -0500109# VMwareGuestInfo datasource is useable.
akutz0b519f72019-06-02 14:58:57 -0500110echo "installing dscheck"
akutzd1f67642019-05-30 12:33:50 -0500111curl -sSL -o "/usr/bin/dscheck_VMwareGuestInfo" \
akutz9efe9a22019-05-22 13:46:51 -0500112 "${REPO_SLUG}/${GIT_REF}/dscheck_VMwareGuestInfo.sh"
akutzd1f67642019-05-30 12:33:50 -0500113chmod 0755 "/usr/bin/dscheck_VMwareGuestInfo"
akutz9efe9a22019-05-22 13:46:51 -0500114
Andrew Kutz4f66b8b2018-09-16 18:28:59 -0500115echo "So long, and thanks for all the fish."