如何使用 power shell 脚本访问 Azure 中的 Kudu

How to access Kudu in Azure using power shell script

我正在尝试通过 power shell 脚本访问 Kudu。 Link 看起来像:https://adc-dev.scm.azurewebsites.net。我需要复制位于 D:\home\site\wwwroot\bin\apache-tomcat-8.0.33\webapps 位置的 war 文件 link.

目前我正在通过添加 FTP 任务使用 VSTS 部署 war 文件。但在部署最新的 war 之前,我想在 Azure Kudu 位置的某个位置备份旧的 war ,比如:D:\home\site\wwwroot\bin\apache-tomcat-8.0.33(根文件夹到 war 位置).所以在那之后我可以删除 war 并在 Kudu 中部署最新的 war 文件。

如何做到这一点?我的意思是如何使用 power shell 脚本访问 kudu。请建议我。

您可以参考下面的这个帖子,了解如何在 VSTS build/release 中通过 Azure PowerShell 调用 Kudu API:

关于通过Kudu复制文件,可以使用命令Kudu API (Post /api/command):

Kudu REST API

更新:

通过 Kudu 调用 Command 的简单示例 API:

  function RunCommand($dir,$command,$resourceGroupName, $webAppName, $slotName = $null){
        $kuduApiAuthorisationToken = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $slotName
        $kuduApiUrl="https://$webAppName.scm.azurewebsites.net/api/command"
        $Body = 
          @{
          "command"=$command;
           "dir"=$dir
           } 
        $bodyContent=@($Body) | ConvertTo-Json
        Write-Host $bodyContent
         Invoke-RestMethod -Uri $kuduApiUrl `
                            -Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} `
                            -Method POST -ContentType "application/json" -Body $bodyContent
    }


RunCommand "site\wwwroot\bin\apache-tomcat-8.0.33\webapps" "copy xx.war ..\xx.war /y" "[resource group]" "[web app]"

要访问 Kudu API,获取您的 WebApp:

$app = Get-AzWebApp -ResourceGroupName "your RG" -Name "your App"

然后获取应用程序的发布凭据:

$resourceName = "$($app.Name)/publishingcredentials";
$resourceType = "Microsoft.Web/sites/config";
$publishingCredentials = Invoke-AzResourceAction `
        -ResourceGroupName $app.ResourceGroup `
        -ResourceType $resourceType `
        -ResourceName $resourceName `
        -Action list `
        -ApiVersion $apiVersion `
        -Force;

格式化 username/password 适合 HTTP 请求:

$user = $publishingCredentials.Properties.PublishingUserName;
$pass = $publishingCredentials.Properties.PublishingPassword;
$creds = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("${user}:${pass}")));

终于可以访问Kudu了API:

$header = @{
    Authorization = "Basic $creds"
};
$kuduApiBaseUrl = "https://$($app.Name).scm.azurewebsites.net";

示例,检查是否安装了扩展:

$extensionName = "Microsoft.AspNetCore.AzureAppServices.SiteExtension";
$kuduApiUrl = "$kuduApiBaseUrl/api/siteextensions/$extensionName";
$response = Invoke-RestMethod -Method 'Get' -Uri $kuduApiUrl -Headers $header;

示例,获取可用扩展列表:

$kuduApiUrl = "$kuduApiBaseUrl/api/extensionfeed";
$response = Invoke-RestMethod -Method 'Get' -Uri $kuduApiUrl -Headers $header;

例如,安装一个扩展:

$kuduApiUrl = "$kuduApiBaseUrl/api/siteextensions";
$response = Invoke-RestMethod -Method 'Put' -Uri $kuduApiUrl -Headers $header;

API-详情在https://github.com/projectkudu/kudu/wiki/REST-API

也可以访问部署槽。需要为插槽检索应用程序配置,并且需要对基础 URL 进行少量修改。

您可以使用以下代码从 powershell 访问 kudu api -

 //function to Get webapp's publishing credentials    
    function Get-AzWebAppPublishingCredentials($resourceGroupName, $webAppName, $slotName = $null) {
            if ([string]::IsNullOrWhiteSpace($slotName) -or $slotName.ToLower() -eq "production") {
                $resourceType = "Microsoft.Web/sites/config"
                $resourceName = "$webAppName/publishingcredentials"
            }
            else {
                $resourceType = "Microsoft.Web/sites/slots/config"
                $resourceName = "$webAppName/$slotName/publishingcredentials"
            }
            $publishingCredentials = Invoke-AzResourceAction -ResourceGroupName $resourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action list -ApiVersion 2015-08-01 -Force
            return $publishingCredentials
    }

 //function to get authorization header from publishing credentials
     function Get-KuduApiAuthorisationHeaderValue($resourceGroupName, $webAppName, $slotName = $null) {
            $publishingCredentials = Get-AzWebAppPublishingCredentials $resourceGroupName $webAppName $slotName
            $ret = @{ }
            $ret.header = ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword))))
            $ret.url = $publishingCredentials.Properties.scmUri
            return $ret
        }

 //function to call kudu api e.g. to get a file from webapp
    function Get-FileFromWebApp($resourceGroupName, $webAppName, $slotName = "", $kuduPath) {
        $KuduAuth = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $slotName
        $kuduApiAuthorisationToken = $KuduAuth.header
        $kuduApiUrl = $KuduAuth.url + "/api/vfs/$kuduPath"

        Write-Host " Downloading File from WebApp. Source: '$kuduApiUrl'." -ForegroundColor DarkGray
        $tmpPath = "$($env:TEMP)$([guid]::NewGuid()).json"
        $null = Invoke-RestMethod -Uri $kuduApiUrl `
            -Headers @{"Authorization" = $kuduApiAuthorisationToken; "If-Match" = "*" } `
            -Method GET `
            -ContentType "application/json" `
            -OutFile $tmpPath
        $ret = (Get-Content $tmpPath) | Out-String | ConvertFrom-Json
        Remove-Item $tmpPath -Force
        return $ret
    }