Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions VSTSAgent/DSCResources/xVSTSAgent/xVSTSAgent.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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
}
Expand Down Expand Up @@ -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 }
Expand Down
2 changes: 2 additions & 0 deletions VSTSAgent/DSCResources/xVSTSAgent/xVSTSAgent.schema.mof
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

116 changes: 51 additions & 65 deletions VSTSAgent/VSTSAgent.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}


Expand Down Expand Up @@ -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"),

Expand Down Expand Up @@ -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') {
Expand All @@ -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." }
Expand All @@ -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." }
Expand Down Expand Up @@ -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,

Expand Down Expand Up @@ -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"),

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,

Expand Down Expand Up @@ -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,

Expand Down