generate-puppetfile 0.9.8 → 0.9.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd48413a63df74c9306f1d5c0915873b374dd8d2
4
- data.tar.gz: 907880161d8a1fef4710d735e6dbe3ffb475317a
3
+ metadata.gz: 4d1d05ed7199555f2bb1b26860c8f4309cc37361
4
+ data.tar.gz: 80e0478b383aed1a39e634a4f4174249f7ddef99
5
5
  SHA512:
6
- metadata.gz: 2ff6eec9ffbc414e01ae20e80451a576e682a78b4af3c58f81955895291b84165e03427f77521e97621a710579e933943460ea1b54aa6316c2fd17c2a23bd03b
7
- data.tar.gz: d46f2292a2ab1d97317c4a36ae4699dce52106835043ce03642732434a0a7c79b6468ddcc7ea308cd4f5b4f3f16f054dc6496aacc0430d8987fedab970fbd316
6
+ metadata.gz: 064fd4d390492aa6825a68ac9b19f570e81f231c70046df254a74fafcb0e1eb63c2e9e2ee5e2cc4fcf0a3ede788c13f9932e822497b328b6c17b3b23bf89c481
7
+ data.tar.gz: 0e29d6afbd45da58a9d9458bbec782255e6b9e48f95f996fd6bb31a977ea477619cf90c789491376871a08a0f1ac3ba7320f4336564dcca40dfcceb2fcd66640
@@ -5,16 +5,18 @@ require 'fileutils'
5
5
  require 'tempfile'
6
6
  require 'json'
7
7
  require 'mkmf'
8
+ require 'colorize'
8
9
 
9
10
  module GeneratePuppetfile
10
11
  # Internal: The Bin class contains the logic for calling generate_puppetfile at the command line
11
12
  class Bin
12
13
  Module_Regex = Regexp.new("mod ['\"]([a-z0-9_]+\/[a-z0-9_]+)['\"](, ['\"](\\d\.\\d\.\\d)['\"])?", Regexp::IGNORECASE)
13
- @options = {} # Options hash
14
- @workspace = nil # Working directory for module download and inspection
15
- Silence = '>/dev/null 2>&1 '
16
- Puppetfile_Header = '# Modules discovered by generate-puppetfile'
17
- Extras_Note = '# Discovered elements from existing Puppetfile'
14
+ @options = {} # Options hash
15
+ @workspace = nil # Working directory for module download and inspection
16
+ @module_data = {} # key: modulename, value: version number
17
+ Silence = ('>' + File::NULL.to_str + ' 2>&1 ').freeze
18
+ Puppetfile_Header = '# Modules discovered by generate-puppetfile'.freeze
19
+ Extras_Note = '# Discovered elements from existing Puppetfile'.freeze
18
20
 
19
21
  # Public: Initialize a new GeneratePuppetfile::Bin
20
22
  #
@@ -36,28 +38,28 @@ module GeneratePuppetfile
36
38
  helpmsg = "generate-puppetfile: try 'generate-puppetfile --help' for more information."
37
39
 
38
40
  if @args[0].nil? && (! @options[:puppetfile])
39
- $stderr.puts "generate-puppetfile: No modules or existing Puppetfile specified."
41
+ $stderr.puts 'generate-puppetfile: No modules or existing Puppetfile specified.'.red
40
42
  puts helpmsg
41
43
  return 1
42
44
  end
43
45
 
44
- if (! verify_puppet_exists())
45
- $stderr.puts "generate-puppetfile: Could not find a 'puppet' executable."
46
- $stderr.puts " Please make puppet available in your path before trying again."
46
+ unless verify_puppet_exists
47
+ $stderr.puts "generate-puppetfile: Could not find a 'puppet' executable.".red
48
+ $stderr.puts ' Please make puppet available in your path before trying again.'.red
47
49
  return 1
48
50
  end
49
51
 
50
- forge_module_list = Array.new
52
+ forge_module_list = []
51
53
 
52
54
  if @args
53
55
  puts "\nProcessing modules from the command line...\n\n" if @options[:debug]
54
- cli_modules = Array.new
56
+ cli_modules = []
55
57
  @args.each do |modulename|
56
- validate(modulename) && (cli_modules.push(modulename))
58
+ validate(modulename) && cli_modules.push(modulename)
57
59
  end
58
60
  end
59
61
 
60
- puppetfile_contents = Hash.new
62
+ puppetfile_contents = {}
61
63
  extras = []
62
64
  if @options[:puppetfile]
