Skip to content

Commit

Permalink
First release in marketplace
Browse files Browse the repository at this point in the history
  • Loading branch information
keesschollaart81 committed Apr 13, 2017
1 parent 2952e8a commit 425217f
Show file tree
Hide file tree
Showing 78 changed files with 5,521 additions and 40 deletions.
28 changes: 4 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
# vsts-arm-outputs - a VSTS Extension
# ARM Outputs

Using this extenion in your VSTS environment enables you to use the values coming out of the ARM Deployment outputs.
This extension enables you to use the ARM Deployment outputs in your VSTS environment.

This step will use the last successful deployment within the selected resource group. If this deployent has outputs, all of them are copied to VSTS variables by the ARM Output key.

```json
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {},
"variables":{},
"resources":{},
"outputs": {
"dbserver-fqdn": {
"type": "string",
"value": "[reference(concat('Microsoft.Sql/servers/',variables('dbserver-name'))).fullyQualifiedDomainName]"
},
"sample-variable": {
"type": "string",
"value": "[variables('sample-variable')]"
}
}
}
```

This outputs can then be used by default VSTS ways: ```$(sample-variable)```
This outputs can then be used by default VSTS ways: ```$(same-key-as-in-arm-template)```

Usually this task is ran directly after the 'Azure Resource Group Deployment' task.

//todo: screenshot of VSTS
[![screenshot-1](images/screenshots-vsts-arm-outputs-1.png "Screenshot-1")](images/screenshots-vsts-arm-outputs-1.png)
Binary file added arm-outputs/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
138 changes: 138 additions & 0 deletions arm-outputs/ps_modules/VstsAzureHelpers_/ImportFunctions.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
function Import-AzureModule {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[ValidateSet('Azure', 'AzureRM')]
[string[]]$PreferredModule)

Trace-VstsEnteringInvocation $MyInvocation
try {
Write-Verbose "Env:PSModulePath: '$env:PSMODULEPATH'"
if ($PreferredModule -contains 'Azure' -and $PreferredModule -contains 'AzureRM') {
# Attempt to import Azure and AzureRM.
$azure = (Import-FromModulePath -Classic:$true) -or (Import-FromSdkPath -Classic:$true)
$azureRM = (Import-FromModulePath -Classic:$false) -or (Import-FromSdkPath -Classic:$false)
if (!$azure -and !$azureRM) {
throw (Get-VstsLocString -Key AZ_ModuleNotFound)
}
} elseif ($PreferredModule -contains 'Azure') {
# Attempt to import Azure but fallback to AzureRM.
if (!(Import-FromModulePath -Classic:$true) -and
!(Import-FromSdkPath -Classic:$true) -and
!(Import-FromModulePath -Classic:$false) -and
!(Import-FromSdkPath -Classic:$false))
{
throw (Get-VstsLocString -Key AZ_ModuleNotFound)
}
} else {
# Attempt to import AzureRM but fallback to Azure.
if (!(Import-FromModulePath -Classic:$false) -and
!(Import-FromSdkPath -Classic:$false) -and
!(Import-FromModulePath -Classic:$true) -and
!(Import-FromSdkPath -Classic:$true))
{
throw (Get-VstsLocString -Key AZ_ModuleNotFound)
}
}

# Validate the Classic version.
$minimumVersion = [version]'0.8.10.1'
if ($script:azureModule -and $script:azureModule.Version -lt $minimumVersion) {
throw (Get-VstsLocString -Key AZ_RequiresMinVersion0 -ArgumentList $minimumVersion)
}
} finally {
Trace-VstsLeavingInvocation $MyInvocation
}
}

