diff --git a/images/linux/scripts/base/reboot.sh b/images/linux/scripts/base/reboot.sh new file mode 100644 index 000000000..23969fe80 --- /dev/null +++ b/images/linux/scripts/base/reboot.sh @@ -0,0 +1,8 @@ +#!/bin/bash +################################################################################ +## File: reboot.sh +## Desc: Reboot VM +################################################################################ + +echo "Reboot VM" +sudo reboot \ No newline at end of file diff --git a/images/linux/scripts/helpers/document.sh b/images/linux/scripts/helpers/document.sh index 86217160f..e7311689c 100644 --- a/images/linux/scripts/helpers/document.sh +++ b/images/linux/scripts/helpers/document.sh @@ -9,7 +9,7 @@ function WriteItem { echo "METADATA_FILE environment variable must be set to output to Metadata Document!" return 1; else - echo -e "$1" >> "$METADATA_FILE" + echo -e "$1" | sudo tee -a "$METADATA_FILE" fi } diff --git a/images/linux/scripts/helpers/etc-environment.sh b/images/linux/scripts/helpers/etc-environment.sh new file mode 100644 index 000000000..bedc7119d --- /dev/null +++ b/images/linux/scripts/helpers/etc-environment.sh @@ -0,0 +1,65 @@ +#!/bin/bash +################################################################################ +## File: etc-environment.sh +## Desc: Helper functions for source and modify /etc/environment +################################################################################ + +# NB: sed expression use '%' as a delimiter in order to simplify handling +# values containg slashes (i.e. directory path) +# The values containing '%' will break the functions + +function getEtcEnvironmentVar { + var_name="$1" + # remove `var_name=` and possible quotes from the line + grep "^${var_name}=" /etc/environment |sed -E "s%^${var_name}=\"?([^\"]+)\"?.*$%\1%" +} + +function addEtcEnvironmentVar { + var_name="$1" + var_value="$2" + + echo "$var_name=\"$var_value\"" | sudo tee -a /etc/environment +} + +function replaceEtcEnvironmentVar { + var_name="$1" + var_value="$2" + + # modify /etc/environemnt in place by replacing a string that begins with var_name + sudo sed -ie "s%^${var_name}=.*$%${var_name}=\"${var_value}\"%" /etc/environment +} + +function setEtcEnvironmentVar { + var_name="$1" + var_value="$2" + + if grep "$var_name" /etc/environment > /dev/null; then + replaceEtcEnvironmentVar $var_name $var_value + else + addEtcEnvironmentVar $var_name $var_value + fi +} + +function addEtcEnvironmentPathElement { + element="$1" + etc_path=$(getEtcEnvironmentVar PATH) + setEtcEnvironmentVar PATH "${element}:${etc_path}" +} + +# Process /etc/environment as if it were shell script with `export VAR=...` expressions +# The PATH variable is handled specially in order to do not override the existing PATH +# variable. The value of PATH variable read from /etc/environment is added to the end +# of value of the exiting PATH variable exactly as it would happen with real PAM app read +# /etc/environment +# +# TODO: there might be the others variables to be processed in the same way as "PATH" variable +# ie MANPATH, INFOPATH, LD_*, etc. In the current implementation the values from /etc/evironments +# replace the values of the current environment +function reloadEtcEnvironment { + # add `export ` to every variable of /etc/environemnt except PATH and eval the result shell script + eval $(grep -v '^PATH=' /etc/environment | sed -e 's%^%export %') + # handle PATH specially + etc_path=$(getEtcEnvironmentVar PATH) + export PATH="$PATH:$etc_path" +} + diff --git a/images/linux/scripts/installers/homebrew-validate.sh b/images/linux/scripts/installers/homebrew-validate.sh new file mode 100644 index 000000000..a8eef2580 --- /dev/null +++ b/images/linux/scripts/installers/homebrew-validate.sh @@ -0,0 +1,14 @@ +#!/bin/bash +################################################################################ +## File: homebrew-validate.sh +## Desc: Validate the Homebrew can run after reboot without extra configuring +################################################################################ + +# Validate the installation +echo "Validate the Homebrew can run after reboot" + +if ! command -v brew; then + echo "brew cat not run after reboot" + exit 1 +fi + diff --git a/images/linux/scripts/installers/homebrew.sh b/images/linux/scripts/installers/homebrew.sh new file mode 100644 index 000000000..58c8a11de --- /dev/null +++ b/images/linux/scripts/installers/homebrew.sh @@ -0,0 +1,36 @@ +#!/bin/bash +################################################################################ +## File: homebrew.sh +## Desc: Installs the Homebrew on Linux +################################################################################ + +# Source the helpers +source $HELPER_SCRIPTS/document.sh +source $HELPER_SCRIPTS/etc-environment.sh + +# Install the Homebrew on Linux +/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" +eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv) + +# Make brew files and directories writable by any user +sudo chmod -R o+w $HOMEBREW_PREFIX + +# Update /etc/environemnt +## Put HOMEBREW_* variables +brew shellenv|grep 'export HOMEBREW'|sed -E 's/^export (.*);$/\1/' | sudo tee -a /etc/environment +# add brew executables locations to PATH +brew_path=$(brew shellenv|grep '^export PATH' |sed -E 's/^export PATH="([^$]+)\$.*/\1/') +addEtcEnvironmentPathElement "$brew_path" + +# Validate the installation ad hoc +echo "Validate the installation reloading /etc/environment" +reloadEtcEnvironment + +if ! command -v brew; then + echo "brew was not installed" + exit 1 +fi + +# Document the installed version +echo "Document the installed version" +DocumentInstalledItem "Homebrew on Linux ($(brew -v 2>&1))" diff --git a/images/linux/scripts/installers/updatepath.sh b/images/linux/scripts/installers/updatepath.sh index 7d63ceb1e..527ba5240 100644 --- a/images/linux/scripts/installers/updatepath.sh +++ b/images/linux/scripts/installers/updatepath.sh @@ -3,4 +3,5 @@ CARGO_HOME=/usr/share/rust/.cargo DOTNET_TOOLS_HOME=/home/runner/.dotnet/tools PHP_COMPOSER_HOME=/home/runner/.config/composer/vendor/bin -echo "PATH=${CARGO_HOME}/bin:${PATH}:${DOTNET_TOOLS_HOME}:${PHP_COMPOSER_HOME}" | tee -a /etc/environment \ No newline at end of file +BREW_PATH=/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin +echo "PATH=${BREW_PATH}:${CARGO_HOME}/bin:${PATH}:${DOTNET_TOOLS_HOME}:${PHP_COMPOSER_HOME}" | tee -a /etc/environment diff --git a/images/linux/ubuntu1604.json b/images/linux/ubuntu1604.json index 95ddc8cd3..4d4138d8c 100644 --- a/images/linux/ubuntu1604.json +++ b/images/linux/ubuntu1604.json @@ -132,6 +132,36 @@ ], "execute_command": "sudo sh -c '{{ .Vars }} {{ .Path }}'" }, + { + "type": "shell", + "scripts": [ + "{{template_dir}}/scripts/installers/homebrew.sh" + ], + "environment_vars": [ + "METADATA_FILE={{user `metadata_file`}}", + "HELPER_SCRIPTS={{user `helper_script_folder`}}", + "DEBIAN_FRONTEND=noninteractive" + ], + "execute_command": "/bin/sh -c '{{ .Vars }} {{ .Path }}'" + }, + { + "type": "shell", + "expect_disconnect": true, + "scripts": [ + "{{template_dir}}/scripts/base/reboot.sh" + ], + "execute_command": "/bin/sh -c '{{ .Vars }} {{ .Path }}'" + }, + { + "type": "shell", + "pause_before": "30s", + "timeout": "10m", + "start_retry_timeout": "10s", + "scripts": [ + "{{template_dir}}/scripts/installers/homebrew-validate.sh" + ], + "execute_command": "/bin/sh -c '{{ .Vars }} {{ .Path }}'" + }, { "type": "shell", "scripts": [ @@ -191,7 +221,6 @@ "{{template_dir}}/scripts/installers/dpkg-config.sh", "{{template_dir}}/scripts/installers/mongodb.sh", "{{template_dir}}/scripts/installers/rndgenerator.sh" - ], "environment_vars": [ "METADATA_FILE={{user `metadata_file`}}", diff --git a/images/linux/ubuntu1804.json b/images/linux/ubuntu1804.json index 0fffd8c13..444077348 100644 --- a/images/linux/ubuntu1804.json +++ b/images/linux/ubuntu1804.json @@ -135,6 +135,36 @@ ], "execute_command": "sudo sh -c '{{ .Vars }} {{ .Path }}'" }, + { + "type": "shell", + "scripts": [ + "{{template_dir}}/scripts/installers/homebrew.sh" + ], + "environment_vars": [ + "METADATA_FILE={{user `metadata_file`}}", + "HELPER_SCRIPTS={{user `helper_script_folder`}}", + "DEBIAN_FRONTEND=noninteractive" + ], + "execute_command": "/bin/sh -c '{{ .Vars }} {{ .Path }}'" + }, + { + "type": "shell", + "expect_disconnect": true, + "scripts": [ + "{{template_dir}}/scripts/base/reboot.sh" + ], + "execute_command": "/bin/sh -c '{{ .Vars }} {{ .Path }}'" + }, + { + "type": "shell", + "pause_before": "30s", + "timeout": "10m", + "start_retry_timeout": "10s", + "scripts": [ + "{{template_dir}}/scripts/installers/homebrew-validate.sh" + ], + "execute_command": "/bin/sh -c '{{ .Vars }} {{ .Path }}'" + }, { "type": "shell", "scripts": [