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 +8 -8
- data/lib/generate_puppetfile/bin.rb +99 -52
- data/lib/generate_puppetfile/optparser.rb +10 -0
- data/lib/generate_puppetfile/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MGRhMmMzNmZhZDdiNzQ3ZTIyOThjMDY4OTEwYmQ1N2M3ODhmNzUwNA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NjUzMDg5M2UzMDc1ZDQxNzU0M2Q2MDRlN2VlODI0NmYwOTA2YmRjNw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZGE2MDk0N2VmYzA4ZjFiNzJmZTk5N2M3OTRhZDg4Yjg5MDU5NjA1MWNmNjcz
|
10
|
+
YWMzMGIzM2RkMTFkODI0YzE5MzgwZTUxZTYwYmE2ZjNiZGIzNWZjNzg2YTY0
|
11
|
+
NDI1ODAzM2U4YTZlOTIzMWFhYWMyNzY5ZDMwYjFjYTRmOWVmYjc=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
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 "
|
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
|
-
|
77
|
-
|
78
|
-
|
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
|
-
|
86
|
-
|
87
|
-
|
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:
|
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
|
-
|
121
|
-
|
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 +
|
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
|
-
|
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
|
-
|
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
|
-
|
205
|
+
puppetfile_contents += "# Discovered elements from existing Puppetfile\n" unless extras == []
|
158
206
|
extras.each do |line|
|
159
|
-
|
160
|
-
end
|
207
|
+
puppetfile_contents += "#{line}"
|
208
|
+
end unless extras == []
|
161
209
|
|
162
|
-
|
163
|
-
|
164
|
-
Your Puppetfile has been generated. Copy and paste between the markers:
|
210
|
+
puppetfile_contents
|
211
|
+
end
|
165
212
|
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
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
|
-
|
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
|
|