Migrate /etc/environment modifications to helpers (#660)

* migrate rust

* Migrate PHP composer

* Migrate dotnet core sdk

* rename `var_*` to `variable_*`

* rename *etcEnvironmentVar functions to *etcEnvironmentVariable

* add AppendEtcEnvironmentVariable  and PrependEtcEnvironmentVariable

* rename addEtcEnvironmentPathElement to prependEtcEnvironmentPath

* Remove updatepath.sh

Co-authored-by: Sergey Dolin <v-sedoli@micorosoft.com>
This commit is contained in:
Sergey Dolin
2020-04-14 13:23:44 +05:00
committed by GitHub
parent 44e904205f
commit bb9538cc9b
9 changed files with 55 additions and 36 deletions

View File

@@ -8,42 +8,57 @@
# 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 getEtcEnvironmentVariable {
variable_name="$1"
# remove `variable_name=` and possible quotes from the line
grep "^${variable_name}=" /etc/environment |sed -E "s%^${variable_name}=\"?([^\"]+)\"?.*$%\1%"
}
function addEtcEnvironmentVar {
var_name="$1"
var_value="$2"
function addEtcEnvironmentVariable {
variable_name="$1"
variable_value="$2"
echo "$var_name=\"$var_value\"" | sudo tee -a /etc/environment
echo "$variable_name=\"$variable_value\"" | sudo tee -a /etc/environment
}
function replaceEtcEnvironmentVar {
var_name="$1"
var_value="$2"
function replaceEtcEnvironmentVariable {
variable_name="$1"
variable_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
# modify /etc/environemnt in place by replacing a string that begins with variable_name
sudo sed -ie "s%^${variable_name}=.*$%${variable_name}=\"${variable_value}\"%" /etc/environment
}
function setEtcEnvironmentVar {
var_name="$1"
var_value="$2"
function setEtcEnvironmentVariable {
variable_name="$1"
variable_value="$2"
if grep "$var_name" /etc/environment > /dev/null; then
replaceEtcEnvironmentVar $var_name $var_value
if grep "$variable_name" /etc/environment > /dev/null; then
replaceEtcEnvironmentVariable $variable_name $variable_value
else
addEtcEnvironmentVar $var_name $var_value
addEtcEnvironmentVariable $variable_name $variable_value
fi
}
function addEtcEnvironmentPathElement {
function prependEtcEnvironmentVariable {
variable_name="$1"
element="$2"
# TODO: handle the case if the variable does not exist
existing_value=$(getEtcEnvironmentVariable "${variable_name}")
setEtcEnvironmentVariable "${variable_name}" "${element}:${existing_value}"
}
function appendEtcEnvironmentVariable {
variable_name="$1"
element="$2"
# TODO: handle the case if the variable does not exist
existing_value=$(getEtcEnvironmentVariable "${variable_name}")
setEtcEnvironmentVariable "${variable_name}" "${existing_value}:${element}"
}
function prependEtcEnvironmentPath {
element="$1"
etc_path=$(getEtcEnvironmentVar PATH)
setEtcEnvironmentVar PATH "${element}:${etc_path}"
prependEtcEnvironmentVariable PATH "${element}"
}
# Process /etc/environment as if it were shell script with `export VAR=...` expressions
@@ -59,7 +74,7 @@ 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)
etc_path=$(getEtcEnvironmentVariable PATH)
export PATH="$PATH:$etc_path"
}

View File

@@ -5,6 +5,7 @@
################################################################################
# Source the helpers for use with the script
source $HELPER_SCRIPTS/etc-environment.sh
source $HELPER_SCRIPTS/document.sh
LSB_RELEASE=$(lsb_release -rs)
@@ -276,6 +277,9 @@ php composer-setup.php
sudo mv composer.phar /usr/bin/composer
php -r "unlink('composer-setup.php');"
# Update /etc/environment
prependEtcEnvironmentPath /home/runner/.config/composer/vendor/bin
# Add composer bin folder to path
echo 'export PATH="$PATH:$HOME/.config/composer/vendor/bin"' >> /etc/skel/.bashrc
@@ -293,6 +297,7 @@ for cmd in php php5.6 php7.0 php7.1 php7.2 php7.3 php7.4 composer phpunit; do
fi
done
# Document what was added to the image
echo "Lastly, documenting what we added to the metadata file"
DocumentInstalledItem "PHP 5.6 ($(php5.6 --version | head -n 1))"

View File

@@ -5,6 +5,7 @@
################################################################################
# Source the helpers for use with the script
source $HELPER_SCRIPTS/etc-environment.sh
source $HELPER_SCRIPTS/document.sh
LSB_RELEASE=$(lsb_release -rs)
@@ -193,6 +194,9 @@ php composer-setup.php
sudo mv composer.phar /usr/bin/composer
php -r "unlink('composer-setup.php');"
# Update /etc/environment
prependEtcEnvironmentPath /home/runner/.config/composer/vendor/bin
# Add composer bin folder to path
echo 'export PATH="$PATH:$HOME/.config/composer/vendor/bin"' >> /etc/skel/.bashrc

View File

@@ -3,6 +3,7 @@
## File: dotnetcore-sdk.sh
## Desc: Installs .NET Core SDK
################################################################################
source $HELPER_SCRIPTS/etc-environment.sh
source $HELPER_SCRIPTS/apt.sh
source $HELPER_SCRIPTS/document.sh
@@ -95,5 +96,6 @@ done
# NuGetFallbackFolder at /usr/share/dotnet/sdk/NuGetFallbackFolder is warmed up by smoke test
# Additional FTE will just copy to ~/.dotnet/NuGet which provides no benefit on a fungible machine
echo "DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1" | tee -a /etc/environment
setEtcEnvironmentVariable DOTNET_SKIP_FIRST_TIME_EXPERIENCE 1
prependEtcEnvironmentPath /home/runner/.dotnet/tools
echo 'export PATH="$PATH:$HOME/.dotnet/tools"' | tee -a /etc/skel/.bashrc

View File

@@ -20,7 +20,7 @@ sudo chmod -R o+w $HOMEBREW_PREFIX
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"
prependEtcEnvironmentPath "$brew_path"
# Validate the installation ad hoc
echo "Validate the installation reloading /etc/environment"

View File

@@ -5,10 +5,9 @@
################################################################################
# Source the helpers for use with the script
source $HELPER_SCRIPTS/etc-environment.sh
source $HELPER_SCRIPTS/document.sh
set -e
export RUSTUP_HOME=/usr/share/rust/.rustup
export CARGO_HOME=/usr/share/rust/.cargo
@@ -33,6 +32,9 @@ for cmd in rustup rustc rustdoc cargo rustfmt cargo-clippy bindgen cbindgen; do
fi
done
# Update /etc/environemnt
prependEtcEnvironmentPath "${CARGO_HOME}/bin"
# Rust Symlinks are added to a default profile /etc/skel
pushd /etc/skel
ln -sf $RUSTUP_HOME .rustup

View File

@@ -1,7 +0,0 @@
#!/bin/bash
CARGO_HOME=/usr/share/rust/.cargo
DOTNET_TOOLS_HOME=/home/runner/.dotnet/tools
PHP_COMPOSER_HOME=/home/runner/.config/composer/vendor/bin
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

@@ -218,7 +218,6 @@
"{{template_dir}}/scripts/installers/packer.sh",
"{{template_dir}}/scripts/installers/vcpkg.sh",
"{{template_dir}}/scripts/installers/zeit-now.sh",
"{{template_dir}}/scripts/installers/updatepath.sh",
"{{template_dir}}/scripts/installers/dpkg-config.sh",
"{{template_dir}}/scripts/installers/mongodb.sh",
"{{template_dir}}/scripts/installers/rndgenerator.sh"

View File

@@ -222,7 +222,6 @@
"{{template_dir}}/scripts/installers/packer.sh",
"{{template_dir}}/scripts/installers/vcpkg.sh",
"{{template_dir}}/scripts/installers/zeit-now.sh",
"{{template_dir}}/scripts/installers/updatepath.sh",
"{{template_dir}}/scripts/installers/dpkg-config.sh",
"{{template_dir}}/scripts/installers/mongodb.sh",
"{{template_dir}}/scripts/installers/rndgenerator.sh"