blob: 6a51de155571ac3cf3120e9584e61fcd08e7b26c [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
akutz45f316d2020-09-22 13:04:05 -050062# The following modules are required:
63# * netifaces
64# If a module is already installed then it is assumed to be compatible.
65# Otherwise an attempt is made to install the module with pip.
66if [ -z "$(get_py_mod_dir netifaces)" ]; then
akutz0b519f72019-06-02 14:58:57 -050067 echo "installing requirements"
68 if [ -z "$(get_py_mod_dir pip)" ]; then
69 echo "pip is required" 1>&2
70 exit 1
71 fi
72 _requirements="requirements.txt"
73 if [ ! -f "${_requirements}" ]; then
74 _requirements="$(mktemp)"
75 curl -sSL -o "${_requirements}" "${REPO_SLUG}/${GIT_REF}/requirements.txt"
76 fi
77 case "${PYTHON_VERSION}" in
78 2)
79 python -m pip install -r "${_requirements}"
80 ;;
81 3)
82 python3 -m pip install -r "${_requirements}"
83 ;;
84 esac
85fi
86
Andrew Kutz4f66b8b2018-09-16 18:28:59 -050087# Download the cloud init datasource into the cloud-init's "sources" directory.
akutz0b519f72019-06-02 14:58:57 -050088echo "installing datasource"
89curl -sSL -o "${PY_MOD_CLOUD_INIT}/sources/DataSourceVMwareGuestInfo.py" \
Andrew Kutz4f66b8b2018-09-16 18:28:59 -050090 "${REPO_SLUG}/${GIT_REF}/DataSourceVMwareGuestInfo.py"
91
akutz0b519f72019-06-02 14:58:57 -050092# Make sure that the datasource can execute without error on this host.
93echo "validating datasource"
94case "${PYTHON_VERSION}" in
952)
96 python "${PY_MOD_CLOUD_INIT}/sources/DataSourceVMwareGuestInfo.py" 1>/dev/null
97 ;;
983)
99 python3 "${PY_MOD_CLOUD_INIT}/sources/DataSourceVMwareGuestInfo.py" 1>/dev/null
100 ;;
101esac
102
Andrew Kutz4f66b8b2018-09-16 18:28:59 -0500103# Add the configuration file that tells cloud-init what datasource to use.
akutz0b519f72019-06-02 14:58:57 -0500104echo "installing config"
akutzf808b452018-10-10 14:16:47 -0500105mkdir -p /etc/cloud/cloud.cfg.d
106curl -sSL -o /etc/cloud/cloud.cfg.d/99-DataSourceVMwareGuestInfo.cfg \
Andrew Kutz4f66b8b2018-09-16 18:28:59 -0500107 "${REPO_SLUG}/${GIT_REF}/99-DataSourceVMwareGuestInfo.cfg"
108
akutzd1f67642019-05-30 12:33:50 -0500109# Download program used by ds-identify to determine whether or not the
akutz9efe9a22019-05-22 13:46:51 -0500110# VMwareGuestInfo datasource is useable.
akutz0b519f72019-06-02 14:58:57 -0500111echo "installing dscheck"
akutzd1f67642019-05-30 12:33:50 -0500112curl -sSL -o "/usr/bin/dscheck_VMwareGuestInfo" \
akutz9efe9a22019-05-22 13:46:51 -0500113 "${REPO_SLUG}/${GIT_REF}/dscheck_VMwareGuestInfo.sh"
akutzd1f67642019-05-30 12:33:50 -0500114chmod 0755 "/usr/bin/dscheck_VMwareGuestInfo"
akutz9efe9a22019-05-22 13:46:51 -0500115
Andrew Kutz4f66b8b2018-09-16 18:28:59 -0500116echo "So long, and thanks for all the fish."