63
65
  puts "\nProcessing the puppetfile '#{@options[:puppetfile]}'...\n\n" if @options[:debug]
@@ -71,33 +73,29 @@ module GeneratePuppetfile
71
73
  list_extras(puppetfile_contents[:extras]) if puppetfile_contents[:extras] && @options[:debug]
72
74
 
73
75
  unless forge_module_list != [] || puppetfile_contents[:extras] != []
74
- $stderr.puts "\nNo valid modules or existing Puppetfile content was found. Exiting.\n\n"
76
+ $stderr.puts "\nNo valid modules or existing Puppetfile content was found. Exiting.\n\n".red
75
77
  return 1
76
78
  end
77
79
 
78
- create_workspace()
80
+ create_workspace
79
81
  @modulepath = "--modulepath #{@workspace} "
80
82
 
81
- download_modules(forge_module_list)
83
+ return 2 if download_modules(forge_module_list) == 2
84
+ @module_data = generate_module_data
82
85
  puppetfile_contents = generate_puppetfile_contents(extras)
83
86
 
87
+ create_puppetfile(puppetfile_contents) if @options[:create_puppetfile]
84
88
 
85
- if @options[:create_puppetfile]
86
- create_puppetfile(puppetfile_contents)
87
- end
88
-
89
- unless @options[:silent]
90
- display_puppetfile(puppetfile_contents)
91
- end
89
+ display_puppetfile(puppetfile_contents) unless @options[:silent]
92
90
 
93
91
  if @options[:create_fixtures]
94
- fixtures_data = generate_fixtures_data()
92
+ fixtures_data = generate_fixtures_data
95
93
  write_fixtures_data(fixtures_data)
96
94
  end
97
95
 
98
- cleanup_workspace()
96
+ cleanup_workspace
99
97
 
100
- return 0
98
+ 0
101
99
  end
102
100
 
103
101
  # Public: Display the generated Puppetfile to STDOUT with delimiters
@@ -114,51 +112,51 @@ Your Puppetfile has been generated. Copy and paste between the markers:
114
112
 
115
113
  # Public: Create a Puppetfile on disk
116
114
  # The Puppetfile will be called 'Puppetfile' in the current working directory
117
- def create_puppetfile (puppetfile_contents)
115
+ def create_puppetfile(puppetfile_contents)
118
116
  File.open('Puppetfile', 'w') do |file|
119
117
  file.write puppetfile_contents
120
118
  end
121
119
  end
122
120
 
123
121
  # Public: Validates that a provided module name is valid.
124
- def validate (modulename)
122
+ def validate(modulename)
125
123
  success = (modulename =~ /[a-z0-9_]\/[a-z0-9_]/i)
126
- $stderr.puts "'#{modulename}' is not a valid module name. Skipping." unless success
124
+ $stderr.puts "'#{modulename}' is not a valid module name. Skipping.".red unless success
127
125
  success
128
126
  end
129
127
 
130
128
  # Public: Display the list of Forge modules collected.
131
- def list_forge_modules (module_list)
129
+ def list_forge_modules(module_list)
132
130
  unless @options[:silent]
133
131
  puts "\nListing discovered modules from CLI and/or Puppetfile:\n\n"
134
132
  module_list.each do |name|
135
133
  puts " #{name}"
136
134
  end
137
- puts ""
135
+ puts ''
138
136
  end
139
137
  end
140
138
 
141
139
  # Public: Display the list of extra statements found in the Puppetfile.
142
- def list_extras (extras)
140
+ def list_extras(extras)
143
141
  unless @options[:silent] || (extras == [])
144
142
  puts "\nExtras found in the existing Puppetfile:\n\n"
145
143
  extras.each do |line|
146
144
  puts " #{line}"
147
145
  end
148
- puts ""
146
+ puts ''
149
147
  end
150
148
  end
151
149
 
152
150
  # Public: Read and parse the contents of an existing Puppetfile
153
- def read_puppetfile (puppetfile)
151
+ def read_puppetfile(puppetfile)
154
152
  puppetfile_contents = {
155
- :modules => Array.new,
156
- :extras => Array.new,
153
+ modules: [],
154
+ extras: []
157
155
  }
158
156
 
159
157
  File.foreach(puppetfile) do |line|
160
158
  if Module_Regex.match(line)
161
- name = $1
159
+ name = Regexp.last_match(1)
162
160
  print " #{name} looks like a forge module.\n" if @options[:debug]
163
161
  puppetfile_contents[:modules].push(name)
164
162
  else
