From 74bfafcb52520d91e1da4f40edb3a6f435db1b18 Mon Sep 17 00:00:00 2001 From: Azure SDK Bot <53356347+azure-sdk@users.noreply.github.com> Date: Sun, 14 Feb 2021 15:56:25 -0800 Subject: [PATCH] Sync eng/common directory with azure-sdk-tools for PR 1371 (#1634) * Rework verify agent OS step. * Address feedback * Justing string matching instead. * ! instead of -not Co-authored-by: Mitch Denny --- .../templates/steps/verify-agent-os.yml | 45 +++++-------------- eng/common/scripts/Verify-AgentOS.ps1 | 13 ++++++ 2 files changed, 25 insertions(+), 33 deletions(-) create mode 100644 eng/common/scripts/Verify-AgentOS.ps1 diff --git a/eng/common/pipelines/templates/steps/verify-agent-os.yml b/eng/common/pipelines/templates/steps/verify-agent-os.yml index 12eaa0d3e..33b7ed2c6 100644 --- a/eng/common/pipelines/templates/steps/verify-agent-os.yml +++ b/eng/common/pipelines/templates/steps/verify-agent-os.yml @@ -1,37 +1,16 @@ parameters: - OSVmImage: $(OSVmImage) + - name: ScriptDirectory + type: string + default: 'eng/common/scripts' + - name: AgentImage + type: string steps: - - task: PythonScript@0 - displayName: Verify Agent OS + - task: PowerShell@2 + displayName: Verify agent OS inputs: - scriptSource: inline - script: | - # Script verifies the operating system for the platform on which it is being run - # Used in build pipelines to verify the build agent os - # Variable: The friendly name or image name of the os to verify against - from __future__ import print_function - import sys - import platform - - os_parameter = "${{ parameters.OSVmImage }}".lower() - if os_parameter.startswith('mac') or os_parameter.startswith('darwin'): - os_parameter = 'macOS' - elif os_parameter.startswith('ubuntu') or os_parameter.startswith('linux'): - os_parameter = 'Linux' - elif os_parameter.startswith('vs') or os_parameter.startswith('win'): - os_parameter = 'Windows' - else: - raise Exception('Variable OSVmImage is empty or has an unexpected value [${{ parameters.OSVmImage }}]') - - - print("Job requested to run on OS: %s" % (os_parameter)) - - agent_os = platform.system() - agent_os = 'macOS' if agent_os == 'Darwin' else agent_os - - if (agent_os.lower() == os_parameter.lower()): - print('Job ran on OS: %s' % (agent_os)) - print('##vso[task.setvariable variable=OSName]%s' % (agent_os)) - else: - raise Exception('Job ran on the wrong OS: %s' % (agent_os)) + pwsh: true + workingDirectory: $(System.DefaultWorkingDirectory) + filePath: ${{ parameters.ScriptDirectory }}/Verify-AgentOS.ps1 + arguments: > + -AgentImage ${{ parameters.AgentImage }} \ No newline at end of file diff --git a/eng/common/scripts/Verify-AgentOS.ps1 b/eng/common/scripts/Verify-AgentOS.ps1 new file mode 100644 index 000000000..3fd6c7170 --- /dev/null +++ b/eng/common/scripts/Verify-AgentOS.ps1 @@ -0,0 +1,13 @@ +param ( + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [string] $AgentImage +) + +function Throw-InvalidOperatingSystem { + throw "Invalid operating system detected. Operating system was: $([System.Runtime.InteropServices.RuntimeInformation]::OSDescription), expected image was: $AgentImage" +} + +if ($AgentImage -match "windows|win|MMS2019" -and !$IsWindows) { Throw-InvalidOperatingSystem } +if ($AgentImage -match "ubuntu" -and !$IsLinux) { Throw-InvalidOperatingSystem } +if ($AgentImage -match "macos" -and !$IsMacOs) { Throw-InvalidOperatingSystem } \ No newline at end of file