vagrant-puppetfile 0.3.1 → 0.4.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: c916261b2d2647f4f3437eb35a7da808f3b4a55d
4
- data.tar.gz: 45a8f76b44327825869b71869a12f88190287d37
3
+ metadata.gz: d630dfea4265619b7d7011cc3ca686893bd92159
4
+ data.tar.gz: ce55251db0c8d90e18aefb0cb01d43c465ba7e95
5
5
  SHA512:
6
- metadata.gz: f98ab16b73ff66f6f742b8a30dc6c11e2689f64b33aa15b27e60971af84488530074e30ddd710515f1bb42cc85c18c68eb739c8a8b0d29a43e3c4d933879ee7e
7
- data.tar.gz: 159fdbfb79d554ac9937730e5275d310149370d25d284939adf401f026607c0cb1f987f8e30ebd7dacc10ccddde7d65ab2ef3b2728ffb660b66b02b23ad1cf10
6
+ metadata.gz: bb7211571ab50f91db3b13ced038517a6b90c8e7844edaa0d327e13867b5d16fe07635bb8eedde310c3d59c1a562d3005a8d3479c79bf3643f470542906e05eb
7
+ data.tar.gz: 2979bf2e51e9ac37773a55c13d9b17b32553957939290f87b8ef8f1daf7048eeef40849b3192596028d2b65f7106f1cbc783d5852271256369d7af861e0709d3
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright (c) 2016 Catalyst.net Ltd
2
+ # Copyright (c) 2016-2017 Catalyst.net Ltd
3
3
  #
4
4
  # This file is part of vagrant-puppetfile.
5
5
  #
@@ -21,6 +21,6 @@
21
21
  module Vagrant
22
22
  module Puppetfile
23
23
  NAME = 'vagrant-puppetfile'
24
- VERSION = '0.3.1'
24
+ VERSION = '0.4.0'
25
25
  end
26
26
  end
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright (c) 2016 Catalyst.net Ltd
2
+ # Copyright (c) 2016-2017 Catalyst.net Ltd
3
3
  #
4
4
  # This file is part of vagrant-puppetfile.
5
5
  #
@@ -25,13 +25,17 @@ require 'shellwords'
25
25
 
26
26
  module Vagrant
27
27
  module Puppetfile
28
+ DEFAULT_PUPPETFILE_PATH = 'Puppetfile'
29
+ DEFAULT_PUPPETFILE_EVALUATOR = 'autodetect'
30
+
28
31
  class Provisioner < Vagrant.plugin('2', :provisioner)
29
- def initialize(machine, _)
32
+ def initialize(machine, config)
33
+ @config = config
30
34
  @logger = Vagrant::UI::Prefixed.new(machine.env.ui, machine.name)
31
35
  end
32
36
 
33
37
  def provision
34
- Puppetfile.new.eval(@logger)
38
+ Puppetfile.new(@config.path).evaluator(@config.evaluator, @logger).install!
35
39
  rescue Exception => e
36
40
  @logger.error("Failed to evaluate Puppetfile: #{e.message}")
37
41
  end
@@ -39,25 +43,29 @@ module Vagrant
39
43
 
40
44
  class Command < Vagrant.plugin('2', :command)
41
45
  def execute
42
- # FIXME use vagrant ui objects for these
43
- stdout = Logger.new(STDOUT)
44
- stderr = Logger.new(STDERR)
46
+ puppetfile = DEFAULT_PUPPETFILE_PATH
47
+ evaluator = DEFAULT_PUPPETFILE_EVALUATOR
45
48
 
46
49
  options = OptionParser.new do |o|
47
50
  o.banner = 'Usage: vagrant puppetfile install'
48
51
  o.separator ''
49
52
  o.separator 'Options:'
50
53
  o.on('-h', '--help', 'Print this help') do
51
- return stdout.info(o.help)
54
+ return @env.ui.info(o.help)
55
+ end
56
+ o.on('-e', '--evaluator TYPE', 'Specify the Puppetfile evaluator') do |type|
57
+ evaluator = type
58
+ end
59
+ o.on('-f', '--file PUPPETFILE', 'Specify an alternative Puppetfile') do |path|
60
+ puppetfile = path
52
61
  end
53
62
  end
54
63
 
55
64
  case parse_options(options)
56
65
  when ['install']
57
- stdout.notice('Evaluating Puppetfile ...')
58
- Puppetfile.new.eval(stdout)
66
+ Puppetfile.new(puppetfile).evaluator(evaluator, @env.ui).install!
59
67
  else
