generate-puppetfile 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NDRjNTE2ZWYzNGU0MDNhNWYxYmYzYTg3MWI2NTc1YTJkNzlkM2U5Yg==
4
+ MGRhMmMzNmZhZDdiNzQ3ZTIyOThjMDY4OTEwYmQ1N2M3ODhmNzUwNA==
5
5
  data.tar.gz: !binary |-
6
- Y2JkNjk3MWUyOTU4NDRjYzIwNTM4YWQ2YzU2YTA1ZjI1ZGNlYTkwNQ==
6
+ NjUzMDg5M2UzMDc1ZDQxNzU0M2Q2MDRlN2VlODI0NmYwOTA2YmRjNw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MmUxMTMwYjRmODdhZjk5OTE2Y2YwOTY2MTYwNjY4OTYyZTEzNjcwZDAwYTBh
10
- MmI0YmIyMjA4ZGE5NzM4NmFiNmFkMzI4ZDYyZWFjYzU3NDQ0OGQ4NDQ1MDM2
11
- NmZlYzAwMDg5NWM4MWZmY2U4NjJmYjg4ODlkM2NkMTcyNGFiNzE=
9
+ ZGE2MDk0N2VmYzA4ZjFiNzJmZTk5N2M3OTRhZDg4Yjg5MDU5NjA1MWNmNjcz
10
+ YWMzMGIzM2RkMTFkODI0YzE5MzgwZTUxZTYwYmE2ZjNiZGIzNWZjNzg2YTY0
11
+ NDI1ODAzM2U4YTZlOTIzMWFhYWMyNzY5ZDMwYjFjYTRmOWVmYjc=
12
12
  data.tar.gz: !binary |-
13
- MjdmN2YzYWJkOTY3MzY1NmQ5ZmYzNzFlNDg2YjMwM2ExNzBkYWFiODc3NTdh
14
- MDdkMmUzZTViNDYzMjNkOGNhMTlhM2FjZjNiYjJlYjk3NWVkODY1NzY5YzVj
15
- NGUzZWY2MzUxYTk5NmI3M2MxZjM0YjkyYWU3Nzk0YWFhNmEzYTM=
13
+ NzljNTgzYzhjYmY3MDAxNzMxNjRmMWE0ZjM1ZmRhZWNhNTZmYTliMWVhNTRi
14
+ ZTEwZjYxZjIwNjM0ZTA4NGJjMDYzYTkwNzk4NGYyNTFiOWI1MjYxMjRlNjYw
15
+ N2U4YWIzOThhYjQ3NGE1MjRiOGU0N2JhMWZhYjM5NGM4NWIyZTY=
@@ -8,7 +8,9 @@ module GeneratePuppetfile
8
8
  # Internal: The Bin class contains the logic for calling generate_puppetfile at the command line
9
9
  class Bin
10
10
  Module_regex = Regexp.new("mod ['\"]([a-z0-9_]+\/[a-z0-9_]+)['\"](, ['\"](\\d\.\\d\.\\d)['\"])?", Regexp::IGNORECASE)
11
- @options = {}
11
+ @options = {} # Options hash
12
+ @workspace = nil # Working directory for module download and inspection
13
+ Silence = '>/dev/null 2>&1 '
12
14
 
13
15
  # Public: Initialize a new GeneratePuppetfile::Bin
14
16
  #
@@ -30,7 +32,7 @@ module GeneratePuppetfile
30
32
  helpmsg = "generate-puppetfile: try 'generate-puppetfile --help' for more information."
31
33
 
32
34
  if @args[0].nil? && (! @options[:puppetfile])
33
- puts "generate-puppetfile: No modules or existing Puppetfile specified."
35
+ $stderr.puts "generate-puppetfile: No modules or existing Puppetfile specified."
34
36
  puts helpmsg
35
37
  return 1
36
38
  end
@@ -59,34 +61,78 @@ module GeneratePuppetfile
59
61
  list_forge_modules(forge_module_list) if puppetfile_contents && @options[:debug]
60
62
  list_extras(puppetfile_contents[:extras]) if puppetfile_contents[:extras] && @options[:debug]
61
63
 
