diff --git a/.gitignore b/.gitignore index edb819a..2fff520 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ /spec/fixtures/manifests /spec/fixtures/modules /Gemfile.lock +/.yardwarns +/.yardoc +/doc diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c48d85..a58d9ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,12 @@ ## 0.1.10 -* added **eypconf_group** fact +* added **eypconf_servergroup** fact +* added **download** type to download files checking if the resulting file is empty ## 0.1.9 -* removed bool2httpd function +* removed **bool2httpd** function ## 0.1.8 diff --git a/README.md b/README.md index bf4560f..06ca27b 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,23 @@ in ERB files you can call this module's functions using: <%= scope.function_bool2onoff([@trace]) %> ``` +to download a file you can use the download type: + +``` +download { 'salt': + url => 'https://repo.saltstack.com/yum/redhat/salt-repo-latest-2.el7.noarch.rpm', + creates => '/tmp/test_repo_salt', +} +``` + ## Reference +### types + +#### download + +download a file using wget checking that the resulting file is not empty + ### functions #### bool2boolstr diff --git a/lib/facter/eypconf_group.rb b/lib/facter/eypconf_group.rb deleted file mode 100644 index 932a634..0000000 --- a/lib/facter/eypconf_group.rb +++ /dev/null @@ -1,25 +0,0 @@ -if File.exists?('/opt/eypconf/id/group.sh') then - nodetype = Facter::Util::Resolution.exec('bash /opt/eypconf/id/group.sh').to_s -else - nodetype = Facter::Util::Resolution.exec('bash -c \'if [ -f /opt/eypconf/id/group ]; then cat /opt/eypconf/id/type | paste -sd,; fi\'') -end - -unless nodetype.nil? or nodetype.empty? - Facter.add('eypconf_group') do - setcode do - nodetype - end - end - - Facter.add('eypconf_group_uppercase') do - setcode do - nodetype.upcase - end - end - - Facter.add('eypconf_group_lowercase') do - setcode do - nodetype.downcase - end - end -end diff --git a/lib/facter/eypconf_servergroup.rb b/lib/facter/eypconf_servergroup.rb new file mode 100644 index 0000000..c43148e --- /dev/null +++ b/lib/facter/eypconf_servergroup.rb @@ -0,0 +1,25 @@ +if File.exists?('/opt/eypconf/id/servergroup.sh') then + servergroup = Facter::Util::Resolution.exec('bash /opt/eypconf/id/servergroup.sh').to_s +else + servergroup = Facter::Util::Resolution.exec('bash -c \'if [ -f /opt/eypconf/id/servergroup ]; then cat /opt/eypconf/id/server; fi\'') +end + +unless servergroup.nil? or servergroup.empty? + Facter.add('eypconf_servergroup') do + setcode do + servergroup + end + end + + Facter.add('eypconf_servergroup_uppercase') do + setcode do + servergroup.upcase + end + end + + Facter.add('eypconf_servergroup_lowercase') do + setcode do + servergroup.downcase + end + end +end diff --git a/lib/puppet/provider/download/download_linux.rb b/lib/puppet/provider/download/download_linux.rb new file mode 100644 index 0000000..3c23a1e --- /dev/null +++ b/lib/puppet/provider/download/download_linux.rb @@ -0,0 +1,47 @@ +Puppet::Type.type(:download).provide(:download_linux) do + + def check_created_file(file) + if File.exists?(file) + if File.zero?(file) + self.debug("Found #{file} but empty") + return false + else + self.debug("Found #{file}") + return true + end + else + self.debug("Not found #{file}") + return false + end + end + + def run_wget_command(url) + + command = ["wget"] + command.push(url) + command.push("-O ", 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/download.rb b/lib/puppet/type/download.rb new file mode 100644 index 0000000..711797f --- /dev/null +++ b/lib/puppet/type/download.rb @@ -0,0 +1,61 @@ +Puppet::Type.newtype(:download) do + @doc = 'download files' + + 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_wget + return :notrun + else + return self.should + end + end + + def sync + output, status = provider.run_wget_command(value) + self.fail("Error executing wget; returned #{status}: '#{output}'") unless status == 0 + end + end + + newparam(:cwd, :parent => Puppet::Parameter::Path) do + desc "The working directory under which the wget command should be executed." + defaultto("/tmp") + end + + newparam(:creates) do + desc "file created" + + 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_wget(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_wget(true) + end + +end