function Import-FromModulePath {
[CmdletBinding()]
param(
[switch]$Classic)

Trace-VstsEnteringInvocation $MyInvocation
try {
# Determine which module to look for.
if ($Classic) {
$name = "Azure"
} else {
$name = "AzureRM"
}

# Attempt to resolve the module.
Write-Verbose "Attempting to find the module '$name' from the module path."
$module = Get-Module -Name $name -ListAvailable | Select-Object -First 1
if (!$module) {
return $false
}

# Import the module.
Write-Host "##[command]Import-Module -Name $($module.Path) -Global"
$module = Import-Module -Name $module.Path -Global -PassThru
Write-Verbose "Imported module version: $($module.Version)"

if ($Classic) {
# Store the imported Azure module.
$script:azureModule = $module
} else {
# The AzureRM module was imported.

# Validate the AzureRM.profile module can be found.
$profileModule = Get-Module -Name AzureRM.profile -ListAvailable | Select-Object -First 1
if (!$profileModule) {
throw (Get-VstsLocString -Key AZ_AzureRMProfileModuleNotFound)
}

# Import and then store the AzureRM.profile module.
Write-Host "##[command]Import-Module -Name $($profileModule.Path) -Global"
$script:azureRMProfileModule = Import-Module -Name $profileModule.Path -Global -PassThru
Write-Verbose "Imported module version: $($script:azureRMProfileModule.Version)"
}

return $true
} finally {
Trace-VstsLeavingInvocation $MyInvocation
}
}

