add Homebrew on Linux (#589)

* Add homebrew installation and validation scripts

* Update `/etc/environment` in `updatepath.sh` script

Keep this code for sake of compatibility with the existing installation logic. It will be changed in its own PR

* Remove env. variables from homebrew validation task

* Move reboot to the dedicated script and task

Co-authored-by: Sergey Dolin <v-sedoli@micorosoft.com>
This commit is contained in:
Sergey Dolin
2020-04-10 14:20:16 +05:00
committed by GitHub
parent 14491c1a23
commit e1fb068300
8 changed files with 186 additions and 3 deletions

View File

@@ -0,0 +1,8 @@
#!/bin/bash
################################################################################
## File: reboot.sh
## Desc: Reboot VM
################################################################################
echo "Reboot VM"
sudo reboot

View File

@@ -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
}

View File

@@ -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"
}

View File

@@ -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

View File

@@ -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))"

View File

@@ -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
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

View File

@@ -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`}}",

View File

@@ -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": [