summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/provider/vcsrepo/git.rb170
-rw-r--r--lib/puppet/provider/vcsrepo/hg.rb12
-rw-r--r--lib/puppet/provider/vcsrepo/p4.rb278
-rw-r--r--lib/puppet/type/vcsrepo.rb20
4 files changed, 403 insertions, 77 deletions
diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb
index 5c878ed..0a142bb 100644
--- a/lib/puppet/provider/vcsrepo/git.rb
+++ b/lib/puppet/provider/vcsrepo/git.rb
@@ -31,67 +31,49 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo)
FileUtils.rm_rf(@resource.value(:path))
end
+ # Checks to see if the current revision is equal to the revision on the
+ # remote (whether on a branch, tag, or reference)
+ #
+ # @return [Boolean] Returns true if the repo is on the latest revision
def latest?
- at_path do
- return self.revision == self.latest
- end
+ return revision == latest_revision
end
+ # Just gives the `should` value that we should be setting the repo to if
+ # latest? returns false
+ #
+ # @return [String] Returns the target sha/tag/branch
def latest
- branch = on_branch?
- if !branch
- return get_revision('HEAD')
- else
- return get_revision("#{@resource.value(:remote)}/#{branch}")
- end
+ @resource.value(:revision)
end
+ # Get the current revision of the repo (tag/branch/sha)
+ #
+ # @return [String] Returns the branch/tag if the current sha matches the
+ # remote; otherwise returns the current sha.
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('rev-parse', @resource.value(:revision)).chomp }
- else
- # if it's not a tag, look for it as a local ref
- canonical = at_path { git_with_identity('rev-parse', '--revs-only', @resource.value(:revision)).chomp }
- if canonical.empty?
- # git rev-parse executed properly but didn't find the ref;
- # look for it in the remote
- remote_ref = at_path { git_with_identity('ls-remote', '--heads', '--tags', @resource.value(:remote), @resource.value(:revision)).chomp }
- if remote_ref.empty?
- fail("#{@resource.value(:revision)} is not a local or remote ref")
- end
-
- # $ git ls-remote --heads --tags origin feature/cvs
- # 7d4244b35e72904e30130cad6d2258f901c16f1a refs/heads/feature/cvs
- canonical = remote_ref.split.first
- end
- end
-
- if current == canonical
- @resource.value(:revision)
- else
- current
- end
+ #HEAD is the default, but lets just be explicit here.
+ get_revision('HEAD')
end
+ # Is passed the desired reference, whether a tag, rev, or branch. Should
+ # handle transitions from a rev/branch/tag to a rev/branch/tag. Detached
+ # heads should be treated like bare revisions.
+ #
+ # @param [String] desired The desired revision to which the repo should be
+ # set.
def revision=(desired)
+ #just checkout tags and shas; fetch has already happened so they should be updated.
checkout(desired)
- if local_branch_revision?
- # reset instead of pull to avoid merge conflicts. assuming remote is
- # 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_with_identity('reset', '--hard', "#{@resource.value(:remote)}/#{desired}")
- if detached?
- git_with_identity('checkout', "#{@resource.value(:revision)}")
- git_with_identity('pull')
- end
- }
+ #branches require more work.
+ if local_branch_revision?(desired)
+ #reset instead of pull to avoid merge conflicts. assuming remote is
+ #updated and authoritative.
+ #TODO 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_with_identity('reset', '--hard', "#{@resource.value(:remote)}/#{desired}") }
end
+ #TODO Would this ever reach here if it is bare?
if @resource.value(:ensure) != :bare
update_submodules
end
@@ -130,10 +112,12 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo)
private
+ # @!visibility private
def bare_git_config_exists?
File.exist?(File.join(@resource.value(:path), 'config'))
end
+ # @!visibility private
def clone_repository(source, path)
check_force
args = ['clone']
@@ -156,6 +140,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo)
end
end
+ # @!visibility private
def check_force
if path_exists? and not path_empty?
if @resource.value(:force)
@@ -167,6 +152,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo)
end
end
+ # @!visibility private
def init_repository(path)
check_force
if @resource.value(:ensure) == :bare && working_copy_exists?
@@ -193,6 +179,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo)
# <path>/.git
# to:
# <path>/
+ # @!visibility private
def convert_working_copy_to_bare
notice "Converting working copy repository to bare repository"
FileUtils.mv(File.join(@resource.value(:path), '.git'), tempdir)
@@ -206,6 +193,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo)
# <path>/
# to:
# <path>/.git
+ # @!visibility private
def convert_bare_to_working_copy
notice "Converting bare repository to working copy repository"
FileUtils.mv(@resource.value(:path), tempdir)
@@ -218,6 +206,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo)
end
end
+ # @!visibility private
def commits_in?(dot_git)
Dir.glob(File.join(dot_git, 'objects/info/*'), File::FNM_DOTMATCH) do |e|
return true unless %w(. ..).include?(File::basename(e))
@@ -225,26 +214,35 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo)
false
end
+ # Will checkout a rev/branch/tag using the locally cached versions. Does not
+ # handle upstream branch changes
+ # @!visibility private
def checkout(revision = @resource.value(:revision))
if !local_branch_revision? && remote_branch_revision?
- at_path { git_with_identity('checkout', '-b', revision, '--track', "#{@resource.value(:remote)}/#{revision}") }
+ #non-locally existant branches (perhaps switching to a branch that has never been checked out)
+ at_path { git_with_identity('checkout', '--force', '-b', revision, '--track', "#{@resource.value(:remote)}/#{revision}") }
else
+ #tags, locally existant branches (perhaps outdated), and shas
at_path { git_with_identity('checkout', '--force', revision) }
end
end
+ # @!visibility private
def reset(desired)
at_path do
git_with_identity('reset', '--hard', desired)
end
end
+ # @!visibility private
def update_submodules
at_path do
git_with_identity('submodule', 'update', '--init', '--recursive')
end
end
+ # Determins if the branch exists at the upstream but has not yet been locally committed
+ # @!visibility private
def remote_branch_revision?(revision = @resource.value(:revision))
# git < 1.6 returns '#{@resource.value(:remote)}/#{revision}'
# git 1.6+ returns 'remotes/#{@resource.value(:remote)}/#{revision}'
@@ -252,64 +250,93 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo)
branch unless branch.empty?
end
+ # Determins if the branch is already cached locally
+ # @!visibility private
def local_branch_revision?(revision = @resource.value(:revision))
at_path { branches.include?(revision) }
end
+ # @!visibility private
def tag_revision?(revision = @resource.value(:revision))
at_path { tags.include?(revision) }
end
+ # @!visibility private
def branches
at_path { git_with_identity('branch', '-a') }.gsub('*', ' ').split(/\n/).map { |line| line.strip }
end
+ # @!visibility private
def on_branch?
at_path {
matches = git_with_identity('branch', '-a').match /\*\s+(.*)/
- matches[1] unless matches[1].match /detached/
- }
- end
-
- def detached?
- at_path {
- git_with_identity('branch', '-a').match /\*\s+\(detached from.*\)/
+ matches[1] unless matches[1].match /(\(detached from|\(no branch)/
}
end
+ # @!visibility private
def tags
at_path { git_with_identity('tag', '-l') }.split(/\n/).map { |line| line.strip }
end
+ # @!visibility private
def set_excludes
at_path { open('.git/info/exclude', 'w') { |f| @resource.value(:excludes).each { |ex| f.write(ex + "\n") }}}
end
- def get_revision(rev)
- if @resource.value(:force) && working_copy_exists?
- create
- end
- if !working_copy_exists?
- create
- end
- at_path do
- update_remote_origin_url
- git_with_identity('fetch', @resource.value(:remote))
- git_with_identity('fetch', '--tags', @resource.value(:remote))
+ # Finds the latest revision or sha of the current branch if on a branch, or
+ # of HEAD otherwise.
+ # @note Calls create which can forcibly destroy and re-clone the repo if
+ # force => true
+ # @see get_revision
+ #
+ # @!visibility private
+ # @return [String] Returns the output of get_revision
+ def latest_revision
+ #TODO Why is create called here anyway?
+ create if @resource.value(:force) && working_copy_exists?
+ create if !working_copy_exists?
+
+ if branch = on_branch?
+ return get_revision("#{@resource.value(:remote)}/#{branch}")
+ else
+ return get_revision
end
+ end
+
+ # Returns the current revision given if the revision is a tag or branch and
+ # matches the current sha. If the current sha does not match the sha of a tag
+ # or branch, then it will just return the sha (ie, is not in sync)
+ #
+ # @!visibility private
+ #
+ # @param [String] rev The revision of which to check if it is current
+ # @return [String] Returns the tag/branch of the current repo if it's up to
+ # date; otherwise returns the sha of the requested revision.
+ def get_revision(rev = 'HEAD')
+ update_references
current = at_path { git_with_identity('rev-parse', rev).strip }
if @resource.value(:revision)
- if local_branch_revision?
+ if tag_revision?
+ # git-rev-parse will give you the hash of the tag object itself rather
+ # than the commit it points to by default. Using tag^0 will return the
+ # actual commit.
+ canonical = at_path { git_with_identity('rev-parse', "#{@resource.value(:revision)}^0").strip }
+ elsif local_branch_revision?
canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).strip }
elsif remote_branch_revision?
- canonical = at_path { git_with_identity('rev-parse', "#{@resource.value(:remote)}/" + @resource.value(:revision)).strip }
+ canonical = at_path { git_with_identity('rev-parse', "#{@resource.value(:remote)}/#{@resource.value(:revision)}").strip }
+ else
+ #look for a sha (could match invalid shas)
+ canonical = at_path { git_with_identity('rev-parse', '--revs-only', @resource.value(:revision)).strip }
end
+ fail("#{@resource.value(:revision)} is not a local or remote ref") if canonical.nil? or canonical.empty?
current = @resource.value(:revision) if current == canonical
end
- update_owner_and_excludes
return current
end
+ # @!visibility private
def update_owner_and_excludes
if @resource.value(:owner) or @resource.value(:group)
set_ownership
@@ -319,6 +346,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo)
end
end
+ # @!visibility private
def git_with_identity(*args)
if @resource.value(:identity)
Tempfile.open('git-helper') do |f|
diff --git a/lib/puppet/provider/vcsrepo/hg.rb b/lib/puppet/provider/vcsrepo/hg.rb
index 56ca527..090f019 100644
--- a/lib/puppet/provider/vcsrepo/hg.rb
+++ b/lib/puppet/provider/vcsrepo/hg.rb
@@ -6,7 +6,7 @@ Puppet::Type.type(:vcsrepo).provide(:hg, :parent => Puppet::Provider::Vcsrepo) d
commands :hg => 'hg'
optional_commands :su => 'su'
- has_features :reference_tracking, :ssh_identity, :user
+ has_features :reference_tracking, :ssh_identity, :user, :basic_auth
def create
if !@resource.value(:source)
@@ -108,6 +108,16 @@ Puppet::Type.type(:vcsrepo).provide(:hg, :parent => Puppet::Provider::Vcsrepo) d
if args.length > 0 and args[-1].is_a? Hash
options.merge!(args.pop)
end
+
+ if @resource.value(:basic_auth_username) && @resource.value(:basic_auth_password)
+ args += [
+ "--config", "\"auth.x.prefix=#{@resource.value(:source)}\"",
+ "--config", "\"auth.x.username=#{@resource.value(:basic_auth_username)}\"",
+ "--config", "\"auth.x.password=#{@resource.value(:basic_auth_password)}\"",
+ "--config", "\"auth.x.schemes=http https\""
+ ]
+ end
+
if options[:remote] and @resource.value(:identity)
args += ["--ssh", "ssh -oStrictHostKeyChecking=no -oPasswordAuthentication=no -oKbdInteractiveAuthentication=no -oChallengeResponseAuthentication=no -i #{@resource.value(:identity)}"]
end
diff --git a/lib/puppet/provider/vcsrepo/p4.rb b/lib/puppet/provider/vcsrepo/p4.rb
new file mode 100644
index 0000000..4f53415
--- /dev/null
+++ b/lib/puppet/provider/vcsrepo/p4.rb
@@ -0,0 +1,278 @@
+require File.join(File.dirname(__FILE__), '..', 'vcsrepo')
+
+Puppet::Type.type(:vcsrepo).provide(:p4, :parent => Puppet::Provider::Vcsrepo) do
+ desc "Supports Perforce depots"
+
+ has_features :filesystem_types, :reference_tracking, :p4config
+
+ def create
+ # create or update client
+ create_client(client_name)
+
+ # if source provided, sync client
+ source = @resource.value(:source)
+ if source
+ revision = @resource.value(:revision)
+ sync_client(source, revision)
+ end
+
+ update_owner
+ end
+
+ def working_copy_exists?
+ # Check if the server is there, or raise error
+ p4(['info'], {:marshal => false})
+
+ # Check if workspace is setup
+ args = ['where']
+ args.push(@resource.value(:path) + "...")
+ hash = p4(args, {:raise => false})
+
+ return (hash['code'] != "error")
+ end
+
+ def exists?
+ working_copy_exists?
+ end
+
+ def destroy
+ args = ['client']
+ args.push('-d', '-f')
+ args.push(client_name)
+ p4(args)
+ FileUtils.rm_rf(@resource.value(:path))
+ end
+
+ def latest?
+ rev = self.revision
+ if rev
+ (rev >= self.latest)
+ else
+ true
+ end
+ end
+
+ def latest
+ args = ['changes']
+ args.push('-m1', @resource.value(:source))
+ hash = p4(args)
+
+ return hash['change'].to_i
+ end
+
+ def revision
+ args = ['cstat']
+ args.push(@resource.value(:source))
+ hash = p4(args, {:marshal => false})
+ hash = marshal_cstat(hash)
+
+ revision = 0
+ if hash && hash['code'] != 'error'
+ hash['data'].each do |c|
+ if c['status'] == 'have'
+ change = c['change'].to_i
+ revision = change if change > revision
+ end
+ end
+ end
+ return revision
+ end
+
+ def revision=(desired)
+ sync_client(@resource.value(:source), desired)
+ update_owner
+ end
+
+ private
+
+ def update_owner
+ if @resource.value(:owner) or @resource.value(:group)
+ set_ownership
+ end
+ end
+
+ # Sync the client workspace files to head or specified revision.
+ # Params:
+ # +source+:: Depot path to sync
+ # +revision+:: Perforce change list to sync to (optional)
+ def sync_client(source, revision)
+ Puppet.debug "Syncing: #{source}"
+ args = ['sync']
+ if revision
+ args.push(source + "@#{revision}")
+ else
+ args.push(source)
+ end
+ p4(args)
+ end
+
+ # Returns the name of the Perforce client workspace
+ def client_name
+ p4config = @resource.value(:p4config)
+
+ # default (generated) client name
+ path = @resource.value(:path)
+ host = Facter.value('hostname')
+ default = "puppet-" + Digest::MD5.hexdigest(path + host)
+
+ # check config for client name
+ set_client = nil
+ if p4config && File.file?(p4config)
+ open(p4config) do |f|
+ m = f.grep(/^P4CLIENT=/).pop
+ p = /^P4CLIENT=(.*)$/
+ set_client = p.match(m)[1] if m
+ end
+ end
+
+ return set_client || ENV['P4CLIENT'] || default
+ end
+
+ # Create (or update) a client workspace spec.
+ # If a client name is not provided then a hash based on the path is used.
+ # Params:
+ # +client+:: Name of client workspace
+ # +path+:: The Root location of the Perforce client workspace
+ def create_client(client)
+ Puppet.debug "Creating client: #{client}"
+
+ # fetch client spec
+ hash = parse_client(client)
+ hash['Root'] = @resource.value(:path)
+ hash['Description'] = "Generated by Puppet VCSrepo"
+
+ # check is source is a Stream
+ source = @resource.value(:source)
+ if source
+ parts = source.split(/\//)
+ if parts && parts.length >= 4
+ source = "//" + parts[2] + "/" + parts[3]
+ streams = p4(['streams', source], {:raise => false})
+ if streams['code'] == "stat"
+ hash['Stream'] = streams['Stream']
+ notice "Streams" + streams['Stream'].inspect
+ end
+ end
+ end
+
+ # save client spec
+ save_client(hash)
+ end
+
+
+ # Fetches a client workspace spec from Perforce and returns a hash map representation.
+ # Params:
+ # +client+:: name of the client workspace
+ def parse_client(client)
+ args = ['client']
+ args.push('-o', client)
+ hash = p4(args)
+
+ return hash
+ end
+
+
+ # Saves the client workspace spec from the given hash
+ # Params:
+ # +hash+:: hash map of client spec
+ def save_client(hash)
+ spec = String.new
+ view = "\nView:\n"
+
+ hash.keys.sort.each do |k|
+ v = hash[k]
+ next if( k == "code" )
+ if(k.to_s =~ /View/ )
+ view += "\t#{v}\n"
+ else
+ spec += "#{k.to_s}: #{v.to_s}\n"
+ end
+ end
+ spec += view
+
+ args = ['client']
+ args.push('-i')
+ p4(args, {:input => spec, :marshal => false})
+ end
+
+ # Sets Perforce Configuration environment.
+ # P4CLIENT generated, but overwitten if defined in config.
+ def config
+ p4config = @resource.value(:p4config)
+
+ cfg = Hash.new
+ cfg.store 'P4CONFIG', p4config if p4config
+ cfg.store 'P4CLIENT', client_name
+ return cfg
+ end
+
+ def p4(args, options = {})
+ # Merge custom options with defaults
+ opts = {
+ :raise => true, # Raise errors
+ :marshal => true, # Marshal output
+ }.merge(options)
+
+ cmd = ['p4']
+ cmd.push '-R' if opts[:marshal]
+ cmd.push args
+ cmd_str = cmd.respond_to?(:join) ? cmd.join(' ') : cmd
+
+ Puppet.debug "environment: #{config}"
+ Puppet.debug "command: #{cmd_str}"
+
+ hash = Hash.new
+ Open3.popen3(config, cmd_str) do |i, o, e, t|
+ # Send input stream if provided
+ if(opts[:input])
+ Puppet.debug "input:\n" + opts[:input]
+ i.write opts[:input]
+ i.close
+ end
+
+ if(opts[:marshal])
+ hash = Marshal.load(o)
+ else
+ hash['data'] = o.read
+ end
+
+ # Raise errors, Perforce or Exec
+ if(opts[:raise] && !e.eof && t.value != 0)
+ raise Puppet::Error, "\nP4: #{e.read}"
+ end
+ if(opts[:raise] && hash['code'] == 'error' && t.value != 0)
+ raise Puppet::Error, "\nP4: #{hash['data']}"
+ end
+ end
+
+ Puppet.debug "hash: #{hash}\n"
+ return hash
+ end
+
+ # helper method as cstat does not Marshal
+ def marshal_cstat(hash)
+ data = hash['data']
+ code = 'error'
+
+ list = Array.new
+ change = Hash.new
+ data.each_line do |l|
+ p = /^\.\.\. (.*) (.*)$/
+ m = p.match(l)
+ if m
+ change[m[1]] = m[2]
+ if m[1] == 'status'
+ code = 'stat'
+ list.push change
+ change = Hash.new
+ end
+ end
+ end
+
+ hash = Hash.new
+ hash.store 'code', code
+ hash.store 'data', list
+ return hash
+ end
+
+end
diff --git a/lib/puppet/type/vcsrepo.rb b/lib/puppet/type/vcsrepo.rb
index 3dd7bc6..f678389 100644
--- a/lib/puppet/type/vcsrepo.rb
+++ b/lib/puppet/type/vcsrepo.rb
@@ -40,6 +40,9 @@ Puppet::Type.newtype(:vcsrepo) do
feature :depth,
"The provider can do shallow clones"
+ feature :p4config,
+ "The provider understands Perforce Configuration"
+
ensurable do
attr_accessor :latest
@@ -98,10 +101,14 @@ Puppet::Type.newtype(:vcsrepo) do
if prov
if prov.working_copy_exists?
if @resource.value(:force)
- notice "Deleting current repository before recloning"
- prov.destroy
- notice "Create repository from latest"
- prov.create
+ if noop
+ notice "Noop Mode - Would have deleted repository and re-created from latest"
+ else
+ notice "Deleting current repository before recloning"
+ prov.destroy
+ notice "Create repository from latest"
+ prov.create
+ end
end
(@should.include?(:latest) && prov.latest?) ? :latest : :present
elsif prov.class.feature?(:bare_repositories) and prov.bare_exists?
@@ -204,8 +211,11 @@ Puppet::Type.newtype(:vcsrepo) do
desc "The value to be used to do a shallow clone."
end
+ newparam :p4config, :required_features => [:p4config] do
+ desc "The Perforce P4CONFIG environment."
+ end
+
autorequire(:package) do
['git', 'git-core']
end
-
end