From 93815a9e4a4a953239d10eafada533aee16e9038 Mon Sep 17 00:00:00 2001 From: Nate Mueller Date: Wed, 27 Jul 2011 19:28:23 -0700 Subject: Add an identity param, respected by the git provider, to use a non-standard ssh identity file --- lib/puppet/provider/vcsrepo/git.rb | 70 +++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 24 deletions(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 8de1313..4fbb534 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -6,7 +6,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) ##TODO modify the commands below so that the su - is included commands :git => 'git' defaultfor :git => :exists - has_features :bare_repositories, :reference_tracking + has_features :bare_repositories, :reference_tracking, :ssh_identity def create if !@resource.value(:source) @@ -50,11 +50,11 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) def revision update_references - current = at_path { git('rev-parse', 'HEAD').chomp } + current = at_path { git_with_identity('rev-parse', 'HEAD').chomp } if tag_revision?(@resource.value(:revision)) - canonical = at_path { git('show', @resource.value(:revision)).scan(/commit (.*)/).to_s } + canonical = at_path { git_with_identity('show', @resource.value(:revision)).scan(/commit (.*)/).to_s } else - canonical = at_path { git('rev-parse', @resource.value(:revision)).chomp } + canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).chomp } end if current == canonical @@ -71,7 +71,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) # authoritative. # might be worthwhile to have an allow_local_changes param to decide # whether to reset or pull when we're ensuring latest. - at_path { git('reset', '--hard', "origin/#{desired}") } + at_path { git_with_identity('reset', '--hard', "origin/#{desired}") } end if @resource.value(:ensure) != :bare update_submodules @@ -93,7 +93,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) def update_references at_path do - git('fetch', '--tags', 'origin') + git_with_identity('fetch', '--tags', 'origin') end end @@ -111,7 +111,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) end if !File.exist?(File.join(@resource.value(:path), '.git')) args.push(source, path) - git(*args) + git_with_identity(*args) else notice "Repo has already been cloned" end @@ -142,7 +142,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) args << '--bare' end at_path do - git(*args) + git_with_identity(*args) end end end @@ -173,7 +173,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) FileUtils.mv(tempdir, File.join(@resource.value(:path), '.git')) if commits_in?(File.join(@resource.value(:path), '.git')) reset('HEAD') - git('checkout', '-f') + git_with_identity('checkout', '-f') update_owner_and_excludes end end @@ -189,24 +189,24 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) if local_branch_revision? reset(revision) elsif tag_revision? - at_path { git('checkout', revision) } + at_path { git_with_identity('checkout', revision) } elsif remote_branch_revision? - at_path { git('checkout', '-b', revision, '--track', "origin/#{revision}") } + at_path { git_with_identity('checkout', '-b', revision, '--track', "origin/#{revision}") } end end def reset(desired) at_path do - git('reset', '--hard', desired) + git_with_identity('reset', '--hard', desired) end end def update_submodules at_path do - git('submodule', 'init') - git('submodule', 'update') - git('submodule', 'foreach', 'git', 'submodule', 'init') - git('submodule', 'foreach', 'git', 'submodule', 'update') + git_with_identity('submodule', 'init') + git_with_identity('submodule', 'update') + git_with_identity('submodule', 'foreach', 'git', 'submodule', 'init') + git_with_identity('submodule', 'foreach', 'git', 'submodule', 'update') end end @@ -228,15 +228,15 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) end def branches - at_path { git('branch', '-a') }.gsub('*', ' ').split(/\n/).map { |line| line.strip } + at_path { git_with_identity('branch', '-a') }.gsub('*', ' ').split(/\n/).map { |line| line.strip } end def on_branch? - at_path { git('branch', '-a') }.split(/\n/).grep(/\*/).to_s.gsub('*', '').strip + at_path { git_with_identity('branch', '-a') }.split(/\n/).grep(/\*/).to_s.gsub('*', '').strip end def tags - at_path { git('tag', '-l') }.split(/\n/).map { |line| line.strip } + at_path { git_with_identity('tag', '-l') }.split(/\n/).map { |line| line.strip } end def set_excludes @@ -248,15 +248,15 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) create end at_path do - git('fetch', 'origin') - git('fetch', '--tags', 'origin') + git_with_identity('fetch', 'origin') + git_with_identity('fetch', '--tags', 'origin') end - current = at_path { git('rev-parse', rev).strip } + current = at_path { git_with_identity('rev-parse', rev).strip } if @resource.value(:revision) if local_branch_revision? - canonical = at_path { git('rev-parse', @resource.value(:revision)).strip } + canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).strip } elsif remote_branch_revision? - canonical = at_path { git('rev-parse', 'origin/' + @resource.value(:revision)).strip } + canonical = at_path { git_with_identity('rev-parse', 'origin/' + @resource.value(:revision)).strip } end current = @resource.value(:revision) if current == canonical end @@ -271,4 +271,26 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) set_excludes end end + + def git_with_identity(*args) + if @resource.value(:identity) + Tempfile.open('git-helper') do |f| + f.puts '#!/bin/sh' + f.puts "exec ssh -i #{@resource.value(:identity)} $*" + f.close + + FileUtils.chmod(0755, f.path) + env_save = ENV['GIT_SSH'] + ENV['GIT_SSH'] = f.path + + ret = git(*args) + + ENV['GIT_SSH'] = env_save + + return ret + end + else + git(*args) + end + end end -- cgit v1.2.3 From 16e2e1efc2cc41f603cf499394d3c117a301a078 Mon Sep 17 00:00:00 2001 From: Nate Mueller Date: Wed, 27 Jul 2011 19:52:03 -0700 Subject: Fixed an issue with 8e51aebd4cf77c7d68ee that broke git when you didn't specify a revision --- lib/puppet/provider/vcsrepo/git.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 4fbb534..e624b49 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -51,6 +51,8 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) def revision update_references current = at_path { git_with_identity('rev-parse', 'HEAD').chomp } + return current unless @resource.value(:revision) + if tag_revision?(@resource.value(:revision)) canonical = at_path { git_with_identity('show', @resource.value(:revision)).scan(/commit (.*)/).to_s } else -- cgit v1.2.3 From 26308908c72f57d2c8f537f8f761d9d2f6fa739b Mon Sep 17 00:00:00 2001 From: Nate Mueller Date: Wed, 27 Jul 2011 19:52:36 -0700 Subject: Updated for ruby 1.9. Not sure how important this actually is. Works on both now. --- lib/puppet/provider/vcsrepo/git.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/provider/vcsrepo/git.rb') diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index e624b49..6756fc0 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -234,7 +234,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) end def on_branch? - at_path { git_with_identity('branch', '-a') }.split(/\n/).grep(/\*/).to_s.gsub('*', '').strip + at_path { git_with_identity('branch', '-a') }.split(/\n/).grep(/\*/).first.to_s.gsub('*', '').strip end def tags @@ -260,7 +260,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) elsif remote_branch_revision? canonical = at_path { git_with_identity('rev-parse', 'origin/' + @resource.value(:revision)).strip } end - current = @resource.value(:revision) if current == canonical + current = @resource.value(:revision) if current == canonical end return current end -- cgit v1.2.3