Stefan Saasen avatar Stefan Saasen committed ad6c018

Add vcsrepo puppet module

Comments (0)

Files changed (49)

modules/vcsrepo/Gemfile

+source :rubygems
+gem 'rake', '~> 0.8.7'
+gem 'rspec', '~> 1.2.9'
+gem 'mocha', '~> 0.12.7', :require => false
+gem 'puppet', '~> 2.7'

modules/vcsrepo/Gemfile.lock

+GEM
+  remote: http://rubygems.org/
+  specs:
+    facter (1.6.13)
+    metaclass (0.0.1)
+    mocha (0.12.7)
+      metaclass (~> 0.0.1)
+    puppet (2.7.19)
+      facter (~> 1.5)
+    rake (0.8.7)
+    rspec (1.2.9)
+
+PLATFORMS
+  ruby
+
+DEPENDENCIES
+  mocha (~> 0.12.7)
+  puppet (~> 2.7)
+  rake (~> 0.8.7)
+  rspec (~> 1.2.9)

modules/vcsrepo/LICENSE

+Copyright (C) 2010-2012 Puppet Labs Inc.
+
+Puppet Labs can be contacted at: info@puppetlabs.com
+
+This program and entire repository is free software; you can
+redistribute it and/or modify it under the terms of the GNU
+General Public License as published by the Free Software
+Foundation; either version 2 of the License, or any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

modules/vcsrepo/Modulefile

+name 'puppetlabs/vcsrepo'
+version '0.1.2'

modules/vcsrepo/README.BZR.markdown

+Using vcsrepo with Bazaar
+=========================
+
+To create a blank repository
+----------------------------
+
+Define a `vcsrepo` without a `source` or `revision`:
+
+    vcsrepo { "/path/to/repo":
+      ensure   => present,
+      provider => bzr
+    }
+
+To branch from an existing repository
+-------------------------------------
+
+Provide the `source` location:
+
+    vcsrepo { "/path/to/repo":
+        ensure   => present,
+        provider => bzr,
+        source   => 'lp:myproj'
+    }
+
+For a specific revision, use `revision` with a valid revisionspec
+(see `bzr help revisionspec` for more information on formatting a revision):
+
+    vcsrepo { "/path/to/repo":
+        ensure   => present,
+        provider => bzr,
+        source   => 'lp:myproj',
+        revision => 'menesis@pov.lt-20100309191856-4wmfqzc803fj300x'
+    }
+
+For sources that use SSH (eg, `bzr+ssh://...`, `sftp://...`)
+------------------------------------------------------------
+
+Manage your SSH keys with Puppet and use `require` in your `vcsrepo`
+to ensure they are present.  For more information, see the `require`
+metaparameter documentation[1].
+
+More Examples
+-------------
+
+For examples you can run, see `examples/bzr/`
+
+[1]: http://docs.puppetlabs.com/references/stable/metaparameter.html#require

modules/vcsrepo/README.CVS.markdown

+Using vcsrepo with CVS
+======================
+
+To create a blank repository
+----------------------------
+
+Define a `vcsrepo` without a `source` or `revision`:
+
+    vcsrepo { "/path/to/repo":
+      ensure => present,
+      provider => cvs
+    }
+
+To checkout/update from a repository
+------------------------------------
+
+To get the current mainline:
+
+    vcsrepo { "/path/to/workspace":
+        ensure => present,
+        provider => cvs,
+        source => ":pserver:anonymous@example.com:/sources/myproj"
+    }
+
+You can use the `compression` parameter (it works like CVS `-z`):
+
+    vcsrepo { "/path/to/workspace":
+        ensure => present,
+        provider => cvs,
+        compression => 3,
+        source => ":pserver:anonymous@example.com:/sources/myproj"
+    }
+
+For a specific tag, use `revision`:
+
+    vcsrepo { "/path/to/workspace":
+        ensure => present,
+        provider => cvs,
+        compression => 3,
+        source => ":pserver:anonymous@example.com:/sources/myproj",
+        revision => "SOMETAG"
+    }
+
+For sources that use SSH
+------------------------
+
+Manage your SSH keys with Puppet and use `require` in your `vcsrepo`
+to ensure they are present.  For more information, see the `require`
+metaparameter documentation[1].
+
+More Examples
+-------------
+
+For examples you can run, see `examples/cvs/`
+
+[1]: http://docs.puppetlabs.com/references/stable/metaparameter.html#require

modules/vcsrepo/README.GIT.markdown

+Using vcsrepo with Git
+======================
+
+To create a blank repository
+----------------------------
+
+Define a `vcsrepo` without a `source` or `revision`:
+
+    vcsrepo { "/path/to/repo":
+      ensure => present,
+      provider => git
+    }
+
+If you're defining this for a central/"official" repository, you'll
+probably want to make it a "bare" repository.  Do this by setting
+`ensure` to `bare` instead of `present`:
+
+    vcsrepo { "/path/to/repo":
+        ensure => bare,
+        provider => git
+    }
+
+To clone/pull a repository
+----------------------------
+
+To get the current [master] HEAD:
+
+    vcsrepo { "/path/to/repo":
+        ensure => present,
+        provider => git,
+        source => "git://example.com/repo.git"
+    }
+
+For a specific revision or branch (can be a commit SHA, tag or branch name):
+
+    vcsrepo { "/path/to/repo":
+        ensure => present,
+        provider => git,
+        source => 'git://example.com/repo.git',
+        revision => '0c466b8a5a45f6cd7de82c08df2fb4ce1e920a31'
+    }
+
+    vcsrepo { "/path/to/repo":
+        ensure => present,
+        provider => git,
+        source => 'git://example.com/repo.git',
+        revision => '1.1.2rc1'
+    }
+
+    vcsrepo { "/path/to/repo":
+        ensure => present,
+        provider => git,
+        source => 'git://example.com/repo.git',
+        revision => 'development'
+    }
+
+Check out as a user:
+
+    vcsrepo { "/path/to/repo":
+        ensure => present,
+        provider => git,
+        source => 'git://example.com/repo.git',
+        revision => '0c466b8a5a45f6cd7de82c08df2fb4ce1e920a31',
+        user => 'someUser'
+    }
+
+Keep the repository at the latest revision (note: this will always overwrite local changes to the repository):
+
+    vcsrepo { "/path/to/repo":
+        ensure => latest,
+        provider => git,
+        source => 'git://example.com/repo.git',
+        revision => 'master',
+    }
+
+For sources that use SSH (eg, `username@server:...`)
+----------------------------------------------------
+
+Manage your SSH keys with Puppet and use `require` in your `vcsrepo`
+to ensure they are present.  For more information, see the `require`
+metaparameter documentation[1].
+
+More Examples
+-------------
+
+For examples you can run, see `examples/git/`
+
+[1]: http://docs.puppetlabs.com/references/stable/metaparameter.html#require
+

modules/vcsrepo/README.HG.markdown

