Initial commit
This commit is contained in:
commit
cb2efcdfaa
73
lib/puppet/provider/vcsrepo/git.rb
Normal file
73
lib/puppet/provider/vcsrepo/git.rb
Normal file
@ -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
|
31
lib/puppet/type/vcsrepo.rb
Normal file
31
lib/puppet/type/vcsrepo.rb
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user