Andrew Kutz | 4f66b8b | 2018-09-16 18:28:59 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame^] | 3 | # Exit as soon as there is an unexpected error. |
| 4 | set -e |
| 5 | |
Andrew Kutz | 4f66b8b | 2018-09-16 18:28:59 -0500 | [diff] [blame] | 6 | # |
| 7 | # usage: install.sh |
Ivan Mikushin | 6ba6bcb | 2019-05-23 19:43:55 -0700 | [diff] [blame] | 8 | # curl -sSL https://raw.githubusercontent.com/vmware/cloud-init-vmware-guestinfo/master/install.sh | sh - |
Andrew Kutz | 4f66b8b | 2018-09-16 18:28:59 -0500 | [diff] [blame] | 9 | # |
| 10 | |
Andrew Kutz | 4f66b8b | 2018-09-16 18:28:59 -0500 | [diff] [blame] | 11 | # The repository from which to fetch the cloud-init datasource and config files. |
Ivan Mikushin | 6ba6bcb | 2019-05-23 19:43:55 -0700 | [diff] [blame] | 12 | REPO_SLUG="${REPO_SLUG:-https://raw.githubusercontent.com/vmware/cloud-init-vmware-guestinfo}" |
Andrew Kutz | 4f66b8b | 2018-09-16 18:28:59 -0500 | [diff] [blame] | 13 | |
| 14 | # The git reference to use. This can be a branch or tag name as well as a commit ID. |
| 15 | GIT_REF="${GIT_REF:-master}" |
| 16 | |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame^] | 17 | if ! command -v curl >/dev/null 2>&1; then |
| 18 | echo "curl is required" 1>&2 |
| 19 | exit 1 |
| 20 | fi |
| 21 | |
| 22 | if ! 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 |
| 26 | fi |
| 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. |
| 31 | PYTHON_VERSION= |
| 32 | get_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. |
| 50 | PY_MOD_CLOUD_INIT="$(get_py_mod_dir cloudinit)" |
| 51 | if [ -z "${PY_MOD_CLOUD_INIT}" ]; then |
| 52 | echo "cloudinit is required" 1>&2 |
| 53 | exit 1 |
| 54 | fi |
| 55 | if echo "${PY_MOD_CLOUD_INIT}" | grep -q python2; then |
| 56 | PYTHON_VERSION=2 |
| 57 | else |
| 58 | PYTHON_VERSION=3 |
| 59 | fi |
| 60 | echo "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. |
| 65 | if [ -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 |
| 84 | fi |
| 85 | |
Andrew Kutz | 4f66b8b | 2018-09-16 18:28:59 -0500 | [diff] [blame] | 86 | # Download the cloud init datasource into the cloud-init's "sources" directory. |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame^] | 87 | echo "installing datasource" |
| 88 | curl -sSL -o "${PY_MOD_CLOUD_INIT}/sources/DataSourceVMwareGuestInfo.py" \ |
Andrew Kutz | 4f66b8b | 2018-09-16 18:28:59 -0500 | [diff] [blame] | 89 | "${REPO_SLUG}/${GIT_REF}/DataSourceVMwareGuestInfo.py" |
| 90 | |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame^] | 91 | # Make sure that the datasource can execute without error on this host. |
| 92 | echo "validating datasource" |
| 93 | case "${PYTHON_VERSION}" in |
| 94 | 2) |
| 95 | python "${PY_MOD_CLOUD_INIT}/sources/DataSourceVMwareGuestInfo.py" 1>/dev/null |
| 96 | ;; |
| 97 | 3) |
| 98 | python3 "${PY_MOD_CLOUD_INIT}/sources/DataSourceVMwareGuestInfo.py" 1>/dev/null |
| 99 | ;; |
| 100 | esac |
| 101 | |
Andrew Kutz | 4f66b8b | 2018-09-16 18:28:59 -0500 | [diff] [blame] | 102 | # Add the configuration file that tells cloud-init what datasource to use. |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame^] | 103 | echo "installing config" |
akutz | f808b45 | 2018-10-10 14:16:47 -0500 | [diff] [blame] | 104 | mkdir -p /etc/cloud/cloud.cfg.d |
| 105 | curl -sSL -o /etc/cloud/cloud.cfg.d/99-DataSourceVMwareGuestInfo.cfg \ |
Andrew Kutz | 4f66b8b | 2018-09-16 18:28:59 -0500 | [diff] [blame] | 106 | "${REPO_SLUG}/${GIT_REF}/99-DataSourceVMwareGuestInfo.cfg" |
| 107 | |
akutz | d1f6764 | 2019-05-30 12:33:50 -0500 | [diff] [blame] | 108 | # Download program used by ds-identify to determine whether or not the |
akutz | 9efe9a2 | 2019-05-22 13:46:51 -0500 | [diff] [blame] | 109 | # VMwareGuestInfo datasource is useable. |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame^] | 110 | echo "installing dscheck" |
akutz | d1f6764 | 2019-05-30 12:33:50 -0500 | [diff] [blame] | 111 | curl -sSL -o "/usr/bin/dscheck_VMwareGuestInfo" \ |
akutz | 9efe9a2 | 2019-05-22 13:46:51 -0500 | [diff] [blame] | 112 | "${REPO_SLUG}/${GIT_REF}/dscheck_VMwareGuestInfo.sh" |
akutz | d1f6764 | 2019-05-30 12:33:50 -0500 | [diff] [blame] | 113 | chmod 0755 "/usr/bin/dscheck_VMwareGuestInfo" |
akutz | 9efe9a2 | 2019-05-22 13:46:51 -0500 | [diff] [blame] | 114 | |
Andrew Kutz | 4f66b8b | 2018-09-16 18:28:59 -0500 | [diff] [blame] | 115 | echo "So long, and thanks for all the fish." |