+Using vcsrepo with Mercurial
+============================
+
+To create a blank repository
+----------------------------
+
+Define a `vcsrepo` without a `source` or `revision`:
+
+    vcsrepo { "/path/to/repo":
+      ensure   => present,
+      provider => hg
+    }
+
+To clone/pull & update a repository
+-----------------------------------
+
+To get the default branch tip:
+
+    vcsrepo { "/path/to/repo":
+        ensure   => present,
+        provider => hg,
+        source   => "http://hg.example.com/myrepo"
+    }
+
+For a specific changeset, use `revision`:
+
+    vcsrepo { "/path/to/repo":
+        ensure   => present,
+        provider => hg,
+        source   => "http://hg.example.com/myrepo"
+        revision => '21ea4598c962'
+    }
+
+You can also set `revision` to a tag:
+
+    vcsrepo { "/path/to/repo":
+        ensure   => present,
+        provider => hg,
+        source   => "http://hg.example.com/myrepo"
+        revision => '1.1.2'
+    }
+
+For sources that use SSH (eg, `ssh://...`)
+------------------------------------------
+
+Manage your SSH keys with Puppet and use `require` in your `vcsrepo`
+to ensure they are present.  For more information, see the `require`
+metaparameter documentation[1].
+
+More Examples
+-------------
+
+For examples you can run, see `examples/hg/`
+
+[1]: http://docs.puppetlabs.com/references/stable/metaparameter.html#require

modules/vcsrepo/README.SVN.markdown

+Using vcsrepo with Subversion
+=============================
+
+To create a blank repository
+----------------------------
+
+To create a blank repository suitable for use as a central repository,
+define a `vcsrepo` without a `source` or `revision`:
+
+    vcsrepo { "/path/to/repo":
+      ensure   => present,
+      provider => svn
+    }
+
+To checkout from a repository
+-----------------------------
+
+Provide a `source` qualified to the branch/tag you want:
+
+    vcsrepo { "/path/to/repo":
+        ensure   => present,
+        provider => svn,
+        source   => "svn://svnrepo/hello/branches/foo"
+    }
+
+You can provide a specific `revision`:
+
+    vcsrepo { "/path/to/repo":
+        ensure   => present,
+        provider => svn,
+        source   => "svn://svnrepo/hello/branches/foo",
+        revision => '1234'
+    }
+
+For sources that use SSH (eg, `svn+ssh://...`)
+----------------------------------------------
+
+Manage your SSH keys with Puppet and use `require` in your `vcsrepo`
+to ensure they are present.  For more information, see the `require`
+metaparameter documentation[1].
+
+More Examples
+-------------
+
+For examples you can run, see `examples/svn/`
+
+[1]: http://docs.puppetlabs.com/references/stable/metaparameter.html#require

modules/vcsrepo/README.markdown

+vcsrepo
+=======
+
+Purpose
+-------
+
+This provides a single type, `vcsrepo`.
+
+This type can be used to describe:
+
+* A working copy checked out from a (remote or local) source, at an
+  arbitrary revision
+* A "blank" working copy not associated with a source (when it makes
+  sense for the VCS being used)
+* A "blank" central repository (when the distinction makes sense for the VCS
+  being used)
+
+Supported Version Control Systems
+---------------------------------
+
+This module supports a wide range of VCS types, each represented by a
+separate provider.
+
+For information on how to use this module with a specific VCS, see
+`README.<VCS>.markdown`.
+
+License
+-------
+
+See LICENSE.

modules/vcsrepo/Rakefile

+require 'spec/rake/spectask'
+Spec::Rake::SpecTask.new(:spec) do |spec|
+  spec.libs << 'lib' << 'spec'
+  spec.spec_files = FileList['spec/**/*_spec.rb']
+end
+
+Spec::Rake::SpecTask.new(:rcov) do |spec|
+  spec.libs << 'lib' << 'spec'
+  spec.pattern = 'spec/**/*_spec.rb'
+  spec.rcov = true
+end
+
+task :default => :spec

modules/vcsrepo/examples/bzr/branch.pp

+vcsrepo { '/tmp/vcstest-bzr-branch':
+  ensure   => present,
+  provider => bzr,
+  source   => 'lp:do',
+  revision => '1312',
+}

modules/vcsrepo/examples/bzr/init_repo.pp

+vcsrepo { '/tmp/vcstest-bzr-init':
+  ensure   => present,
+  provider => bzr,
+}

modules/vcsrepo/examples/cvs/local.pp

+vcsrepo { '/tmp/vcstest-cvs-repo':
+  ensure   => present,
+  provider => cvs,
+}
+
+vcsrepo { '/tmp/vcstest-cvs-workspace-local':
+  ensure   => present,
+  provider => cvs,
+  source   => '/tmp/vcstest-cvs-repo',
+  require  => Vcsrepo['/tmp/vcstest-cvs-repo'],
+}

modules/vcsrepo/examples/cvs/remote.pp

+vcsrepo { '/tmp/vcstest-cvs-workspace-remote':
+  ensure   => present,
+  provider => cvs,
+  source   => ':pserver:anonymous@cvs.sv.gnu.org:/sources/leetcvrt',
+}

modules/vcsrepo/examples/git/bare_init.pp

+vcsrepo { '/tmp/vcstest-git-bare':
+  ensure   => bare,
+  provider => git,
+}

modules/vcsrepo/examples/git/clone.pp

+vcsrepo { '/tmp/vcstest-git-clone':
+  ensure   => present,
+  provider => git,
+  source   => 'git://github.com/bruce/rtex.git',
+}

modules/vcsrepo/examples/git/working_copy_init.pp

+vcsrepo { '/tmp/vcstest-git-wc':
+  ensure   => present,
+  provider => git,
+}

modules/vcsrepo/examples/hg/clone.pp

+vcsrepo { '/tmp/vcstest-hg-clone':
+  ensure   => present,
+  provider => hg,
+  source   => 'http://hg.basho.com/riak',
+  revision => 'riak-0.5.3',
+}

modules/vcsrepo/examples/hg/init_repo.pp

+vcsrepo { '/tmp/vcstest-hg-init':
+  ensure   => present,
+  provider => hg,
+}

modules/vcsrepo/examples/svn/checkout.pp

+vcsrepo { '/tmp/vcstest-svn-checkout':
+  ensure   => present,
+  provider => svn,
+  source   => 'http://svn.edgewall.org/repos/babel/trunk',
+}

modules/vcsrepo/examples/svn/server.pp

+vcsrepo { '/tmp/vcstest-svn-server':
+  ensure   => present,
+  provider => svn,
+}

modules/vcsrepo/lib/puppet/provider/vcsrepo.rb

+require 'tmpdir'
+require 'digest/md5'
+require 'fileutils'
+
+# Abstract
+class Puppet::Provider::Vcsrepo < Puppet::Provider
+
+  private
+
+  def set_ownership
+    owner = @resource.value(:owner) || nil
+    group = @resource.value(:group) || nil
+    FileUtils.chown_R(owner, group, @resource.value(:path))
+  end
+
+  def path_exists?
+    File.directory?(@resource.value(:path))
+  end
+
+  # Note: We don't rely on Dir.chdir's behavior of automatically returning the
+  # value of the last statement -- for easier stubbing.
+  def at_path(&block) #:nodoc:
+    value = nil
+    Dir.chdir(@resource.value(:path)) do
+      value = yield
+    end
+    value
+  end
+
+  def tempdir
+    @tempdir ||= File.join(Dir.tmpdir, 'vcsrepo-' + Digest::MD5.hexdigest(@resource.value(:path)))
+  end
+
+end

modules/vcsrepo/lib/puppet/provider/vcsrepo/bzr.rb

