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_]+)['\"](, ['\"](\\d\.\\d\.\\d)['\"])?", Regexp::IGNORECASE)

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


20
21
22
# File 'lib/generate_puppetfile/bin.rb', line 20

def initialize(args)
  @args = args
end

Instance Method Details

#generate_puppetfile_contents(module_list, extras) ⇒ Object

Public: Generate a new Puppetfile’s contents based on a list of modules and any extras found in an existing Puppetfile.

module_list is an array of forge module names to be downloaded extras is an array of strings



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/generate_puppetfile/bin.rb', line 121

def generate_puppetfile_contents (module_list, extras)
  unless module_list != [] || extras != []
  puts "\nNo valid modules or existing Puppetfile content was found. Exiting.\n\n"
  exit 1
  end

  workspace = Dir.mktmpdir

  modulepath = "--modulepath #{workspace.chomp} "
  silence    = '>/dev/null 2>&1 '

  puts "\nInstalling modules. This may take a few minutes.\n"
  module_list.each do |name|
    command  = "puppet module install #{name} "
    command += modulepath + silence

    system(command)
  end

  puppetfile_header = "forge 'http://forge.puppetlabs.com'\n\n# Modules discovered by generate-puppetfile\n  EOF\n\n  puppetfile_body = `puppet module list \#{modulepath} 2>/dev/null`\n\n  puppetfile_body.gsub!(/\\x1B\\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/, '') # Strips ANSI color codes\n  puppetfile_body.gsub!(/^\\/.*$/, '')\n  puppetfile_body.gsub!(/-/,      '/')\n  puppetfile_body.gsub!(/\u251C\u2500\u2500 /,   \"mod '\")\n  puppetfile_body.gsub!(/\u2514\u2500\u2500 /,   \"mod '\")\n  puppetfile_body.gsub!(/ \\(v/,   \"', '\")\n  puppetfile_body.gsub!(/\\)$/,    \"'\")\n  puppetfile_body.gsub!(/^$\\n/,   '')\n\n  puppetfile_footer = \"# Discovered elements from existing Puppetfile\\n\"\n  extras.each do |line|\n    puppetfile_footer += \"\#{line}\"\n  end if extras\n\n  puts <<-EOF\n\nYour Puppetfile has been generated. Copy and paste between the markers:\n\n=======================================================================\n\#{puppetfile_header}\n\#{puppetfile_body}\n\#{puppetfile_footer}\n=======================================================================\n  EOF\n\n  FileUtils.rm_rf(workspace)\nend\n"

#list_extras(extras) ⇒ Object

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



84
85
86
87
88
89
90
# File 'lib/generate_puppetfile/bin.rb', line 84

def list_extras (extras)
  puts "\nExtras found in the existing Puppetfile:\n\n"
  extras.each do |line|
    puts "    #{line}"
  end
  puts ""
end

#list_forge_modules(module_list) ⇒ Object

Public: Display the list of Forge modules collected.



75
76
77
78
79
80
81
# File 'lib/generate_puppetfile/bin.rb', line 75

def list_forge_modules (module_list)
  puts "\nListing discovered modules from CLI and/or Puppetfile:\n\n"
  module_list.each do |name|
    puts "    #{name}"
  end
  puts ""
end

#read_puppetfile(puppetfile) ⇒ Object

Public: Read and parse the contents of an existing Puppetfile



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/generate_puppetfile/bin.rb', line 93

def read_puppetfile (puppetfile)
  puppetfile_contents = {
    :modules => Array.new,
    :extras  => Array.new,
  }

  File.foreach(puppetfile) do |line|
    if Module_regex.match(line)
      name = $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 =~ /# Discovered elements from existing Puppetfile/

      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 ($?)



27
28
29
30
31
32
33
34
35
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
# File 'lib/generate_puppetfile/bin.rb', line 27

def run
  @options = GeneratePuppetfile::OptParser.parse(@args)

  helpmsg = "generate-puppetfile: try 'generate-puppetfile --help' for more information."

  if @args[0].nil? && (! @options[:puppetfile])
    puts "generate-puppetfile: No modules or existing Puppetfile specified."
    puts helpmsg
    return 1
  end

  forge_module_list = Array.new

  if @args
    puts "\nProcessing modules from the command line...\n\n" if @options[:debug]
    cli_modules = Array.new
    @args.each do |modulename|
      validate(modulename) && (cli_modules.push(modulename))
    end
  end

  puppetfile_contents = Hash.new
  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]

  generate_puppetfile_contents(forge_module_list, extras)

  return 0
end

#validate(modulename) ⇒ Object

Public: Validates that a provided module name is valid.



68
69
70
71
72
# File 'lib/generate_puppetfile/bin.rb', line 68

def validate (modulename)
  success = (modulename =~ /[a-z0-9_]\/[a-z0-9_]/i)
  puts "    '#{modulename}' is not a valid module name. Skipping." unless success
  success
end