如何使用PowerShell DSC将构建输出文件传输到Azure VM? [英] How do I transfer my build output files to an Azure VM using PowerShell DSC?

查看:248
本文介绍了如何使用PowerShell DSC将构建输出文件传输到Azure VM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在和DSC玩耍,我认为这是一个非常棒的平台。我做了一些测试来自动部署我们的TFS构建输出,并自动安装Web应用程序并配置环境。



这是比较容易的,因为我可以通过我的放置文件夹路径到DSC脚本,使用内部网络上的文件共享,并使用配置内的相对路径选择我们的每个模块。



我现在的问题是在如何将其扩展到Azure虚拟机。我们希望创建这些脚本以自动部署到Azure上托管的质量检查和生产服务器。由于它们不在我们的域中,我不能再使用文件资源来传输文件,但同时我想要完全相同的功能:我会喜欢以某种方式将配置指向我们的构建输出文件夹,并将文件从那里复制到虚拟机。



有些方法可以轻松地复制放置文件夹文件在这些远程计算机上运行的配置中,不共享相同的网络和域?我成功配置了VM,通过https使用证书接受DSC呼叫,我刚刚发现 Azure PowerShell cmdlet使您可以将配置上传到Azure存储并自动在虚拟机中运行(这似乎是比我做的好多了),但是我仍然不知道如何在配置脚本运行时从虚拟机内部访问我的构建输出。

解决方案

我最终使用 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.

这篇关于如何使用PowerShell DSC将构建输出文件传输到Azure VM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