且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用PowerShell DSC将构建输出文件传输到Azure VM?

更新时间:2022-11-06 23:25:42

我最终使用 Publish-AzureVMDscExtension cmdlet创建本地zip文件,将我的构建输出附加到zip,然后发布zip,这些行:

  function Publish-AzureDscConfiguration 
{
[CmdletBinding()]
参数(
[参数(Manda t
$ string


$ b开始{}
处理
{
$ zippedConfigurationPath =$ ConfigurationPath.zip ;

Publish-AzureVMDscConfiguration -ConfigurationPath:$ ConfigurationPath -ConfigurationArchivePath:$ zippedConfigurationPath -Force

$ tempFolderName = [System.Guid] :: NewGuid()。ToString();
$ tempFolderPath =$ env:TEMP\ $ tempFolderName;
$ dropFolderPath =$ tempFolderPath\BuildDrop;

try {
Write-Verbose创建临时文件夹和符号链接,以在'$ tempFolderPath'...创建输出;
New-Item -ItemType:Directory -Path:$ tempFolderPath;
New-Symlink -LiteralPath:$ dropFolderPath -TargetPath:$ PWD;
Invoke-Expression.\7za a $ tempFolderPath\BuildDrop.zip $ dropFolderPath -r -x!'7za.exe'-x!'DscDeployment.ps1';

Write-Verbose将组件文件添加到'$ zippedConfigurationPath'中的DSC包...;
Invoke-Expression.\7za a $ zippedConfigurationPath $ dropFolderPath.zip;
}
finally {
Write-Verbose在'$ tempFolderPath'...中删除符号链接和临时文件夹;
Remove-ReparsePoint -Path:$ dropFolderPath;
删除项-PATH:$ tempFolderPath -Recurse -Force;
}
发布-AzureVMDscConfiguration -ConfigurationPath:$ zippedConfigurationPath -Force
}
结束{}
}

通过使用Azure使用的zip中的zip,我可以访问PowerShell DSC扩展的工作目录中的内容(在DSCWork文件夹中)。我尝试只是将drop文件夹直接添加到zip中(不先将其压缩),但是DSC扩展将文件夹复制到模块路径,认为它是一个模块。



我对这个解决方案还没有完全满足,而且我有一个一些问题已经,但它在我的脑海里是有道理的,应该正常工作。


I've been toying around with DSC and I think it's an awesome platform. I made a few tests to automate the deployment of our TFS build outputs and to automatically install the web applications and configure an environment.

This was relatively easy, as I could pass my drop folder path to the DSC script using a file share on our internal network, and use relative paths inside the configuration to select each of our modules.

My problem now is in how to expand this to Azure virtual machines. We wanted to create these scripts to automatically deploy to our QA and Production servers, which are hosted on Azure. Since they are not in our domain, I can't use the File resource anymore to transfer the files, but at the same time I want exactly the same functionality: I'd like to somehow point the configuration to our build output folder and copy the files from there to the virtual machines.

Is there some way I can copy the drop folder files easily from inside a configuration that is run on these remote computers, without sharing the same network and domain? I successfully configured the VMs to accept DSC calls over https using certificates, and I just found out that the Azure PowerShell cmdlets enable you to upload a configuration to Azure storage and run it in the VMs automatically (which seems a lot better than what I did) but I still don't know how I'd get access to my build outputs from inside the virtual machine when the configuration script is run.

I ended up using the Publish-AzureVMDscExtension cmdlet to create a local zip file, appending my build outputs to the zip, and then publishing the zip, something along those lines:

function Publish-AzureDscConfiguration
{
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory)]
        [string] $ConfigurationPath
    )

    Begin{}
    Process
    {
        $zippedConfigurationPath = "$ConfigurationPath.zip";

        Publish-AzureVMDscConfiguration -ConfigurationPath:$ConfigurationPath -ConfigurationArchivePath:$zippedConfigurationPath -Force

        $tempFolderName = [System.Guid]::NewGuid().ToString();
        $tempFolderPath = "$env:TEMP\$tempFolderName";
        $dropFolderPath = "$tempFolderPath\BuildDrop";

        try{
            Write-Verbose "Creating temporary folder and symbolic link to build outputs at '$tempFolderPath' ...";
            New-Item -ItemType:Directory -Path:$tempFolderPath;
            New-Symlink -LiteralPath:$dropFolderPath -TargetPath:$PWD;
            Invoke-Expression ".\7za a $tempFolderPath\BuildDrop.zip $dropFolderPath -r -x!'7za.exe' -x!'DscDeployment.ps1'";

            Write-Verbose "Adding component files to DSC package in '$zippedConfigurationPath'...";
            Invoke-Expression ".\7za a $zippedConfigurationPath $dropFolderPath.zip";
        }
        finally{
            Write-Verbose "Removing symbolic link and temporary folder at '$tempFolderPath'...";
            Remove-ReparsePoint -Path:$dropFolderPath;
            Remove-Item -Path:$tempFolderPath -Recurse -Force;
        }
        Publish-AzureVMDscConfiguration -ConfigurationPath:$zippedConfigurationPath -Force
    }
    End{}
}

By using a zip inside the zip used by Azure, I can access the inner contents in the working directory of the PowerShell DSC Extension (in the DSCWork folder). I tried just adding the drop folder directly to the zip (without zipping it first), but then the DSC Extension copies the folder to the modules path, thinking it is a module.

I'm not completely happy with this solution yet, and I'm having a few problems already, but it makes sense in my mind and should work fine.