62
- generate_puppetfile_contents(forge_module_list, extras)
64
+ unless forge_module_list != [] || puppetfile_contents[:extras] != []
65
+ $stderr.puts "\nNo valid modules or existing Puppetfile content was found. Exiting.\n\n"
66
+ return 1
67
+ end
68
+
69
+ create_workspace()
70
+ @modulepath = "--modulepath #{@workspace} "
71
+
72
+ download_modules(forge_module_list)
73
+ puppetfile_contents = generate_puppetfile_contents(extras)
74
+
75
+
76
+ if @options[:create_puppetfile]
77
+ create_puppetfile(puppetfile_contents)
78
+ end
79
+
80
+ unless @options[:silent]
81
+ display_puppetfile(puppetfile_contents)
82
+ end
83
+
84
+ cleanup_workspace()
63
85
 
64
86
  return 0
65
87
  end
66
88
 
89
+ # Public: Display the generated Puppetfile to STDOUT with delimiters
90
+ def display_puppetfile(puppetfile_contents)
91
+ puts <<-EOF
92
+
93
+ Your Puppetfile has been generated. Copy and paste between the markers:
94
+
95
+ =======================================================================
96
+ #{puppetfile_contents.chomp}
97
+ =======================================================================
98
+ EOF
99
+ end
100
+
101
+ # Public: Create a Puppetfile on disk
102
+ # The Puppetfile will be called 'Puppetfile' in the current working directory
103
+ def create_puppetfile (puppetfile_contents)
104
+ File.open('Puppetfile', 'w') do |file|
105
+ file.write puppetfile_contents
106
+ end
107
+ end
108
+
67
109
  # Public: Validates that a provided module name is valid.
68
110
  def validate (modulename)
69
111
  success = (modulename =~ /[a-z0-9_]\/[a-z0-9_]/i)
70
- puts " '#{modulename}' is not a valid module name. Skipping." unless success
112
+ $stderr.puts "'#{modulename}' is not a valid module name. Skipping." unless success
71
113
  success
72
114
  end
73
115
 
74
116
  # Public: Display the list of Forge modules collected.
75
117
  def list_forge_modules (module_list)
76
- puts "\nListing discovered modules from CLI and/or Puppetfile:\n\n"
77
- module_list.each do |name|
78
- puts " #{name}"
118
+ unless @options[:silent]
119
+ puts "\nListing discovered modules from CLI and/or Puppetfile:\n\n"
120
+ module_list.each do |name|
121
+ puts " #{name}"
122
+ end
123
+ puts ""
79
124
  end
80
- puts ""
81
125
  end
82
126
 
83
127
  # Public: Display the list of extra statements found in the Puppetfile.
84
128
  def list_extras (extras)
85
- puts "\nExtras found in the existing Puppetfile:\n\n"
86
- extras.each do |line|
87
- puts " #{line}"
129
+ unless @options[:silent] || (extras == [])
130
+ puts "\nExtras found in the existing Puppetfile:\n\n"
131
+ extras.each do |line|
132
+ puts " #{line}"
133
+ end
134
+ puts ""
88
135
  end
89
- puts ""
90
136
  end
91
137
 
92
138
  # Public: Read and parse the contents of an existing Puppetfile
@@ -113,64 +159,65 @@ module GeneratePuppetfile
113
159
  puppetfile_contents
114
160
  end
115
161
 
116
- # Public: Generate a new Puppetfile's contents based on a list of modules
117
- # and any extras found in an existing Puppetfile.
162
+ # Public: Download the list of modules and their dependencies to @workspace
118
163
  #
119
164
  # module_list is an array of forge module names to be downloaded
120
- # extras is an array of strings
121
- def generate_puppetfile_contents (module_list, extras)
122
- unless module_list != [] || extras != []
123
- puts "\nNo valid modules or existing Puppetfile content was found. Exiting.\n\n"
124
- exit 1
125
- end
126
-
127
- workspace = Dir.mktmpdir
128
-
129
- modulepath = "--modulepath #{workspace.chomp} "
130
- silence = '>/dev/null 2>&1 '
131
-
132
- puts "\nInstalling modules. This may take a few minutes.\n"
165
+ def download_modules(module_list)
166
+ puts "\nInstalling modules. This may take a few minutes.\n" unless @options[:silent]
133
167
  module_list.each do |name|
134
168
  command = "puppet module install #{name} "
135
- command += modulepath + silence
169
+ command += @modulepath + Silence
136
170
 
137
171
  system(command)
138
172
  end
