Showing posts with label Source Control. Show all posts
Showing posts with label Source Control. Show all posts

Wednesday, 28 September 2016

Updating web.config for Azure Releases via Team Service Releases

Updating web.config for Azure Releases via Team Service Releases

Problem

I want to be able to modify various parts of a web.config file of a azure app service when I release to certain live environments.

Team services already provisions features such as slots that allow you to have specific appconfig and connection strings specific to certain slots.

If you want to modify other slots Team Service also has a specific type of task that can tokenize and transform a web.config:



However I did not feel this was an elegant or succint solutions.  Instead I wanted a task that would ensure that my stage/live and any other environments have the correct settings.

To do this I used Powershell or a Powershell task in TeamServices:

Solution

Instead I use a powershell which runs against a live release in Team Services. This powershell script will switch slots (effectively putting the code live) then will ensure the stage and live web.config have the correct settings.  

After that I run an integration test that ensures my web.config transformations are correct.

The powershell is broken up into 4 tasks:
  1.  Perform a swap of slots
  2. Download the web.config for an environment via FTP to a build client temp directory
  3. Perform web.config transform
  4. Re-upload the modified file via FTP
Below is the script:

#
# SwapSlots.ps1
#

param (
   [string] $AzureWebsiteName,
   [string] $From,
   [string] $To
)

Switch-AzureWebsiteSlot -Name $AzureWebsiteName -Slot1 $From -Slot2 $To -Force -Verbose


function DownloadFile ($sourceuri,$targetpath,$username,$password){
 # Create a FTPWebRequest object to handle the connection to the ftp server
 $ftprequest = [System.Net.FtpWebRequest]::create($sourceuri)
# set the request's network credentials for an authenticated connection
 $ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password)
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
 $ftprequest.UseBinary = $true
 $ftprequest.KeepAlive = $false
# send the ftp request to the server
 $ftpresponse = $ftprequest.GetResponse()
# get a download stream from the server response
 $responsestream = $ftpresponse.GetResponseStream()
# create the target file on the local system and the download buffer
 try
 {
 $targetfile = New-Object IO.FileStream ($targetpath,[IO.FileMode]::Create)
 [byte[]]$readbuffer = New-Object byte[] 1024
# loop through the download stream and send the data to the target file
 do{
 $readlength = $responsestream.Read($readbuffer,0,1024)
 $targetfile.Write($readbuffer,0,$readlength)
 }
 while ($readlength -ne 0)
$targetfile.close()
 }
 catch
 {
 $_|fl * -Force
 }
}

function UploadFile ($sourceuri,$targetpath,$username,$password){

 # Create a FTPWebRequest object to handle the connection to the ftp server
 $ftprequest = [System.Net.FtpWebRequest]::create($sourceuri)
# set the request's network credentials for an authenticated connection
 $ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password)
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
 $ftprequest.UseBinary = $true
 $ftprequest.KeepAlive = $false
# Read the File for Upload
$FileContent = gc -en byte $targetpath
$ftprequest.ContentLength = $FileContent.Length
# Get Stream Request by bytes
$Run = $ftprequest.GetRequestStream()
$Run.Write($FileContent, 0, $FileContent.Length)
# Cleanup
$Run.Close()
$Run.Dispose()
}

function ModifyWebConfigEndPoint ($file, $oldValue, $newValue) {
    #Mod File for production
    $webConfig = $file
    $doc = (Get-Content $webConfig) -as [Xml]
    $root = $doc.get_DocumentElement();
    $newSet = $root.'system.serviceModel'.client.endpoint.address.Replace($oldValue,$newValue);
    $root.'system.serviceModel'.client.endpoint.address = $newSet
    $doc.Save($webConfig)
}

# Global Settings
$Password = "<FTPPassword>"
$RemoteFile = "ftp://ftpURL/web.config"

