Permalink
Browse files

Initial Write

This module will use the mcollective module to install an MC agent for pushing code updates to puppetmasters
  • Loading branch information...
1 parent 2f93546 commit 973257b22757b3b80d547d2905ef16d97ce54309 @ccaum ccaum committed Sep 13, 2011
Showing with 205 additions and 0 deletions.
  1. +118 −0 ext/commit-hook
  2. +30 −0 files/agent/puppetupdate.ddl
  3. +41 −0 files/agent/puppetupdate.rb
  4. +7 −0 manifests/init.pp
  5. +9 −0 tests/init.pp
View
@@ -0,0 +1,118 @@
+#!/usr/bin/env ruby
+
+begin
+ require 'mcollective'
+rescue LoadError => e
+ puts "Please install MCollective on subversion server"
+end
+
+include MCollective::RPC
+
+def update_master(environment)
+ #Activate our puppet agent
+ mc = rpcclient('puppetupdate')
+ mc.class_filter /puppetmaster/
+ mc.update :environment => environment, :rcs => 'svn'
+end
+
+GOLD_REGEX = /puppet\/tags\/GOLD/
+TRUNK_REGEX = /puppet\/trunk/
+MANIFEST_REGEX = /\.pp$/
+
+class CommitCmd
+ attr_reader :status, :output, :cmd
+
+ def initialize(cmd)
+ @cmd = cmd
+ @status = nil
+ end
+
+ def run
+ @output = `#{@cmd} 2>&1`
+ @status = $?
+ end
+end
+
+repos = ARGV[0]
+txn = ARGV[1]
+
+if repos.nil? or txn.nil?
+ puts "Invalid commands"
+ exit(1)
+end
+
+filelistcmd = CommitCmd.new("/usr/bin/svnlook changed -t #{txn} #{repos}")
+filelistcmd.run
+
+if filelistcmd.status.exitstatus != 0
+ puts "Error getting change list:"
+ puts filelistcmd.output
+ exit(1)
+end
+
+errors = []
+
+workdir = "/tmp/puppet.commit.#{$$}"
+Dir.mkdir(workdir)
+ENV["HOME"] = workdir
+ENV["RUBYLIB"] = "/path/to/libdir"
+
+filestocleanup = []
+filelistcmd.output.split("\n").each do |l|
+ op = l[0..3].strip
+ f = l[4..-1].strip
+ next if not ["U", "A"].include?(op)
+ next if TRUNK_REGEX !~ l
+ next if MANIFEST_REGEX !~ l
+ wf = f.gsub("/", ".")
+ filestocleanup << wf
+ filecatcmd = CommitCmd.new("/usr/bin/svnlook cat -t #{txn} #{repos} #{f} > #{workdir}/#{wf}")
+ filecatcmd.run
+ if filecatcmd.status.exitstatus != 0
+ errors << "Error reading #{f}:\n #{filecatcmd.output.gsub("\n", "\n ")}"
+ next
+ end
+
+ puppetcmd = CommitCmd.new("/usr/scea/puppet/bin/puppet --noop --color=false --parseonly --ignoreimport #{workdir}/#{wf}")
+ puppetcmd.run
+ if puppetcmd.status.exitstatus != 0
+ errors << "Puppet errors in #{f}:\n #{puppetcmd.output.gsub("\n", "\n ")}"
+ next
+ end
+
+end
+
+ # cleanup
+ begin
+ filestocleanup.each do |f|
+ File.unlink("#{workdir}/#{f}")
+ end
+ Dir.rmdir(workdir)
+ rescue StandardError => e
+ puts "Error cleaning up: #{e}"
+end
+
+# print errors
+if not errors.empty?
+ puts
+ errors.each do |e|
+ puts e
+ end
+ exit(errors.length)
+end
+
+#Update puppet masters if necessary
+env_array = Array.new
+filelistcmd.output.split("\n").each do |l|
+ if (TRUNK_EXP =~ l) and (! env_array.include? 'development')
+ env_array << 'development'
+ end
+
+ if (GOLD_EXP =~ l) and (! env_array.include? 'production')
+ env_array << 'production'
+ end
+
+ env_array.each { |env| update_master env }
+end
+
+exit(0)
@@ -0,0 +1,30 @@
+metadata :name => "Puppet Update",
+ :description => "Agent To Update SVN checkouts on puppet masters",
+ :author => "Carl Caum",
+ :license => "GPLv2",
+ :version => "1.0",
+ :url => "http://puppetlabs.com",
+ :timeout => 10
+
+action update, :description => "Update Environment on puppet master" do
+ input :rcs,
+ :prompt => "RCS",
+ :description => "Revision Control System (git or svn)",
+ :type => :string,
+ :validation => /^svn$|^git$/,
+ :option => false,
+ :maxlength => 90
+
+ input :environment,
+ :prompt => "Environment",
+ :description => "Which environment to update",
+ :type => :string,
+ :validation => '.',
+ :optional => false,
+ :maxlength => 90
+
+ output :output,
+ :description => "output",
+ :display_as => "Output"
+
+end
@@ -0,0 +1,41 @@
+module MCollective
+ module Agent
+ # An agent that uses Reductive Labs Puppet to manage packages
+ #
+ # See http://code.google.com/p/mcollective-plugins/
+ #
+ # Released under the terms of the GPL, same as Puppet
+ class Puppetupdate<RPC::Agent
+ metadata :name => "Puppet Update",
+ :description => "Agent To Update SVN checkouts on puppet masters",
+ :author => "Carl Caum",
+ :license => "GPLv2",
+ :version => "1.0",
+ :url => "http://puppetlabs.com",
+ :timeout => 10
+
+ action 'update' do
+ begin
+ require 'puppet'
+ rescue LoadError => e
+ reply[:error] = "Cannot load Puppet"
+ end
+
+ case request[:rcs]
+ when 'svn'
+ command = "svn update"
+ when 'git'
+ command = "git pull"
+ else
+ reply[:error] = "Unknown RCS #{request[:rcs]}"
+ end
+
+ Puppet[:config] = '/etc/puppet/puppet.conf'
+ Puppet.parse_config
+ Puppet::Node::Environment.new(request[:environment])[:modulepath].split(':').each do |path|
+ run "#{command} #{path}"
+ end
+ end
+ end
+ end
+end
View
@@ -0,0 +1,7 @@
+class mcmasterupdate {
+ mcollective::plugins::plugin { 'puppetupdate':
+ ddl => true,
+ type => 'agent',
+ module_source => 'puppet:///modules/mcmasterupdate',
+ }
+}
View
@@ -0,0 +1,9 @@
+class { 'mcollective':
+ mc_security_psk => 'abc123',
+ client => true,
+ stomp_port => 6163,
+ #stomp_server => 'ec2-107-20-16-247.compute-1.amazonaws.com',
+ manage_packages => true,
+}
+
+include mcmasterupdate

0 comments on commit 973257b

Please sign in to comment.