function Import-FromSdkPath {
[CmdletBinding()]
param([switch]$Classic)

Trace-VstsEnteringInvocation $MyInvocation
try {
if ($Classic) {
$partialPath = 'Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Azure.psd1'
} else {
$partialPath = 'Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\AzureRM.Profile\AzureRM.Profile.psd1'
}

foreach ($programFiles in @(${env:ProgramFiles(x86)}, $env:ProgramFiles)) {
if (!$programFiles) {
continue
}

$path = [System.IO.Path]::Combine($programFiles, $partialPath)
Write-Verbose "Checking if path exists: $path"
if (Test-Path -LiteralPath $path -PathType Leaf) {
# Import the module.
Write-Host "##[command]Import-Module -Name $path -Global"
$module = Import-Module -Name $path -Global -PassThru
Write-Verbose "Imported module version: $($module.Version)"

# Store the imported module.
if ($Classic) {
$script:azureModule = $module
} else {
$script:azureRMProfileModule = $module
}

return $true
}
}

return $false
} finally {
Trace-VstsLeavingInvocation $MyInvocation
}
}
201 changes: 201 additions & 0 deletions arm-outputs/ps_modules/VstsAzureHelpers_/InitializeFunctions.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
function Add-Certificate {
[CmdletBinding()]
param([Parameter(Mandatory=$true)]$Endpoint)

# Add the certificate to the cert store.
$bytes = [System.Convert]::FromBase64String($Endpoint.Auth.Parameters.Certificate)
$certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$certificate.Import($bytes)
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store(
([System.Security.Cryptography.X509Certificates.StoreName]::My),
([System.Security.Cryptography.X509Certificates.StoreLocation]::CurrentUser))
$store.Open(([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite))
$store.Add($certificate)
$store.Close()
return $certificate
}

function Format-Splat {
[CmdletBinding()]
param([Parameter(Mandatory = $true)][hashtable]$Hashtable)

# Collect the parameters (names and values) in an array.
$parameters = foreach ($key in $Hashtable.Keys) {
$value = $Hashtable[$key]
# If the value is a bool, format the parameter as a switch (ending with ':').
if ($value -is [bool]) { "-$($key):" } else { "-$key" }
$value
}

$OFS = " "
"$parameters" # String join the array.
}

function Initialize-AzureSubscription {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
$Endpoint,
[Parameter(Mandatory=$false)]
[string]$StorageAccount)

#Set UserAgent for Azure Calls
Set-UserAgent

$environmentName = "AzureCloud"
if($Endpoint.Data.Environment) {
$environmentName = $Endpoint.Data.Environment
}

if ($Endpoint.Auth.Scheme -eq 'Certificate') {
# Certificate is only supported for the Azure module.
if (!$script:azureModule) {
throw (Get-VstsLocString -Key AZ_CertificateAuthNotSupported)
}

# Add the certificate to the cert store.
$certificate = Add-Certificate -Endpoint $Endpoint

# Setup the additional parameters.
$additional = @{ }
if ($StorageAccount) {
$additional['CurrentStorageAccountName'] = $StorageAccount
}

# Set the subscription.
Write-Host "##[command]Set-AzureSubscription -SubscriptionName $($Endpoint.Data.SubscriptionName) -SubscriptionId $($Endpoint.Data.SubscriptionId) -Certificate ******** -Environment $environmentName $(Format-Splat $additional)"
Set-AzureSubscription -SubscriptionName $Endpoint.Data.SubscriptionName -SubscriptionId $Endpoint.Data.SubscriptionId -Certificate $certificate -Environment $environmentName @additional
Set-CurrentAzureSubscription -SubscriptionId $Endpoint.Data.SubscriptionId -StorageAccount $StorageAccount
} elseif ($Endpoint.Auth.Scheme -eq 'UserNamePassword') {
$psCredential = New-Object System.Management.Automation.PSCredential(
$Endpoint.Auth.Parameters.UserName,
(ConvertTo-SecureString $Endpoint.Auth.Parameters.Password -AsPlainText -Force))

# Add account (Azure).
if ($script:azureModule) {
try {
Write-Host "##[command]Add-AzureAccount -Credential $psCredential"
$null = Add-AzureAccount -Credential $psCredential
} catch {
# Provide an additional, custom, credentials-related error message.
Write-VstsTaskError -Message $_.Exception.Message
throw (New-Object System.Exception((Get-VstsLocString -Key AZ_CredentialsError), $_.Exception))
}
}

# Add account (AzureRM).
if ($script:azureRMProfileModule) {
try {
Write-Host "##[command]Add-AzureRMAccount -Credential $psCredential"
$null = Add-AzureRMAccount -Credential $psCredential
} catch {
# Provide an additional, custom, credentials-related error message.
Write-VstsTaskError -Message $_.Exception.Message
throw (New-Object System.Exception((Get-VstsLocString -Key AZ_CredentialsError), $_.Exception))
}
}

# Select subscription (Azure).
if ($script:azureModule) {
Set-CurrentAzureSubscription -SubscriptionId $Endpoint.Data.SubscriptionId -StorageAccount $StorageAccount
}

# Select subscription (AzureRM).
if ($script:azureRMProfileModule) {
Set-CurrentAzureRMSubscription -SubscriptionId $Endpoint.Data.SubscriptionId
}
} elseif ($Endpoint.Auth.Scheme -eq 'ServicePrincipal') {
$psCredential = New-Object System.Management.Automation.PSCredential(
$Endpoint.Auth.Parameters.ServicePrincipalId,
(ConvertTo-SecureString $Endpoint.Auth.Parameters.ServicePrincipalKey -AsPlainText -Force))
if ($script:azureModule -and $script:azureModule.Version -lt ([version]'0.9.9')) {
# Service principals arent supported from 0.9.9 and greater in the Azure module.
try {
Write-Host "##[command]Add-AzureAccount -ServicePrincipal -Tenant $($Endpoint.Auth.Parameters.TenantId) -Credential $psCredential"
$null = Add-AzureAccount -ServicePrincipal -Tenant $Endpoint.Auth.Parameters.TenantId -Credential $psCredential
} catch {
# Provide an additional, custom, credentials-related error message.
Write-VstsTaskError -Message $_.Exception.Message
throw (New-Object System.Exception((Get-VstsLocString -Key AZ_ServicePrincipalError), $_.Exception))
}

Set-CurrentAzureSubscription -SubscriptionId $Endpoint.Data.SubscriptionId -StorageAccount $StorageAccount
} elseif ($script:azureModule) {
# Throw if >=0.9.9 Azure.
throw (Get-VstsLocString -Key "AZ_ServicePrincipalAuthNotSupportedAzureVersion0" -ArgumentList $script:azureModule.Version)
} else {
# Else, this is AzureRM.
try {
Write-Host "##[command]Add-AzureRMAccount -ServicePrincipal -Tenant $($Endpoint.Auth.Parameters.TenantId) -Credential $psCredential -EnvironmentName $environmentName"
$null = Add-AzureRMAccount -ServicePrincipal -Tenant $Endpoint.Auth.Parameters.TenantId -Credential $psCredential -EnvironmentName $environmentName
} catch {
# Provide an additional, custom, credentials-related error message.
Write-VstsTaskError -Message $_.Exception.Message
throw (New-Object System.Exception((Get-VstsLocString -Key AZ_ServicePrincipalError), $_.Exception))
}

Set-CurrentAzureRMSubscription -SubscriptionId $Endpoint.Data.SubscriptionId -TenantId $Endpoint.Auth.Parameters.TenantId
}
} else {
throw (Get-VstsLocString -Key AZ_UnsupportedAuthScheme0 -ArgumentList $Endpoint.Auth.Scheme)
}
}