+require File.join(File.dirname(__FILE__), '..', 'vcsrepo')
+
+Puppet::Type.type(:vcsrepo).provide(:bzr, :parent => Puppet::Provider::Vcsrepo) do
+  desc "Supports Bazaar repositories"
+
+  optional_commands   :bzr => 'bzr'
+  has_features :reference_tracking
+
+  def create
+    if !@resource.value(:source)
+      create_repository(@resource.value(:path))
+    else
+      clone_repository(@resource.value(:revision))
+    end
+  end
+
+  def working_copy_exists?
+    File.directory?(File.join(@resource.value(:path), '.bzr'))
+  end
+
+  def exists?
+    working_copy_exists?
+  end
+
+  def destroy
+    FileUtils.rm_rf(@resource.value(:path))
+  end
+
+  def revision
+    at_path do
+      current_revid = bzr('version-info')[/^revision-id:\s+(\S+)/, 1]
+      desired = @resource.value(:revision)
+      begin
+        desired_revid = bzr('revision-info', desired).strip.split(/\s+/).last
+      rescue Puppet::ExecutionFailure
+        # Possible revid available during update (but definitely not current)
+        desired_revid = nil
+      end
+      if current_revid == desired_revid
+        desired
+      else
+        current_revid
+      end
+    end
+  end
+
+  def revision=(desired)
+    bzr('update', '-r', desired, @resource.value(:path))
+  end
+
+  private
+
+  def create_repository(path)
+    bzr('init', path)
+  end
+
+  def clone_repository(revision)
+    args = ['branch']
+    if revision
+      args.push('-r', revision)
+    end
+    args.push(@resource.value(:source),
+              @resource.value(:path))
+    bzr(*args)
+  end
+
+end

modules/vcsrepo/lib/puppet/provider/vcsrepo/cvs.rb

+require File.join(File.dirname(__FILE__), '..', 'vcsrepo')
+
+Puppet::Type.type(:vcsrepo).provide(:cvs, :parent => Puppet::Provider::Vcsrepo) do
+  desc "Supports CVS repositories/workspaces"
+
+  optional_commands   :cvs => 'cvs'
+  has_features :gzip_compression, :reference_tracking, :modules
+
+  def create
+    if !@resource.value(:source)
+      create_repository(@resource.value(:path))
+    else
+      checkout_repository
+    end
+    update_owner
+  end
+
+  def exists?
+    if @resource.value(:source)
+      directory = File.join(@resource.value(:path), 'CVS')
+    else
+      directory = File.join(@resource.value(:path), 'CVSROOT')
+    end
+    File.directory?(directory)
+  end
+
+  def working_copy_exists?
+    File.directory?(File.join(@resource.value(:path), 'CVS'))
+  end
+
+  def destroy
+    FileUtils.rm_rf(@resource.value(:path))
+  end
+
+  def latest?
+    debug "Checking for updates because 'ensure => latest'"
+    at_path do
+      # We cannot use -P to prune empty dirs, otherwise
+      # CVS would report those as "missing", regardless
+      # if they have contents or updates.
+      is_current = (cvs('-nq', 'update', '-d').strip == "")
+      if (!is_current) then debug "There are updates available on the checkout's current branch/tag." end
+      return is_current
+    end
+  end
+
+  def latest
+    # CVS does not have a conecpt like commit-IDs or change
+    # sets, so we can only have the current branch name (or the
+    # requested one, if that differs) as the "latest" revision.
+    should = @resource.value(:revision)
+    current = self.revision
+    return should != current ? should : current
+  end
+
+  def revision
+    if !@rev
+      if File.exist?(tag_file)
+        contents = File.read(tag_file).strip
+        # Note: Doesn't differentiate between N and T entries
+        @rev = contents[1..-1]
+      else
+        @rev = 'HEAD'
+      end
+      debug "Checkout is on branch/tag '#{@rev}'"
+    end
+    return @rev
+  end
+
+  def revision=(desired)
+    at_path do
+      cvs('update', '-dr', desired, '.')
+      update_owner
+      @rev = desired
+    end
+  end
+
+  private
+
+  def tag_file
+    File.join(@resource.value(:path), 'CVS', 'Tag')
+  end
+
+  def checkout_repository
+    dirname, basename = File.split(@resource.value(:path))
+    Dir.chdir(dirname) do
+      args = ['-d', @resource.value(:source)]
+      if @resource.value(:compression)
+        args.push('-z', @resource.value(:compression))
+      end
+      args.push('checkout')
+      if @resource.value(:revision)
+        args.push('-r', @resource.value(:revision))
+      end
+      args.push('-d', basename, module_name)
+      cvs(*args)
+    end
+  end
+
+  # When the source:
+  # * Starts with ':' (eg, :pserver:...)
+  def module_name
+    if (m = @resource.value(:module))
+      m
+    elsif (source = @resource.value(:source))
+      source[0, 1] == ':' ? File.basename(source) : '.'
+    end
+  end
+
+  def create_repository(path)
+    cvs('-d', path, 'init')
+  end
+
+  def update_owner
+    if @resource.value(:owner) or @resource.value(:group)
+      set_ownership
+    end
+  end
+end

modules/vcsrepo/lib/puppet/provider/vcsrepo/dummy.rb

+require File.join(File.dirname(__FILE__), '..', 'vcsrepo')
+
+Puppet::Type.type(:vcsrepo).provide(:dummy, :parent => Puppet::Provider::Vcsrepo) do
+  desc "Dummy default provider"
+
+  defaultfor :vcsrepo => :dummy
+
+  def working_copy_exists?
+    providers = @resource.class.providers.map{|x| x.to_s}.sort.reject{|x| x == "dummy"}.join(", ") rescue "none"
+    raise("vcsrepo resource must have a provider, available: #{providers}")
+  end
+end

modules/vcsrepo/lib/puppet/provider/vcsrepo/git.rb

