diff --git a/images/linux/scripts/helpers/etc-environment.sh b/images/linux/scripts/helpers/etc-environment.sh index bedc7119d..f0a68cf40 100644 --- a/images/linux/scripts/helpers/etc-environment.sh +++ b/images/linux/scripts/helpers/etc-environment.sh @@ -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" } diff --git a/images/linux/scripts/installers/1604/php.sh b/images/linux/scripts/installers/1604/php.sh index 147cedd6b..44f2ad3b7 100644 --- a/images/linux/scripts/installers/1604/php.sh +++ b/images/linux/scripts/installers/1604/php.sh @@ -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))" diff --git a/images/linux/scripts/installers/1804/php.sh b/images/linux/scripts/installers/1804/php.sh index 09d5cd96b..8ab10d785 100644 --- a/images/linux/scripts/installers/1804/php.sh +++ b/images/linux/scripts/installers/1804/php.sh @@ -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 diff --git a/images/linux/scripts/installers/dotnetcore-sdk.sh b/images/linux/scripts/installers/dotnetcore-sdk.sh index d0e138103..70d712e28 100644 --- a/images/linux/scripts/installers/dotnetcore-sdk.sh +++ b/images/linux/scripts/installers/dotnetcore-sdk.sh @@ -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 diff --git a/images/linux/scripts/installers/homebrew.sh b/images/linux/scripts/installers/homebrew.sh index 58c8a11de..17622f5cb 100644 --- a/images/linux/scripts/installers/homebrew.sh +++ b/images/linux/scripts/installers/homebrew.sh @@ -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" diff --git a/images/linux/scripts/installers/rust.sh b/images/linux/scripts/installers/rust.sh index 850868a4d..c3f0ae949 100644 --- a/images/linux/scripts/installers/rust.sh +++ b/images/linux/scripts/installers/rust.sh @@ -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 diff --git a/images/linux/scripts/installers/updatepath.sh b/images/linux/scripts/installers/updatepath.sh deleted file mode 100644 index 527ba5240..000000000 --- a/images/linux/scripts/installers/updatepath.sh +++ /dev/null @@ -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 diff --git a/images/linux/ubuntu1604.json b/images/linux/ubuntu1604.json index bf47f0c4c..c38547674 100644 --- a/images/linux/ubuntu1604.json +++ b/images/linux/ubuntu1604.json @@ -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" diff --git a/images/linux/ubuntu1804.json b/images/linux/ubuntu1804.json index d2129a208..1c4e704b5 100644 --- a/images/linux/ubuntu1804.json +++ b/images/linux/ubuntu1804.json @@ -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"