function Set-CurrentAzureSubscription {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$SubscriptionId,
[string]$StorageAccount)

$additional = @{ }
if ($script:azureModule.Version -lt ([version]'0.8.15')) {
$additional['Default'] = $true # The Default switch is required prior to 0.8.15.
}

Write-Host "##[command]Select-AzureSubscription -SubscriptionId $SubscriptionId $(Format-Splat $additional)"
$null = Select-AzureSubscription -SubscriptionId $SubscriptionId @additional
if ($StorageAccount) {
Write-Host "##[command]Set-AzureSubscription -SubscriptionId $SubscriptionId -CurrentStorageAccountName $StorageAccount"
Set-AzureSubscription -SubscriptionId $SubscriptionId -CurrentStorageAccountName $StorageAccount
}
}

function Set-CurrentAzureRMSubscription {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$SubscriptionId,
[string]$TenantId)

$additional = @{ }
if ($TenantId) { $additional['TenantId'] = $TenantId }
Write-Host "##[command]Select-AzureRMSubscription -SubscriptionId $SubscriptionId $(Format-Splat $additional)"
$null = Select-AzureRMSubscription -SubscriptionId $SubscriptionId @additional
}

function Set-UserAgent {
[CmdletBinding()]
param()

$userAgent = Get-VstsTaskVariable -Name AZURE_HTTP_USER_AGENT
if ($userAgent) {
Set-UserAgent_Core -UserAgent $userAgent
}
}

function Set-UserAgent_Core {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$UserAgent)

Trace-VstsEnteringInvocation $MyInvocation
try {
[Microsoft.Azure.Common.Authentication.AzureSession]::ClientFactory.AddUserAgent($UserAgent)
} catch {
Write-Verbose "Set-UserAgent failed with exception message: $_.Exception.Message"
} finally {
Trace-VstsLeavingInvocation $MyInvocation
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"loc.messages.AZ_AzureRMProfileModuleNotFound": "Das Modul \"AzureRM.Profile\" wurde nicht gefunden. Das Modul \"AzureRM\" ist ggf. nicht vollständig installiert. Das Ausführen der folgenden PowerShell-Befehle aus einer Sitzung mit erhöhten Rechten behebt dieses Problem möglicherweise: \"Import-Module AzureRM\", \"Install-AzureRM\".",
"loc.messages.AZ_CertificateAuthNotSupported": "Zertifikatbasierte Authentifizierung wird nicht unterstützt. Das Azure PowerShell-Modul wurde nicht gefunden.",
"loc.messages.AZ_CredentialsError": "Fehler mit den für die Bereitstellung verwendeten Azure-Anmeldeinformationen.",
"loc.messages.AZ_ModuleNotFound": "Das Azure-Modul und das AzureRM-Modul wurden nicht gefunden. Wenn das Modul vor Kurzem installiert wurde, versuchen Sie es nach dem Neustart des VSTS-Task-Agents erneut.",
"loc.messages.AZ_RequiresMinVersion0": "Die erforderliche Mindestversion ({0}) des Azure PowerShell-Moduls ist nicht installiert.",
"loc.messages.AZ_ServicePrincipalError": "Fehler im für die Bereitstellung verwendeten Dienstprinzipal.",
"loc.messages.AZ_ServicePrincipalAuthNotSupportedAzureVersion0": "Dienstprinzipalauthentifizierung wird in Version \"{0}\" des Azure-Moduls nicht unterstützt.",
"loc.messages.AZ_UnsupportedAuthScheme0": "Nicht unterstütztes Authentifizierungsschema \"{0}\" für den Azure-Endpunkt."
}
Loading

0 comments on commit 425217f

Please sign in to comment.