# Download and modify Config for production (master)
$LocalFile = $env:Temp + "\web.config"
Write-Host "TempDirectoryIs"$LocalFile
$Username = "ftpusername"
#Remove-Item $LocalFile
DownloadFile $RemoteFile $LocalFile $Username $Password
ModifyWebConfigEndPoint $LocalFile 'val1' 'val2'
UploadFile $RemoteFile $LocalFile $Username $Password
Write-Host "Modified web config for "

# Download and modify Config for production (staging)
$LocalFile = $env:Temp + "\web.config"
Write-Host "TempDirectoryIs"$LocalFile
$Username = "ftpusername"
#Remove-Item $LocalFile
DownloadFile $RemoteFile $LocalFile $Username $Password
ModifyWebConfigEndPoint $LocalFile 'val1' 'val2'
UploadFile $RemoteFile $LocalFile $Username $Password
Write-Host "Modified web config for (staging)"

Its worth noting that the function that does the changing of the web.config is specific to in my case service endpoints but you can create specific or generic functions to update any part of the web.config file as it is just xml.

function ModifyWebConfigEndPoint ($file, $oldValue, $newValue) {
    #Mod File for production
    $webConfig = $file
    $doc = (Get-Content $webConfig) -as [Xml]
    $root = $doc.get_DocumentElement();
    $newSet = $root.'system.serviceModel'.client.endpoint.address.Replace($oldValue,$newValue);
    $root.'system.serviceModel'.client.endpoint.address = $newSet
    $doc.Save($webConfig)
}

Wednesday, 15 October 2014

Managing Microsoft Dynamics CRM Solutions in Source Control and Visual Studio

Background

We now manage our development world using agile.  And as such we link work items to specific user stories and features.  In our development team we are doing this via TFS.  When ever we check source code in we find the relevant work item, which is parented by a user story and feature alike.  

This is fine if you are writing code in a native Visual Studio code-base, but not fine if you are wanting to link changes made to your dynamics CRM to specific work items.  

Intended Solution

My idea was to export or unpack a specific set of changes to CRM via the unmanaged solutions save them unpacked as XML to a location where my source control could pick them up and then apply them.  Once in source control I could then associate changes to work items for CRM customization.

Once in source control I could then re-pack the solution into a deploy-able zip so that I can then apply this to me staging environment.  

So in summary the steps are:
  1. Get a list of solutions for a particular CRM instance from Visual Studio
  2. Export the solution 
  3. Unpack the solution to a location visible to source control
  4. Check in changes for solution
  5. Continuous integration server would then pick up any changes from the solution and re-pack them into deploy-able zip
  6. deploy to staging environment

Create a Visual Studio Extension that lists solutions for an instance of CRM

Using the Visual Studio SDK and the CRM SDK  I have created a Visual Studio Extension.

As you can see it populates a list of solutions in CRM based upon the Organization name:
CRM Solutions Manager
The user selects a solution from the list and then clicks download.  This used the CRM SDK and webservice to pull down the ZIP as a stream of bytes and then unpacks to a given location.

The location and CRM webservice call details are stored in a series of options built into the Visual Studio Extension.  As per below

CRM Solutions Manager Options
Once the download is complete it will download to the given source path where your source control software (Ours is TFS) can pick it up, at that point you can add the unpacked XML can be added to source control. As seen below.



As if by magic you now have CRM Solutions unpacked managed within visual studio and in source control.  The next step is to link this to your CI or build process.

Re-pack the solution from check in and Build Deploy-able solution zip

Once the files are checked into source control, we would have CI (Continuous Integration) or build server in place to pick up those changes and download them.

Our CI server has Cruise Control.NET installed and also Microsoft Dynamics CRM SDK.

As part of your build process you then run a command that packs the solution ready for deployment to your staging or UAT environment.

The command is as follows:

C:\CRM_SDK\SDK\Bin\SolutionPackager.exe /action Pack /zipfile <YourLocation>.zip /folder <YourLocationFolder>
If you like the visual Studio Extension related to this post please comment and I will look at providing it.