Last active
August 29, 2015 14:28
-
-
Save jparrill/83bc14f0078f4caa55df to your computer and use it in GitHub Desktop.
A custom Puppet function to create a copy of files in a path without desired extension. Path=./<module>/lib/puppet/parser/functions/copy_config_files.rb.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'find' | |
| require 'fileutils' | |
| module Puppet::Parser::Functions | |
| newfunction(:copy_config_files) do |args| | |
| function_notice(["Using copy_config_files script..."]) | |
| ## Get all files with extension as follows | |
| config_files = [] | |
| Find.find(args[1]) do |path| | |
| config_files << path if path =~ /.*\.template$/ | |
| end ## End Find | |
| ## Validate or Copy files | |
| # Validate will check if file without extension exists | |
| # Copy will create a new file in the same path without extension | |
| case args[0] | |
| when 'copy' then | |
| config_files.each do |src_file| | |
| dest_file = src_file.sub(/\.template$/, '') | |
| FileUtils.copy(src_file, dest_file) unless File.exists?(dest_file) | |
| function_notice(["Creating: '#{dest_file}'"]) unless File.exists?(dest_file) | |
| end ## End Each | |
| when 'validate' then | |
| valid = true | |
| config_files.each do |org_file| | |
| new_file = org_file.sub(/\.template$/, '') | |
| valid = false unless File.exists?(new_file) | |
| function_notice(["Fail!: '#{new_file}'"]) unless File.exists?(new_file) | |
| end ## End Each | |
| raise Puppet::ParseError, "Error Validating copied files" | |
| else | |
| raise Puppet::ParseError, "You must to choose copy or validate as first argument" | |
| end ## End Case | |
| end ## End Function | |
| end ## End Module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment