DevOps with Windows – Chocolatey

Conceptually Package manager is well understood space for someone having slightest understanding of how *nix environment get’s managed, but when it comes to windows it was untracked space till recently. This was piece of stack which was ironically missing for so long that once you get hands on you will feel how on earth you were living without it. NuGet and Chocolatey are the two buzz words making lots of noises and deemed as the future for windows server management.

What Is Chocolatey?

Chocolatey builds on top of NuGet packaging format to provide a package management  for Microsoft Windows applications , Chocolatey is  kind of yum or apt-get but for windows. Its CLI based and can be used to decentralize packaging.  It has a central repository located at http://chocolatey.org/.

If you have ever used windows build in provider you probably be aware of the issues it has. It doesn’t really do versioning and seems misfit for upgrading. Any organization looking for long term solution to ensure that latest versions are always installed for them build in package provider may not be the recommended option .Chocolatey takes care of all this with very little effort. In contrast to default provider which has no dependency Chocolatey requires your machine to have Powershell 2.0 & .Net framework 4.0 installed. Installation of packages from Chocolatey is one command line that reaches out to internet and pulls it down. That would be version-able and upgradable; you can specify this version of package and that that’s what gets installed.

Recommended way of Chocolatey installation is by executing PowerShell script.

Chocolatey With AWS

AWS offers windows instances with both their offering; under IAAS you can launch windows instance as EC2, whereas with PAAS you can get that via Elastic beanstalk.

Using Cloud Formation:

Using ‘cfn-init’ AWS Cloud Formation supports the download of files and execution of commands on Windows EC2 instance. Bootstrapping of Windows instance using Cloud Formation is lot simpler than any other ways. We can leverage this offering to install Chocolatey while launching the server using CFT. While doing this using Cloud formation we have to execute PowerShell.exe and provide the install command to that. One thing to be take care of that Chocolatey installer and the packages it installs may modify the machine’s PATH environment variable. This adds complexity since subsequent commands after these installations are executed in the same session, which does not have the updated PATH. To overcome this, we utilize a command file to set the session’s PATH to that of the machine before it executes our command. We will create a command file ‘ewmp.cmd’ to execute a command with the machine’s PATH, and then we will proceed with Chocolatey and any other installation. With below sample we will be installing Chocolatey and then install Firefox with Chocolatey as provider.

“AWS::CloudFormation::Init”: {

“config”: {

“files” : {

“c:/tools/ewmp.cmd” : {

“content”: “@ECHO OFF\nFOR /F \”tokens=3,*\” %%a IN (‘REG QUERY \”HKLM\\System\\CurrentControlSet\\Control\\Session Manager\\Environment\” /v PATH’) DO PATH %%a%%b\n%*”

}

},

“commands” : {

“1-install-chocolatey” : {

“command” : “powershell -NoProfile -ExecutionPolicy unrestricted -Command \”Invoke-Expression ((New-Object Net.WebClient).DownloadString(‘https://chocolatey.org/install.ps1’))\””

},

“2-install-firefox” : {

“command” : “c:\\tools\\ewmp choco install firefox”

}

}

}

}

 

Using AWS Elastic Beanstalk:

AWS Elastic Beanstalk supports the downloading of files and execution of commands on instance creation using container customization. We can leverage this feature to install Chocolatey.

The aforementioned installation can get translated into AWS Elastic Beanstalk config files to enable use of Chocolatey in Elastic Beanstalk. The change while doing using Elastic Beanstalk; we will create YAML .config files inside the .ebextensions folder of our source bundle.

files:

c:/tools/ewmp.cmd:

content: |

@ECHO OFF

FOR /F “tokens=3,*” %%a IN (‘REG QUERY “HKLM\System\CurrentControlSet\Control\Session Manager\Environment” /v PATH’) DO PATH %%a%%b

%*

commands:

1-install-chocolatey:

command: powershell -NoProfile -ExecutionPolicy unrestricted -Command “Invoke-Expression ((New-Object Net.WebClient).DownloadString(‘https://chocolatey.org/install.ps1’))”

2-install-firefox:

command: c:\tools\ewmp choco install firefox

 

Above will work in the same way as cloud formation sample did , it will Create a command file ‘ewmp.cmd’ to execute a command with the machine’s PATH before installing Chocolatey and Firefox.

 

P.S. Chocolatey can best be used as package provider for puppet on windows. Puppet offers great support in promotion and execution of Chocolatey on Windows. 

 

 

Credits – Utkarsh Pandey

 

  • April 19, 2016
  • blog