173
+ end
174
+
175
+ # Public: generate the list of modules in Puppetfile format from the @workspace
176
+ def generate_module_output ()
177
+ module_output = `puppet module list #{@modulepath} 2>/dev/null`
178
+
179
+ module_output.gsub!(/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/, '') # Strips ANSI color codes
180
+ module_output.gsub!(/^\/.*$/, '')
181
+ module_output.gsub!(/-/, '/')
182
+ module_output.gsub!(/├── /, "mod '")
183
+ module_output.gsub!(/└── /, "mod '")
184
+ module_output.gsub!(/ \(v/, "', '")
185
+ module_output.gsub!(/\)$/, "'")
186
+ module_output.gsub!(/^$\n/, '')
187
+
188
+ module_output
189
+ end
139
190
 
140
- puppetfile_header = <<-EOF
191
+ # Public: Generate a new Puppetfile's contents based on a list of modules
192
+ # and any extras found in an existing Puppetfile.
193
+ #
194
+ # extras is an array of strings
195
+ def generate_puppetfile_contents (extras)
196
+
197
+ puppetfile_contents = <<-EOF
141
198
  forge 'http://forge.puppetlabs.com'
142
199
 
143
200
  # Modules discovered by generate-puppetfile
144
201
  EOF
145
202
 
146
- puppetfile_body = `puppet module list #{modulepath} 2>/dev/null`
147
-
148
- puppetfile_body.gsub!(/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/, '') # Strips ANSI color codes
149
- puppetfile_body.gsub!(/^\/.*$/, '')
150
- puppetfile_body.gsub!(/-/, '/')
151
- puppetfile_body.gsub!(/├── /, "mod '")
152
- puppetfile_body.gsub!(/└── /, "mod '")
153
- puppetfile_body.gsub!(/ \(v/, "', '")
154
- puppetfile_body.gsub!(/\)$/, "'")
155
- puppetfile_body.gsub!(/^$\n/, '')
203
+ puppetfile_contents += generate_module_output()
156
204
 
157
- puppetfile_footer = "# Discovered elements from existing Puppetfile\n"
205
+ puppetfile_contents += "# Discovered elements from existing Puppetfile\n" unless extras == []
158
206
  extras.each do |line|
159
- puppetfile_footer += "#{line}"
160
- end if extras
207
+ puppetfile_contents += "#{line}"
208
+ end unless extras == []
161
209
 
162
- puts <<-EOF
163
-
164
- Your Puppetfile has been generated. Copy and paste between the markers:
210
+ puppetfile_contents
211
+ end
165
212
 
166
- =======================================================================
167
- #{puppetfile_header}
168
- #{puppetfile_body}
169
- #{puppetfile_footer}
170
- =======================================================================
171
- EOF
213
+ # Public: Create a temporary workspace for module manipulation
214
+ def create_workspace()
215
+ @workspace = (Dir.mktmpdir).chomp
216
+ end
172
217
 
173
- FileUtils.rm_rf(workspace)
218
+ # Public: Remove the workspace (with prejudice)
219
+ def cleanup_workspace ()
220
+ FileUtils.rm_rf(@workspace)
174
221
  end
175
222
  end
176
223
  end
@@ -21,12 +21,22 @@ module GeneratePuppetfile
21
21
  options[:puppetfile] = file
22
22
  end
23
23
 
24
+ opts.on('-c', '--create_puppetfile', 'Create a Puppetfile in the working directory. Warning: overwrites any existing file with the same name.') do
25
+ options[:create_puppetfile] = true
26
+ end
27
+
28
+ opts.on('-s', '--silent', 'Run in silent mode. Supresses all non-debug output. Adds the -c flag automatically.') do
29
+ options[:silent] = true
30
+ options[:create_puppetfile] = true
31
+ end
32
+
24
33
  opts.on('-d', '--debug', 'Enable debug logging') do
25
34
  options[:debug] = true
26
35
  end
27
36
 
28
37
  opts.on_tail('-v', '--version', 'Show version') do
29
38
  puts "generate-puppetfile v#{GeneratePuppetfile::VERSION}"
39
+ exit
30
40
  end
31
41
  end
32
42
 
@@ -1,3 +1,3 @@
1
1
  module GeneratePuppetfile
2
- VERSION = '0.9.3'
2
+ VERSION = '0.9.4'
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.9.3
4
+ version: 0.9.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Nelson