Initial commit
authorroot <root@localhost.localdomain>
Tue, 23 Feb 2010 01:40:07 +0000 (19:40 -0600)
committerroot <root@localhost.localdomain>
Tue, 23 Feb 2010 01:40:07 +0000 (19:40 -0600)
lib/puppet/provider/vcsrepo/git.rb [new file with mode: 0644]
lib/puppet/type/vcsrepo.rb [new file with mode: 0644]

diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb
new file mode 100644 (file)
index 0000000..298d1dc
--- /dev/null
@@ -0,0 +1,73 @@
+Puppet::Type.type(:vcsrepo).provide(:git) do
+  desc "Supports Git repositories"
+
+  commands :git => 'git'
+
+  def create
+    if !@resource.value(:source)
+      init_repository(@resource.value(:path))
+    else
+      clone_repository(@resource.value(:source), @resource.value(:path))
+      reset(@resource.value(:revision)) if @resource.value(:revision)
+    end
+  end
+
+  def exists?
+    File.directory?(@resource.value(:path))
+  end
+
+  def destroy
+    FileUtils.rm_rf(@resource.value(:path))
+  end
+  
+  def revision
+    current = Dir.chdir(@resource.value(:path)) do
+      git('rev-parse', 'HEAD')
+    end
+    canonical = Dir.chdir(@resource.value(:path)) do
+      git('rev-parse', @resource.value(:revision))
+    end
+    if current == canonical
+      @resource.value(:revision)
+    else
+      current
+    end
+  end
+
+  def revision=(desired)
+    fetch
+    reset(desired)
+  end
+
+  private
+
+  def clone_repository(source, path)
+    parent = File.dirname(path)
+    FileUtils.mkdir_p(parent)
+    git('clone', source, path)
+  end
+
+  def fetch
+    at_path do
+      git('fetch', 'origin')
+    end
+  end
+
+  def init_repository(path)
+    FileUtils.mkdir_p(path)
+    at_path do
+      git('init')
+    end
+  end
+
+  def reset(desired)
+    at_path do
+      git('reset', '--hard', desired)
+    end
+  end
+
+  def at_path(&block)
+    Dir.chdir(@resource.value(:path), &block)
+  end
+
+end
diff --git a/lib/puppet/type/vcsrepo.rb b/lib/puppet/type/vcsrepo.rb
new file mode 100644 (file)
index 0000000..7db571e
--- /dev/null
@@ -0,0 +1,31 @@
+require 'pathname'
+
+Puppet::Type.newtype(:vcsrepo) do
+  desc "A local version control repository"
+
+  ensurable
+
+  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 URL for the repository"
+    validate do |value|
+      URI.parse(value)
+    end
+  end
+
+  newproperty(:revision) do
+    desc "The revision of the repository"
+    newvalue(/^\S+$/)
+  end
+
+end