From cee6ec6d30287cb79c6fa8aa768b0bd300cf6b08 Mon Sep 17 00:00:00 2001 From: Jordi Prats Date: Mon, 8 Jan 2018 14:37:12 +0100 Subject: [PATCH] gitclone type --- CHANGELOG.md | 4 ++ .../provider/gitclone/gitclone_linux.rb | 43 +++++++++++++ lib/puppet/type/gitclone.rb | 61 +++++++++++++++++++ metadata.json | 2 +- 4 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 lib/puppet/provider/gitclone/gitclone_linux.rb create mode 100644 lib/puppet/type/gitclone.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index a58d9ef..2ca5740 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## 0.1.11 + +* added **git clone** type to clone repos + ## 0.1.10 * added **eypconf_servergroup** fact diff --git a/lib/puppet/provider/gitclone/gitclone_linux.rb b/lib/puppet/provider/gitclone/gitclone_linux.rb new file mode 100644 index 0000000..e438112 --- /dev/null +++ b/lib/puppet/provider/gitclone/gitclone_linux.rb @@ -0,0 +1,43 @@ +Puppet::Type.type(:gitclone).provide(:gitclone_linux) do + + def check_created_file(file) + if File.exists?(file+'/.git') + self.debug("Found #{file}") + return true + else + self.debug("Not found #{file}") + return false + end + end + + def run_gitclone_command(url) + + command = ["git"] + command.push("clone") + command.push(url) + command.push(resource[:creates]) + + if resource[:cwd] + Dir.chdir resource[:cwd] do + run_command(command) + end + else + run_command(command) + end + end + + private + + def run_command(command) + command = command.join ' ' + output = Puppet::Util::Execution.execute(command, { + :uid => 'root', + :gid => 'root', + :failonfail => false, + :combine => true, + :override_locale => true, + }) + [output, $CHILD_STATUS.dup] + end + +end diff --git a/lib/puppet/type/gitclone.rb b/lib/puppet/type/gitclone.rb new file mode 100644 index 0000000..bb752f2 --- /dev/null +++ b/lib/puppet/type/gitclone.rb @@ -0,0 +1,61 @@ +Puppet::Type.newtype(:gitclone) do + @doc = 'clone git repos' + + newparam(:name) do + desc "An arbitrary tag for your own reference" + isnamevar + end + + newproperty(:url) do + desc 'URL to download' + + defaultto { @resource[:name] } + + # If needing to run the SQL command, return a fake value that will trigger + # a sync, else return the expected SQL command so no sync takes place + def retrieve + if @resource.should_run_gitclone + return :notrun + else + return self.should + end + end + + def sync + output, status = provider.run_gitclone_command(value) + self.fail("Error executing git clone; returned #{status}: '#{output}'") unless status == 0 + end + end + + newparam(:cwd, :parent => Puppet::Parameter::Path) do + desc "The working directory under which the git clone command should be executed." + defaultto("/tmp") + end + + newparam(:creates) do + desc "where to clone the repo" + + def matches(value) + provider.check_created_file(value) + end + end + + newparam(:refreshonly, :boolean => true) do + desc "If 'true', it will only be executed via a notify/subscribe event." + + defaultto(:false) + newvalues(:true, :false) + end + + def should_run_gitclone(refreshing = false) + creates_param = @parameters[:creates] + return false if !creates_param.nil? && !creates_param.value.nil? && creates_param.matches(creates_param.value) + return false if !refreshing && @parameters[:refreshonly].value == :true + true + end + + def refresh + self.property(:command).sync if self.should_run_gitclone(true) + end + +end diff --git a/metadata.json b/metadata.json index a585710..adb23f0 100644 --- a/metadata.json +++ b/metadata.json @@ -1,6 +1,6 @@ { "name": "eyp-eyplib", - "version": "0.1.10", + "version": "0.1.11", "author": "eyp", "summary": "Utility functions for puppet modules", "license": "Apache-2.0",