Class: GeneratePuppetfile::Bin

Inherits:
Object
  • Object
show all
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_]+)['\"](,\\s+['\"](\\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

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



254
255
256
257
258
259
260
# File 'lib/generate_puppetfile/bin.rb', line 254

def _download_module(name)
  command  = "puppet module install #{name} "
  command += @modulepath + Silence

  puts "Calling '#{command}'" if @options[:debug]
  system(command)
end

#cleanup_workspaceObject

Public: Remove the workspace (with prejudice)



334
335
336
# File 'lib/generate_puppetfile/bin.rb', line 334

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



141
142
143
144
145
# File 'lib/generate_puppetfile/bin.rb', line 141

def create_puppetfile(puppetfile_contents)
  File.open('Puppetfile', 'w') do |file|
    file.write puppetfile_contents
  end
end

#create_workspaceObject

Public: Create a temporary workspace for module manipulation



329
330
331
# File 'lib/generate_puppetfile/bin.rb', line 329

def create_workspace
  @workspace = Dir.mktmpdir.chomp
end

#display_puppetfile(puppetfile_contents) ⇒ Object

Public: Display the generated Puppetfile to STDOUT with delimiters



128
129
130
131
132
133
134
135
136
137
# File 'lib/generate_puppetfile/bin.rb', line 128

def display_puppetfile(puppetfile_contents)
  puts "\nYour Puppetfile has been generated. Copy and paste between the markers:\n\n=======================================================================\n\#{puppetfile_contents.chomp}\n=======================================================================\n  EOF\nend\n"

#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



236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/generate_puppetfile/bin.rb', line 236

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_dataObject

Public: Generate a simple fixtures file.



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/generate_puppetfile/bin.rb', line 339

def generate_fixtures_data
  # 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] 
    data = "\#{shortname}:\n  repo: \"\#{modulename}\"\n  ref: \"\#{version}\"\n    EOF\n    data.gsub!(/^ *ref.*$\\n/, '') unless version != nil\n\n    fixtures_data += data\n  end\n\n  fixtures_data\nend\n"

#generate_forge_module_outputObject

Public: generate the list of modules in Puppetfile format from the @workspace



296
297
298
299
300
301
302
# File 'lib/generate_puppetfile/bin.rb', line 296

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_from_modulepathObject

Public: generate the module data from the modulepath (@workspace)



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/generate_puppetfile/bin.rb', line 263

def generate_module_data_from_modulepath
  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_module_data_from_PuppetfileObject

Public: generate the module data from an existing Puppetfile



283
284
285
286
287
288
289
290
291
292
293
# File 'lib/generate_puppetfile/bin.rb', line 283

def generate_module_data_from_Puppetfile
  puppetfile_contents = read_puppetfile_with_versions(@options[:puppetfile])

  modules = {}
  puppetfile_contents[:modules].each do |name, version|
    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



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/generate_puppetfile/bin.rb', line 308

def generate_puppetfile_contents(extras)
  puppetfile_contents = "forge 'http://forge.puppetlabs.com'\n\n\#{Puppetfile_Header}\n  EOF\n\n  puppetfile_contents += generate_forge_module_output\n\n  puppetfile_contents += \"\#{Extras_Note}\\n\" unless extras == []\n  extras.each do |line|\n    puppetfile_contents += line.to_s\n  end unless extras == []\n\n  # Strip out all contents with --ignore_comments\n  puppetfile_contents.gsub!(/^#.*$\\n/, '') if @options[:ignore_comments]\n\n  puppetfile_contents\nend\n"

#list_extras(extras) ⇒ Object

Public: Display the list of extra statements found in the Puppetfile.



166
167
168
169
170
171
172
173
174
# File 'lib/generate_puppetfile/bin.rb', line 166

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.



155
156
157
158
159
160
161
162
163
# File 'lib/generate_puppetfile/bin.rb', line 155

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



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/generate_puppetfile/bin.rb', line 177

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

#read_puppetfile_with_versions(puppetfile) ⇒ Object

Public: Read and parse the contents of an existing Puppetfile



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/generate_puppetfile/bin.rb', line 202

def read_puppetfile_with_versions(puppetfile)
  puppetfile_contents = {
    modules: [],
    extras: []
  }

  File.foreach(puppetfile) do |line|
    if Module_Regex.match(line)
      name    = Regexp.last_match(1)
      version = Regexp.last_match(3)
      print "    #{name} looks like a forge module.\n" if @options[:debug]
      puppetfile_contents[:modules].push([name, version])
    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

#runObject

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# 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 (@options[:fixtures_only] && ! @options[:puppetfile] )
    $stderr.puts "generate-puppetfile: --fixtures-only is only valid when a '-p <Puppetfile>' is used as well.\n".red
    puts helpmsg
    return 1
  end

  if @args[0].nil? && (! @options[:puppetfile])
    $stderr.puts "generate-puppetfile: No modules or existing Puppetfile specified.\n".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.\n".red
    return 1
  end


  forge_module_list = []

  # When using --fixtures-only, simply parse the provided Puppetfile and get out
  if @options[:fixtures_only]
    @module_data = generate_module_data_from_Puppetfile
    fixtures_data = generate_fixtures_data
    write_fixtures_data(fixtures_data)
    return 0
  end

  # For everything else, run through the whole thing
  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
    if cli_modules == [] && ! @options[:puppetfile]
      $stderr.puts "No valid modules were found to process.".red
      return 1
    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_from_modulepath
  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.



148
149
150
151
152
# File 'lib/generate_puppetfile/bin.rb', line 148

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_existsObject

Public: Verify that Puppet is available in the path



228
229
230
231
# File 'lib/generate_puppetfile/bin.rb', line 228

def verify_puppet_exists
  MakeMakefile::Logging.instance_variable_set(:@logfile, File::NULL)
  find_executable0('puppet')
end

#write_fixtures_data(data) ⇒ Object



396
397
398
399
400
# File 'lib/generate_puppetfile/bin.rb', line 396

def write_fixtures_data(data)
  File.open('.fixtures.yml', 'w') do |file|
    file.write data
  end
end