bug 1458329: add support for puppet manifests in a git repo. r=aki
authorBen Hearsum <bhearsum@mozilla.com>
Mon, 04 Jun 2018 10:26:26 -0400
changeset 6612 e2bbbba1e62504794fba51e90876bfc0fbb7eb2b
parent 6610 b1b3c1999b372267c1068194024c9e8116001e14
child 6613 4baa0eea5074a91092912de2486c22c59b7a2832
child 6615 cbdb17db2fa78db6595305a7c39825b504d2793a
push id5319
push userbhearsum@mozilla.com
push dateMon, 04 Jun 2018 14:26:46 +0000
reviewersaki
bugs1458329
bug 1458329: add support for puppet manifests in a git repo. r=aki
manifests/moco-config.pp
modules/config/manifests/base.pp
modules/puppetmaster/manifests/manifests.pp
modules/puppetmaster/templates/update.sh.erb
--- a/manifests/moco-config.pp
+++ b/manifests/moco-config.pp
@@ -98,17 +98,17 @@ class config inherits config::base {
         default => 'unknown',
     }
 
     # this is a round-robin DNS containing all of the moco puppet masters.  This is the
     # only way to communicate to apt that the masters are all mirrors of one another.
     # See https://bugzilla.mozilla.org/show_bug.cgi?id=906785
     $apt_repo_server            = 'puppetagain-apt.pvt.build.mozilla.org'
     $distinguished_puppetmaster = 'releng-puppet2.srv.releng.scl3.mozilla.com'