60
- stderr.warn(options.help)
68
+ @env.ui.warn(options.help)
61
69
  exit 1
62
70
  end
63
71
  end
@@ -67,88 +75,179 @@ module Vagrant
67
75
  end
68
76
  end
69
77
 
78
+ class Config < Vagrant.plugin('2', :config)
79
+ attr_accessor :path
80
+ attr_accessor :evaluator
81
+
82
+ def initialize
83
+ @path = UNSET_VALUE
84
+ @evaluator = UNSET_VALUE
85
+ end
86
+
87
+ def validate(*_)
88
+ errors = _detected_errors
89
+ errors << 'path must be a String' unless @path.is_a? String
90
+ errors << 'evaluator must be a String' unless @evaluator.is_a? String
91
+ Hash[Vagrant::Puppetfile::Plugin.name => errors]
92
+ end
93
+
94
+ def finalize!
95
+ @path = DEFAULT_PUPPETFILE_PATH if @path == UNSET_VALUE
96
+ @evaluator = DEFAULT_PUPPETFILE_EVALUATOR if @evaluator == UNSET_VALUE
97
+ end
98
+ end
99
+
70
100
  class Plugin < Vagrant.plugin('2')
71
101
  name 'puppetfile'
72
- provisioner('puppetfile') { Provisioner }
73
- command('puppetfile') { Command }
102
+ config(:puppetfile, :provisioner) { Config }
103
+ command(:puppetfile) { Command }
104
+ provisioner(:puppetfile) { Provisioner }
74
105
  end
75
106
 
76
107
  class Puppetfile < File
77
- def initialize(*args)
78
- super('Puppetfile', *args)
108
+ def evaluator(type, logger)
109
+ case File.basename(type)
110
+ when 'librarian-puppet'
111
+ Evaluators::Librarian.new(type, path, logger)
112
+ when 'r10k'
113
+ Evaluators::R10k.new(type, path, logger)
114
+ when 'puppet'
115
+ Evaluators::Puppet.new(type, path, logger)
116
+ when 'autodetect'
117
+ evaluator('librarian-puppet', logger) rescue
118
+ evaluator('r10k', logger) rescue
119
+ evaluator('puppet', logger)
120
+ else
121
+ fail Evaluators::ConfigurationError, type
122
+ end
79
123
  end
124
+ end
80
125
 
81
- def eval(logger)
82
- Evaluator.new(logger).instance_eval(Puppetfile.read(path))
126
+ module Evaluators
127
+ class InstallationError < Vagrant::Errors::VagrantError
128
+ def error_message
129
+ 'Puppetfile module installation failed'
130
+ end
83
131
  end
84
- end
85
132
 
86
- class Logger < Logger
87
- alias detail info
88
- def initialize(*_)
89
- super
90
- self.formatter = Proc.new { |_, _, _, message| message }
133
+ class ConfigurationError < Vagrant::Errors::VagrantError
134
+ def error_message
135
+ 'Invalid vagrant-puppetfile plugin configuration'
136
+ end
91
137
  end
92
138
 
93
- def notice(message)
94
- info("Notice: #{message}\n")
139
+ class NoEvaluatorError < Vagrant::Errors::VagrantError
140
+ def error_message
141
+ 'No Puppetfile evaluator was found'
142
+ end
95
143
  end
96
- end
97
144
 
98
- class Evaluator
99
- if defined? Vagrant
100
- class Exception < Vagrant::Errors::VagrantError
101
- def error_message
102
- 'Puppetfile module installation failed'
145
+ class Base
146
+ attr_reader :logger
147
+ attr_reader :path
148
+ attr_reader :puppetfile
149
+
150
+ ENV_CLEAN_KEYS = %w(GEM_HOME GEM_PATH)
151
+
152
+ def initialize(path, puppetfile, logger)
153
+ @logger = logger
154
+ @path = path
155
+ @puppetfile = puppetfile
156
+ fail NoEvaluatorError, path unless available?
157
+ end
158
+
159
+ def command(*args)
160
+ ::Bundler.with_clean_env do
161
+ ENV_CLEAN_KEYS.each { |k| ENV.delete(k) }
162
+ Open3.capture2e(path, *args.map(&:shellescape))
103
163
  end
104
164
  end
105
- end
106
165
 