@@ -175,7 +173,7 @@ Your Puppetfile has been generated. Copy and paste between the markers:
175
173
  end
176
174
 
177
175
  # Public: Verify that Puppet is available in the path
178
- def verify_puppet_exists()
176
+ def verify_puppet_exists
179
177
  MakeMakefile::Logging.instance_variable_set(:@logfile, File::NULL)
180
178
  find_executable0('puppet')
181
179
  end
@@ -184,13 +182,12 @@ Your Puppetfile has been generated. Copy and paste between the markers:
184
182
  #
185
183
  # module_list is an array of forge module names to be downloaded
186
184
  def download_modules(module_list)
187
- puts "\nInstalling modules. This may take a few minutes.\n" unless @options[:silent]
185
+ puts "\nInstalling modules. This may take a few minutes.\n\n" unless @options[:silent]
188
186
  module_list.each do |name|
189
- if !_download_module(name)
190
- $stderr.puts "There was a problem with the module name '#{name}'."
191
- $stderr.puts " Check that module exists as you spelled it and/or your connectivity to the puppet forge."
192
- exit 2
193
- end
187
+ next if _download_module(name)
188
+ $stderr.puts "There was a problem with the module name '#{name}'.".red
189
+ $stderr.puts ' Check that module exists as you spelled it and/or your connectivity to the puppet forge.'.red
190
+ return 2
194
191
  end
195
192
  end
196
193
 
@@ -204,20 +201,33 @@ Your Puppetfile has been generated. Copy and paste between the markers:
204
201
  puts "Calling '#{command}'" if @options[:debug]
205
202
  system(command)
206
203
  end
207
-
208
- # Public: generate the list of modules in Puppetfile format from the @workspace
209
- def generate_module_output ()
210
- module_output = `puppet module list #{@modulepath} 2>/dev/null`
204
+
205
+ # Public: generate the module data the @workspace
206
+ def generate_module_data
207
+ command = "puppet module list #{@modulepath} 2>#{File::NULL}"
208
+ puts "Calling '#{command}'" if @options[:debug]
209
+ module_output = `#{command}`
211
210
 
212
211
  module_output.gsub!(/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/, '') # Strips ANSI color codes
213
- module_output.gsub!(/^\/.*$/, '')
214
- module_output.gsub!(/-/, '/')
215
- module_output.gsub!(/├── /, "mod '")
216
- module_output.gsub!(/└── /, "mod '")
217
- module_output.gsub!(/ \(v/, "', '")
218
- module_output.gsub!(/\)$/, "'")
219
- module_output.gsub!(/^$\n/, '')
212
+ modules = {}
213
+ module_output.each_line do |line|
214
+ next unless line =~ / \(v/
215
+ line.tr!('-', '/')
216
+ line.gsub!(/^\S* /, '')
217
+ line += line
218
+ name, version = line.match(/(\S+) \(v(.+)\)/).captures
219
+ modules[name] = version
220
+ $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]
221
+ end
222
+ modules
223
+ end
220
224
 
225
+ # Public: generate the list of modules in Puppetfile format from the @workspace
226
+ def generate_forge_module_output
227
+ module_output = ''
228
+ @module_data.keys.each do |modulename|
229
+ module_output += "mod #{modulename}, '#{@module_data[modulename]}'\n"
230
+ end
221
231
  module_output
222
232
  end
223
233
 
@@ -225,36 +235,35 @@ Your Puppetfile has been generated. Copy and paste between the markers:
225
235
  # and any extras found in an existing Puppetfile.
226
236
  #
227
237
  # extras is an array of strings
228
- def generate_puppetfile_contents (extras)
229
-
238
+ def generate_puppetfile_contents(extras)
230
239
  puppetfile_contents = <<-EOF
231
240
  forge 'http://forge.puppetlabs.com'
232
241
 
233
242
  #{Puppetfile_Header}
234
243
  EOF
235
244
 
236
- puppetfile_contents += generate_module_output()
245
+ puppetfile_contents += generate_forge_module_output
237
246
 
238
247
  puppetfile_contents += "#{Extras_Note}\n" unless extras == []
239
248
  extras.each do |line|
240
- puppetfile_contents += "#{line}"
249
+ puppetfile_contents += line.to_s
241
250
  end unless extras == []
242
251
 
243
252
  puppetfile_contents
244
253
  end
245
254
 
246
255
  # Public: Create a temporary workspace for module manipulation
247
- def create_workspace()
248
- @workspace = (Dir.mktmpdir).chomp
256
+ def create_workspace
257
+ @workspace = Dir.mktmpdir.chomp
249
258
  end
