Class: GeneratePuppetfile::Bin
- Inherits:
-
Object
- Object
- GeneratePuppetfile::Bin
- Defined in:
- lib/generate_puppetfile/bin.rb
Overview
Internal: The Bin class contains the logic for calling generate_puppetfile at the command line
Constant Summary collapse
- Module_Regex =
Regexp.new("mod ['\"]([a-z0-9_]+\/[a-z0-9_]+)['\"](, ['\"](\\d\.\\d\.\\d)['\"])?", Regexp::IGNORECASE)
- Silence =
('>' + File::NULL.to_str + ' 2>&1 ').freeze
- Puppetfile_Header =
'# Modules discovered by generate-puppetfile'.freeze
- Extras_Note =
'# Discovered elements from existing Puppetfile'.freeze
Instance Method Summary collapse
-
#_download_module(name) ⇒ Object
Private: Download an individual module.
-
#cleanup_workspace ⇒ Object
Public: Remove the workspace (with prejudice).
-
#create_puppetfile(puppetfile_contents) ⇒ Object
Public: Create a Puppetfile on disk The Puppetfile will be called ‘Puppetfile’ in the current working directory.
-
#create_workspace ⇒ Object
Public: Create a temporary workspace for module manipulation.
-
#display_puppetfile(puppetfile_contents) ⇒ Object
Public: Display the generated Puppetfile to STDOUT with delimiters.
-
#download_modules(module_list) ⇒ Object
Public: Download the list of modules and their dependencies to @workspace.
-
#generate_fixtures_data ⇒ Object
Public: Generate a simple fixtures file.
-
#generate_forge_module_output ⇒ Object
Public: generate the list of modules in Puppetfile format from the @workspace.
-
#generate_module_data ⇒ Object
Public: generate the module data the @workspace.
-
#generate_puppetfile_contents(extras) ⇒ Object
Public: Generate a new Puppetfile’s contents based on a list of modules and any extras found in an existing Puppetfile.
-
#initialize(args) ⇒ Bin
constructor
Public: Initialize a new GeneratePuppetfile::Bin.
-
#list_extras(extras) ⇒ Object
Public: Display the list of extra statements found in the Puppetfile.
-
#list_forge_modules(module_list) ⇒ Object
Public: Display the list of Forge modules collected.
-
#read_puppetfile(puppetfile) ⇒ Object
Public: Read and parse the contents of an existing Puppetfile.
-
#run ⇒ Object
Public: Run generate-puppetfile at the command line.
-
#validate(modulename) ⇒ Object
Public: Validates that a provided module name is valid.
-
#verify_puppet_exists ⇒ Object
Public: Verify that Puppet is available in the path.
- #write_fixtures_data(data) ⇒ Object
Constructor Details
#initialize(args) ⇒ Bin
Public: Initialize a new GeneratePuppetfile::Bin
args - Array of command line arguments as Strings to be passed to GeneratePuppetfile::OptParser.parse
Example:
GeneratePuppetfile::Bin.new(ARGV).run
29 30 31 |
# File 'lib/generate_puppetfile/bin.rb', line 29 def initialize(args) @args = args end |
Instance Method Details
#_download_module(name) ⇒ Object
Private: Download an individual module
_download_module
208 209 210 211 212 213 214 |
# File 'lib/generate_puppetfile/bin.rb', line 208 def _download_module(name) command = "puppet module install #{name} " command += @modulepath + Silence puts "Calling '#{command}'" if @options[:debug] system(command) end |
#cleanup_workspace ⇒ Object
Public: Remove the workspace (with prejudice)
272 273 274 |
# File 'lib/generate_puppetfile/bin.rb', line 272 def cleanup_workspace FileUtils.rm_rf(@workspace) end |
#create_puppetfile(puppetfile_contents) ⇒ Object
Public: Create a Puppetfile on disk The Puppetfile will be called ‘Puppetfile’ in the current working directory
121 122 123 124 125 |
# File 'lib/generate_puppetfile/bin.rb', line 121 def create_puppetfile(puppetfile_contents) File.open('Puppetfile', 'w') do |file| file.write puppetfile_contents end end |
#create_workspace ⇒ Object
Public: Create a temporary workspace for module manipulation
267 268 269 |
# File 'lib/generate_puppetfile/bin.rb', line 267 def create_workspace @workspace = Dir.mktmpdir.chomp end |
#display_puppetfile(puppetfile_contents) ⇒ Object
Public: Display the generated Puppetfile to STDOUT with delimiters
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/generate_puppetfile/bin.rb', line 108 def display_puppetfile(puppetfile_contents) puts <<-EOF Your Puppetfile has been generated. Copy and paste between the markers: ======================================================================= #{puppetfile_contents.chomp} ======================================================================= EOF end |
#download_modules(module_list) ⇒ Object
Public: Download the list of modules and their dependencies to @workspace
module_list is an array of forge module names to be downloaded
190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/generate_puppetfile/bin.rb', line 190 def download_modules(module_list) puts "\nInstalling modules. This may take a few minutes.\n\n" unless @options[:silent] @download_errors = '' module_list.each do |name| next if _download_module(name) @download_errors << "There was a problem with the module name '#{name}'.".red + "\n" end if @download_errors != '' @download_errors << ' Check that modules exist as under the listed name, and/or your connectivity to the puppet forge.'.red + "\n\n" @download_errors << 'Here is the PARTIAL Puppetfile that would have been generated.'.red + "\n\n\n" end end |
#generate_fixtures_data ⇒ Object
Public: Generate a simple fixtures file.
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/generate_puppetfile/bin.rb', line 277 def generate_fixtures_data puts "\nGenerating .fixtures.yml using module name #{@options[:modulename]}" unless @options[:silent] # Determine if there are symlinks, either for the default modulename, or for anything in the modulepath symlinks = [] modulepath = '' if (File.exists?('environment.conf') and environment_conf = File.read('environment.conf')) puts "\nGenerating .fixtures.yml for a controlrepo." unless @options[:silent] environment_conf.split("\n").each do |line| modulepath = (line.split('='))[1].gsub(/\s+/,'') if line =~ /^modulepath/ end paths = modulepath.split(':').delete_if { |path| path =~ /^\$/ } paths.each do |path| Dir["#{path}/*"].each do |module_location| module_name = File.basename(module_location) module_path = module_location symlinks << { :name => module_name, :path => '"#{source_dir}/' + module_path + '"', } end end else puts "\nGenerating .fixtures.yml using module name #{@options[:modulename]}." unless @options[:silent] symlinks << { :name => @options[:modulename], :path => '"#{source_dir}"', } end # Header for fixtures file creates symlinks for the controlrepo's modulepath, or for the current module" fixtures_data = "fixtures:\n" if symlinks fixtures_data += " symlinks:\n" symlinks.each do |symlink| fixtures_data += " #{symlink[:name]}: #{symlink[:path]}\n" end end fixtures_data += " forge_modules:\n" if @module_data != {} @module_data.keys.each do |modulename| shortname = modulename.split('/')[1] version = @module_data[modulename] fixtures_data += <<-EOF #{shortname}: repo: "#{modulename}" ref: "#{version}" EOF end fixtures_data end |
#generate_forge_module_output ⇒ Object
Public: generate the list of modules in Puppetfile format from the @workspace
237 238 239 240 241 242 243 |
# File 'lib/generate_puppetfile/bin.rb', line 237 def generate_forge_module_output module_output = '' @module_data.keys.each do |modulename| module_output += "mod '#{modulename}', '#{@module_data[modulename]}'\n" end module_output end |
#generate_module_data ⇒ Object
Public: generate the module data the @workspace
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/generate_puppetfile/bin.rb', line 217 def generate_module_data command = "puppet module list #{@modulepath} 2>#{File::NULL}" puts "Calling '#{command}'" if @options[:debug] module_output = `#{command}` module_output.gsub!(/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/, '') # Strips ANSI color codes modules = {} module_output.each_line do |line| next unless line =~ / \(v/ line.tr!('-', '/') line.gsub!(/^\S* /, '') line += line name, version = line.match(/(\S+) \(v(.+)\)/).captures modules[name] = version $stderr.puts "Module #{name} has a version of #{version}, it may be deprecated. For more information, visit https://forge.puppet.com/#{name}".blue if version =~ /999/ and ! @options[:silent] end modules end |
#generate_puppetfile_contents(extras) ⇒ Object
Public: Generate a new Puppetfile’s contents based on a list of modules and any extras found in an existing Puppetfile.
extras is an array of strings
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/generate_puppetfile/bin.rb', line 249 def generate_puppetfile_contents(extras) puppetfile_contents = <<-EOF forge 'http://forge.puppetlabs.com' #{Puppetfile_Header} EOF puppetfile_contents += generate_forge_module_output puppetfile_contents += "#{Extras_Note}\n" unless extras == [] extras.each do |line| puppetfile_contents += line.to_s end unless extras == [] puppetfile_contents end |
#list_extras(extras) ⇒ Object
Public: Display the list of extra statements found in the Puppetfile.
146 147 148 149 150 151 152 153 154 |
# File 'lib/generate_puppetfile/bin.rb', line 146 def list_extras(extras) unless @options[:silent] || (extras == []) puts "\nExtras found in the existing Puppetfile:\n\n" extras.each do |line| puts " #{line}" end puts '' end end |
#list_forge_modules(module_list) ⇒ Object
Public: Display the list of Forge modules collected.
135 136 137 138 139 140 141 142 143 |
# File 'lib/generate_puppetfile/bin.rb', line 135 def list_forge_modules(module_list) unless @options[:silent] puts "\nListing discovered modules from CLI and/or Puppetfile:\n\n" module_list.each do |name| puts " #{name}" end puts '' end end |
#read_puppetfile(puppetfile) ⇒ Object
Public: Read and parse the contents of an existing Puppetfile
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/generate_puppetfile/bin.rb', line 157 def read_puppetfile(puppetfile) puppetfile_contents = { modules: [], extras: [] } File.foreach(puppetfile) do |line| if Module_Regex.match(line) name = Regexp.last_match(1) print " #{name} looks like a forge module.\n" if @options[:debug] puppetfile_contents[:modules].push(name) else next if line =~ /^forge/ next if line =~ /^\s+$/ next if line =~ /#{Puppetfile_Header}/ next if line =~ /#{Extras_Note}/ puppetfile_contents[:extras].push(line) end end puppetfile_contents end |
#run ⇒ Object
Public: Run generate-puppetfile at the command line.
Returns an Integer exit code for the shell ($?)
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/generate_puppetfile/bin.rb', line 36 def run @options = GeneratePuppetfile::OptParser.parse(@args) helpmsg = "generate-puppetfile: try 'generate-puppetfile --help' for more information." if @args[0].nil? && (! @options[:puppetfile]) $stderr.puts 'generate-puppetfile: No modules or existing Puppetfile specified.'.red puts helpmsg return 1 end unless verify_puppet_exists $stderr.puts "generate-puppetfile: Could not find a 'puppet' executable.".red $stderr.puts ' Please make puppet available in your path before trying again.'.red return 1 end forge_module_list = [] if @args puts "\nProcessing modules from the command line...\n\n" if @options[:debug] cli_modules = [] @args.each do |modulename| validate(modulename) && cli_modules.push(modulename) end end puppetfile_contents = {} extras = [] if @options[:puppetfile] puts "\nProcessing the puppetfile '#{@options[:puppetfile]}'...\n\n" if @options[:debug] puppetfile_contents = read_puppetfile(@options[:puppetfile]) extras = puppetfile_contents[:extras] end forge_module_list.push(*cli_modules) if @args forge_module_list.push(*puppetfile_contents[:modules]) if puppetfile_contents[:modules] list_forge_modules(forge_module_list) if puppetfile_contents && @options[:debug] list_extras(puppetfile_contents[:extras]) if puppetfile_contents[:extras] && @options[:debug] unless forge_module_list != [] || puppetfile_contents[:extras] != [] $stderr.puts "\nNo valid modules or existing Puppetfile content was found. Exiting.\n\n".red return 1 end create_workspace @modulepath = "--modulepath #{@workspace} " return 2 if download_modules(forge_module_list) == 2 @module_data = generate_module_data puppetfile_contents = generate_puppetfile_contents(extras) if @download_errors == '' create_puppetfile(puppetfile_contents) if @options[:create_puppetfile] display_puppetfile(puppetfile_contents) unless @options[:silent] if @options[:create_fixtures] fixtures_data = generate_fixtures_data write_fixtures_data(fixtures_data) end cleanup_workspace else $stderr.puts @download_errors display_puppetfile(puppetfile_contents) unless @options[:silent] return 2 end 0 end |
#validate(modulename) ⇒ Object
Public: Validates that a provided module name is valid.
128 129 130 131 132 |
# File 'lib/generate_puppetfile/bin.rb', line 128 def validate(modulename) success = (modulename =~ /[a-z0-9_]\/[a-z0-9_]/i) $stderr.puts "'#{modulename}' is not a valid module name. Skipping.".red unless success success end |
#verify_puppet_exists ⇒ Object
Public: Verify that Puppet is available in the path
182 183 184 185 |
# File 'lib/generate_puppetfile/bin.rb', line 182 def verify_puppet_exists MakeMakefile::Logging.instance_variable_set(:@logfile, File::NULL) find_executable0('puppet') end |
#write_fixtures_data(data) ⇒ Object
333 334 335 336 337 |
# File 'lib/generate_puppetfile/bin.rb', line 333 def write_fixtures_data(data) File.open('.fixtures.yml', 'w') do |file| file.write data end end |