107
- def unsupported(option, method, *args)
108
- option = option.inspect
109
- args = args.map(&:inspect).join(', ')
110
- @logger.error "Unsupported option #{option} in `#{method} #{args}'"
111
- raise Exception
166
+ def run(*args)
167
+ output, status = command(*args)
168
+ if status.success?
169
+ output.lines.each { |l| logger.detail(l.chomp) }
170
+ else
171
+ output.lines.each { |l| logger.error(l.chomp) }
172
+ fail InstallationError, output
173
+ end
174
+ end
112
175
  end
113
176
 
114
- def initialize(logger)
115
- @logger = logger
116
- @moduledir = 'modules'
117
- end
177
+ class R10k < Base
178
+ def available?
179
+ command('version').last.success?
180
+ rescue
181
+ false
182
+ end
118
183
 
119
- def moduledir(path)
120
- @moduledir = path
184
+ def install!
185
+ run('puppetfile', 'install', '--verbose')
186
+ end
121
187
  end
122
188
 
123
- def forge(forge)
124
- @forge = forge
189
+ class Librarian < Base
190
+ def available?
191
+ command('version').last.success?
192
+ rescue
193
+ false
194
+ end
195
+
196
+ def install!
197
+ run('install', '--verbose')
198
+ end
125
199
  end
126
200
 
127
- def mod(name, version = :latest, options = {})
128
- if version.is_a?(Hash)
129
- options, version = version, :latest
201
+ class Puppet < Base
202
+ def initialize(path, puppetlabs, logger)
203
+ super
204
+ @moduledir = 'modules'
205
+ end
206
+
207
+ def available?
208
+ command('--version').last.success?
209
+ rescue
210
+ false
211
+ end
212
+
213
+ def install!
214
+ logger.detail("Notice: Evaluating #{puppetfile} ...")
215
+ instance_eval(File.read(puppetfile))
130
216
  end
131
217
 
132
- unless options.is_a?(Hash) and options.empty?
133
- unsupported options, 'mod', name, version, options
218
+ def unsupported(option, method, *args)
219
+ option = option.inspect
220
+ args = args.map(&:inspect).join(', ')
221
+ logger.error("Unsupported option #{option} in `#{method} #{args}'")
222
+ fail InstallationError, path
134
223
  end
135
224
 
136
- unless version.is_a?(String) or version == :latest
137
- unsupported version, 'mod', name, version
225
+ def moduledir(moduledir)
226
+ @moduledir = moduledir
138
227
  end
139
228
 
140
- module_option = '--target-dir', @moduledir
141
- forge_option = '--module_repository', @forge if @forge.is_a?(String)
142
- version_option = '--version', version if version.is_a?(String)
229
+ def forge(forge)
230
+ @forge = forge
231
+ end
143
232
 
144
- command = 'puppet', 'module', 'install', name, *module_option, *forge_option, *version_option
145
- output, status = Open3.capture2e *command.map(&:shellescape)
233
+ def mod(name, version = :latest, options = {})
234
+ if version.is_a?(Hash)
235
+ options, version = version, :latest
236
+ end
146
237
 
147
- if status.success?
148
- output.lines.each { |l| @logger.detail(l) }
149
- else
150
- output.lines.each { |l| @logger.error(l) }
151
- raise Exception, output
238
+ unless options.is_a?(Hash) and options.empty?
239
+ unsupported(options, 'mod', name, version, options)
240
+ end
241
+
242
+ unless version.is_a?(String) or version == :latest
243
+ unsupported(version, 'mod', name, version)
244
+ end
245
+
246
+ module_option = '--target-dir', @moduledir
247
+ forge_option = '--module_repository', @forge if @forge.is_a? String
248
+ version_option = '--version', version if version.is_a? String
249
+
250
+ run('module', 'install', '--verbose', name, *module_option, *forge_option, *version_option)
152
251
  end
153
252
  end
154
253
  end
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright (c) 2016 Catalyst.net Ltd
2
+ # Copyright (c) 2016-2017 Catalyst.net Ltd
3
3
  #
4
4
  # This file is part of vagrant-puppetfile.
5
5
  #
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-puppetfile
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Hanson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-15 00:00:00.000000000 Z
11
+ date: 2017-05-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Automatically installs puppet modules from a Puppetfile during Vagrant
14
14
  provisioning
@@ -40,7 +40,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
40
  version: '0'
41
41
  requirements: []
42
42
  rubyforge_project:
43
- rubygems_version: 2.4.5.1
43
+ rubygems_version: 2.5.1
44
44
  signing_key:
45
45
  specification_version: 4
46
46
  summary: Puppetfile provisioner