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)
Silence =
'>/dev/null 2>&1 '

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


22
23
24
# File 'lib/generate_puppetfile/bin.rb', line 22

def initialize(args)
  @args = args
end

Instance Method Details

#cleanup_workspaceObject

Public: Remove the workspace (with prejudice)



219
220
221
# File 'lib/generate_puppetfile/bin.rb', line 219

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



103
104
105
106
107
# File 'lib/generate_puppetfile/bin.rb', line 103

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



214
215
216
# File 'lib/generate_puppetfile/bin.rb', line 214

def create_workspace()
  @workspace = (Dir.mktmpdir).chomp
end

#display_puppetfile(puppetfile_contents) ⇒ Object

Public: Display the generated Puppetfile to STDOUT with delimiters



90
91
92
93
94
95
96
97
98
99
# File 'lib/generate_puppetfile/bin.rb', line 90

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



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

def download_modules(module_list)
  puts "\nInstalling modules. This may take a few minutes.\n" unless @options[:silent]
  module_list.each do |name|
    command  = "puppet module install #{name} "
    command += @modulepath + Silence

    system(command)
  end
end

#generate_module_outputObject

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



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/generate_puppetfile/bin.rb', line 176

def generate_module_output ()
  module_output = `puppet module list #{@modulepath} 2>/dev/null`

  module_output.gsub!(/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/, '') # Strips ANSI color codes
  module_output.gsub!(/^\/.*$/, '')
  module_output.gsub!(/-/,      '/')
  module_output.gsub!(/├── /,   "mod '")
  module_output.gsub!(/└── /,   "mod '")
  module_output.gsub!(/ \(v/,   "', '")
  module_output.gsub!(/\)$/,    "'")
  module_output.gsub!(/^$\n/,   '')

  module_output
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



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/generate_puppetfile/bin.rb', line 195

def generate_puppetfile_contents (extras)

  puppetfile_contents = "forge 'http://forge.puppetlabs.com'\n\n# Modules discovered by generate-puppetfile\n  EOF\n\n  puppetfile_contents += generate_module_output()\n\n  puppetfile_contents += \"# Discovered elements from existing Puppetfile\\n\" unless extras == []\n  extras.each do |line|\n    puppetfile_contents += \"\#{line}\"\n  end unless extras == []\n\n  puppetfile_contents\nend\n"

#list_extras(extras) ⇒ Object

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



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

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.



117
118
119
120
121
122
123
124
125
# File 'lib/generate_puppetfile/bin.rb', line 117

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



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/generate_puppetfile/bin.rb', line 139

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



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/generate_puppetfile/bin.rb', line 29

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."
    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]

  unless forge_module_list != [] || puppetfile_contents[:extras] != []
    $stderr.puts "\nNo valid modules or existing Puppetfile content was found. Exiting.\n\n"
    return 1
  end

  create_workspace()
  @modulepath = "--modulepath #{@workspace} "

  download_modules(forge_module_list)
  puppetfile_contents = generate_puppetfile_contents(extras)


  if @options[:create_puppetfile]
    create_puppetfile(puppetfile_contents)
  end

  unless @options[:silent]
    display_puppetfile(puppetfile_contents)
  end

  cleanup_workspace()

  return 0
end

#validate(modulename) ⇒ Object

Public: Validates that a provided module name is valid.



110
111
112
113
114
# File 'lib/generate_puppetfile/bin.rb', line 110

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