generate-puppetfile 0.11.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4796755a11eed130c7ffdf5587687d38ec0d903f
4
- data.tar.gz: fb597256b5e7283a42354cdf13fe9a8b572d6725
3
+ metadata.gz: 20ff01c403b03aacd1a849c841fef1723543d739
4
+ data.tar.gz: 2571ee7127adad7638a9e0cf2f1f773cc7d1bf96
5
5
  SHA512:
6
- metadata.gz: 09b2664d006c404941337f80b7a35c3591804ef05b5a594dd446e8fc290319a0162c2075dd49bd6d3ad2de48ace8229c6038020185ceb73f31b64257d398e1cc
7
- data.tar.gz: b8ba776dd3e63f6feb081110eb28eb3aae59433388c2bafd03d4e91fa57d53ec5ca9705ec9e487f3fed5664aeceaa3c1ad3f79197af180f68b82a377c49cba21
6
+ metadata.gz: a3d7878cea4c6ccb4ede987cd424cdeddae79c8f79f80ca835af479a0d28fbc944406b8e7d327bf24b42c1ad419f57f81e9eb0f3f55c7706b30cf56c6ff14e5c
7
+ data.tar.gz: 14eff83bc5121fefe0736e19e28fe6a5c7bd4edc8a0b85c66366ec5781e806c5ea186663267aeb2a7c914713f672aafeef537238849c9330e10a9ffc5d7366c4
data/README.md CHANGED
@@ -21,7 +21,7 @@ Installing modules. This may take a few minutes.
21
21
  Your Puppetfile has been generated. Copy and paste between the markers:
22
22
 
23
23
  =======================================================================
24
- forge 'http://forge.puppetlabs.com'
24
+ forge 'https://forge.puppet.com'
25
25
 
26
26
  # Modules discovered by generate-puppetfile
27
27
  mod 'echocat/nfs', '1.7.1'
@@ -48,7 +48,7 @@ Installing modules. This may take a few minutes.
48
48
  Your Puppetfile has been generated. Copy and paste between the markers:
49
49
 
50
50
  =======================================================================
51
- forge 'http://forge.puppetlabs.com'
51
+ forge 'https://forge.puppet.com'
52
52
 
53
53
  # Modules discovered by generate-puppetfile
54
54
  mod 'ajjahn/dhcp', '0.2.0'
@@ -101,19 +101,10 @@ mod 'lab_config',
101
101
  =======================================================================
