Saturday, May 16, 2015

Managing your Azure infrastructure as code - part 4 (Chef and DSC)

Chef and DSC

DSC is a new management platform in Windows PowerShell that enables deploying and managing configuration data for software services and managing the environment in which these services run.
DSC provides a set of Windows PowerShell language extensions, new Windows PowerShell cmdlets, and resources that you can use to declaratively specify how you want your software environment to be configured. It also provides a means to maintain and manage existing configurations.
With the release of Chef Client 11.16.0 PowerShell Desired State Configuration (DSC) support was introduced into Chef Client for Windows. With this feature DSC resources can be executed from Chef. Both DSC and chef are similar in the way that both are idempotent, take similar approaches to the concept of resources, describe the configuration of a system, and then take the steps required to do that configuration. The most important difference between Chef and DSC is that Chef uses Ruby and DSC is exposed as configuration data from within Windows PowerShell. The dsc_resource resource allows any DSC resource to be used in a Chef recipe, as well as any custom resources that have been added to your Windows PowerShell environment.
The syntax for using the dsc_resource resource in a recipe is as follows:
dsc_resource "name" do
  resource :dsc_resource_name
  property :dsc_property_name, "property_value"
  property ...
  ...
end

For e.g the below given code shows how the windowsfeature resource is used to install asp.net 4.5 on the node.
dsc_resource 'aspnet' do
   resource :windowsfeature
   property :ensure, 'Present'
   property :name, "web-asp-net45"
 end

LCM and WMF 5.0

Before using the dsc_resource, you need to ensure that Windows Management Framework 5.0 is installed on the target node. Also the refresh mode of the LCM should be set to disabled. To set the LCM refresh mode to disabled, you can create a configuration as given below and execute it.
Configuration LCMSettings {
    Node $env:COMPUTERNAME {
       LocalConfigurationManager {
           RefreshMode = "Disabled"
       }
    }
}

LCMSettings



Creating a cookbook for using DSC resources

To use the DSC resources, create a cookbook and edit the default.rb file in recipes folder to add the content as given below.

dsc_resource 'iis' do
   resource :windowsfeature
   property :ensure, 'Present'
   property :name, "web-server"
 end
 dsc_resource 'aspnet' do
   resource :windowsfeature
   property :ensure, 'Present'
   property :name, "web-asp-net45"
 end
 dsc_resource 'defaulthtml' do
                resource :file
                property :ensure, 'Present'
                property :type, 'File'
                property :destinationpath, 'C:\inetpub\wwwroot\index.htm'
                property :contents, 'html of the page'
 end
dsc_resource 'iisstart' do
                resource :file
                property :ensure, 'Absent'
                property :type, 'File'
                property :sourcepath, 'C:\inetpub\wwwroot\iisstart.htm'
                property :destinationpath, 'C:\inetpub\wwwroot\iisstart.htm'
 end

Upload the cookbook to the server and chef the chef server whether the cookbook is available.

Bootstrapping the chef client

To apply the configuration on the machine, use the command
knife bootstrap windows winrm cheftestvm00.cloudapp.net -x ‘user’ -P 'password' -r "recipe[azurewebrole]"
After executing the command, you can access the default url for the server and check whether the contents match the one given in the file resource.




No comments: