diff --git a/VSTSAgent/DSCResources/xVSTSAgent/xVSTSAgent.psm1 b/VSTSAgent/DSCResources/xVSTSAgent/xVSTSAgent.psm1 index ddd3135..196d4db 100644 --- a/VSTSAgent/DSCResources/xVSTSAgent/xVSTSAgent.psm1 +++ b/VSTSAgent/DSCResources/xVSTSAgent/xVSTSAgent.psm1 @@ -156,7 +156,15 @@ function Set-TargetResource { $Ensure = 'Present', [System.Boolean] - $PrefixComputerName = $false + $PrefixComputerName = $false, + + [parameter(Mandatory = $false)] + [System.String] + $RequiredVersion, + + [parameter(Mandatory = $false)] + [System.String] + $GitHubApiToken ) if ( Test-TargetResource @PSBoundParameters ) { return } @@ -181,6 +189,12 @@ function Set-TargetResource { if ( $Work ) { $installArgs['Work'] = $Work } if ( $LogonCredential ) { $installArgs['LogonCredential'] = $LogonCredential } if ( $ProxyUrl ) { $installArgs['ProxyUrl'] = $ProxyUrl } + if ( $RequiredVersion ) + { + $installArgs['MinimumVersion'] = $RequiredVersion + $installArgs['MaximumVersion'] = $RequiredVersion + } + if ($GitHubApiToken) { $installArgs['GitHubApiToken'] = $GitHubApiToken } Install-VSTSAgent @installArgs } @@ -262,7 +276,15 @@ function Test-TargetResource { $Ensure = 'Present', [System.Boolean] - $PrefixComputerName = $false + $PrefixComputerName = $false, + + [parameter(Mandatory = $false)] + [System.String] + $RequiredVersion, + + [parameter(Mandatory = $false)] + [System.String] + $GitHubApiToken ) if ( $PrefixComputerName ) { $Name = Get-PrefixComputerName $Name } diff --git a/VSTSAgent/DSCResources/xVSTSAgent/xVSTSAgent.schema.mof b/VSTSAgent/DSCResources/xVSTSAgent/xVSTSAgent.schema.mof index 2bae686..b5a0bcd 100644 --- a/VSTSAgent/DSCResources/xVSTSAgent/xVSTSAgent.schema.mof +++ b/VSTSAgent/DSCResources/xVSTSAgent/xVSTSAgent.schema.mof @@ -17,5 +17,7 @@ class xVSTSAgent : OMI_BaseResource [Write] String ProxyUrl; [Write] Boolean PrefixComputerName; [Write, ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure; + [Write] String RequiredVersion; + [Write] String GitHubApiToken; }; diff --git a/VSTSAgent/VSTSAgent.psm1 b/VSTSAgent/VSTSAgent.psm1 index ea387bd..7986e18 100644 --- a/VSTSAgent/VSTSAgent.psm1 +++ b/VSTSAgent/VSTSAgent.psm1 @@ -104,62 +104,62 @@ function Find-VSTSAgent { [parameter(Mandatory = $true, ParameterSetName = 'MinMaxVersion')] [VSTSAgentVersion]$MaximumVersion, - [parameter(Mandatory = $true, ParameterSetName = 'RequiredVersion')] - [VSTSAgentVersion]$RequiredVersion, - [parameter(Mandatory = $true, ParameterSetName = 'Latest')] [switch]$Latest, [parameter(Mandatory = $false)] - [string]$Platform + [string]$Platform, + + [parameter(Mandatory = $false)] + [string]$GitHubApiToken ) - if ( $Latest ) { + $foundAgents = @() - $findArgs = @{ } - if ( $Platform ) { $findArgs['Platform'] = $Platform } - $sortedAgents = Find-VSTSAgent @findArgs | Sort-Object -Descending -Property Version - $sortedAgents | Where-Object { $_.Version -eq $sortedAgents[0].Version } - return - } + if ($Platform) { $Platform += "-x64" } - Set-SecurityProtocol + $client = (New-Object System.Net.WebClient) + $client.Headers["User-Agent"] = "request" + if ($GitHubApiToken) { $client.Headers["Authorization"] = ("Bearer "+$GitHubApiToken) } - $rootUri = [uri]"https://github.com" - $releasesRelativeUri = [uri]"/Microsoft/vsts-agent/releases" - - $page = [uri]::new( $rootUri, $releasesRelativeUri ) - $queriedPages = @() + $releases = $client.DownloadString("https://api.github.com/repos/microsoft/vsts-agent/releases") | ConvertFrom-JSON - do { - - $result = Invoke-WebRequest $page -UseBasicParsing - $result.Links.href | Where-Object { $_ -match "vsts-agent-(\w+)-x64-(\d+\.\d+\.\d+)\..+$" } | ForEach-Object { - - $instance = [PSCustomObject] @{ - 'Platform' = $Matches[1] - 'Version' = [VSTSAgentVersion]$Matches[2] - 'Uri' = [uri]::new($_, [System.UriKind]::RelativeOrAbsolute) - } + $releases = ($releases | Where-Object { -not $_.prerelease }) - # Make it absolute - if ( -not $instance.Uri.IsAbsoluteUri ) { $instance.Uri = [uri]::new($rootUri, $instance.Uri) } - - if ( $RequiredVersion -and $instance.Version -ne $RequiredVersion) { return } - if ( $MinimumVersion -and $instance.Version -lt $MinimumVersion) { return } - if ( $MaximumVersion -and $instance.Version -gt $MaximumVersion) { return } - if ( $Platform -and $instance.Platform -ne $Platform) { return } - - Write-Verbose "Found agent at $($instance.Uri)" - Write-Output $instance + $releases | ForEach-Object { + $release = $_ + try { + $assetData = $client.DownloadString($release.assets.browser_download_url) | ConvertFrom-JSON + if ($assetData) { + $assetData | ForEach-Object { + + $asset = $_ + if ($asset.name -notlike 'vsts*') { return } + + $agent = [PSCustomObject] @{ + 'Platform' = $asset.platform + 'Version' = [VSTSAgentVersion]$asset.version + 'Uri' = [uri]::new($asset.downloadUrl, [System.UriKind]::RelativeOrAbsolute) + } + + if ($Platform -and $agent.Platform -ne $Platform) { return } + if ( $MinimumVersion -and $agent.Version -lt $MinimumVersion) { return } + if ( $MaximumVersion -and $agent.Version -gt $MaximumVersion) { return } + + $foundAgents += $agent + } + } } + catch { + } + } - $queriedPages += $page - $page = $result.Links.href | Where-Object { - $_ -match "$releasesRelativeUri\?after=v(\d+\.\d+\.\d+)$" -and $queriedPages -notcontains $_ - } | Select-Object -First 1 - - } while ($page) + if ($Latest -and $foundAgents -and $foundAgents.Count -gt 1) { + $foundAgents[0] + } + else { + $foundAgents + } } @@ -207,9 +207,6 @@ function Install-VSTSAgent { [parameter(Mandatory = $true, ParameterSetName = 'MinMaxVersion')] [VSTSAgentVersion]$MaximumVersion, - [parameter(Mandatory = $true, ParameterSetName = 'RequiredVersion')] - [VSTSAgentVersion]$RequiredVersion, - [parameter(Mandatory = $false)] [string]$AgentDirectory = [IO.Path]::Combine($env:USERPROFILE, "VSTSAgents"), @@ -253,7 +250,10 @@ function Install-VSTSAgent { [pscredential]$LogonCredential, [parameter(Mandatory = $false)] - [string]$Cache = [io.Path]::Combine($env:USERPROFILE, ".vstsagents") + [string]$Cache = [io.Path]::Combine($env:USERPROFILE, ".vstsagents"), + + [parameter(Mandatory = $false)] + [string]$GitHubApiToken ) if ($PSVersionTable.Platform -and $PSVersionTable.Platform -ne 'Win32NT') { @@ -273,7 +273,7 @@ function Install-VSTSAgent { $findArgs = @{ 'Platform' = 'win' } if ( $MinimumVersion ) { $findArgs['MinimumVersion'] = $MinimumVersion } if ( $MaximumVersion ) { $findArgs['MaximumVersion'] = $MaximumVersion } - if ( $RequiredVersion ) { $findArgs['RequiredVersion'] = $RequiredVersion } + if ( $GitHubApiToken ) { $findArgs['GitHubApiToken'] = $GitHubApiToken } $agent = Find-VSTSAgent @findArgs | Sort-Object -Descending -Property Version | Select-Object -First 1 if ( -not $agent ) { throw "Could not find agent matching requirements." } @@ -291,7 +291,9 @@ function Install-VSTSAgent { } Write-Verbose "Downloading agent from $($agent.Uri)" - try { Start-BitsTransfer -Source $agent.Uri -Destination $destPath } + try { + (New-Object System.Net.WebClient).DownloadFile($agent.Uri, $destPath) + } catch { throw "Downloading $($agent.Uri) failed: $_" } } else { Write-Verbose "Skipping download as $destPath already exists." } @@ -376,9 +378,6 @@ function Uninstall-VSTSAgent { [parameter(Mandatory = $true, ParameterSetName = 'MinMaxVersion')] [VSTSAgentVersion]$MaximumVersion, - [parameter(Mandatory = $true, ParameterSetName = 'RequiredVersion')] - [VSTSAgentVersion]$RequiredVersion, - [parameter(Mandatory = $false)] [string]$AgentDirectory, @@ -445,9 +444,6 @@ function Get-VSTSAgent { [parameter(Mandatory = $true, ParameterSetName = 'MinMaxVersion')] [VSTSAgentVersion]$MaximumVersion, - [parameter(Mandatory = $true, ParameterSetName = 'RequiredVersion')] - [VSTSAgentVersion]$RequiredVersion, - [parameter(Mandatory = $false)] [string]$AgentDirectory = [io.Path]::Combine($env:USERPROFILE, "VSTSAgents"), @@ -479,10 +475,6 @@ function Get-VSTSAgent { $version = & $configPath --version - if ( $RequiredVersion -and $version -ne $RequiredVersion) { - Write-Verbose "Skipping agent because $version not match $RequiredVersion" - return - } if ( $MinimumVersion -and $version -lt $MinimumVersion) { Write-Verbose "Skipping agent because $version is less than $MinimumVersion" return @@ -543,9 +535,6 @@ function Start-VSTSAgent { [parameter(Mandatory = $true, ParameterSetName = 'MinMaxVersion')] [VSTSAgentVersion]$MaximumVersion, - [parameter(Mandatory = $true, ParameterSetName = 'RequiredVersion')] - [VSTSAgentVersion]$RequiredVersion, - [parameter(Mandatory = $false)] [string]$AgentDirectory, @@ -589,9 +578,6 @@ function Stop-VSTSAgent { [parameter(Mandatory = $true, ParameterSetName = 'MinMaxVersion')] [VSTSAgentVersion]$MaximumVersion, - [parameter(Mandatory = $true, ParameterSetName = 'RequiredVersion')] - [VSTSAgentVersion]$RequiredVersion, - [parameter(Mandatory = $false)] [string]$AgentDirectory,