+require File.join(File.dirname(__FILE__), '..', 'vcsrepo')
+
+Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) do
+  desc "Supports Git repositories"
+
+  ##TODO modify the commands below so that the su - is included
+  optional_commands :git => 'git',
+                    :su => 'su'
+  has_features :bare_repositories, :reference_tracking, :ssh_identity, :multiple_remotes, :user
+
+  def create
+    if !@resource.value(:source)
+      init_repository(@resource.value(:path))
+    else
+      clone_repository(@resource.value(:source), @resource.value(:path))
+      if @resource.value(:revision)
+        if @resource.value(:ensure) == :bare
+          notice "Ignoring revision for bare repository"
+        else
+          checkout
+        end
+      end
+      if @resource.value(:ensure) != :bare
+        update_submodules
+      end
+    end
+    update_owner_and_excludes
+  end
+
+  def destroy
+    FileUtils.rm_rf(@resource.value(:path))
+  end
+
+  def latest?
+    at_path do
+      return self.revision == self.latest
+    end
+  end
+
+  def latest
+    branch = on_branch?
+    if branch == 'master'
+      return get_revision("#{@resource.value(:remote)}/HEAD")
+    elsif branch == '(no branch)'
+      return get_revision('HEAD')
+    else
+      return get_revision("#{@resource.value(:remote)}/%s" % branch)
+    end
+  end
+
+  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
+      canonical = at_path { git_with_identity('rev-parse', @resource.value(:revision)).chomp }
+    end
+
+    if current == canonical
+      @resource.value(:revision)
+    else
+      current
+    end
+  end
+
+  def revision=(desired)
+    checkout(desired)
+    if local_branch_revision?(desired)
+      # 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}") }
+    end
+    if @resource.value(:ensure) != :bare
+      update_submodules
+    end
+    update_owner_and_excludes
+  end
+
+  def bare_exists?
+    bare_git_config_exists? && !working_copy_exists?
+  end
+
+  def working_copy_exists?
+    File.directory?(File.join(@resource.value(:path), '.git'))
+  end
+
+  def exists?
+    working_copy_exists? || bare_exists?
+  end
+
+  def update_remote_origin_url
+    current = git_with_identity('config', 'remote.origin.url')
+    unless @resource.value(:source).nil?
+      if current.nil? or current.strip != @resource.value(:source)
+        git_with_identity('config', 'remote.origin.url', @resource.value(:source))
+      end
+    end
+  end
+
+  def update_references
+    at_path do
+      update_remote_origin_url
+      git_with_identity('fetch', @resource.value(:remote))
+      git_with_identity('fetch', '--tags', @resource.value(:remote))
+      update_owner_and_excludes
+    end
+  end
+
+  private
+
+  def bare_git_config_exists?
+    File.exist?(File.join(@resource.value(:path), 'config'))
+  end
+
+  def clone_repository(source, path)
+    check_force
+    args = ['clone']
+    if @resource.value(:ensure) == :bare
+      args << '--bare'
+    end
+    if !File.exist?(File.join(@resource.value(:path), '.git'))
+      args.push(source, path)
+      Dir.chdir("/") do
+        git_with_identity(*args)
+      end
+    else
+      notice "Repo has already been cloned"
+    end
+  end
+
+  def check_force
+    if path_exists?
+      if @resource.value(:force)
+        notice "Removing %s to replace with vcsrepo." % @resource.value(:path)
+        destroy
+      else
+        raise Puppet::Error, "Could not create repository (non-repository at path)"
+      end
+    end
+  end
+
+  def init_repository(path)
+    check_force
+    if @resource.value(:ensure) == :bare && working_copy_exists?
+      convert_working_copy_to_bare
+    elsif @resource.value(:ensure) == :present && bare_exists?
+      convert_bare_to_working_copy
+    else
+      # normal init
+      FileUtils.mkdir(@resource.value(:path))
+      FileUtils.chown(@resource.value(:user), nil, @resource.value(:path)) if @resource.value(:user)
+      args = ['init']
+      if @resource.value(:ensure) == :bare
+        args << '--bare'
+      end
+      at_path do
+        git_with_identity(*args)
+      end
+    end
+  end
+
+  # Convert working copy to bare
+  #
+  # Moves:
+  #   <path>/.git
+  # to:
+  #   <path>/
+  def convert_working_copy_to_bare
+    notice "Converting working copy repository to bare repository"
+    FileUtils.mv(File.join(@resource.value(:path), '.git'), tempdir)
+    FileUtils.rm_rf(@resource.value(:path))
+    FileUtils.mv(tempdir, @resource.value(:path))
+  end
+
+  # Convert bare to working copy
+  #
+  # Moves:
+  #   <path>/
+  # to:
+  #   <path>/.git
+  def convert_bare_to_working_copy
+    notice "Converting bare repository to working copy repository"
+    FileUtils.mv(@resource.value(:path), tempdir)
+    FileUtils.mkdir(@resource.value(:path))
+    FileUtils.mv(tempdir, File.join(@resource.value(:path), '.git'))
+    if commits_in?(File.join(@resource.value(:path), '.git'))
+      reset('HEAD')
+      git_with_identity('checkout', '-f')
+      update_owner_and_excludes
+    end
+  end
+
+  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))
+    end
+    false
+  end
+
+  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}") }
+    else
+      at_path { git_with_identity('checkout', '--force', revision) }
+    end
+  end
+
+  def reset(desired)
+    at_path do
+      git_with_identity('reset', '--hard', desired)
+    end
+  end
+
+  def update_submodules
+    at_path do
+      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
+
+  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}'
+    branch = at_path { branches.grep /(remotes\/)?#{@resource.value(:remote)}\/#{revision}/ }
+    if branch.length > 0
+      return branch
+    end
+  end
+
+  def local_branch_revision?(revision = @resource.value(:revision))
+    at_path { branches.include?(revision) }
+  end
+
+  def tag_revision?(revision = @resource.value(:revision))
+    at_path { tags.include?(revision) }
+  end
+
+  def branches
+    at_path { git_with_identity('branch', '-a') }.gsub('*', ' ').split(/\n/).map { |line| line.strip }
+  end
+
+  def on_branch?
+    at_path { git_with_identity('branch', '-a') }.split(/\n/).grep(/\*/).first.to_s.gsub('*', '').strip
+  end
+
+  def tags
+    at_path { git_with_identity('tag', '-l') }.split(/\n/).map { |line| line.strip }
+  end
+
+  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 !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))
+    end
+    current = at_path { git_with_identity('rev-parse', rev).strip }
+    if @resource.value(:revision)
+      if 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 }
+      end
+      current = @resource.value(:revision) if current == canonical
+    end
+    update_owner_and_excludes
+    return current
+  end
+
+  def update_owner_and_excludes
+    if @resource.value(:owner) or @resource.value(:group)
+      set_ownership
+    end
+    if @resource.value(:excludes)
+      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 -oStrictHostKeyChecking=no -oPasswordAuthentication=no -oKbdInteractiveAuthentication=no -oChallengeResponseAuthentication=no -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
+    elsif @resource.value(:user)
+      su(@resource.value(:user), '-c', "git #{args.join(' ')}" )
+    else
+      git(*args)
+    end
+  end
+end

modules/vcsrepo/lib/puppet/provider/vcsrepo/hg.rb

