2016-05-10 14 views
0

我试图自动化使用ARM模板创建Java appservice的过程,然后利用FTP发布方法为Tomcat上载Java WAR文件。如何使用Azure CLI为新创建/更新的appservice使用ARM模板获取发布凭证

继Azure的CLI不起作用 -

$ azure resource show <resource-group> <appservice-name>/publishingcredentials Microsoft.Web/sites/config 2015-08-01 

error: The resource type could not be found in the namespace 'Microsoft.Web' for api version '2015-08-01' 

但这部作品在Azure中的PowerShell -

$resource = Invoke-AzureRmResourceAction -ResourceGroupName <resource-group> -ResourceType Microsoft.Web/sites/config -ResourceName <appservice-name>/publishingcredentials -Action list -ApiVersion 2015-08-01 -Force 
$resource.Properties 

我已经提到的命名空间从资源管理器https://resources.azure.com,但没能找到合适的语法来获取FTP用户名和密码,以便稍后可以在脚本中使用该用户名和密码来上传WAR文件。

回答

1

我找到了一个替代方案。

如果资源组是手动创建的,并且资源组下的任何网站的FTP发布凭证都是手动设置的。然后,这将为资源组设置一个备用FTP凭证 - 不是特定于任何网站(或可能是整个帐户的全局帐户)。

每个网站一旦创建,就会在FTP服务器上设置不同的域,并且备用凭据适用于任何网站 - ftp:///[email protected]

现在来自Jenkin,如果我们根据相同资源组下的git分支名称创建一个新的siteName,我们也可以使用上面预先配置的FTP凭证作为新的siteName以及上传应用程序存档。

我希望有一种方法可以使用Azure CLI检索至少备用凭据。资源管理器将其显示在 - https://management.azure.com/providers/Microsoft.Web/publishingUsers/web?api-version=2015-08-01之下。

或即使ARM模板下的“siteConfig”属性可以设置为配置publisherUsername和publishingPassword。我试过这个选项,但这些属性被忽略。

0

Azure CLI azure resource show与Azure PowerShell Get-AzureRmResource类似,它始终使用“GET”方法。如果你添加“-vv”选项,你会看到它使用的REST API。另一方面,PowerShell Invoke-AzureRmResourceAction正在使用“POST”方法。如果您将'-debug'选项添加到Invoke-AzureRmResourceAction您将能够看到REST API。

https://management.azure.com/subscriptions/<your subscription id>/resourceGroups/<resource group>/providers/Microsoft.Web/sites/<app service>/config/publishingcredentials/list?api-version=2015-08-01 

这里是一个PowerShell脚本调用这个REST API:

Add-Type -Path 'C:\Program Files\Microsoft Azure Active Directory Connect\Microsoft.IdentityModel.Clients.ActiveDirectory.dll' 

# The tenant ID of you Subscription. You can use tenant name instead. 
$tenantID = "<the tenant ID of your Subscription>" 

# You can leave the variables as what they are, if you are under Azure Cloud Environment. 
$loginEndpoint = "https://login.windows.net/" 
$managementResourceURI = "https://management.core.windows.net/" 
$redirectURI = New-Object System.Uri ("urn:ietf:wg:oauth:2.0:oob") 
$clientID = "1950a258-227b-4e31-a9cf-717495945fc2" 

# Fill in the below variables. 
$subscriptionID = "<your subscription id>" 
$resouceGroup = "<your resource group>" 
$appService = "<your app service>" 
$username = "<your Azure account>" 


# Constructing the authentication string. 
$authString = $loginEndpoint + $tenantID 

# Use the above authentication string to create an authentication context. 
$authenticationContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext ($authString, $false) 

$promptBehaviour = [Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior]::Auto 

$userIdentifierType = [Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifierType]::RequiredDisplayableId 

$userIdentifier = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifier ($username, $userIdentifierType) 


# Prompt for signing in. 
$authenticationResult = $authenticationContext.AcquireToken($managementResourceURI, $clientID, $redirectURI, $promptBehaviour, $userIdentifier); 

# construct authorization header for the REST API. 
$authHeader = $authenticationResult.AccessTokenType + " " + $authenticationResult.AccessToken 
$headers = @{"Authorization"=$authHeader; "Content-Type"="application/json"} 

# Invoke the REST API. 
Invoke-RestMethod -Method POST -Uri "https://management.azure.com/subscriptions/$subscriptionID/resourceGroups/$resourceGroup/providers/Microsoft.Web/sites/$appService/config/publishingcredentials/list?api-version=2015-08-01" ` 
        -Headers $headers 

如果你是Linux系统下,你可以使用卷曲通过OAuth 2,以下bash脚本会给你出版凭证您的网络应用程序。但是,为了使用我的脚本,您需要create a service principleinstall curl in you Linux system

#!/bin/bash 

tenantID="<the tenant id of your subscription>" 

client_id="<the client id of your AD application>" 

client_secret="<a key you added to your AD application>" 

body="grant_type=client_credentials&client_id=$client_id&client_secret=$client_secret&resource=https%3A%2F%2Fmanagement.core.windows.net%2F" 

authorization=$(curl -X POST -H "Content-Type: application/x-www-form-urlencoded" --data-ascii "$body" "https://login.microsoftonline.com/$tenantID/oauth2/token" | python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["token_type"]+" "+obj["access_token"])') 


subscriptionID="<your subscription id>" 
resourceGroup="<the resource group of you web app>" 
appService="<your web app>" 

curl -X POST -H "Authorization: $authorization" --data-ascii "" "https://management.azure.com/subscriptions/$subscriptionID/resourceGroups/$resourceGroup/providers/Microsoft.Web/sites/$appService/config/publishingcredentials/list?api-version=2015-08-01" 
+0

感谢您提供使用REST API获取发布凭证的详细示例。我完全可以在其他情况下使用这个例子。在Azure CLI修复为支持第2级资源之前,我想我必须使用Windows机器来处理Jenkin服务器并使用Azure PowerShell。 –

+0

我已经检查过Azure CLI,他们实际上使用'--parent'选项来支持Level 2资源。然而,这对你的情况没有帮助,因为'azure resource show'总是使用“GET”方法,而在你的情况下,你需要使用“POST”方法。我会稍微更新我的答案。 –

相关问题