Marcus Young

Software. Microcontrollers. Beer.

Automatically Deploy GIT Code to Windows With Puppet

| Comments

Recently I’ve been looking for ways of keeping a box up-to-date as far as git is concerned, and for windows the options are pretty limited. The vast majority of DevOps seem to like Puppet, so I decided to give it a try. The problem is windows isn’t very good without a few helper programs such as ssh, but we’ll get around that with Cygwin. The master box will be a Centos 6.4 x64 host.

Puppet Master

The first thing we’ll need to do is install the EPEL and Puppet repositories with, then install the puppet software and start the master process:

1
2
3
# rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# rpm -Uvh http://yum.puppetlabs.com/el/6/products/x86_64/puppetlabs-release-6-7.noarch.rpm
# sudo yum install -y puppet-server && chkconfig puppetmaster on && service puppetmaster start

Let’s go ahead and get some files prepared. Create a git repo in /opt called test with a file in it (test.txt), committed to master:

1
2
3
4
5
6
7
$ cd /opt
$ sudo mkdir test && chmod 766 test
$ cd test
$ git init
$ touch test.txt
$ git add test.txt
$ git commit -a -m "Adding empty file"

One last thing we’ll need for puppet to work with git is VCSRepo, so let’s get that:

1
# puppet module install puppetlabs/vcsrepo

Now that puppet is ready to serve it out, we have to create some files.

  • /etc/puppet/manifests/site.pp
1
2
3
node 'server-vm.blindrage.ad' {
  include git
}}
  • /etc/puppet/modules/git/manifests/init.pp
1
2
3
class git {
  class { git::clone: }
}
  • /etc/puppet/modules/git/manifests/clone.pp
1
2
3
4
5
6
7
8
9
class git::clone ($repo='dev', $username='marcus.young') {
  vcsrepo { "C:/${repo}":
    ensure   => latest,
    owner    => $owner,
    provider => git,
    source   => "git+ssh://${username}@foreman/~/repos/test",
    revision => $repo,
  }
}

Puppet Client

The first two steps are going to be to install cygwin, making sure to add git and openssh, then the puppet client software (making sure to point to your linux host as the master).

When this is done, stop the windows puppet service and make sure it is not set for startup. We’re going to control this process with a custom task schedule.

Next, add the path to your cygwin bin folder to the PATH environment variable. If you don’t know how, it’s probably something similar to C:\cygwin\bin, and you’ll add it according to this guide. You’re close now!

Using cygwin, generate a public/private key pair (if using git+ssh) without a password and give the public key to the master box.

If you’ve done it right, you can clone the repo in cygwin without a password using this line:

1
$ git clone git+ssh://{username}@{master}/opt/test

The last step is to generate a cert and sign it.

From the client:

1
puppet agent -t --waitforcert 60

Then on the master:

1
# puppet cert sign --all

The client should now finish and download your git code! Congrats!

However, this isn’t automagic yet. To do that, create a scheduled task that runs every 2 minutes (or whenever) that runs a daemon file.
The easiest way is to create C:\Program Files\Puppet Labs\Puppet\bin\puppet_daemon.bat which contains:

1
2
3
4
@echo off
echo Running Puppet agent on demand ...
cd "%~dp0"
call puppet.bat agent --test %*

Congrats. You’re done! To test, make a change to the git repo and wait two minutes!

Comments