From d2bb24e33860090a7051ce9ef6dfbb695cf23447 Mon Sep 17 00:00:00 2001 From: Paul Allen Date: Wed, 11 Jun 2014 22:32:21 +0100 Subject: Basic Perforce provider Supports sync and client create/update --- lib/puppet/provider/vcsrepo/p4.rb | 217 ++++++++++++++++++++++++++++++++++++++ lib/puppet/type/vcsrepo.rb | 19 ++++ 2 files changed, 236 insertions(+) create mode 100644 lib/puppet/provider/vcsrepo/p4.rb diff --git a/lib/puppet/provider/vcsrepo/p4.rb b/lib/puppet/provider/vcsrepo/p4.rb new file mode 100644 index 0000000..2fdae70 --- /dev/null +++ b/lib/puppet/provider/vcsrepo/p4.rb @@ -0,0 +1,217 @@ +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, :p4_config + + def create + # create or update client + create_client(client_name, @resource.value(:path)) + + # 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) + + if hash['status'] == "have" + return hash['change'].to_i + else + return 0 + end + 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) + notice "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 + path = @resource.value(:path) + client = @resource.value(:p4client) + if not client + client = "puppet-" + Digest::MD5.hexdigest(path) + end + return client + 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, path) + notice "Creating client: #{client}" + hash = parse_client(client) + hash['Root'] = path + hash['Description'] = "Generated by Puppet VCSrepo" + 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.each do |k,v| + 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 + + def config + p4port = @resource.value(:p4port) + p4user = @resource.value(:p4user) + p4charset = @resource.value(:p4charset) + p4client = @resource.value(:p4client) || client_name + + cfg = Hash.new + cfg.store 'P4USER', p4user if p4user + cfg.store 'P4PORT', p4port if p4port + cfg.store 'P4CHARSET', p4charset if p4charset + cfg.store 'P4CLIENT', p4client if p4client + + 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 + + notice "environment: #{config}" + notice "command: #{cmd_str}" + + hash = Hash.new + Open3.popen3(config, cmd_str) do |i, o, e, t| + # Send input stream if provided + if(opts[:input]) + notice "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]) + p4_err = "P4: " + hash['data'] if(hash['code'] == 'error') + raise Puppet::DevError, "#{p4_err}\n#{e.read}\nExit: #{t.value}" if(t.value != 0) + end + end + + notice "hash: #{hash}\n" + return hash + end + +end diff --git a/lib/puppet/type/vcsrepo.rb b/lib/puppet/type/vcsrepo.rb index 0e4450b..ad964c5 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 :p4_config, + "The provider understands Perforce Configuration" + ensurable do attr_accessor :latest @@ -209,6 +212,22 @@ Puppet::Type.newtype(:vcsrepo) do desc "The value to be used to do a shallow clone." end + newparam :p4port, :required_features => [:p4_config] do + desc "The Perforce P4PORT environment." + end + + newparam :p4user, :required_features => [:p4_config] do + desc "The Perforce P4USER environment." + end + + newparam :p4client, :required_features => [:p4_config] do + desc "The Perforce P4CLIENT environment." + end + + newparam :p4charset, :required_features => [:p4_config] do + desc "The Perforce P4CHARSET environment." + end + autorequire(:package) do ['git', 'git-core'] end -- cgit v1.2.3 From b16e4c0f9251279fe2211f651459654e0759dbf0 Mon Sep 17 00:00:00 2001 From: Paul Allen Date: Thu, 12 Jun 2014 17:49:19 +0100 Subject: Markdown and examples --- README.P4.markdown | 82 ++++++++++++++++++++++++++++++++++++++++++++ examples/p4/create_client.pp | 5 +++ examples/p4/delete_client.pp | 5 +++ examples/p4/latest_client.pp | 6 ++++ examples/p4/sync_client.pp | 7 ++++ 5 files changed, 105 insertions(+) create mode 100644 README.P4.markdown create mode 100644 examples/p4/create_client.pp create mode 100644 examples/p4/delete_client.pp create mode 100644 examples/p4/latest_client.pp create mode 100644 examples/p4/sync_client.pp diff --git a/README.P4.markdown b/README.P4.markdown new file mode 100644 index 0000000..39fffd2 --- /dev/null +++ b/README.P4.markdown @@ -0,0 +1,82 @@ +Using vcsrepo with Perforce +=========================== + +To create an empty Workspace +---------------------------- + +Define a `vcsrepo` without a `source` or `revision`: + + vcsrepo { "/path/to/repo": + ensure => present, + provider => p4 + } + +If no `p4client` name is provided a workspace generated name is calculated based on the +Digest of path. For example: + + puppet-91bc00640c4e5a17787286acbe2c021c + +Providing a `p4client` name will create/update the client workspace in Perforce. + + vcsrepo { "/path/to/repo": + ensure => present, + provider => p4 + p4client => "my_client_ws" + } + +To create/update and sync a Perforce workspace +---------------------------------------------- + +To sync a depot path to head (latest): + + vcsrepo { "/path/to/repo": + ensure => present, + provider => p4, + source => '//depot/branch/...' + } + +For a specific changelist, use `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' + } + +Check out as a user by setting `p4user`: + + vcsrepo { "/path/to/repo": + ensure => present, + provider => p4, + source => '//depot/branch/...', + p4user => 'user' + } + +You can set `p4port` to specify a Perforce server: + + vcsrepo { "/path/to/repo": + ensure => present, + provider => p4, + source => '//depot/branch/...', + p4port => 'ssl:perforce.com:1666' + } + +If `p4port`, `p4user`, `p4charset` or `p4client` are specified they will override the +environment variabels P4PORT, P4USER, etc... If a P4CONFIG file is defined, the config +file settings will take precedence. + + +More Examples +------------- + +For examples you can run, see `examples/p4/` diff --git a/examples/p4/create_client.pp b/examples/p4/create_client.pp new file mode 100644 index 0000000..f7605e6 --- /dev/null +++ b/examples/p4/create_client.pp @@ -0,0 +1,5 @@ +vcsrepo { "/tmp/vcstest-p4-create_client": + ensure => present, + provider => p4, + p4client => "puppet-test001" +} \ 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..315222f --- /dev/null +++ b/examples/p4/delete_client.pp @@ -0,0 +1,5 @@ +vcsrepo { "/tmp/vcstest-p4-create_client": + ensure => absent, + provider => p4, + p4client => "puppet-test001" +} \ 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..719e180 --- /dev/null +++ b/examples/p4/latest_client.pp @@ -0,0 +1,6 @@ +vcsrepo { "/tmp/vcstest-p4-create_client": + ensure => latest, + provider => p4, + p4client => "puppet-test001", + 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..c1128bc --- /dev/null +++ b/examples/p4/sync_client.pp @@ -0,0 +1,7 @@ +vcsrepo { "/tmp/vcstest-p4-create_client": + ensure => present, + provider => p4, + p4client => "puppet-test001", + source => "//depot/..." + revision => "30" +} \ No newline at end of file -- cgit v1.2.3 From 58f9cb20657a7be01777c920a37279f30dc1fce6 Mon Sep 17 00:00:00 2001 From: Paul Allen Date: Thu, 12 Jun 2014 17:50:55 +0100 Subject: Spec tests for p4 provider Test create, destroy and exists? access points. --- spec/unit/puppet/provider/vcsrepo/p4_spec.rb | 73 ++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 spec/unit/puppet/provider/vcsrepo/p4_spec.rb 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..822106d --- /dev/null +++ b/spec/unit/puppet/provider/vcsrepo/p4_spec.rb @@ -0,0 +1,73 @@ +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 => "Root: /tmp/vcsrepo\nDescription: Generated by Puppet 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[:p4client] = 'client_ws' + resource[:revision] = '1' + provider.expects(:p4).with(['client', '-o', resource.value(:p4client)]).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' + resource[:p4client] = 'client_ws' + provider.expects(:p4).with(['client', '-o', resource.value(:p4client)]).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 + provider.expects(:p4).with(['client', '-o', "puppet-51f0a4b45dbfc10614df94f0a189c16f"]).returns({}) + provider.expects(:p4).with(['client', '-i'], spec) + provider.create + end + end + end + + describe 'destroying' do + it "it should remove the directory" do + resource[:p4client] = 'test_client' + provider.expects(:p4).with(['client', '-d', '-f', resource.value(:p4client)]) + 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 -- cgit v1.2.3 From 8256eff1800e1377c7cf2b961ef63074a2ede0d4 Mon Sep 17 00:00:00 2001 From: Paul Allen Date: Mon, 16 Jun 2014 17:04:52 +0100 Subject: Add support for passing password/ticket MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Uses param ‘p4passwd’ to set P4PASSWD environment to pass a valid ticket or password. --- README.P4.markdown | 15 ++++++++++++--- lib/puppet/provider/vcsrepo/p4.rb | 2 ++ lib/puppet/type/vcsrepo.rb | 4 ++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/README.P4.markdown b/README.P4.markdown index 39fffd2..f92f616 100644 --- a/README.P4.markdown +++ b/README.P4.markdown @@ -71,9 +71,18 @@ You can set `p4port` to specify a Perforce server: p4port => 'ssl:perforce.com:1666' } -If `p4port`, `p4user`, `p4charset` or `p4client` are specified they will override the -environment variabels P4PORT, P4USER, etc... If a P4CONFIG file is defined, the config -file settings will take precedence. +You can set `p4passwd` for authentication : + + vcsrepo { "/path/to/repo": + ensure => present, + provider => p4, + source => '//depot/branch/...', + p4port => 'ssl:perforce.com:1666' + } + +If `p4port`, `p4user`, `p4charset`, `p4passwd` or `p4client` are specified they will +override the environment variabels P4PORT, P4USER, etc... If a P4CONFIG file is +defined, the config file settings will take precedence. More Examples diff --git a/lib/puppet/provider/vcsrepo/p4.rb b/lib/puppet/provider/vcsrepo/p4.rb index 2fdae70..df1ec21 100644 --- a/lib/puppet/provider/vcsrepo/p4.rb +++ b/lib/puppet/provider/vcsrepo/p4.rb @@ -162,12 +162,14 @@ Puppet::Type.type(:vcsrepo).provide(:p4, :parent => Puppet::Provider::Vcsrepo) d p4port = @resource.value(:p4port) p4user = @resource.value(:p4user) p4charset = @resource.value(:p4charset) + p4passwd = @resource.value(:p4passwd) p4client = @resource.value(:p4client) || client_name cfg = Hash.new cfg.store 'P4USER', p4user if p4user cfg.store 'P4PORT', p4port if p4port cfg.store 'P4CHARSET', p4charset if p4charset + cfg.store 'P4PASSWD', p4passwd if p4passwd cfg.store 'P4CLIENT', p4client if p4client return cfg diff --git a/lib/puppet/type/vcsrepo.rb b/lib/puppet/type/vcsrepo.rb index ad964c5..649789a 100644 --- a/lib/puppet/type/vcsrepo.rb +++ b/lib/puppet/type/vcsrepo.rb @@ -227,6 +227,10 @@ Puppet::Type.newtype(:vcsrepo) do newparam :p4charset, :required_features => [:p4_config] do desc "The Perforce P4CHARSET environment." end + + newparam :p4passwd, :required_features => [:p4_config] do + desc "The Perforce P4PASSWD environment." + end autorequire(:package) do ['git', 'git-core'] -- cgit v1.2.3 From 24e2f91ffb0091bc6f67b4d90f9702690bc1179f Mon Sep 17 00:00:00 2001 From: Paul Allen Date: Fri, 20 Jun 2014 09:59:42 +0100 Subject: Detab to 2 spaces. --- examples/p4/create_client.pp | 6 +- examples/p4/delete_client.pp | 6 +- examples/p4/latest_client.pp | 8 +- examples/p4/sync_client.pp | 10 +- lib/puppet/provider/vcsrepo/p4.rb | 388 +++++++++++++-------------- spec/unit/puppet/provider/vcsrepo/p4_spec.rb | 12 +- 6 files changed, 215 insertions(+), 215 deletions(-) diff --git a/examples/p4/create_client.pp b/examples/p4/create_client.pp index f7605e6..94017bc 100644 --- a/examples/p4/create_client.pp +++ b/examples/p4/create_client.pp @@ -1,5 +1,5 @@ vcsrepo { "/tmp/vcstest-p4-create_client": - ensure => present, - provider => p4, - p4client => "puppet-test001" + ensure => present, + provider => p4, + p4client => "puppet-test001" } \ No newline at end of file diff --git a/examples/p4/delete_client.pp b/examples/p4/delete_client.pp index 315222f..20bdd47 100644 --- a/examples/p4/delete_client.pp +++ b/examples/p4/delete_client.pp @@ -1,5 +1,5 @@ vcsrepo { "/tmp/vcstest-p4-create_client": - ensure => absent, - provider => p4, - p4client => "puppet-test001" + ensure => absent, + provider => p4, + p4client => "puppet-test001" } \ No newline at end of file diff --git a/examples/p4/latest_client.pp b/examples/p4/latest_client.pp index 719e180..54f9062 100644 --- a/examples/p4/latest_client.pp +++ b/examples/p4/latest_client.pp @@ -1,6 +1,6 @@ vcsrepo { "/tmp/vcstest-p4-create_client": - ensure => latest, - provider => p4, - p4client => "puppet-test001", - source => "//depot/..." + ensure => latest, + provider => p4, + p4client => "puppet-test001", + source => "//depot/..." } \ No newline at end of file diff --git a/examples/p4/sync_client.pp b/examples/p4/sync_client.pp index c1128bc..e22f6b0 100644 --- a/examples/p4/sync_client.pp +++ b/examples/p4/sync_client.pp @@ -1,7 +1,7 @@ vcsrepo { "/tmp/vcstest-p4-create_client": - ensure => present, - provider => p4, - p4client => "puppet-test001", - source => "//depot/..." - revision => "30" + ensure => present, + provider => p4, + p4client => "puppet-test001", + 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 index df1ec21..de3bc7a 100644 --- a/lib/puppet/provider/vcsrepo/p4.rb +++ b/lib/puppet/provider/vcsrepo/p4.rb @@ -1,219 +1,219 @@ require File.join(File.dirname(__FILE__), '..', 'vcsrepo') Puppet::Type.type(:vcsrepo).provide(:p4, :parent => Puppet::Provider::Vcsrepo) do - desc "Supports Perforce depots" + desc "Supports Perforce depots" - has_features :filesystem_types, :reference_tracking, :p4_config - - def create - # create or update client - create_client(client_name, @resource.value(:path)) - - # if source provided, sync client - source = @resource.value(:source) - if source - revision = @resource.value(:revision) - sync_client(source, revision) - end + has_features :filesystem_types, :reference_tracking, :p4_config + + def create + # create or update client + create_client(client_name, @resource.value(:path)) + + # if source provided, sync client + source = @resource.value(:source) + if source + revision = @resource.value(:revision) + sync_client(source, revision) + end - update_owner - 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 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 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 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? + 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 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) - - if hash['status'] == "have" - return hash['change'].to_i - else - return 0 - end - end + def revision + args = ['cstat'] + args.push(@resource.value(:source)) + hash = p4(args) + + if hash['status'] == "have" + return hash['change'].to_i + else + return 0 + end + end - def revision=(desired) - sync_client(@resource.value(:source), desired) - update_owner - end + def revision=(desired) + sync_client(@resource.value(:source), desired) + update_owner + end - private + 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: + 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) - notice "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 - path = @resource.value(:path) - client = @resource.value(:p4client) - if not client - client = "puppet-" + Digest::MD5.hexdigest(path) - end - return client - 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 + def sync_client(source, revision) + notice "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 + path = @resource.value(:path) + client = @resource.value(:p4client) + if not client + client = "puppet-" + Digest::MD5.hexdigest(path) + end + return client + 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, path) - notice "Creating client: #{client}" - hash = parse_client(client) - hash['Root'] = path - hash['Description'] = "Generated by Puppet VCSrepo" - save_client(hash) - end + def create_client(client, path) + notice "Creating client: #{client}" + hash = parse_client(client) + hash['Root'] = path + hash['Description'] = "Generated by Puppet VCSrepo" + save_client(hash) + end - # Fetches a client workspace spec from Perforce and returns a hash map representation. + # 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) + 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 + 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.each do |k,v| - 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 - - def config - p4port = @resource.value(:p4port) - p4user = @resource.value(:p4user) - p4charset = @resource.value(:p4charset) - p4passwd = @resource.value(:p4passwd) - p4client = @resource.value(:p4client) || client_name - - cfg = Hash.new - cfg.store 'P4USER', p4user if p4user - cfg.store 'P4PORT', p4port if p4port - cfg.store 'P4CHARSET', p4charset if p4charset - cfg.store 'P4PASSWD', p4passwd if p4passwd - cfg.store 'P4CLIENT', p4client if p4client - - 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 - - notice "environment: #{config}" - notice "command: #{cmd_str}" - - hash = Hash.new - Open3.popen3(config, cmd_str) do |i, o, e, t| - # Send input stream if provided - if(opts[:input]) - notice "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]) - p4_err = "P4: " + hash['data'] if(hash['code'] == 'error') - raise Puppet::DevError, "#{p4_err}\n#{e.read}\nExit: #{t.value}" if(t.value != 0) - end - end - - notice "hash: #{hash}\n" - return hash - end - + def save_client(hash) + spec = String.new + view = "\nView:\n" + + hash.each do |k,v| + 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 + + def config + p4port = @resource.value(:p4port) + p4user = @resource.value(:p4user) + p4charset = @resource.value(:p4charset) + p4passwd = @resource.value(:p4passwd) + p4client = @resource.value(:p4client) || client_name + + cfg = Hash.new + cfg.store 'P4USER', p4user if p4user + cfg.store 'P4PORT', p4port if p4port + cfg.store 'P4CHARSET', p4charset if p4charset + cfg.store 'P4PASSWD', p4passwd if p4passwd + cfg.store 'P4CLIENT', p4client if p4client + + 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 + + notice "environment: #{config}" + notice "command: #{cmd_str}" + + hash = Hash.new + Open3.popen3(config, cmd_str) do |i, o, e, t| + # Send input stream if provided + if(opts[:input]) + notice "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]) + p4_err = "P4: " + hash['data'] if(hash['code'] == 'error') + raise Puppet::DevError, "#{p4_err}\n#{e.read}\nExit: #{t.value}" if(t.value != 0) + end + end + + notice "hash: #{hash}\n" + return hash + end + end diff --git a/spec/unit/puppet/provider/vcsrepo/p4_spec.rb b/spec/unit/puppet/provider/vcsrepo/p4_spec.rb index 822106d..2c202f2 100644 --- a/spec/unit/puppet/provider/vcsrepo/p4_spec.rb +++ b/spec/unit/puppet/provider/vcsrepo/p4_spec.rb @@ -15,7 +15,7 @@ describe Puppet::Type.type(:vcsrepo).provider(:p4) do Puppet::Util.stubs(:which).with('p4').returns('/usr/local/bin/p4') end - spec = { + spec = { :input => "Root: /tmp/vcsrepo\nDescription: Generated by Puppet VCSrepo\n\nView:\n", :marshal => false } @@ -46,17 +46,17 @@ describe Puppet::Type.type(:vcsrepo).provider(:p4) do context "when a client and source are not given" do it "should execute 'p4 client'" do - provider.expects(:p4).with(['client', '-o', "puppet-51f0a4b45dbfc10614df94f0a189c16f"]).returns({}) - provider.expects(:p4).with(['client', '-i'], spec) - provider.create + provider.expects(:p4).with(['client', '-o', "puppet-51f0a4b45dbfc10614df94f0a189c16f"]).returns({}) + provider.expects(:p4).with(['client', '-i'], spec) + provider.create end end end describe 'destroying' do it "it should remove the directory" do - resource[:p4client] = 'test_client' - provider.expects(:p4).with(['client', '-d', '-f', resource.value(:p4client)]) + resource[:p4client] = 'test_client' + provider.expects(:p4).with(['client', '-d', '-f', resource.value(:p4client)]) expects_rm_rf provider.destroy end -- cgit v1.2.3 From ac4e14770fc04db02be2f48f6de675e90e3f6b29 Mon Sep 17 00:00:00 2001 From: Paul Allen Date: Fri, 20 Jun 2014 11:45:58 +0100 Subject: Moved p4.markdown content to project's markdown --- README.P4.markdown | 91 ----------------------------------------------------- README.markdown | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 91 deletions(-) delete mode 100644 README.P4.markdown diff --git a/README.P4.markdown b/README.P4.markdown deleted file mode 100644 index f92f616..0000000 --- a/README.P4.markdown +++ /dev/null @@ -1,91 +0,0 @@ -Using vcsrepo with Perforce -=========================== - -To create an empty Workspace ----------------------------- - -Define a `vcsrepo` without a `source` or `revision`: - - vcsrepo { "/path/to/repo": - ensure => present, - provider => p4 - } - -If no `p4client` name is provided a workspace generated name is calculated based on the -Digest of path. For example: - - puppet-91bc00640c4e5a17787286acbe2c021c - -Providing a `p4client` name will create/update the client workspace in Perforce. - - vcsrepo { "/path/to/repo": - ensure => present, - provider => p4 - p4client => "my_client_ws" - } - -To create/update and sync a Perforce workspace ----------------------------------------------- - -To sync a depot path to head (latest): - - vcsrepo { "/path/to/repo": - ensure => present, - provider => p4, - source => '//depot/branch/...' - } - -For a specific changelist, use `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' - } - -Check out as a user by setting `p4user`: - - vcsrepo { "/path/to/repo": - ensure => present, - provider => p4, - source => '//depot/branch/...', - p4user => 'user' - } - -You can set `p4port` to specify a Perforce server: - - vcsrepo { "/path/to/repo": - ensure => present, - provider => p4, - source => '//depot/branch/...', - p4port => 'ssl:perforce.com:1666' - } - -You can set `p4passwd` for authentication : - - vcsrepo { "/path/to/repo": - ensure => present, - provider => p4, - source => '//depot/branch/...', - p4port => 'ssl:perforce.com:1666' - } - -If `p4port`, `p4user`, `p4charset`, `p4passwd` or `p4client` are specified they will -override the environment variabels P4PORT, P4USER, etc... If a P4CONFIG file is -defined, the config file settings will take precedence. - - -More Examples -------------- - -For examples you can run, see `examples/p4/` diff --git a/README.markdown b/README.markdown index 39cd249..6dc36d2 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: * [CVS (cvs)](#cvs) * [Git (git)](#git) * [Mercurial (hg)](#mercurial) +* [Perforce (p4)](#perforce) * [Subversion (svn)](#subversion) ###Bazaar @@ -314,6 +316,96 @@ 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` name is provided a workspace generated name is calculated based on the +Digest of path. For example: + + puppet-91bc00640c4e5a17787286acbe2c021c + +Providing a `p4client` name will create/update the client workspace in Perforce. The +value replaces the P4CLIENT environment variable. + + vcsrepo { "/path/to/repo": + ensure => present, + provider => p4 + p4client => "my_client_ws" + } + +#####To create/update and sync a Perforce workspace + +To sync a depot path to head (latest): + + vcsrepo { "/path/to/repo": + ensure => present, + provider => p4, + source => '//depot/branch/...' + } + +For a specific changelist, use `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' + } + +Check out as a user by setting `p4user`: + + vcsrepo { "/path/to/repo": + ensure => present, + provider => p4, + source => '//depot/branch/...', + p4user => 'user' + } + +You can set `p4port` to specify a Perforce server: + + vcsrepo { "/path/to/repo": + ensure => present, + provider => p4, + source => '//depot/branch/...', + p4port => 'ssl:perforce.com:1666' + } + +You can set `p4passwd` for authentication : + + vcsrepo { "/path/to/repo": + ensure => present, + provider => p4, + source => '//depot/branch/...', + p4port => 'ssl:perforce.com:1666' + } + +If `p4port`, `p4user`, `p4charset`, `p4passwd` or `p4client` are specified they will +override the environment variabels P4PORT, P4USER, etc... If a P4CONFIG file is defined, +the config file settings will take precedence. + +#####Further Examples + +For examples you can run, see `examples/p4/` + ###Subversion #####To create a blank repository -- cgit v1.2.3 From 319f9fbe1954fd172da638f3ccd76e58c3ec8c7f Mon Sep 17 00:00:00 2001 From: Paul Allen Date: Mon, 23 Jun 2014 15:51:28 +0100 Subject: Added support for p4config. - Removed p4port, p4client, p4user to keep name space clean. - Changed notify to Puppet.debug - Updated markdown and examples - Updated unit tests --- README.markdown | 45 ++++++------------------ examples/p4/create_client.pp | 5 ++- examples/p4/delete_client.pp | 5 ++- examples/p4/latest_client.pp | 3 +- examples/p4/sync_client.pp | 5 ++- lib/puppet/provider/vcsrepo/p4.rb | 51 +++++++++++++++------------- lib/puppet/type/vcsrepo.rb | 22 ++---------- spec/unit/puppet/provider/vcsrepo/p4_spec.rb | 15 ++++---- 8 files changed, 57 insertions(+), 94 deletions(-) diff --git a/README.markdown b/README.markdown index 6dc36d2..5f8aa9d 100644 --- a/README.markdown +++ b/README.markdown @@ -329,18 +329,19 @@ connection settings. provider => p4 } -If no `p4client` name is provided a workspace generated name is calculated based on the -Digest of path. For example: +If no `P4CLIENT` environment name is provided a workspace generated name is calculated +based on the Digest of path. For example: puppet-91bc00640c4e5a17787286acbe2c021c -Providing a `p4client` name will create/update the client workspace in Perforce. The -value replaces the P4CLIENT environment variable. +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 - p4client => "my_client_ws" + provider => p4, + p4config => '.p4config' } #####To create/update and sync a Perforce workspace @@ -371,36 +372,10 @@ You can also set `revision` to a label: revision => 'my_label' } -Check out as a user by setting `p4user`: - - vcsrepo { "/path/to/repo": - ensure => present, - provider => p4, - source => '//depot/branch/...', - p4user => 'user' - } - -You can set `p4port` to specify a Perforce server: - - vcsrepo { "/path/to/repo": - ensure => present, - provider => p4, - source => '//depot/branch/...', - p4port => 'ssl:perforce.com:1666' - } - -You can set `p4passwd` for authentication : - - vcsrepo { "/path/to/repo": - ensure => present, - provider => p4, - source => '//depot/branch/...', - p4port => 'ssl:perforce.com:1666' - } +#####To authenticate against the Perforce server -If `p4port`, `p4user`, `p4charset`, `p4passwd` or `p4client` are specified they will -override the environment variabels P4PORT, P4USER, etc... If a P4CONFIG file is defined, -the config file settings will take precedence. +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 diff --git a/examples/p4/create_client.pp b/examples/p4/create_client.pp index 94017bc..f491701 100644 --- a/examples/p4/create_client.pp +++ b/examples/p4/create_client.pp @@ -1,5 +1,4 @@ -vcsrepo { "/tmp/vcstest-p4-create_client": +vcsrepo { "/tmp/vcstest/p4_client_root": ensure => present, - provider => p4, - p4client => "puppet-test001" + provider => p4 } \ No newline at end of file diff --git a/examples/p4/delete_client.pp b/examples/p4/delete_client.pp index 20bdd47..239349b 100644 --- a/examples/p4/delete_client.pp +++ b/examples/p4/delete_client.pp @@ -1,5 +1,4 @@ -vcsrepo { "/tmp/vcstest-p4-create_client": +vcsrepo { "/tmp/vcstest/p4_client_root": ensure => absent, - provider => p4, - p4client => "puppet-test001" + provider => p4 } \ No newline at end of file diff --git a/examples/p4/latest_client.pp b/examples/p4/latest_client.pp index 54f9062..9dcd68e 100644 --- a/examples/p4/latest_client.pp +++ b/examples/p4/latest_client.pp @@ -1,6 +1,5 @@ -vcsrepo { "/tmp/vcstest-p4-create_client": +vcsrepo { "/tmp/vcstest/p4_client_root": ensure => latest, provider => p4, - p4client => "puppet-test001", source => "//depot/..." } \ No newline at end of file diff --git a/examples/p4/sync_client.pp b/examples/p4/sync_client.pp index e22f6b0..55dc9dc 100644 --- a/examples/p4/sync_client.pp +++ b/examples/p4/sync_client.pp @@ -1,7 +1,6 @@ -vcsrepo { "/tmp/vcstest-p4-create_client": +vcsrepo { "/tmp/vcstest/p4_client_root": ensure => present, provider => p4, - p4client => "puppet-test001", - source => "//depot/..." + 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 index de3bc7a..ec40cf3 100644 --- a/lib/puppet/provider/vcsrepo/p4.rb +++ b/lib/puppet/provider/vcsrepo/p4.rb @@ -3,7 +3,7 @@ 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, :p4_config + has_features :filesystem_types, :reference_tracking, :p4config def create # create or update client @@ -90,7 +90,7 @@ Puppet::Type.type(:vcsrepo).provide(:p4, :parent => Puppet::Provider::Vcsrepo) d # +source+:: Depot path to sync # +revision+:: Perforce change list to sync to (optional) def sync_client(source, revision) - notice "Syncing: #{source}" + Puppet.debug "Syncing: #{source}" args = ['sync'] if revision args.push(source + "@" + revision) @@ -102,12 +102,23 @@ Puppet::Type.type(:vcsrepo).provide(:p4, :parent => Puppet::Provider::Vcsrepo) d # Returns the name of the Perforce client workspace def client_name + p4config = @resource.value(:p4config) + + # default (generated) client name path = @resource.value(:path) - client = @resource.value(:p4client) - if not client - client = "puppet-" + Digest::MD5.hexdigest(path) + default = "puppet-" + Digest::MD5.hexdigest(path) + + # 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 client + + return set_client || ENV['P4CLIENT'] || default end # Create (or update) a client workspace spec. @@ -116,7 +127,7 @@ Puppet::Type.type(:vcsrepo).provide(:p4, :parent => Puppet::Provider::Vcsrepo) d # +client+:: Name of client workspace # +path+:: The Root location of the Perforce client workspace def create_client(client, path) - notice "Creating client: #{client}" + Puppet.debug "Creating client: #{client}" hash = parse_client(client) hash['Root'] = path hash['Description'] = "Generated by Puppet VCSrepo" @@ -158,20 +169,14 @@ Puppet::Type.type(:vcsrepo).provide(:p4, :parent => Puppet::Provider::Vcsrepo) d p4(args, {:input => spec, :marshal => false}) end + # Sets Perforce Configuration environment. + # P4CLIENT generated, but overwitten if defined in config. def config - p4port = @resource.value(:p4port) - p4user = @resource.value(:p4user) - p4charset = @resource.value(:p4charset) - p4passwd = @resource.value(:p4passwd) - p4client = @resource.value(:p4client) || client_name - - cfg = Hash.new - cfg.store 'P4USER', p4user if p4user - cfg.store 'P4PORT', p4port if p4port - cfg.store 'P4CHARSET', p4charset if p4charset - cfg.store 'P4PASSWD', p4passwd if p4passwd - cfg.store 'P4CLIENT', p4client if p4client + p4config = @resource.value(:p4config) + cfg = Hash.new + cfg.store 'P4CONFIG', p4config if p4config + cfg.store 'P4CLIENT', client_name return cfg end @@ -187,14 +192,14 @@ Puppet::Type.type(:vcsrepo).provide(:p4, :parent => Puppet::Provider::Vcsrepo) d cmd.push args cmd_str = cmd.respond_to?(:join) ? cmd.join(' ') : cmd - notice "environment: #{config}" - notice "command: #{cmd_str}" + 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]) - notice "input:\n" + opts[:input] + Puppet.debug "input:\n" + opts[:input] i.write opts[:input] i.close end @@ -212,7 +217,7 @@ Puppet::Type.type(:vcsrepo).provide(:p4, :parent => Puppet::Provider::Vcsrepo) d end end - notice "hash: #{hash}\n" + Puppet.debug "hash: #{hash}\n" return hash end diff --git a/lib/puppet/type/vcsrepo.rb b/lib/puppet/type/vcsrepo.rb index 649789a..7ada022 100644 --- a/lib/puppet/type/vcsrepo.rb +++ b/lib/puppet/type/vcsrepo.rb @@ -40,7 +40,7 @@ Puppet::Type.newtype(:vcsrepo) do feature :depth, "The provider can do shallow clones" - feature :p4_config, + feature :p4config, "The provider understands Perforce Configuration" ensurable do @@ -212,26 +212,10 @@ Puppet::Type.newtype(:vcsrepo) do desc "The value to be used to do a shallow clone." end - newparam :p4port, :required_features => [:p4_config] do - desc "The Perforce P4PORT environment." - end - - newparam :p4user, :required_features => [:p4_config] do - desc "The Perforce P4USER environment." - end - - newparam :p4client, :required_features => [:p4_config] do - desc "The Perforce P4CLIENT environment." + newparam :p4config, :required_features => [:p4config] do + desc "The Perforce P4CONFIG environment." end - newparam :p4charset, :required_features => [:p4_config] do - desc "The Perforce P4CHARSET environment." - end - - newparam :p4passwd, :required_features => [:p4_config] do - desc "The Perforce P4PASSWD 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 index 2c202f2..2bb81c8 100644 --- a/spec/unit/puppet/provider/vcsrepo/p4_spec.rb +++ b/spec/unit/puppet/provider/vcsrepo/p4_spec.rb @@ -24,9 +24,10 @@ describe Puppet::Type.type(:vcsrepo).provider(:p4) do context 'with source and revision' do it "should execute 'p4 sync' with the revision" do resource[:source] = 'something' - resource[:p4client] = 'client_ws' resource[:revision] = '1' - provider.expects(:p4).with(['client', '-o', resource.value(:p4client)]).returns({}) + 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 @@ -36,8 +37,9 @@ describe Puppet::Type.type(:vcsrepo).provider(:p4) do context 'without revision' do it "should just execute 'p4 sync' without a revision" do resource[:source] = 'something' - resource[:p4client] = 'client_ws' - provider.expects(:p4).with(['client', '-o', resource.value(:p4client)]).returns({}) + 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 @@ -55,8 +57,9 @@ describe Puppet::Type.type(:vcsrepo).provider(:p4) do describe 'destroying' do it "it should remove the directory" do - resource[:p4client] = 'test_client' - provider.expects(:p4).with(['client', '-d', '-f', resource.value(:p4client)]) + ENV['P4CLIENT'] = 'test_client' + + provider.expects(:p4).with(['client', '-d', '-f', 'test_client']) expects_rm_rf provider.destroy end -- cgit v1.2.3 From 457035ec1c15df0d53abf7232dad63b2722b1720 Mon Sep 17 00:00:00 2001 From: Paul Allen Date: Mon, 23 Jun 2014 16:22:35 +0100 Subject: Add hostname to Digest for default client name. --- README.markdown | 2 +- lib/puppet/provider/vcsrepo/p4.rb | 3 ++- spec/unit/puppet/provider/vcsrepo/p4_spec.rb | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.markdown b/README.markdown index 5f8aa9d..575950c 100644 --- a/README.markdown +++ b/README.markdown @@ -330,7 +330,7 @@ connection settings. } If no `P4CLIENT` environment name is provided a workspace generated name is calculated -based on the Digest of path. For example: +based on the Digest of path and hostname. For example: puppet-91bc00640c4e5a17787286acbe2c021c diff --git a/lib/puppet/provider/vcsrepo/p4.rb b/lib/puppet/provider/vcsrepo/p4.rb index ec40cf3..612cc56 100644 --- a/lib/puppet/provider/vcsrepo/p4.rb +++ b/lib/puppet/provider/vcsrepo/p4.rb @@ -106,7 +106,8 @@ Puppet::Type.type(:vcsrepo).provide(:p4, :parent => Puppet::Provider::Vcsrepo) d # default (generated) client name path = @resource.value(:path) - default = "puppet-" + Digest::MD5.hexdigest(path) + host = Facter.value('hostname') + default = "puppet-" + Digest::MD5.hexdigest(path + host) # check config for client name set_client = nil diff --git a/spec/unit/puppet/provider/vcsrepo/p4_spec.rb b/spec/unit/puppet/provider/vcsrepo/p4_spec.rb index 2bb81c8..e45650a 100644 --- a/spec/unit/puppet/provider/vcsrepo/p4_spec.rb +++ b/spec/unit/puppet/provider/vcsrepo/p4_spec.rb @@ -48,7 +48,7 @@ describe Puppet::Type.type(:vcsrepo).provider(:p4) do context "when a client and source are not given" do it "should execute 'p4 client'" do - provider.expects(:p4).with(['client', '-o', "puppet-51f0a4b45dbfc10614df94f0a189c16f"]).returns({}) + provider.expects(:p4).with(['client', '-o', "puppet-1c5e7a8e4f702e5091dfba173bc0e7c0"]).returns({}) provider.expects(:p4).with(['client', '-i'], spec) provider.create end -- cgit v1.2.3 From e1aa644dd075c275dd22d85cdc03c88dda029d87 Mon Sep 17 00:00:00 2001 From: Paul Allen Date: Tue, 24 Jun 2014 13:57:30 +0100 Subject: Support streams and fix Marshal for 'p4 cstat' --- README.markdown | 6 ++-- lib/puppet/provider/vcsrepo/p4.rb | 76 ++++++++++++++++++++++++++++++++------- 2 files changed, 67 insertions(+), 15 deletions(-) diff --git a/README.markdown b/README.markdown index 575950c..d977886 100644 --- a/README.markdown +++ b/README.markdown @@ -346,15 +346,15 @@ defining `p4config`. If a configuration is defined, then the environment variab #####To create/update and sync a Perforce workspace -To sync a depot path to head (latest): +To sync a depot path to head, ensure `latest`: vcsrepo { "/path/to/repo": - ensure => present, + ensure => latest, provider => p4, source => '//depot/branch/...' } -For a specific changelist, use `revision`: +For a specific changelist, ensure `present` and specify a `revision`: vcsrepo { "/path/to/repo": ensure => present, diff --git a/lib/puppet/provider/vcsrepo/p4.rb b/lib/puppet/provider/vcsrepo/p4.rb index 612cc56..da9c953 100644 --- a/lib/puppet/provider/vcsrepo/p4.rb +++ b/lib/puppet/provider/vcsrepo/p4.rb @@ -7,7 +7,7 @@ Puppet::Type.type(:vcsrepo).provide(:p4, :parent => Puppet::Provider::Vcsrepo) d def create # create or update client - create_client(client_name, @resource.value(:path)) + create_client(client_name) # if source provided, sync client source = @resource.value(:source) @@ -63,13 +63,19 @@ Puppet::Type.type(:vcsrepo).provide(:p4, :parent => Puppet::Provider::Vcsrepo) d def revision args = ['cstat'] args.push(@resource.value(:source)) - hash = p4(args) + hash = p4(args, {:marshal => false}) + hash = marshal_cstat(hash) - if hash['status'] == "have" - return hash['change'].to_i - else - return 0 + 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) @@ -93,7 +99,7 @@ Puppet::Type.type(:vcsrepo).provide(:p4, :parent => Puppet::Provider::Vcsrepo) d Puppet.debug "Syncing: #{source}" args = ['sync'] if revision - args.push(source + "@" + revision) + args.push(source + "@#{revision}") else args.push(source) end @@ -127,11 +133,29 @@ Puppet::Type.type(:vcsrepo).provide(:p4, :parent => Puppet::Provider::Vcsrepo) d # Params: # +client+:: Name of client workspace # +path+:: The Root location of the Perforce client workspace - def create_client(client, path) + def create_client(client) Puppet.debug "Creating client: #{client}" + + # fetch client spec hash = parse_client(client) - hash['Root'] = path + 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 @@ -212,9 +236,11 @@ Puppet::Type.type(:vcsrepo).provide(:p4, :parent => Puppet::Provider::Vcsrepo) d end # Raise errors, Perforce or Exec - if(opts[:raise]) - p4_err = "P4: " + hash['data'] if(hash['code'] == 'error') - raise Puppet::DevError, "#{p4_err}\n#{e.read}\nExit: #{t.value}" if(t.value != 0) + 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 @@ -222,4 +248,30 @@ Puppet::Type.type(:vcsrepo).provide(:p4, :parent => Puppet::Provider::Vcsrepo) d 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 -- cgit v1.2.3 From f88ae5fd6c746de3b454e38a8616f016ada8cff9 Mon Sep 17 00:00:00 2001 From: Paul Allen Date: Tue, 24 Jun 2014 14:16:05 +0100 Subject: Sort keys on hash generate same test result. Keys in the Client spec were processed out of order and failing tests. --- lib/puppet/provider/vcsrepo/p4.rb | 3 ++- spec/unit/puppet/provider/vcsrepo/p4_spec.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/puppet/provider/vcsrepo/p4.rb b/lib/puppet/provider/vcsrepo/p4.rb index da9c953..4f53415 100644 --- a/lib/puppet/provider/vcsrepo/p4.rb +++ b/lib/puppet/provider/vcsrepo/p4.rb @@ -179,7 +179,8 @@ Puppet::Type.type(:vcsrepo).provide(:p4, :parent => Puppet::Provider::Vcsrepo) d spec = String.new view = "\nView:\n" - hash.each do |k,v| + hash.keys.sort.each do |k| + v = hash[k] next if( k == "code" ) if(k.to_s =~ /View/ ) view += "\t#{v}\n" diff --git a/spec/unit/puppet/provider/vcsrepo/p4_spec.rb b/spec/unit/puppet/provider/vcsrepo/p4_spec.rb index e45650a..1261915 100644 --- a/spec/unit/puppet/provider/vcsrepo/p4_spec.rb +++ b/spec/unit/puppet/provider/vcsrepo/p4_spec.rb @@ -16,7 +16,7 @@ describe Puppet::Type.type(:vcsrepo).provider(:p4) do end spec = { - :input => "Root: /tmp/vcsrepo\nDescription: Generated by Puppet VCSrepo\n\nView:\n", + :input => "Description: Generated by Puppet VCSrepo\nRoot: /tmp/vcsrepo\n\nView:\n", :marshal => false } -- cgit v1.2.3 From 0435f81a3b8821a36dedc038982c3efdab90a3b1 Mon Sep 17 00:00:00 2001 From: Paul Allen Date: Fri, 27 Jun 2014 14:14:47 +0100 Subject: Clear P4CLIENT Environment before test --- spec/unit/puppet/provider/vcsrepo/p4_spec.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/unit/puppet/provider/vcsrepo/p4_spec.rb b/spec/unit/puppet/provider/vcsrepo/p4_spec.rb index 1261915..3a4605b 100644 --- a/spec/unit/puppet/provider/vcsrepo/p4_spec.rb +++ b/spec/unit/puppet/provider/vcsrepo/p4_spec.rb @@ -48,6 +48,8 @@ describe Puppet::Type.type(:vcsrepo).provider(:p4) do context "when a client and source are not given" do it "should execute 'p4 client'" do + ENV['P4CLIENT'] = nil + provider.expects(:p4).with(['client', '-o', "puppet-1c5e7a8e4f702e5091dfba173bc0e7c0"]).returns({}) provider.expects(:p4).with(['client', '-i'], spec) provider.create -- cgit v1.2.3 From b5b2dd296eb30df25ea3d4ca125ca451b162393c Mon Sep 17 00:00:00 2001 From: Paul Allen Date: Fri, 27 Jun 2014 17:44:27 +0100 Subject: Calculate client workspace name for test case (The value of host will be different) --- spec/unit/puppet/provider/vcsrepo/p4_spec.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spec/unit/puppet/provider/vcsrepo/p4_spec.rb b/spec/unit/puppet/provider/vcsrepo/p4_spec.rb index 3a4605b..2d382da 100644 --- a/spec/unit/puppet/provider/vcsrepo/p4_spec.rb +++ b/spec/unit/puppet/provider/vcsrepo/p4_spec.rb @@ -50,7 +50,11 @@ describe Puppet::Type.type(:vcsrepo).provider(:p4) do it "should execute 'p4 client'" do ENV['P4CLIENT'] = nil - provider.expects(:p4).with(['client', '-o', "puppet-1c5e7a8e4f702e5091dfba173bc0e7c0"]).returns({}) + 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 -- cgit v1.2.3