250
259
 
251
260
  # Public: Remove the workspace (with prejudice)
252
- def cleanup_workspace ()
261
+ def cleanup_workspace
253
262
  FileUtils.rm_rf(@workspace)
254
263
  end
255
264
 
256
265
  # Public: Generate a simple fixtures file.
257
- def generate_fixtures_data ()
266
+ def generate_fixtures_data
258
267
  puts "\nGenerating .fixtures.yml using module name #{@options[:modulename]}" unless @options[:silent]
259
268
 
260
269
  # Header for fixtures file creates a symlink for the current module"
@@ -264,27 +273,24 @@ fixtures:
264
273
  #{@options[:modulename]}: "\#{source_dir}"
265
274
  EOF
266
275
 
267
-
268
- module_directories = Dir.glob("#{@workspace}/*")
269
- if (module_directories != [])
270
- fixtures_data += " repositories:\n"
276
+ fixtures_data += " forge_modules:\n" if @module_data != {}
277
+ @module_data.keys.each do |modulename|
278
+ shortname = modulename.split('/')[1]
279
+ version = @module_data[modulename]
280
+ fixtures_data += <<-EOF
281
+ #{shortname}:
282
+ repo: "#{modulename}"
283
+ ref: "#{version}"
284
+ EOF
271
285
  end
272
- module_directories.each do |module_directory|
273
- name = File.basename(module_directory)
274
- file = File.read("#{module_directory}/metadata.json")
275
- source = (JSON.parse(file))["source"]
276
- fixtures_data += " #{name}: #{source}\n"
277
- puts "Found a module '#{name}' with a project page of #{source}." if @options[:debug]
278
- end unless module_directories == []
279
286
 
280
287
  fixtures_data
281
288
  end
282
289
 
283
- def write_fixtures_data (data)
290
+ def write_fixtures_data(data)
284
291
  File.open('.fixtures.yml', 'w') do |file|
285
292
  file.write data
286
293
  end
287
294
  end
288
-
289
295
  end
290
296
  end
@@ -13,7 +13,7 @@ module GeneratePuppetfile
13
13
  options[:modulename] = 'profile'
14
14
 
15
15
  opts = OptionParser.new do |opts|
16
- opts.banner = "generate-puppetfile [OPTIONS] [<MODULE> ... <MODULE>]"
16
+ opts.banner = 'generate-puppetfile [OPTIONS] [<MODULE> ... <MODULE>]'
17
17
 
18
18
  opts.on('-p', '--puppetfile FILE', 'Name of existing Puppetfile to verify and update') do |file|
19
19
  unless File.readable?(file)
@@ -50,7 +50,7 @@ module GeneratePuppetfile
50
50
  exit
51
51
  end
52
52
  end
53
-
53
+
54
54
  opts.parse!(args)
55
55
  options
56
56
  end
@@ -1,3 +1,3 @@
1
1
  module GeneratePuppetfile
2
- VERSION = '0.9.8'
2
+ VERSION = '0.9.9'.freeze
3
3
  end
metadata CHANGED
@@ -1,15 +1,30 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: generate-puppetfile
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.8
4
+ version: 0.9.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Nelson
8
+ - Joseph Yaworski
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2016-04-16 00:00:00.000000000 Z
12
+ date: 2016-06-14 00:00:00.000000000 Z
12
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: colorize
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
13
28
  - !ruby/object:Gem::Dependency
14
29
  name: rake
15
30
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +81,34 @@ dependencies:
66
81
  - - ~>
67
82
  - !ruby/object:Gem::Version
68
83
  version: '1'
84
+ - !ruby/object:Gem::Dependency
85
+ name: puppet
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: rubocop
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '='
103
+ - !ruby/object:Gem::Version
104
+ version: 0.39.0
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '='
110
+ - !ruby/object:Gem::Version
111
+ version: 0.39.0
69
112
  description: Generate a Puppetfile for use with r10k based on an existing file or
70
113
  a list of modules.
71
114
  email: rnelson0@gmail.com
@@ -92,7 +135,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
92
135
  requirements:
93
136
  - - '>='
94
137
  - !ruby/object:Gem::Version
95
- version: '0'
138
+ version: 2.0.0
96
139
  required_rubygems_version: !ruby/object:Gem::Requirement
97
140
  requirements:
98
141
  - - '>='
@@ -105,3 +148,4 @@ signing_key:
105
148
  specification_version: 4
106
149
  summary: Generate a Puppetfile
107
150
  test_files: []
151
+ has_rdoc: