summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.markdown67
-rw-r--r--examples/p4/create_client.pp4
-rw-r--r--examples/p4/delete_client.pp4
-rw-r--r--examples/p4/latest_client.pp5
-rw-r--r--examples/p4/sync_client.pp6
-rw-r--r--lib/puppet/provider/vcsrepo/p4.rb278
-rw-r--r--lib/puppet/type/vcsrepo.rb7
-rw-r--r--spec/unit/puppet/provider/vcsrepo/p4_spec.rb82
8 files changed, 453 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown
index a8575a6..f83c7fd 100644
--- a/README.markdown
+++ b/README.markdown
@@ -13,6 +13,7 @@
* [CVS](#cvs)
* [Git](#git)
* [Mercurial](#mercurial)
+ * [Perforce](#perforce)
* [Subversion](#subversion)
5. [Reference - An under-the-hood peek at what the module is doing and how](#reference)
* [Type: vcsrepo](#type-vcsrepo)
@@ -57,6 +58,7 @@ The vcsrepo module works with the following VCSs:
* [Bazaar (bzr)](#bazaar)
* [CVS (cvs)](#cvs)
* [Mercurial (hg)](#mercurial)
+* [Perforce (p4)](#perforce)
* [Subversion (svn)](#subversion)
**Note:* Git is the only VCS provider officially [supported](https://forge.puppetlabs.com/supported) by Puppet Labs.
@@ -327,6 +329,71 @@ When your source uses SSH, such as 'ssh://...', you can manage your SSH keys wit
For more examples using Mercurial, see `examples/hg/`.
+###Perforce
+
+#####To create an empty Workspace
+
+To create an empty Workspace, define a `vcsrepo` without a `source` or `revision`. The
+Environment variables P4PORT, P4USER, etc... are used to define the Perforce server
+connection settings.
+
+ vcsrepo { "/path/to/repo":
+ ensure => present,
+ provider => p4
+ }
+
+If no `P4CLIENT` environment name is provided a workspace generated name is calculated
+based on the Digest of path and hostname. For example:
+
+ puppet-91bc00640c4e5a17787286acbe2c021c
+
+A Perforce configuration file can be used by setting the `P4CONFIG` environment or
+defining `p4config`. If a configuration is defined, then the environment variable for
+`P4CLIENT` is replaced.
+
+ vcsrepo { "/path/to/repo":
+ ensure => present,
+ provider => p4,
+ p4config => '.p4config'
+ }
+
+#####To create/update and sync a Perforce workspace
+
+To sync a depot path to head, ensure `latest`:
+
+ vcsrepo { "/path/to/repo":
+ ensure => latest,
+ provider => p4,
+ source => '//depot/branch/...'
+ }
+
+For a specific changelist, ensure `present` and specify a `revision`:
+
+ vcsrepo { "/path/to/repo":
+ ensure => present,
+ provider => p4,
+ source => '//depot/branch/...',
+ revision => '2341'
+ }
+
+You can also set `revision` to a label:
+
+ vcsrepo { "/path/to/repo":
+ ensure => present,
+ provider => p4,
+ source => '//depot/branch/...',
+ revision => 'my_label'
+ }
+
+#####To authenticate against the Perforce server
+
+Either set the environment variables `P4USER` and `P4PASSWD` or use a configuration file.
+For secure servers set the `P4PASSWD` with a valid ticket generated using `p4 login -p`.
+
+#####Further Examples
+
+For examples you can run, see `examples/p4/`
+
###Subversion
#####To create a blank repository
diff --git a/examples/p4/create_client.pp b/examples/p4/create_client.pp
new file mode 100644
index 0000000..f491701
--- /dev/null
+++ b/examples/p4/create_client.pp
@@ -0,0 +1,4 @@
+vcsrepo { "/tmp/vcstest/p4_client_root":
+ ensure => present,
+ provider => p4
+} \ No newline at end of file
diff --git a/examples/p4/delete_client.pp b/examples/p4/delete_client.pp
new file mode 100644
index 0000000..239349b
--- /dev/null
+++ b/examples/p4/delete_client.pp
@@ -0,0 +1,4 @@
+vcsrepo { "/tmp/vcstest/p4_client_root":
+ ensure => absent,
+ provider => p4
+} \ No newline at end of file
diff --git a/examples/p4/latest_client.pp b/examples/p4/latest_client.pp
new file mode 100644
index 0000000..9dcd68e
--- /dev/null
+++ b/examples/p4/latest_client.pp
@@ -0,0 +1,5 @@
+vcsrepo { "/tmp/vcstest/p4_client_root":
+ ensure => latest,
+ provider => p4,
+ source => "//depot/..."
+} \ No newline at end of file
diff --git a/examples/p4/sync_client.pp b/examples/p4/sync_client.pp
new file mode 100644
index 0000000..55dc9dc
--- /dev/null
+++ b/examples/p4/sync_client.pp
@@ -0,0 +1,6 @@
+vcsrepo { "/tmp/vcstest/p4_client_root":
+ ensure => present,
+ provider => p4,
+ source => "//depot/...",
+ revision => "30"
+} \ No newline at end of file
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 42767ab..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
@@ -208,6 +211,10 @@ 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
diff --git a/spec/unit/puppet/provider/vcsrepo/p4_spec.rb b/spec/unit/puppet/provider/vcsrepo/p4_spec.rb
new file mode 100644
index 0000000..2d382da
--- /dev/null
+++ b/spec/unit/puppet/provider/vcsrepo/p4_spec.rb
@@ -0,0 +1,82 @@
+require 'spec_helper'
+
+describe Puppet::Type.type(:vcsrepo).provider(:p4) do
+
+ let(:resource) { Puppet::Type.type(:vcsrepo).new({
+ :name => 'test',
+ :ensure => :present,
+ :provider => :p4,
+ :path => '/tmp/vcsrepo',
+ })}
+
+ let(:provider) { resource.provider }
+
+ before :each do
+ Puppet::Util.stubs(:which).with('p4').returns('/usr/local/bin/p4')
+ end
+
+ spec = {
+ :input => "Description: Generated by Puppet VCSrepo\nRoot: /tmp/vcsrepo\n\nView:\n",
+ :marshal => false
+ }
+
+ describe 'creating' do
+ context 'with source and revision' do
+ it "should execute 'p4 sync' with the revision" do
+ resource[:source] = 'something'
+ resource[:revision] = '1'
+ ENV['P4CLIENT'] = 'client_ws1'
+
+ provider.expects(:p4).with(['client', '-o', 'client_ws1']).returns({})
+ provider.expects(:p4).with(['client', '-i'], spec)
+ provider.expects(:p4).with(['sync', resource.value(:source) + "@" + resource.value(:revision)])
+ provider.create
+ end
+ end
+
+ context 'without revision' do
+ it "should just execute 'p4 sync' without a revision" do
+ resource[:source] = 'something'
+ ENV['P4CLIENT'] = 'client_ws2'
+
+ provider.expects(:p4).with(['client', '-o', 'client_ws2']).returns({})
+ provider.expects(:p4).with(['client', '-i'], spec)
+ provider.expects(:p4).with(['sync', resource.value(:source)])
+ provider.create
+ end
+ end
+
+ context "when a client and source are not given" do
+ it "should execute 'p4 client'" do
+ ENV['P4CLIENT'] = nil
+
+ path = resource.value(:path)
+ host = Facter.value('hostname')
+ default = "puppet-" + Digest::MD5.hexdigest(path + host)
+
+ provider.expects(:p4).with(['client', '-o', default]).returns({})
+ provider.expects(:p4).with(['client', '-i'], spec)
+ provider.create
+ end
+ end
+ end
+
+ describe 'destroying' do
+ it "it should remove the directory" do
+ ENV['P4CLIENT'] = 'test_client'
+
+ provider.expects(:p4).with(['client', '-d', '-f', 'test_client'])
+ expects_rm_rf
+ provider.destroy
+ end
+ end
+
+ describe "checking existence" do
+ it "should check for the directory" do
+ provider.expects(:p4).with(['info'], {:marshal => false}).returns({})
+ provider.expects(:p4).with(['where', resource.value(:path) + "..."], {:raise => false}).returns({})
+ provider.exists?
+ end
+ end
+
+end