102
102
  ```
103
103
 
104
- ## Changes
105
- Documentation of important pre-1.0.0 changes
106
-
107
- * *0.10.0*: `--fixtures` has been replaced with `--create-fixtures`. `-f` is still an alias for this argument.
108
-
109
104
  ## Limitations
110
105
 
111
- * Parsing of an existing Puppetfile is naive. Anything that doesn't look like a forge module is preserved and then added to the end of the new Puppetfile. Verify that no ordering errors are introduced before using the new file.
112
-
113
- ## Thanks
114
- Many thanks to the following people for contributing to generate-puppetfile
115
- * [Tomy Lobo](https://github.com/TomyLobo)
116
- * [Ben Ford](https://github.com/binford2k)
106
+ * There is no defined standard for a `Puppetfile`'s contents. `generate-puppetfile` knows about some of the most common types of module references and attempts to treat them as references instead of strings. Any unknown references will be appended as raw content following the known references.
107
+ * Verify that no ordering errors are introduced before using the new file.
117
108
 
118
109
  ## Contributing
119
110
 
@@ -6,15 +6,21 @@ require 'tempfile'
6
6
  require 'json'
7
7
  require 'mkmf'
8
8
  require 'colorize'
9
+ require 'uri'
9
10
 
10
11
  module GeneratePuppetfile
11
12
  # Internal: The Bin class contains the logic for calling generate_puppetfile at the command line
12
13
  class Bin
13
- Module_Regex = Regexp.new("mod ['\"]([a-z0-9_]+\/[a-z0-9_]+)['\"](,\\s+['\"](\\d+\.\\d+\.\\d+)['\"])?", Regexp::IGNORECASE)
14
- @options = {} # Options hash
15
- @workspace = nil # Working directory for module download and inspection
16
- @module_data = {} # key: modulename, value: version number
17
- @download_errors = '' # A list of errors encountered while downloading modules. Should remain empty.
14
+ Module_Regex = %r{^\s*mod ['\"]([a-z0-9_]+\/[a-z0-9_]+)['\"](,\s+['\"](\d+\.\d+\.\d+)['\"])?\s*$}
15
+ Repository_Regex = %r{^\s*mod\s+['\"](\w+)['\"]\s*,\s*$}
16
+ Location_Only_Regex = %r{^\s+:git\s+=>\s+['\"](\S+)['\"]\s*$}
17
+ Location_Plus_Regex = %r{^\s+:git\s+=>\s+['\"](\S+)['\"]\s*,\s*$}
18
+ Type_ID_Regex = %r{^\s+:(\w+)\s+=>\s+['\"](\S+)['\"]\s*$}
19
+ Forge_Regex = %r{^forge}
20
+ Blanks_Regex = %r{^\s*$}
21
+ Comments_Regex = %r{^\s*#}
22
+ Skipall_Regex = %r{^forge|^\s*$|^#}
23
+
18
24
  Silence = ('>' + File::NULL.to_str + ' 2>&1 ').freeze
19
25
  Puppetfile_Header = '# Modules discovered by generate-puppetfile'.freeze
20
26
  Extras_Note = '# Discovered elements from existing Puppetfile'.freeze
@@ -28,6 +34,11 @@ module GeneratePuppetfile
28
34
  # GeneratePuppetfile::Bin.new(ARGV).run
29
35
  def initialize(args)
30
36
  @args = args
37
+ @options = {} # Options hash
38
+ @workspace = nil # Working directory for module download and inspection
39
+ @module_data = {} # key: modulename, value: version number
40
+ @repository_data = [] # Non-forge modules. Array of hashes containing name, location, type, and ID
41
+ @download_errors = '' # A list of errors encountered while downloading modules. Should remain empty.
31
42
  end
32
43
 
33
44
  # Public: Run generate-puppetfile at the command line.
@@ -60,8 +71,9 @@ module GeneratePuppetfile
60
71
  forge_module_list = []
61
72
 
62
73
  # When using --fixtures-only, simply parse the provided Puppetfile and get out
63
- if @options[:fixtures_only]
74
+ if @options[:fixtures_only] && @options[:puppetfile]
64
75
  @module_data = generate_module_data_from_Puppetfile
76
+ @repository_data = generate_repository_data_from_Puppetfile
65
77
  fixtures_data = generate_fixtures_data
66
78
  write_fixtures_data(fixtures_data)
67
79
  return 0
@@ -81,6 +93,7 @@ module GeneratePuppetfile
81
93
  end
82
94
 
83
95
  puppetfile_contents = {}
96
+ # Currently, ALL statements not including a forge module are listed as extras. The @repository_data should be removed from the extras eventually (#54)
84
97
  extras = []
85
98
  if @options[:puppetfile]
86
99
  puts "\nProcessing the puppetfile '#{@options[:puppetfile]}'...\n\n" if @options[:debug]
@@ -110,6 +123,7 @@ module GeneratePuppetfile
110
123
  display_puppetfile(puppetfile_contents) unless @options[:silent]
111
124
 
112
125
  if @options[:create_fixtures]
126
+ @repository_data = generate_repository_data_from_Puppetfile if @options[:puppetfile]
113
127
  fixtures_data = generate_fixtures_data
114
128
  write_fixtures_data(fixtures_data)
115
129
  end
@@ -186,8 +200,8 @@ Your Puppetfile has been generated. Copy and paste between the markers:
186
200
  print " #{name} looks like a forge module.\n" if @options[:debug]
187
201
  puppetfile_contents[:modules].push(name)
188
202
  else
189
- next if line =~ /^forge/
190
- next if line =~ /^\s+$/
203
+ next if line =~ Forge_Regex
204
+ next if line =~ Blanks_Regex
191
205
  next if line =~ /#{Puppetfile_Header}/
192
206
  next if line =~ /#{Extras_Note}/
193
207
 
@@ -212,8 +226,8 @@ Your Puppetfile has been generated. Copy and paste between the markers:
212
226
  print " #{name} looks like a forge module.\n" if @options[:debug]
213
227
  puppetfile_contents[:modules].push([name, version])
214
228
  else
215
- next if line =~ /^forge/
216
- next if line =~ /^\s+$/
229
+ next if line =~ Forge_Regex
230
+ next if line =~ Blanks_Regex
217
231
  next if line =~ /#{Puppetfile_Header}/
218
232
  next if line =~ /#{Extras_Note}/
219
233
 
@@ -292,11 +306,82 @@ Your Puppetfile has been generated. Copy and paste between the markers:
292
306
  modules
293
307
  end
294
308
 
309
+ # Public: generate the ad hoc repository (non-forge module) data from an existing Puppetfile
310
+ # Returns an array of hashes with keys name, location, type, id
311
+ def generate_repository_data_from_Puppetfile
312
+ repositories = []
313
+
314
+ # Open the Puppetfile
315
+ File.open(@options[:puppetfile], 'r') do |fh|
316
+ while (line = fh.gets) != nil
317
+ # Skip blank lines, comments, anything that looks like a forge module
318
+ next if line =~ Skipall_Regex
319
+ next if Module_Regex.match(line)
320
+ # When we see /mod 'modulename',/ it is possibly a properly formatted fixture
321
+ if Repository_Regex.match(line)
322
+ complete = false
323
+ name = Regexp.last_match(1)
324
+ while (line = fh.gets) != nil
325
+ next if line =~ Skipall_Regex
326
+ if Location_Only_Regex.match(line)
327
+ # The Puppetfile may specify just a location /:git => 'https://github.com/author/puppet-modulename'/
328
+ # We do not validate the URI protocol, just that it is a valid URI
329
+ location = Regexp.last_match(1)
330
+ puts "Found module #{name} with location #{location}" if @options[:debug]
331
+ unless location.match(URI.regexp)
332
+ puts "#{location} is not a valid URI, skipping this repo" if @options[:debug]
333
+ break
334
+ end
335
+ repositories << {name: name, location: location}
336
+ complete = true
337
+ elsif Location_Plus_Regex.match(line)
338
+ # Or it may provide more, with a trailing comma
339
+ # :git => 'https://github.com/author/puppet-modulename',
340
+ # :ref => '1.0.0'
341
+ location = Regexp.last_match(1)
342
+ while (line = fh.gets) != nil
343
+ next if line =~ Skipall_Regex
344
+ if Type_ID_Regex.match(line)
345
+ type = Regexp.last_match(1)
346
+ id = Regexp.last_match(2)
347
+ puts "Found module #{name} with location #{location}, #{type} of #{id}" if @options[:debug]
348
+ unless location.match(URI.regexp)
349
+ puts "#{location} is not a valid URI, skipping this repo" if @options[:debug]
350
+ break
351
+ end
352
+ repositories << {name: name, location: location, type: type, id: id}
353
+ complete = true
354
+ else
355
+ # If the :git line ends with a comma but no type/ID is found, ignore it, we cannot properly determine the fixture
356
+ puts "Found module #{name} at location #{location}. Expected type/ID information but did not find any, skipping." if @options[:debug]
357
+ complete = true
358
+ end
359
+ break if complete
360
+ end
361
+ else
362
+ # If the /mod 'modulename',/ line is not followed with a :git string, ignore it, we cannot properly determine the fixture
363
+ puts "Found a reference to module #{name} but no location (:git) was provided, skipping." if @options[:debug]
364
+ complete = true
365
+ end
366
+ break if complete
367
+ end
368
+ end
369
+ end
370
+ end
371
+
372
+ repositories
373
+ end
374
+
375
+
295
376
  # Public: generate the list of modules in Puppetfile format from the @workspace
296
377
  def generate_forge_module_output
378
+ return '' if @module_data.empty?
379
+
380
+ max_length = @module_data.keys.max_by {|mod| mod.length}.length + 3 # room for extra chars
297
381
  module_output = ''
298
- @module_data.keys.each do |modulename|
299
- module_output += "mod '#{modulename}', '#{@module_data[modulename]}'\n"
382
+
383
+ @module_data.each do |modulename, version|
384
+ module_output += sprintf("mod %-#{max_length}s '%s'\n", "'#{modulename}',", version)
300
385
  end
301
386
  module_output
302
387
  end
@@ -307,7 +392,7 @@ Your Puppetfile has been generated. Copy and paste between the markers:
307
392
  # extras is an array of strings
308
393
  def generate_puppetfile_contents(extras)
309
394
  puppetfile_contents = <<-EOF
310
- forge 'http://forge.puppetlabs.com'
395
+ forge 'https://forge.puppet.com'
311
396
 
312
397
  #{Puppetfile_Header}
313
398
  EOF
@@ -376,18 +461,42 @@ forge 'http://forge.puppetlabs.com'
376
461
  end
377
462
  end
378
463
 
379
- fixtures_data += " forge_modules:\n" if @module_data != {}
380
- @module_data.keys.each do |modulename|
381
- shortname = modulename.split('/')[1]
382
- version = @module_data[modulename]
383
- data = <<-EOF
464
+ unless @repository_data.empty?
465
+ fixtures_data += " repositories:\n"
466
+ @repository_data.each do |repodata|
467
+ # Each repository has two or pieces of data
468
+ # Mandatory: the module name, the URI/location
469
+ # Optional: the type (ref, branch, commit, etc.) and ID (tag, branch name, commit hash, etc.)
470
+ name = repodata[:name]
471
+ location = repodata[:location]
472
+ type = repodata[:type]
473
+ id = repodata[:id]
474
+
475
+ data = <<-EOF
476
+ #{name}:
477
+ repo: "#{location}"
478
+ EOF
479
+ data += " #{type}: \"#{id}\"\n" if (type && id)
480
+
481
+ fixtures_data += data
482
+ end
483
+ end
484
+
485
+
486
+ unless @module_data.empty?
487
+ fixtures_data += " forge_modules:\n"
488
+ @module_data.keys.each do |modulename|
489
+ shortname = modulename.split('/')[1]
490
+ version = @module_data[modulename]
491
+ data = <<-EOF
384
492
  #{shortname}:
385
493
  repo: "#{modulename}"
386
494
  ref: "#{version}"
387
- EOF
388
- data.gsub!(/^ *ref.*$\n/, '') unless version != nil
495
+ EOF
496
+ data.gsub!(/^ *ref.*$\n/, '') unless version != nil
389
497
 
390
- fixtures_data += data
498
+ fixtures_data += data
499
+ end
391
500
  end
392
501
 
393
502
  fixtures_data
@@ -1,3 +1,3 @@
1
1
  module GeneratePuppetfile
2
- VERSION = '0.11.0'.freeze
2
+ VERSION = '1.0.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: generate-puppetfile
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Nelson
@@ -9,132 +9,118 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-03-22 00:00:00.000000000 Z
12
+ date: 2018-01-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: colorize
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - '>='
18
+ - - ">="
19
19
  - !ruby/object:Gem::Version
20
20
  version: '0'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - '>='
25
+ - - ">="
26
26
  - !ruby/object:Gem::Version
27
27
  version: '0'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rake
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - '>='
32
+ - - ">="
33
33
  - !ruby/object:Gem::Version
34
34
  version: '0'
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - '>='
39
+ - - ">="
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: rspec
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ~>
46
+ - - "~>"
47
47
  - !ruby/object:Gem::Version
48
48
  version: '3'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - ~>
53
+ - - "~>"
54
54
  - !ruby/object:Gem::Version
55
55
  version: '3'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: rspec-its
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - ~>
60
+ - - "~>"
61
61
  - !ruby/object:Gem::Version
62
62
  version: '1'
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - ~>
67
+ - - "~>"
68
68
  - !ruby/object:Gem::Version
69
69
  version: '1'
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: json
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - ~>
74
+ - - "~>"
75
75
  - !ruby/object:Gem::Version
76
76
  version: '1'
77
77
  type: :development
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
- - - ~>
81
+ - - "~>"
82
82
  - !ruby/object:Gem::Version
83
83
  version: '1'
84
84
  - !ruby/object:Gem::Dependency
85
85
  name: puppet
86
86
  requirement: !ruby/object:Gem::Requirement
87
87
  requirements:
88
- - - '>='
88
+ - - ">="
89
89
  - !ruby/object:Gem::Version
90
90
  version: '0'
91
91
  type: :development
92
92
  prerelease: false
93
93
  version_requirements: !ruby/object:Gem::Requirement
94
94
  requirements:
95
- - - '>='
95
+ - - ">="
96
96
  - !ruby/object:Gem::Version
97
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
112
98
  - !ruby/object:Gem::Dependency
113
99
  name: github_changelog_generator
114
100
  requirement: !ruby/object:Gem::Requirement
115
101
  requirements:
116
- - - '>='
102
+ - - ">="
117
103
  - !ruby/object:Gem::Version
118
104
  version: '0'
119
105
  type: :development
120
106
  prerelease: false
121
107
  version_requirements: !ruby/object:Gem::Requirement
122
108
  requirements:
123
- - - '>='
109
+ - - ">="
124
110
  - !ruby/object:Gem::Version
125
111
  version: '0'
126
112
  - !ruby/object:Gem::Dependency
127
113
  name: multi_json
128
114
  requirement: !ruby/object:Gem::Requirement
129
115
  requirements:
130
- - - '>='
116
+ - - ">="
131
117
  - !ruby/object:Gem::Version
132
118
  version: '0'
133
119
  type: :development
134
120
  prerelease: false
135
121
  version_requirements: !ruby/object:Gem::Requirement
136
122
  requirements:
137
- - - '>='
123
+ - - ">="
138
124
  - !ruby/object:Gem::Version
139
125
  version: '0'
140
126
  description: Generate a Puppetfile for use with r10k based on an existing file or
@@ -145,8 +131,8 @@ executables:
145
131
  extensions: []
146
132
  extra_rdoc_files: []
147
133
  files:
148
- - bin/generate-puppetfile
149
134
  - README.md
135
+ - bin/generate-puppetfile
150
136
  - lib/generate_puppetfile.rb
151
137
  - lib/generate_puppetfile/bin.rb
152
138
  - lib/generate_puppetfile/optparser.rb
@@ -161,17 +147,17 @@ require_paths:
161
147
  - lib
162
148
  required_ruby_version: !ruby/object:Gem::Requirement
163
149
  requirements:
164
- - - '>='
150
+ - - ">="
165
151
  - !ruby/object:Gem::Version
166
152
  version: 2.0.0
167
153
  required_rubygems_version: !ruby/object:Gem::Requirement
168
154
  requirements:
169
- - - '>='
155
+ - - ">="
170
156
  - !ruby/object:Gem::Version
171
157
  version: '0'
172
158
  requirements: []
173
159
  rubyforge_project:
174
- rubygems_version: 2.0.14
160
+ rubygems_version: 2.6.10
175
161
  signing_key:
176
162
  specification_version: 4
177
163
  summary: Generate a Puppetfile