+require File.join(File.dirname(__FILE__), '..', 'vcsrepo')
+
+Puppet::Type.type(:vcsrepo).provide(:hg, :parent => Puppet::Provider::Vcsrepo) do
+  desc "Supports Mercurial repositories"
+
+  optional_commands   :hg => 'hg'
+  has_features :reference_tracking
+
+  def create
+    if !@resource.value(:source)
+      create_repository(@resource.value(:path))
+    else
+      clone_repository(@resource.value(:revision))
+    end
+    update_owner
+  end
+
+  def working_copy_exists?
+    File.directory?(File.join(@resource.value(:path), '.hg'))
+  end
+
+  def exists?
+    working_copy_exists?
+  end
+
+  def destroy
+    FileUtils.rm_rf(@resource.value(:path))
+  end
+
+  def latest?
+    at_path do
+      return self.revision == self.latest
+    end
+  end
+
+  def latest
+    at_path do
+      begin
+        hg('incoming', '--branch', '.', '--newest-first', '--limit', '1')[/^changeset:\s+(?:-?\d+):(\S+)/m, 1]
+      rescue Puppet::ExecutionFailure
+        # If there are no new changesets, return the current nodeid
+        self.revision
+      end
+    end
+  end
+
+  def revision
+    at_path do
+      current = hg('parents')[/^changeset:\s+(?:-?\d+):(\S+)/m, 1]
+      desired = @resource.value(:revision)
+      if desired
+        # Return the tag name if it maps to the current nodeid
+        mapped = hg('tags')[/^#{Regexp.quote(desired)}\s+\d+:(\S+)/m, 1]
+        if current == mapped
+          desired
+        else
+          current
+        end
+      else
+        current
+      end
+    end
+  end
+
+  def revision=(desired)
+    at_path do
+      begin
+        hg('pull')
+      rescue
+      end
+      begin
+        hg('merge')
+      rescue Puppet::ExecutionFailure
+        # If there's nothing to merge, just skip
+      end
+      hg('update', '--clean', '-r', desired)
+    end
+    update_owner
+  end
+
+  private
+
+  def create_repository(path)
+    hg('init', path)
+  end
+
+  def clone_repository(revision)
+    args = ['clone']
+    if revision
+      args.push('-u', revision)
+    end
+    args.push(@resource.value(:source),
+              @resource.value(:path))
+    hg(*args)
+  end
+
+  def update_owner
+    if @resource.value(:owner) or @resource.value(:group)
+      set_ownership
+    end
+  end
+
+end

modules/vcsrepo/lib/puppet/provider/vcsrepo/svn.rb

+require File.join(File.dirname(__FILE__), '..', 'vcsrepo')
+
+Puppet::Type.type(:vcsrepo).provide(:svn, :parent => Puppet::Provider::Vcsrepo) do
+  desc "Supports Subversion repositories"
+
+  optional_commands :svn      => 'svn',
+           :svnadmin => 'svnadmin'
+
+  has_features :filesystem_types, :reference_tracking, :basic_auth
+
+  def create
+    if !@resource.value(:source)
+      create_repository(@resource.value(:path))
+    else
+      checkout_repository(@resource.value(:source),
+                          @resource.value(:path),
+                          @resource.value(:revision))
+    end
+    update_owner
+  end
+
+  def working_copy_exists?
+    File.directory?(File.join(@resource.value(:path), '.svn'))
+  end
+
+  def exists?
+    working_copy_exists?
+  end
+
+  def destroy
+    FileUtils.rm_rf(@resource.value(:path))
+  end
+
+  def latest?
+    at_path do
+      if self.revision < self.latest then
+        return false
+      else
+        return true
+      end
+    end
+  end
+
+  def buildargs
+    args = ['--non-interactive']
+    if @resource.value(:basic_auth_username) && @resource.value(:basic_auth_password)
+      args.push('--username', @resource.value(:basic_auth_username))
+      args.push('--password', @resource.value(:basic_auth_password))
+      args.push('--no-auth-cache')
+    end
+    return args
+  end
+
+  def latest
+    args = buildargs.push('info', '-r', 'HEAD')
+    at_path do
+      svn(*args)[/^Last Changed Rev:\s+(\d+)/m, 1]
+    end
+  end
+
+  def revision
+    args = buildargs.push('info')
+    at_path do
+      svn(*args)[/^Last Changed Rev:\s+(\d+)/m, 1]
+    end
+  end
+
+  def revision=(desired)
+    args = buildargs.push('update', '-r', desired)
+    at_path do
+      svn(*args)
+    end
+    update_owner
+  end
+
+  private
+
+  def checkout_repository(source, path, revision)
+    args = buildargs.push('checkout')
+    if revision
+      args.push('-r', revision)
+    end
+    args.push(source, path)
+    svn(*args)
+  end
+
+  def create_repository(path)
+    args = ['create']
+    if @resource.value(:fstype)
+      args.push('--fs-type', @resource.value(:fstype))
+    end
+    args << path
+    svnadmin(*args)
+  end
+
+  def update_owner
+    if @resource.value(:owner) or @resource.value(:group)
+      set_ownership
+    end
+  end
+end

modules/vcsrepo/lib/puppet/type/vcsrepo.rb

+require 'pathname'
+
+Puppet::Type.newtype(:vcsrepo) do
+  desc "A local version control repository"
+
+  feature :gzip_compression,
+          "The provider supports explicit GZip compression levels"
+  feature :basic_auth,
+          "The provider supports HTTP Basic Authentication"
+  feature :bare_repositories,
+          "The provider differentiates between bare repositories
+          and those with working copies",
+          :methods => [:bare_exists?, :working_copy_exists?]
+
+  feature :filesystem_types,
+          "The provider supports different filesystem types"
+
+  feature :reference_tracking,
+          "The provider supports tracking revision references that can change
+           over time (eg, some VCS tags and branch names)"
+
+  feature :ssh_identity,
+          "The provider supports a configurable SSH identity file"
+
+  feature :user,
+          "The provider can run as a different user"
+
+  feature :modules,
+          "The repository contains modules that can be chosen of"
+
+  feature :multiple_remotes,
+          "The repository tracks multiple remote repositories"
+
+  ensurable do
+    attr_accessor :latest
+
+    def insync?(is)
+      @should ||= []
+
+      case should
+        when :present
+          return true unless [:absent, :purged, :held].include?(is)
+        when :latest
+          if is == :latest
+            return true
+          else
+            return false
+          end
+		when :bare
+		  return is == :bare
+      end
+    end
+
+    newvalue :present do
+      notice "Creating repository from present"
+      provider.create
+    end
+
+    newvalue :bare, :required_features => [:bare_repositories] do
+	  if !provider.exists?
+        provider.create
+      end
+    end
+
+    newvalue :absent do
+      provider.destroy
+    end
+
+    newvalue :latest, :required_features => [:reference_tracking] do
+      if provider.exists?
+        if provider.respond_to?(:update_references)
+          provider.update_references
+        end
+        if provider.respond_to?(:latest?)
+            reference = provider.latest || provider.revision
+        else
+          reference = resource.value(:revision) || provider.revision
+        end
+        notice "Updating to latest '#{reference}' revision"
+        provider.revision = reference
+      else
+        notice "Creating repository from latest"
+        provider.create
+      end
+    end
+
+    def retrieve
+      prov = @resource.provider
+      if prov
+        if prov.working_copy_exists?
+          (@should.include?(:latest) && prov.latest?) ? :latest : :present
+        elsif prov.class.feature?(:bare_repositories) and prov.bare_exists?
+          :bare
+        else
+          :absent
+        end
+      else
+        raise Puppet::Error, "Could not find provider"
+      end
+    end
+
+  end
+
+  newparam :path do
+    desc "Absolute path to repository"
+    isnamevar
+    validate do |value|
+      path = Pathname.new(value)
+      unless path.absolute?
+        raise ArgumentError, "Path must be absolute: #{path}"
+      end
+    end
+  end
+
+  newparam :source do
+    desc "The source URI for the repository"
+  end
+
+  newparam :fstype, :required_features => [:filesystem_types] do
+    desc "Filesystem type"
+  end
+
+  newproperty :revision do
+    desc "The revision of the repository"
+    newvalue(/^\S+$/)
+  end
+
+  newparam :owner do
+    desc "The user/uid that owns the repository files"
+  end
+
+  newparam :group do
+    desc "The group/gid that owns the repository files"
+  end
+
+  newparam :user do
+    desc "The user to run for repository operations"
+  end
+
+  newparam :excludes do
+    desc "Files to be excluded from the repository"
+  end
+
+  newparam :force do
+    desc "Force repository creation, destroying any files on the path in the process."
+    newvalues(:true, :false)
+    defaultto false
+  end
+
+  newparam :compression, :required_features => [:gzip_compression] do
+    desc "Compression level"
+    validate do |amount|
+      unless Integer(amount).between?(0, 6)
+        raise ArgumentError, "Unsupported compression level: #{amount} (expected 0-6)"
+      end
+    end
+  end
+
+  newparam :basic_auth_username, :required_features => [:basic_auth] do
+    desc "HTTP Basic Auth username"
+  end
+
+  newparam :basic_auth_password, :required_features => [:basic_auth] do
+    desc "HTTP Basic Auth password"
+  end
+
+  newparam :identity, :required_features => [:ssh_identity] do
+    desc "SSH identity file"
+  end
+
+  newparam :module, :required_features => [:modules] do
+    desc "The repository module to manage"
+  end
+
+  newparam :remote, :required_features => [:multiple_remotes] do
+    desc "The remote repository to track"
+    defaultto "origin"
+  end
+
+end

modules/vcsrepo/metadata.json

+{
+  "name": "puppetlabs/vcsrepo",
+  "version": "0.1.2",
+  "source": "UNKNOWN",
+  "author": "puppetlabs",
+  "license": "Apache License, Version 2.0",
+  "summary": "UNKNOWN",
+  "description": "UNKNOWN",
+  "project_page": "UNKNOWN",
+  "dependencies": [
+
+  ],
+  "types": [
+    {
+      "name": "vcsrepo",
+      "doc": "A local version control repository",
+      "properties": [
+        {
+          "name": "ensure",
+          "doc": "  Valid values are `present`, `bare`, `absent`, `latest`."
+        },
+        {
+          "name": "revision",
+          "doc": "The revision of the repository  Values can match `/^\\S+$/`."
+        }
+      ],
+      "parameters": [
+        {
+          "name": "path",
+          "doc": "Absolute path to repository"
+        },
+        {
+          "name": "source",
+          "doc": "The source URI for the repository"
+        },
+        {
+          "name": "fstype",
+          "doc": "Filesystem type  Requires features filesystem_types."
+        },
+        {
+          "name": "owner",
+          "doc": "The user/uid that owns the repository files"
+        },
+        {
+          "name": "group",
+          "doc": "The group/gid that owns the repository files"
+        },
+        {
+          "name": "user",
+          "doc": "The user to run for repository operations"
+        },
+        {
+          "name": "excludes",
+          "doc": "Files to be excluded from the repository"
+        },
+        {
+          "name": "force",
+          "doc": "Force repository creation, destroying any files on the path in the process.  Valid values are `true`, `false`."
+        },
+        {
+          "name": "compression",
+          "doc": "Compression level  Requires features gzip_compression."
+        },
+        {
+          "name": "basic_auth_username",
+          "doc": "HTTP Basic Auth username  Requires features basic_auth."
+        },
+        {
+          "name": "basic_auth_password",
+          "doc": "HTTP Basic Auth password  Requires features basic_auth."
+        },
+        {
+          "name": "identity",
+          "doc": "SSH identity file  Requires features ssh_identity."
+        },
+        {
+          "name": "module",
+          "doc": "The repository module to manage  Requires features modules."
+        },
+        {
+          "name": "remote",
+          "doc": "The remote repository to track  Requires features multiple_remotes."
+        }
+      ],
+      "providers": [
+        {
+          "name": "bzr",
+          "doc": "Supports Bazaar repositories\n\nRequired binaries: `bzr`.  Supported features: `reference_tracking`."
+        },
+        {
+          "name": "cvs",
+          "doc": "Supports CVS repositories/workspaces\n\nRequired binaries: `cvs`.  Supported features: `gzip_compression`, `modules`, `reference_tracking`."
+        },
+        {
+          "name": "dummy",
+          "doc": "Dummy default provider\n\nDefault for `vcsrepo` == `dummy`."
+        },
+        {
+          "name": "git",
+          "doc": "Supports Git repositories\n\nRequired binaries: `git`, `su`.  Supported features: `bare_repositories`, `multiple_remotes`, `reference_tracking`, `ssh_identity`, `user`."
+        },
+        {
+          "name": "hg",
+          "doc": "Supports Mercurial repositories\n\nRequired binaries: `hg`.  Supported features: `reference_tracking`."
+        },
+        {
+          "name": "svn",
+          "doc": "Supports Subversion repositories\n\nRequired binaries: `svn`, `svnadmin`.  Supported features: `basic_auth`, `filesystem_types`, `reference_tracking`."
+        }
+      ]
+    }
+  ],
+  "checksums": {
+    "Gemfile": "5267cfa2cd3ba585074266d1953d8c03",
+    "Gemfile.lock": "fd2fec7051e81117a8e58479730797a2",
+    "LICENSE": "b8d96fef1f55096f9d39326408122136",
+    "Modulefile": "fb14134bceb39eedff322332fab7ea34",
+    "README.BZR.markdown": "97f638d169a1c39d461c3f2c0e2ec32f",
+    "README.CVS.markdown": "720eeee9129c1c474086444efd825de4",
+    "README.GIT.markdown": "7ee485d38000553a28e2d539b0a1860a",
+    "README.HG.markdown": "ef123968ea2c29275a6c914515798e2d",
+    "README.SVN.markdown": "daee8377e9dc1f27224f145452d2e3f4",
+    "README.markdown": "e031416a5be95d1fbe9947732e7ee9eb",
+    "Rakefile": "cc0e9a5225430b6a13c74ae74032ed33",
+    "examples/bzr/branch.pp": "05c66419324a576b9b28df876673580d",
+    "examples/bzr/init_repo.pp": "fadd2321866ffb0aacff698d2dc1f0ca",
+    "examples/cvs/local.pp": "7fbde03a5c71edf168267ae42d0bbcbc",
+    "examples/cvs/remote.pp": "491f18f752752bec6133a88de242c44d",
+    "examples/git/bare_init.pp": "7cf56abffdf99f379153166f18f961f8",
+    "examples/git/clone.pp": "0e3181990c095efee1498ccfca5897fb",
+    "examples/git/working_copy_init.pp": "99d92d9957e78a0c03f9cbed989c79ca",
+    "examples/hg/clone.pp": "c92bbd704a4c2da55fff5f45955ce6d1",
+    "examples/hg/init_repo.pp": "bf5fa0ab48a2f5a1ccb63768d961413d",
+    "examples/svn/checkout.pp": "9ef7a8fbd3a763fa3894efa864047023",
+    "examples/svn/server.pp": "94b26f6e50d5e411b33b1ded1bc2138a",
+    "lib/puppet/provider/vcsrepo/bzr.rb": "99dd23d88e88f3cfd4c610d7eef0531f",
+    "lib/puppet/provider/vcsrepo/cvs.rb": "60525f5be2bbbae7916cd09895309ede",
+    "lib/puppet/provider/vcsrepo/dummy.rb": "2f8159468d6ecc8087debde858a80dd6",
+    "lib/puppet/provider/vcsrepo/git.rb": "897973e79c87f852ae4d98f5b722096f",
+    "lib/puppet/provider/vcsrepo/hg.rb": "ea7524cd71732abb3e8670eeaaf37ac6",
+    "lib/puppet/provider/vcsrepo/svn.rb": "8533be9aaa1c632d9be9e46d3e35c2f6",
+    "lib/puppet/provider/vcsrepo.rb": "f5b8a90080b8c27e2656af8605148928",
+    "lib/puppet/type/vcsrepo.rb": "83b3f41d92989fa7ca80d8b6fb8f7db9",
+    "spec/fixtures/bzr_version_info.txt": "5edb13429faf2f0b9964b4326ef49a65",
+    "spec/fixtures/git_branch_a.txt": "2371229e7c1706c5ab8f90f0cd57230f",
+    "spec/fixtures/hg_parents.txt": "efc28a1bd3f1ce7fb4481f76feed3f6e",
+    "spec/fixtures/hg_tags.txt": "8383048b15adb3d58a92ea0c8b887537",
+    "spec/fixtures/manifests/site.pp": "d41d8cd98f00b204e9800998ecf8427e",
+    "spec/fixtures/svn_info.txt": "978db25720a098e5de48388fe600c062",
+    "spec/spec.opts": "a600ded995d948e393fbe2320ba8e51c",
+    "spec/spec_helper.rb": "a35385c068516f356cf6667b82f83db7",
+    "spec/support/filesystem_helpers.rb": "eb2a8eb3769865004c84e971ccb1396c",
+    "spec/support/fixture_helpers.rb": "61781d99ea201e9da6d23c64a25cc285",
+    "spec/support/provider_example_group.rb": "fab04f82b6531fbb127445345ce6c50c",
+    "spec/unit/puppet/provider/vcsrepo/bzr_spec.rb": "b112a7ae4769eff1da41db7867f06fff",
+    "spec/unit/puppet/provider/vcsrepo/cvs_spec.rb": "ea38bf0b060adcf7c9e43e5ed245c132",
+    "spec/unit/puppet/provider/vcsrepo/dummy_spec.rb": "52ce05ffc829113dc5e74cb4a51c0a71",
+    "spec/unit/puppet/provider/vcsrepo/git_spec.rb": "0713fa3542bace46c15bff00914c3630",
+    "spec/unit/puppet/provider/vcsrepo/hg_spec.rb": "cb4e25597dca41fb75a8d82ac409a14c",
+    "spec/unit/puppet/provider/vcsrepo/svn_spec.rb": "67a40762923bf7cbe8199c3f1d14e943",
+    "spec/unit/puppet/type/README.markdown": "de26a7643813abd6c2e7e28071b1ef94"
+  }
+}

modules/vcsrepo/spec/fixtures/bzr_version_info.txt

+revision-id: menesis@pov.lt-20100309191856-4wmfqzc803fj300x
+date: 2010-03-09 21:18:56 +0200
+build-date: 2010-03-14 00:42:43 -0800
+revno: 2634
+branch-nick: mytest

modules/vcsrepo/spec/fixtures/git_branch_a.txt

+  feature/foo
+  feature/bar
+  feature/baz
+  feature/quux
+  only/local
+* master
+  refactor/foo
+  origin/HEAD
+  origin/feature/foo
+  origin/feature/bar
+  origin/feature/baz
+  origin/feature/quux
+  origin/only/remote
+  origin/master

modules/vcsrepo/spec/fixtures/hg_parents.txt

+changeset:   3:34e6012c783a
+parent:      2:21ea4598c962
+parent:      1:9d0ff0028458
+user:        Test User <test.user@example.com>
+date:        Fri Aug 07 13:13:02 2009 -0400
+summary:     merge

modules/vcsrepo/spec/fixtures/hg_tags.txt

+tip                             1019:bca3f20b249b
+0.9.1                           1017:76ce7cca95d8
+0.9                             1001:dbaa6f4ec585
+0.8                              839:65b66ac0fc83
+0.7.1                            702:e1357f00129f
+0.7                              561:7b2af3b4c968
+0.6.3                            486:e38077f4e4aa
+0.6.2                            405:07bb099b7b10
+0.6.1                            389:93750f3fbbe2
+0.6                              369:34e6012c783a
+0.5.3                            321:5ffa6ae7e699
+0.5.2                            318:fdc2c2e4cebe
+0.5.1                            315:33a5ea0cbe7a
+0.5                              313:47490716f4c9
+0.4                              240:47fa3a14cc63
+0.3.1                            132:bc231db18e1c
+0.3                              130:661615e510dd
+0.2                               81:f98d13b442f6
Add a comment to this file

modules/vcsrepo/spec/fixtures/manifests/site.pp

Empty file added.

modules/vcsrepo/spec/fixtures/svn_info.txt

+Path: .
+URL: http://example.com/svn/trunk
+Repository Root: http://example.com/svn
+Repository UUID: 75246ace-e253-0410-96dd-a7613ca8dc81
+Revision: 4
+Node Kind: directory
+Schedule: normal
+Last Changed Author: jon
+Last Changed Rev: 3
+Last Changed Date: 2008-08-07 11:34:25 -0700 (Thu, 07 Aug 2008)

modules/vcsrepo/spec/spec.opts

+--format
+s
+--colour
+--loadby
+mtime
+--backtrace

modules/vcsrepo/spec/spec_helper.rb

+require 'pathname'
+dir = Pathname.new(__FILE__).parent
+$LOAD_PATH.unshift(dir, dir + 'lib', dir + '../lib')
+
+require 'test/unit'
+require 'mocha'
+require 'puppet'
+gem 'rspec', '>= 1.2.9'
+require 'spec/autorun'
+
+Dir[File.join(File.dirname(__FILE__), 'support', '*.rb')].each do |support_file|
+  require support_file
+end
+
+Spec::Runner.configure do |config|
+  config.mock_with :mocha
+  config.include(FixtureHelpers)
+  config.include(FilesystemHelpers)
+end
+
+# We need this because the RAL uses 'should' as a method.  This
+# allows us the same behaviour but with a different method name.
+class Object
+    alias :must :should
+end

modules/vcsrepo/spec/support/filesystem_helpers.rb

+module FilesystemHelpers
+
+  def expects_chdir(path = resource.value(:path))
+    Dir.expects(:chdir).with(path).at_least_once.yields
+  end
+
+  def expects_mkdir(path = resource.value(:path))
+    Dir.expects(:mkdir).with(path).at_least_once
+  end
+
+  def expects_rm_rf(path = resource.value(:path))
+    FileUtils.expects(:rm_rf).with(path)
+  end
+
+  def expects_directory?(returns = true, path = resource.value(:path))
+    File.expects(:directory?).with(path).returns(returns)
+  end
+end

modules/vcsrepo/spec/support/fixture_helpers.rb

+module FixtureHelpers
+
+  def fixture(name, ext = '.txt')
+    File.read(File.join(File.dirname(__FILE__), '..', 'fixtures', name.to_s + ext))
+  end
+
+end

modules/vcsrepo/spec/support/provider_example_group.rb

+class ProviderExampleGroup < Spec::Example::ExampleGroup
+
+  # Allow access to the current resource
+  attr_reader :resource
+
+  # Build up the values for the resource in this scope
+  before :each do
+    resource_hash = example_group_hierarchy.inject({}) do |memo, klass|
+      memo.merge(klass.options[:resource] || {})
+    end
+    full_hash = resource_hash.merge(:provider => described_class.name)
+    @resource = described_class.resource_type.new(full_hash)
+  end
+
+  # Build the provider
+  subject { described_class.new(@resource) }
+
+  # Allow access to it via +provider+
+  alias :provider :subject
+
+  # Generate a context for a provider operating on a resource with:
+  #
+  # call-seq:
+  #
+  #   # A parameter/property set (when the value isn't important)
+  #   resource_with :source do
+  #     # ...
+  #   end
+  #
+  #   # A parameter/property set to a specific value
+  #   resource_with :source => 'a-specific-value' do
+  #     # ...
+  #   end
+  #
+  # Note: Choose one or the other (mixing will create two separate contexts)
+  #
+  def self.resource_with(*params, &block)
+    params_with_values = params.last.is_a?(Hash) ? params.pop : {}
+    build_value_context(params_with_values, &block)
+    build_existence_context(*params, &block)
+  end
+
+  def self.build_existence_context(*params, &block) #:nodoc:
+    unless params.empty?
+      text = params.join(', ')
+      placeholders = params.inject({}) { |memo, key| memo.merge(key => 'an-unimportant-value') }
+      context("and with a #{text}", {:resource => placeholders}, &block)
+    end
+  end
+
+  def self.build_value_context(params = {}, &block) #:nodoc:
+    unless params.empty?
+      text = params.map { |k, v| "#{k} => #{v.inspect}" }.join(' and with ')
+      context("and with #{text}", {:resource => params}, &block)
+    end
+  end
+
+
+  # Generate a context for a provider operating on a resource without
+  # a given parameter/property.
+  #
+  # call-seq:
+  #
+  #   resource_without :source do
+  #     # ...
+  #   end
+  #
+  def self.resource_without(field, &block)
+    context("and without a #{field}", &block)
+  end
+
+end
+
+Spec::Example::ExampleGroupFactory.register(:provider, ProviderExampleGroup)
+
+# Outside wrapper to lookup a provider and start the spec using ProviderExampleGroup
+def describe_provider(type_name, provider_name, options = {}, &block)
+  provider_class = Puppet::Type.type(type_name).provider(provider_name)
+  describe(provider_class, options.merge(:type => :provider), &block)
+end

modules/vcsrepo/spec/unit/puppet/provider/vcsrepo/bzr_spec.rb

+require 'spec_helper'
+
+describe_provider :vcsrepo, :bzr, :resource => {:path => '/tmp/vcsrepo'} do
+
+  describe 'creating' do
+    resource_with :source do
+      resource_with :revision do
+        it "should execute 'bzr clone -r' with the revision" do
+          provider.expects(:bzr).with('branch', '-r', resource.value(:revision), resource.value(:source), resource.value(:path))
+          provider.create
+        end
+      end
+      resource_without :revision do
+        it "should just execute 'bzr clone' without a revision" do
+          provider.expects(:bzr).with('branch', resource.value(:source), resource.value(:path))
+          provider.create
+        end
+      end
+    end
+    resource_without :source do
+      it "should execute 'bzr init'" do
+        provider.expects(:bzr).with('init', resource.value(:path))
+        provider.create
+      end
+    end
+  end
+
+  describe 'destroying' do
+    it "it should remove the directory" do
+      expects_rm_rf
+      provider.destroy
+    end
+  end
+
+  describe "checking existence" do
+    it "should check for the directory" do
+      expects_directory?(true, File.join(resource.value(:path), '.bzr'))
+      provider.exists?
+    end
+  end
+
+  describe "checking the revision property" do
+    before do
+      expects_chdir
+      provider.expects(:bzr).with('version-info').returns(fixture(:bzr_version_info))
+      @current_revid = 'menesis@pov.lt-20100309191856-4wmfqzc803fj300x'
+    end
+    context "when given a non-revid as the resource revision", :resource => {:revision => '2634'} do
+      context "when its revid is not different than the current revid" do
+        before do
+          provider.expects(:bzr).with('revision-info', resource.value(:revision)).returns("#{resource.value(:revision)} menesis@pov.lt-20100309191856-4wmfqzc803fj300x\n")
+        end
+        it "should return the ref" do
+          provider.revision.should == resource.value(:revision)
+        end
+      end
+      context "when its revid is different than the current revid", :resource => {:revision => '2636'} do
+        it "should return the current revid" do
+          provider.expects(:bzr).with('revision-info', resource.value(:revision)).returns("2635 foo\n")
+          provider.revision.should == @current_revid
+        end
+      end
+    end
+    context "when given a revid as the resource revision" do
+      context "when it is the same as the current revid", :resource => {:revision => 'menesis@pov.lt-20100309191856-4wmfqzc803fj300x'} do
+        before do
+          provider.expects(:bzr).with('revision-info', resource.value(:revision)).returns("1234 #{resource.value(:revision)}\n")
+        end
+        it "should return it" do
+          provider.revision.should == resource.value(:revision)
+        end
+      end
+      context "when it is not the same as the current revid", :resource => {:revision => 'menesis@pov.lt-20100309191856-4wmfqzc803fj300y'} do
+        it "should return the current revid" do
+          provider.expects(:bzr).with('revision-info', resource.value(:revision)).returns("2636 foo\n")
+          provider.revision.should == @current_revid
+        end
+      end
+    end
+  end
+
+  describe "setting the revision property" do
+    it "should use 'bzr update -r' with the revision" do
+      revision = 'somerev'
+      provider.expects(:bzr).with('update', '-r', revision, resource.value(:path))
+      provider.revision = revision
+    end
+  end
+
+end

modules/vcsrepo/spec/unit/puppet/provider/vcsrepo/cvs_spec.rb

+require 'spec_helper'
+
+describe_provider :vcsrepo, :cvs, :resource => {:path => '/tmp/vcsrepo'} do
+
+  describe 'creating' do
+    context "with a source", :resource => {:source => ':ext:source@example.com:/foo/bar'} do
+      resource_with :revision do
+        it "should execute 'cvs checkout' and 'cvs update -r'" do
+          provider.expects(:cvs).with('-d', resource.value(:source), 'checkout', '-r', 'an-unimportant-value', '-d', 'vcsrepo', 'bar')
+          expects_chdir(File.dirname(resource.value(:path)))
+          #provider.expects(:cvs).with('update', '-r', resource.value(:revision), '.')
+          provider.create
+        end
+      end
+
+      resource_without :revision do
+        it "should just execute 'cvs checkout' without a revision" do
+          provider.expects(:cvs).with('-d', resource.value(:source), 'checkout', '-d', File.basename(resource.value(:path)), File.basename(resource.value(:source)))
+          provider.create
+        end
+      end
+
+      context "with a compression", :resource => {:compression => '3'} do
+        it "should just execute 'cvs checkout' without a revision" do
+          provider.expects(:cvs).with('-d', resource.value(:source), '-z', '3', 'checkout', '-d', File.basename(resource.value(:path)), File.basename(resource.value(:source)))
+          provider.create
+        end
+      end
+    end
+
+    context "when a source is not given" do
+      it "should execute 'cvs init'" do
+        provider.expects(:cvs).with('-d', resource.value(:path), 'init')
+        provider.create
+      end
+    end
+  end
+
+  describe 'destroying' do
+    it "it should remove the directory" do
+      expects_rm_rf