如何使用powershell脚本访问azure中的kudu

zqdjd7g9  于 2021-06-20  发布在  Kudu
关注(0)|答案(3)|浏览(420)

我正在尝试通过powershell脚本访问kudu。链接看起来像: https://adc-dev.scm.azurewebsites.net . 我需要复印 war 文件位于 D:\home\site\wwwroot\bin\apache-tomcat-8.0.33\webapps 以上链接中的位置。
目前我正在部署 war 通过添加ftp任务来使用vsts文件。但在部署最新的 war 我想备份旧的 war 在azure kudu的某个位置,可以这样说: D:\home\site\wwwroot\bin\apache-tomcat-8.0.33 (根文件夹到 war 位置)。所以在那之后我可以移除 war 并部署最新的 war 在Kudu归档。
怎么做?我是说如何使用powershell脚本访问kudu。请建议我。

8ftvxx2r

8ftvxx2r1#

您可以参考下面的这个线程,了解如何在vsts构建/发布中通过azure powershell调用kudu api:
在从vsts部署新的部署之前删除azure上的文件和文件夹
关于通过kudu复制文件,可以使用命令kudu api(post/api/command):
Kudurest api
更新:
通过kudu 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]"
yhxst69z

yhxst69z2#

您可以使用以下代码从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
    }
gc0ot86w

gc0ot86w3#

要访问kudu api,请获取您的Web应用程序:

$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
也可以访问部署槽。需要为插槽检索app config,并且需要对基本url进行小的修改。

相关问题