簡體   English   中英

如何使用 Power Shell 腳本訪問 Azure 中的 Kudu

[英]How to access Kudu in Azure using power shell script

我正在嘗試通過 power shell 腳本訪問 Kudu。 鏈接看起來像: https://adc-dev.scm.azurewebsites.net : https://adc-dev.scm.azurewebsites.net 我需要復制位於上面鏈接中D:\\home\\site\\wwwroot\\bin\\apache-tomcat-8.0.33\\webapps位置的war文件。

目前我正在通過添加 FTP 任務使用 VSTS 部署war文件。 但是在部署最新的war之前,我想在 Azure Kudu 位置的某個位置備份舊war ,例如: D:\\home\\site\\wwwroot\\bin\\apache-tomcat-8.0.33war根文件夾位置)。 所以在那之后我可以刪除war並在 Kudu 中部署最新的war文件。

如何做到這一點? 我的意思是如何使用 power shell 腳本訪問 kudu。 請建議我。

要訪問 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;

格式化適合 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 進行小幅修改。

您可以參考下面的這個線程,了解如何在 VSTS 構建/發布中通過 Azure PowerShell 調用 Kudu API:

在從 VSTS 進行新部署之前刪除 Azure 上的文件和折疊

關於通過 Kudu 復制文件,可以使用 Command Kudu API (Post /api/command):

Kudu REST API

更新:

通過 Kudu API 調用 Command 的簡單示例:

  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]"

您可以使用以下代碼從 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
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM