From a89f8396758c222930f297d88b62676fa6e0aacf Mon Sep 17 00:00:00 2001 From: Aleksandr Chebotov <47745270+al-cheb@users.noreply.github.com> Date: Mon, 29 Jun 2020 14:10:01 +0300 Subject: [PATCH] [Ubuntu] Merge several toolset provisioners into single Configure-Toolset script (#1121) * Single toolset * rework conditional logic --- .../scripts/installers/Configure-Toolset.ps1 | 95 +++++++++++++++++++ .../scripts/installers/Install-Toolset.ps1 | 7 +- images/linux/scripts/installers/boost.sh | 19 ---- images/linux/scripts/installers/go.sh | 26 ----- .../linux/{ => toolsets}/toolcache-1604.json | 0 .../linux/{ => toolsets}/toolcache-1804.json | 0 .../linux/{ => toolsets}/toolcache-2004.json | 0 images/linux/{ => toolsets}/toolset-1604.json | 0 images/linux/{ => toolsets}/toolset-1804.json | 0 images/linux/{ => toolsets}/toolset-2004.json | 0 images/linux/ubuntu1604.json | 27 +----- images/linux/ubuntu1804.json | 27 +----- images/linux/ubuntu2004.json | 15 +-- 13 files changed, 107 insertions(+), 109 deletions(-) create mode 100644 images/linux/scripts/installers/Configure-Toolset.ps1 delete mode 100644 images/linux/scripts/installers/boost.sh delete mode 100644 images/linux/scripts/installers/go.sh rename images/linux/{ => toolsets}/toolcache-1604.json (100%) rename images/linux/{ => toolsets}/toolcache-1804.json (100%) rename images/linux/{ => toolsets}/toolcache-2004.json (100%) rename images/linux/{ => toolsets}/toolset-1604.json (100%) rename images/linux/{ => toolsets}/toolset-1804.json (100%) rename images/linux/{ => toolsets}/toolset-2004.json (100%) diff --git a/images/linux/scripts/installers/Configure-Toolset.ps1 b/images/linux/scripts/installers/Configure-Toolset.ps1 new file mode 100644 index 000000000..4a145a5d5 --- /dev/null +++ b/images/linux/scripts/installers/Configure-Toolset.ps1 @@ -0,0 +1,95 @@ +################################################################################ +## File: Configure-Toolset.ps1 +## Team: CI-Build +## Desc: Configure toolset +################################################################################ + +function Get-ToolsetToolFullPath +{ + param + ( + [Parameter(Mandatory)] [string] $ToolName, + [Parameter(Mandatory)] [string] $ToolVersion, + [Parameter(Mandatory)] [string] $ToolArchitecture + ) + + $toolPath = Join-Path -Path $env:AGENT_TOOLSDIRECTORY -ChildPath $toolName + $toolPathVersion = Join-Path -Path $toolPath -ChildPath $toolVersion + $foundVersion = Get-Item $toolPathVersion | Sort-Object -Property {[version]$_.name} -Descending | Select-Object -First 1 + $installationDir = Join-Path -Path $foundVersion -ChildPath $toolArchitecture + return $installationDir +} + +function Add-EnvironmentVariable +{ + param + ( + [Parameter(Mandatory)] [string] $Name, + [Parameter(Mandatory)] [string] $Value, + [string] $FilePath = "/etc/environment" + ) + + $envVar = "{0}={1}" -f $name, $value + Tee-Object -InputObject $envVar -FilePath $filePath -Append +} + +$ErrorActionPreference = "Stop" + +Write-Host "Configure toolset tools environment..." +$toolsEnvironment = @{ + boost = @{ + variableTemplate = "BOOST_ROOT_{0}_{1}_{2}" + } + go = @{ + command = "ln -s {0}/bin/* /usr/bin/" + defaultVariable = "GOROOT" + variableTemplate = "GOROOT_{0}_{1}_X64" + } +} + +$toolset = Get-Content -Path "$env:INSTALLER_SCRIPT_FOLDER/toolset.json" -Raw | ConvertFrom-Json + +foreach ($tool in $toolset.toolcache) +{ + $toolName = $tool.name + $toolArch = $tool.arch + $toolEnvironment = $toolsEnvironment[$toolName] + + if (-not $toolEnvironment) + { + continue + } + + foreach ($toolVersion in $tool.versions) + { + Write-Host "Set $toolName $toolVersion environment variable..." + $toolPath = Get-ToolsetToolFullPath -ToolName $toolName -ToolVersion $toolVersion -ToolArchitecture $toolArch + $envName = $toolEnvironment.variableTemplate -f $toolVersion.split(".") + + # Add environment variable name=value + Add-EnvironmentVariable -Name $envName -Value $toolPath + } + + # Invoke command and add env variable for the default tool version + $toolDefVersion = $tool.default + if (-not $toolDefVersion) + { + continue + } + + $envDefName = $toolEnvironment.defaultVariable + $toolPath = Get-ToolsetToolFullPath -ToolName $toolName -ToolVersion $toolDefVersion -ToolArchitecture $toolArch + + if ($envDefName) + { + Write-Host "Set default $envDefName for $toolName $toolDefVersion environment variable..." + Add-EnvironmentVariable -Name $envDefName -Value $toolPath + } + + if ($toolEnvironment.command) + { + $command = $toolEnvironment.command -f $toolPath + Write-Host "Invoke $command command for default $toolName $toolDefVersion..." + Invoke-Expression -Command $command + } +} diff --git a/images/linux/scripts/installers/Install-Toolset.ps1 b/images/linux/scripts/installers/Install-Toolset.ps1 index ce5bfb0d9..e95645e53 100644 --- a/images/linux/scripts/installers/Install-Toolset.ps1 +++ b/images/linux/scripts/installers/Install-Toolset.ps1 @@ -30,7 +30,7 @@ $ErrorActionPreference = "Stop" $toolsetJson = Get-Content -Path "$env:INSTALLER_SCRIPT_FOLDER/toolset.json" -Raw $toolsToInstall = @("Python", "Node", "Boost", "Go") -$tools = ConvertFrom-Json -InputObject $toolsetJson | Select-Object -ExpandProperty toolcache | Where {$ToolsToInstall -contains $_.Name} +$tools = ConvertFrom-Json -InputObject $toolsetJson | Select-Object -ExpandProperty toolcache | Where-Object {$ToolsToInstall -contains $_.Name} foreach ($tool in $tools) { # Get versions manifest for current tool @@ -44,10 +44,9 @@ foreach ($tool in $tools) { | Select-Object -First 1 Write-Host "Installing $($tool.name) $toolVersion $($tool.arch)..." - if ($asset -ne $null) { + if ($null -ne $asset) { Install-Asset -ReleaseAsset $asset - } - else { + } else { Write-Host "Asset was not found in versions manifest" exit 1 } diff --git a/images/linux/scripts/installers/boost.sh b/images/linux/scripts/installers/boost.sh deleted file mode 100644 index 8f05054dd..000000000 --- a/images/linux/scripts/installers/boost.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/bash -################################################################################ -## File: boost.sh -## Desc: Installs Boost C++ Libraries -################################################################################ - -TOOLSET_PATH="$INSTALLER_SCRIPT_FOLDER/toolset.json" -BOOST_LIB="$AGENT_TOOLSDIRECTORY/boost" -BOOST_VERSIONS=$(cat $TOOLSET_PATH | jq -r '.toolcache[] | select(.name | contains("boost")) | .versions[]') -BOOST_ARCH=$(cat $TOOLSET_PATH | jq -r '.toolcache[] | select(.name | contains("boost")) | .arch') - -# Install Boost -for BOOST_VERSION in ${BOOST_VERSIONS} -do - BOOST_SYMLINK_VER=$(echo "${BOOST_VERSION//[.]/_}") - BOOST_ROOT_VERSION="BOOST_ROOT_$BOOST_SYMLINK_VER" - - echo "$BOOST_ROOT_VERSION=$BOOST_LIB/$BOOST_VERSION/$BOOST_ARCH" | tee -a /etc/environment -done diff --git a/images/linux/scripts/installers/go.sh b/images/linux/scripts/installers/go.sh deleted file mode 100644 index d6f4a9c26..000000000 --- a/images/linux/scripts/installers/go.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash -################################################################################ -## File: go.sh -## Desc: Installs go, configures GOROOT, and adds go to the path -################################################################################ - -# Fail out if any setups fail -set -e - -toolsetJson="$INSTALLER_SCRIPT_FOLDER/toolset.json" -toolsetVersions=(`ls $AGENT_TOOLSDIRECTORY/go`) -defaultVersion=$(jq -r '.toolcache[] | select(.name | contains("go")) | .default' $toolsetJson) - -for toolsetVersion in ${toolsetVersions[@]} -do - major="$(cut -d'.' -f1 <<< "$toolsetVersion")" - minor="$(cut -d'.' -f2 <<< "$toolsetVersion")" - goFolder="$AGENT_TOOLSDIRECTORY/go/$toolsetVersion/x64" - - echo "GOROOT_${major}_${minor}_X64=$goFolder" | tee -a /etc/environment - - if [[ "$toolsetVersion" =~ $defaultVersion ]]; then - ln -s $goFolder/bin/* /usr/bin/ - echo "GOROOT=$goFolder" | tee -a /etc/environment - fi -done \ No newline at end of file diff --git a/images/linux/toolcache-1604.json b/images/linux/toolsets/toolcache-1604.json similarity index 100% rename from images/linux/toolcache-1604.json rename to images/linux/toolsets/toolcache-1604.json diff --git a/images/linux/toolcache-1804.json b/images/linux/toolsets/toolcache-1804.json similarity index 100% rename from images/linux/toolcache-1804.json rename to images/linux/toolsets/toolcache-1804.json diff --git a/images/linux/toolcache-2004.json b/images/linux/toolsets/toolcache-2004.json similarity index 100% rename from images/linux/toolcache-2004.json rename to images/linux/toolsets/toolcache-2004.json diff --git a/images/linux/toolset-1604.json b/images/linux/toolsets/toolset-1604.json similarity index 100% rename from images/linux/toolset-1604.json rename to images/linux/toolsets/toolset-1604.json diff --git a/images/linux/toolset-1804.json b/images/linux/toolsets/toolset-1804.json similarity index 100% rename from images/linux/toolset-1804.json rename to images/linux/toolsets/toolset-1804.json diff --git a/images/linux/toolset-2004.json b/images/linux/toolsets/toolset-2004.json similarity index 100% rename from images/linux/toolset-2004.json rename to images/linux/toolsets/toolset-2004.json diff --git a/images/linux/ubuntu1604.json b/images/linux/ubuntu1604.json index bb58b7758..661847534 100644 --- a/images/linux/ubuntu1604.json +++ b/images/linux/ubuntu1604.json @@ -247,12 +247,12 @@ }, { "type": "file", - "source": "{{template_dir}}/toolcache-1604.json", + "source": "{{template_dir}}/toolsets/toolcache-1604.json", "destination": "{{user `installer_script_folder`}}/toolcache.json" }, { "type": "file", - "source": "{{template_dir}}/toolset-1604.json", + "source": "{{template_dir}}/toolsets/toolset-1604.json", "destination": "{{user `installer_script_folder`}}/toolset.json" }, { @@ -278,6 +278,7 @@ "type": "shell", "scripts":[ "{{template_dir}}/scripts/installers/Install-Toolset.ps1", + "{{template_dir}}/scripts/installers/Configure-Toolset.ps1", "{{template_dir}}/scripts/installers/Validate-Toolset.ps1" ], "environment_vars": [ @@ -287,28 +288,6 @@ ], "execute_command": "sudo sh -c '{{ .Vars }} pwsh -f {{ .Path }}'" }, - { - "type": "shell", - "scripts": [ - "{{template_dir}}/scripts/installers/go.sh" - ], - "environment_vars": [ - "INSTALLER_SCRIPT_FOLDER={{user `installer_script_folder`}}" - ], - "execute_command": "sudo sh -c '{{ .Vars }} {{ .Path }}'" - }, - { - "type": "shell", - "scripts":[ - "{{template_dir}}/scripts/installers/boost.sh" - ], - "environment_vars": [ - "METADATA_FILE={{user `metadata_file`}}", - "HELPER_SCRIPTS={{user `helper_script_folder`}}", - "INSTALLER_SCRIPT_FOLDER={{user `installer_script_folder`}}" - ], - "execute_command": "sudo sh -c '{{ .Vars }} {{ .Path }}'" - }, { "type": "shell", "scripts":[ diff --git a/images/linux/ubuntu1804.json b/images/linux/ubuntu1804.json index 5cbabc001..41cc8c52f 100644 --- a/images/linux/ubuntu1804.json +++ b/images/linux/ubuntu1804.json @@ -251,12 +251,12 @@ }, { "type": "file", - "source": "{{template_dir}}/toolcache-1804.json", + "source": "{{template_dir}}/toolsets/toolcache-1804.json", "destination": "{{user `installer_script_folder`}}/toolcache.json" }, { "type": "file", - "source": "{{template_dir}}/toolset-1804.json", + "source": "{{template_dir}}/toolsets/toolset-1804.json", "destination": "{{user `installer_script_folder`}}/toolset.json" }, { @@ -282,6 +282,7 @@ "type": "shell", "scripts":[ "{{template_dir}}/scripts/installers/Install-Toolset.ps1", + "{{template_dir}}/scripts/installers/Configure-Toolset.ps1", "{{template_dir}}/scripts/installers/Validate-Toolset.ps1" ], "environment_vars": [ @@ -291,28 +292,6 @@ ], "execute_command": "sudo sh -c '{{ .Vars }} pwsh -f {{ .Path }}'" }, - { - "type": "shell", - "scripts": [ - "{{template_dir}}/scripts/installers/go.sh" - ], - "environment_vars": [ - "INSTALLER_SCRIPT_FOLDER={{user `installer_script_folder`}}" - ], - "execute_command": "sudo sh -c '{{ .Vars }} {{ .Path }}'" - }, - { - "type": "shell", - "scripts":[ - "{{template_dir}}/scripts/installers/boost.sh" - ], - "environment_vars": [ - "METADATA_FILE={{user `metadata_file`}}", - "HELPER_SCRIPTS={{user `helper_script_folder`}}", - "INSTALLER_SCRIPT_FOLDER={{user `installer_script_folder`}}" - ], - "execute_command": "sudo sh -c '{{ .Vars }} {{ .Path }}'" - }, { "type": "shell", "scripts":[ diff --git a/images/linux/ubuntu2004.json b/images/linux/ubuntu2004.json index f4131a455..611fdc6bb 100644 --- a/images/linux/ubuntu2004.json +++ b/images/linux/ubuntu2004.json @@ -252,12 +252,12 @@ }, { "type": "file", - "source": "{{template_dir}}/toolcache-2004.json", + "source": "{{template_dir}}/toolsets/toolcache-2004.json", "destination": "{{user `installer_script_folder`}}/toolcache.json" }, { "type": "file", - "source": "{{template_dir}}/toolset-2004.json", + "source": "{{template_dir}}/toolsets/toolset-2004.json", "destination": "{{user `installer_script_folder`}}/toolset.json" }, { @@ -283,6 +283,7 @@ "type": "shell", "scripts":[ "{{template_dir}}/scripts/installers/Install-Toolset.ps1", + "{{template_dir}}/scripts/installers/Configure-Toolset.ps1", "{{template_dir}}/scripts/installers/Validate-Toolset.ps1" ], "environment_vars": [ @@ -292,16 +293,6 @@ ], "execute_command": "sudo sh -c '{{ .Vars }} pwsh -f {{ .Path }}'" }, - { - "type": "shell", - "scripts": [ - "{{template_dir}}/scripts/installers/go.sh" - ], - "environment_vars": [ - "INSTALLER_SCRIPT_FOLDER={{user `installer_script_folder`}}" - ], - "execute_command": "sudo sh -c '{{ .Vars }} {{ .Path }}'" - }, { "type": "shell", "scripts":[