-    $puppet_again_repo          = 'https://hg.mozilla.org/build/puppet/'
+    $puppet_again_repo          = 'https://github.com/mozilla/build-puppet'
     $puppetmaster_extsyncs      = {
         'slavealloc' => {
             'slavealloc_api_url' => 'http://slavealloc.pvt.build.mozilla.org/api/',
         },
         'moco_ldap' => {
             'moco_ldap_uri'   => $::fqdn ? {
                         /.*\.mdc1\.mozilla\.com/             => 'ldap://ldap-slave.vips.private.mdc1.mozilla.com',
                         /.*\.mdc2\.mozilla\.com/             => 'ldap://ldap-slave.vips.private.mdc2.mozilla.com',
--- a/modules/config/manifests/base.pp
+++ b/modules/config/manifests/base.pp
@@ -20,18 +20,18 @@ class config::base {
     # servers to try if that one fails.  This can either name a single puppetmaster, use a DNS
     # alias ('puppet'), or some complex function to determine the correct puppetmaster.  See
     # manifests/moco-config.pp for an advanced usage.
     $puppet_server                  = 'puppet'
     $puppet_servers                 = [$puppet_server]
 
     # The repository and branch that puppetmasters should follow to get the latest
     # manifests
-    $puppet_again_repo              = 'https://hg.mozilla.org/build/puppet'
-    $puppet_again_branch            = 'production'
+    $puppet_again_repo              = 'https://github.com/mozilla/build-puppet'
+    $puppet_again_branch            = 'master'
 
     # The fqdn of the 'distinguished' puppetmaster.  This master serves as the
     # hub in the hub-and-spoke architecture for synchronizing masters and also
     # handles a number of single-host crontasks.  Its failure will not stop
     # puppet jobs from being correctly processed by other masters, but will
     # temporarily halt synchronizations.  There is no default - set this to the
     # hostname of your master.
     $distinguished_puppetmaster     = ''
--- a/modules/puppetmaster/manifests/manifests.pp
+++ b/modules/puppetmaster/manifests/manifests.pp
@@ -9,17 +9,17 @@ class puppetmaster::manifests {
 
     $puppetsync_home      = $puppetmaster::settings::puppetsync_home
     $distinguished_master = $puppetmaster::settings::distinguished_master
 
     # check out the manifests to begin with
     $checkout_dir = '/etc/puppet/production'
     exec {
         'checkout-puppet':
-            command   => "/usr/bin/hg clone ${puppetmaster::settings::manifests_repo} ${checkout_dir}",
+            command   => "/usr/bin/git clone ${puppetmaster::settings::manifests_repo} ${checkout_dir}",
             creates   => $checkout_dir,
             logoutput => on_failure,
             require   => Class['packages::mercurial'];
     }
 
     # and create environment.conf, containing a long(ish) environment timeout
     file {
         "${checkout_dir}/environment.conf":
@@ -29,22 +29,16 @@ class puppetmaster::manifests {
 
     # update the manifests regularly
     file {
         '/etc/puppet/update.sh':
             mode    => '0755',
             owner   => root,
             group   => root,
             content => template('puppetmaster/update.sh.erb');
-        "${checkout_dir}/.hg/hgrc":
-            mode    => '0755',
-            owner   => root,
-            group   => root,
-            content => "[paths]\ndefault = ${puppetmaster::settings::manifests_repo}\n",
-            require => Exec['checkout-puppet'];
         '/etc/puppet/get_rev.sh':
             mode    => '0755',
             owner   => root,
             group   => root,
             content => template('puppetmaster/get_rev.sh.erb');
         '/etc/cron.d/puppetmaster-update.cron':
             content => template('puppetmaster/puppetmaster-update.cron.erb');
     }
--- a/modules/puppetmaster/templates/update.sh.erb
+++ b/modules/puppetmaster/templates/update.sh.erb
@@ -5,77 +5,111 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 
 
 errormailto="<%= scope.lookupvar('::config::puppet_notif_email') %>"
 changemailto="<%= scope.lookupvar('::config::puppet_notif_email') %>"
 repo="<%= scope.lookupvar('::config::puppet_again_repo') %>"
 branch="<%= scope.lookupvar('::config::puppet_again_branch') %>"
 repodir="/etc/puppet/production"
 hg="/usr/local/bin/hg"
+git="/usr/bin/git"
 
 hostname=<%= scope.lookupvar('::fqdn') %>
 cd $repodir
 
-# check for uncommitted changes
-output=`$hg stat`
+output=""
+if [ -e ".hg" ]; then
+    # check for uncommitted changes
+    output=`$hg stat`
+elif [ -e ".git" ]; then
+    output=`$git status -s`
+fi
+
 if [ -n "$output" ]; then
     (
         echo "Uncommitted local changes to $hostname:$repodir!"
         echo ''
         echo $output
     ) | mail -s "[PuppetAgain Errors] Uncommitted local changes in $hostname:$repodir" $errormailto
     exit 1
 fi
 
 try_update() {
-    rev_before=`$hg ident -i`
-    rev_current=`$hg ident -i $repo -r $branch`
-    if [ $? -ne 0 ] || [ -z "$rev_current" ]; then
+    if [ -e ".hg" ]; then
+        rev_before=`$hg ident -i`
+        rev_current=`$hg ident -i $repo -r $branch`
+        if [ $? -ne 0 ] || [ -z "$rev_current" ]; then
+            return 1
+        fi
+
+        if [ $rev_before == $rev_current ]; then
+            # nothing to do
+            return 0
+        fi
+
+        # update (and pass on the exit status)
+        # (hg pull can't do -u and -r at the same time)
+        $hg pull $repo && $hg up -r $branch
+    elif [ -e ".git" ]; then
+        $git fetch origin
+        rev_before=`$git rev-parse HEAD`
+        rev_current=`$git rev-parse "origin/$branch"`
+        if [ -z "$rev_current" ]; then
+            return 1
+        fi
+
+        if [ $rev_before == $rev_current ]; then
+            # nothing to do
+            return 0
+        fi
+
+        # update (and pass on the exit status)
+        $git checkout "origin/$branch"
+    else
         return 1
     fi
-
-    if [ $rev_before == $rev_current ]; then
-        # nothing to do
-        return 0
-    fi
-
-    # update (and pass on the exit status)
-    # (hg pull can't do -u and -r at the same time)
-    $hg pull $repo && $hg up -r $branch
 }
 
-
 # retry that five times
 LOGFILE=$(mktemp)
 ok=false
 for try in {1..5}; do
     if try_update >> $LOGFILE 2>&1; then
         ok=true
         break
     else
         # don't sleep too long, or we'll overlap the next crontask
         sleep 1
     fi
     echo '' >> $LOGFILE
 done
 
 if ! $ok; then
     (
-        echo "Errors pulling from mercurial for puppet on $hostname:"
+        echo "Errors pulling from vcs for puppet on $hostname:"
         echo ''
         cat $LOGFILE
-    ) | mail -s "[PuppetAgain Errors] Errors pulling from mercurial for puppet on $hostname" $errormailto
+    ) | mail -s "[PuppetAgain Errors] Errors pulling from vcs for puppet on $hostname" $errormailto
 else
     # get a new rev, in case something landed during this run
-    rev_after=`$hg ident -i`
+    rev_after=""
+    if [ -e ".hg" ]; then
+        rev_after=`$hg ident -i`
+    elif [ -e ".git" ]; then
+        rev_after=`$git rev-parse HEAD`
+    fi
     if [ $rev_before != $rev_after ]; then
         (
             echo "Puppet changes applied at $hostname:"
             echo ''
-            $hg diff -r $rev_before -r $rev_after
+            if [ -e ".hg" ]; then
+                $hg diff -r $rev_before -r $rev_after
+            elif [ -e ".git" ]; then
+                $git diff -r $rev_before -r $rev_after
+            fi
         ) | mail -s "[PuppetAgain Changes] Puppet changes applied at $hostname" $changemailto
 
         # reload httpd, to flush puppet's caches
         /sbin/service httpd reload >/dev/null 2>&1
     fi
 fi
 
 rm -f $LOGFILE