producer-core 0.5.11 → 0.5.12

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: 36585c1613648e9e3c47084767dbc1d4acb341a4
4
- data.tar.gz: cb961db67e6034e2224cc999ef2c3f27f5502170
3
+ metadata.gz: a59bbda268d60668c1b0bff8dc1f37eefc3a834d
4
+ data.tar.gz: 830f6e8ba0a452024d905679e3536f359476767b
5
5
  SHA512:
6
- metadata.gz: 3e6ed9ca8c684ae00da66d69471f5524e4aad33c4d0016be19cb46374eefb82bb5e15c089663869ec4c15a0b744e63ae146f0ec7b4e695050547f7635c16eb0e
7
- data.tar.gz: 2bb9d2a0c8599b2c869c6fedc291d931411ca9d63670e1dbe6ba0c4110baa96904da302bc323dcbdb25469b60a6f9fe7fa55f9c3cb0d1902e1c369931f3b23e4
6
+ metadata.gz: 37113fab68f768445a648eb0c299aec71b07ec3f1be756195ce96dc711a62cb70759f29cbd9ae86f4fc7e1f4deccd4df5594e7ca585310656c7e279d1bd159fc
7
+ data.tar.gz: 30ffac8b310c4a41ed49c97611f8eacab87401a59e02d7a54d72dc51beb5eaaaedb1abbede07c79f4a6822674e29579e09e79924b52cd709261754302368b3df
data/README.md CHANGED
@@ -227,109 +227,6 @@ design and usage of Domain Specific Languages in Ruby, and refactor
227
227
  all my scripts as "recipes" in a common language.
228
228
 
229
229
 
230
- Sample recipe
231
- -------------
232
-
233
- Based on the previous template example (FreeBSD jails.conf
234
- template):
235
-
236
- ```ruby
237
- require 'producer/stdlib'
238
-
239
- JAILS_ROOT = '/var/jails'.freeze
240
- ZROOT = 'tank/jails'.freeze
241
- INTERFACE = 're0'.freeze
242
- SETS = {
243
- base: '2b028a894d25711ad496762622a52d74b1e32ee04693ad1cf056e3ddcdc23975',
244
- src: 'f919287a5ef51d4f133f27c99c54f2e8054f408d3dd53bc60f4e233cc75ec03d'
245
- }.freeze
246
- JAILS = [
247
- {
248
- name: 'freebsd-10r1',
249
- src: true,
250
- addr4: '10.0.0.1'
251
- },
252
- {
253
- name: 'freebsd-10r1-gcc',
254
- src: true,
255
- addr4: '10.0.0.2'
256
- }
257
- ].freeze
258
-
259
- task :freebsd_archives_fetch do
260
- SETS.keys.each { |set| condition { no_file? "/tmp/#{set}.txz"} }
261
-
262
- SETS.each do |set, sum|
263
- sh <<-eoh
264
- cd /tmp && \
265
- fetch ftp://ftp.freebsd.org:/pub/FreeBSD/releases/amd64/10.1-RELEASE/#{set}.txz && \
266
- sha256 -c #{sum} #{set}.txz
267
- eoh
268
- end
269
- end
270
-
271
- task :jails_fs_create do
272
- condition { no_sh "zfs list #{ZROOT}" }
273
- sh "zfs create -o mountpoint=#{JAILS_ROOT} -o compress=lz4 #{ZROOT}"
274
- end
275
-
276
- JAILS.each do |jail|
277
- root = "#{JAILS_ROOT}/#{jail[:name]}"
278
- fs = "#{ZROOT}/#{jail[:name]}"
279
-
280
- task :jail_initialize do
281
- condition { no_sh "zfs list #{fs}@install" }
282
-
283
- task :jail_fs_create do
284
- condition { no_sh "zfs list #{fs}" }
285
-
286
- sh "zfs create #{fs}"
287
-
288
- SETS.keys.each do |set|
289
- next if set == 'src' && !jail[:src]
290
- sh "tar -JxC #{root}/ -f /tmp/#{set}.txz"
291
- end
292
- end
293
-
294
- task :rc_conf do
295
- file_write_once "#{root}/etc/rc.conf", <<-eoh
296
- hostname=#{jail[:name]}
297
- # ...
298
- eoh
299
- end
300
-
301
- task :root_passwd do
302
- sh "chroot #{root} pw user mod root -w random"
303
- end
304
-
305
- task :mail_aliases do
306
- condition { no_file? "#{root}/etc/mail/aliases.db" }
307
-
308
- sh "chroot #{root} make -C /etc/mail aliases"
309
- end
310
-
311
- freebsd_update_patch_interactive "#{root}/usr/sbin/freebsd-update"
312
-
313
- task :jail_snapshot_install do
314
- sh "zfs snapshot #{fs}@install"
315
- end
316
- end
317
-
318
- task :jail_update do
319
- condition { no_sh "zfs list #{fs}@update" }
320
-
321
- sh "chroot #{root} freebsd-update fetch install"
322
- sh "zfs snapshot #{fs}@update"
323
- end
324
- end
325
-
326
- task :jails_conf do
327
- conf = template 'freebsd/jail.conf', if: INTERFACE, jails: JAILS
328
- file_write_once '/etc/jail.conf', conf
329
- end
330
- ```
331
-
332
-
333
230
  Similar or related code and tools
334
231
  ---------------------------------
335
232
 
@@ -1,7 +1,7 @@
1
1
  module Producer
2
2
  module Core
3
3
  class Action
4
- INSPECT_ARGUMENTS_SUM_LEN = 68.freeze
4
+ INSPECT_ARGUMENTS_SUM_LEN = 68
5
5
 
6
6
  extend Forwardable
7
7
  def_delegators :@env, :input, :output, :error_output, :remote
@@ -34,6 +34,12 @@ module Producer
34
34
  return if arguments.compact.size == size
35
35
  fail ArgumentError, '`%s\' action requires %d arguments' % [name, size]
36
36
  end
37
+
38
+ def convert_options conversions
39
+ conversions.each do |original, convertion|
40
+ options[convertion] = options.delete original if options.key? original
41
+ end
42
+ end
37
43
  end
38
44
  end
39
45
  end
@@ -4,9 +4,8 @@ module Producer
4
4
  class FileWriter < Action
5
5
  def setup
6
6
  check_arguments_size! arguments_size
7
- @path, @content = arguments
8
- @options[:permissions] = @options.delete :mode if options.key? :mode
9
- @options[:owner] = @options.delete :user if options.key? :user
7
+ @path, @content = arguments
8
+ convert_options mode: :permissions, user: :owner
10
9
  end
11
10
 
12
11
  def name
@@ -15,7 +14,7 @@ module Producer
15
14
 
16
15
  def apply
17
16
  fs.file_write @path, @content
18
- fs.setstat @path, @options unless @options.empty?
17
+ fs.setstat @path, options unless options.empty?
19
18
  end
20
19
 
21
20
  private
@@ -5,8 +5,7 @@ module Producer
5
5
  def setup
6
6
  check_arguments_size! 1
7
7
  @path = Pathname.new(arguments.first)
8
- @options[:permissions] = @options.delete :mode if @options.key? :mode
9
- @options[:owner] = @options.delete :user if @options.key? :user
8
+ convert_options mode: :permissions, user: :owner
10
9
  end
11
10
 
12
11
  def name
@@ -17,7 +16,7 @@ module Producer
17
16
  @path.descend do |path|
18
17
  next if fs.dir? path
19
18
  fs.mkdir path.to_s
20
- fs.setstat path.to_s, @options unless @options.empty?
19
+ fs.setstat path.to_s, options unless options.empty?
21
20
  end
22
21
  end
23
22
  end
@@ -25,11 +25,7 @@ module Producer
25
25
  stderr.puts e.message
26
26
  exit EX_USAGE
27
27
  rescue StandardError => e
28
- ef = ErrorFormatter.new(
29
- debug: cli.env.debug?,
30
- force_cause: [RecipeEvaluationError]
31
- )
32
- stderr.puts ef.format e
28
+ stderr.puts ErrorFormatter.new(debug: cli.env.debug?).format e
33
29
  exit EX_SOFTWARE
34
30
  end
35
31
  end
@@ -3,20 +3,8 @@ module Producer
3
3
  class Condition
4
4
  class << self
5
5
  def define_test keyword, test
6
- {
7
- keyword => false,
8
- "no_#{keyword}" => true
9
- }.each do |kw, negated|
10
- define_method(kw) do |*args|
11
- if test.respond_to? :call
12
- args = [test, *args]
13
- klass = Tests::ConditionTest
14
- else
15
- klass = test
16
- end
17
- @tests << klass.new(@env, *args, negated: negated)
18
- end
19
- end
6
+ declare_test_method keyword, test
7
+ declare_test_method "no_#{keyword}", test, negated: true
20
8
  end
21
9
 
22
10
  def evaluate env, *args, &block
@@ -26,6 +14,20 @@ module Producer
26
14
  o.instance_eval { @return_value = return_value }
27
15
  end
28
16
  end
17
+
18
+ private
19
+
20
+ def declare_test_method keyword, test, negated: false
21
+ define_method keyword do |*args|
22
+ if test.respond_to? :call
23
+ args.unshift test
24
+ klass = Tests::ConditionTest
25
+ else
26
+ klass = test
27
+ end
28
+ @tests << klass.new(@env, *args, negated: negated)
29
+ end
30
+ end
29
31
  end
30
32
 
31
33
  extend Forwardable
@@ -8,9 +8,8 @@ module Producer
8
8
  /\/net-sftp/
9
9
  ].freeze
10
10
 
11
- def initialize debug: false, force_cause: []
12
- @debug = debug
13
- @force_cause = force_cause
11
+ def initialize debug: false
12
+ @debug = debug
14
13
  end
15
14
 
16
15
  def debug?
@@ -9,5 +9,6 @@ module Producer
9
9
  RemoteCommandExecutionError = Class.new(RuntimeError)
10
10
  RegistryKeyError = Class.new(RuntimeError)
11
11
  RemoteInvalidError = Class.new(ArgumentError)
12
+ TemplateMissingError = Class.new(RuntimeError)
12
13
  end
13
14
  end
@@ -33,7 +33,7 @@ module Producer
33
33
  end
34
34
 
35
35
  def file_read path
36
- sftp.file.open(path) { |f| f.read }
36
+ sftp.file.open path, &:read
37
37
  rescue Net::SFTP::StatusException
38
38
  nil
39
39
  end
@@ -10,10 +10,7 @@ module Producer
10
10
 
11
11
  def session
12
12
  @session ||= begin
13
- if !@hostname
14
- fail RemoteInvalidError,
15
- "remote target is invalid: `#{@hostname.inspect}'"
16
- end
13
+ check_hostname!
17
14
  Net::SSH.start(@hostname, user_name)
18
15
  end
19
16
  end
@@ -57,6 +54,14 @@ module Producer
57
54
  def cleanup
58
55
  session.close if @session
59
56
  end
57
+
58
+ private
59
+
60
+ def check_hostname!
61
+ return if @hostname
62
+ fail RemoteInvalidError,
63
+ "remote target is invalid: `#{@hostname.inspect}'"
64
+ end
60
65
  end
61
66
  end
62
67
  end
@@ -0,0 +1,26 @@
1
+ module Producer
2
+ module Core
3
+ class Template
4
+ class ERBRenderer
5
+ class << self
6
+ def render file_path, variables = {}
7
+ tpl = ERB.new(File.read(file_path), nil, '-')
8
+ tpl.filename = file_path.to_s
9
+ tpl.result build_erb_binding variables
10
+ end
11
+
12
+ protected
13
+
14
+ def build_erb_binding variables
15
+ Object.new.instance_eval do |o|
16
+ variables.each do |k, v|
17
+ o.instance_variable_set "@#{k}", v
18
+ end
19
+ binding
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ module Producer
2
+ module Core
3
+ class Template
4
+ class YAMLRenderer
5
+ class << self
6
+ def render file_path, *_
7
+ YAML.load(File.read(file_path))
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -2,50 +2,38 @@ module Producer
2
2
  module Core
3
3
  class Template
4
4
  SEARCH_PATH = 'templates'.freeze
5
+ RENDERERS = {
6
+ ERBRenderer => %i[erb],
7
+ YAMLRenderer => %i[yaml]
8
+ }.freeze
5
9
 
6
- def initialize path, search_path: SEARCH_PATH
10
+ def initialize path, search_path: SEARCH_PATH, renderers: RENDERERS
7
11
  @path = Pathname.new(path)
8
12
  @search_path = Pathname.new(search_path)
13
+ @renderers = renderers
9
14
  end
10
15
 
11
16
  def render variables = {}
12
- case (file_path = resolve_path).extname
13
- when '.yaml' then render_yaml file_path
14
- when '.erb' then render_erb file_path, variables
17
+ candidates.each do |c|
18
+ r, * = @renderers.find { |_, v| v.include? c.extname[1..-1].to_sym }
19
+ return r.render c, variables if r
15
20
  end
16
- end
17
-
18
- private
19
21
 
20
- def render_erb file_path, variables = {}
21
- tpl = ERB.new(File.read(file_path), nil, '-')
22
- tpl.filename = file_path.to_s
23
- tpl.result build_erb_binding variables
22
+ fail TemplateMissingError, "template `#{@path}' not found"
24
23
  end
25
24
 
26
- def render_yaml file_path
27
- YAML.load(File.read(file_path))
28
- end
25
+ protected
29
26
 
30
- def resolve_path
27
+ def candidates
31
28
  if @path.to_s =~ /\A\.\//
32
- resolve_suffix @path
29
+ glob_candidates_by_prefix @path
33
30
  else
34
- resolve_suffix @search_path + @path
31
+ glob_candidates_by_prefix @search_path + @path
35
32
  end
36
33
  end
37
34
 
38
- def resolve_suffix path
39
- Pathname.glob("#{path}.{erb,yaml}").first
40
- end
41
-
42
- def build_erb_binding variables
43
- Object.new.instance_eval do |o|
44
- variables.each do |k, v|
45
- o.instance_variable_set "@#{k}", v
46
- end
47
- binding
48
- end
35
+ def glob_candidates_by_prefix path_prefix
36
+ Pathname.glob(path_prefix.to_s + ?*)
49
37
  end
50
38
  end
51
39
  end
@@ -20,6 +20,10 @@ module Producer
20
20
  def pass?
21
21
  verify ^ negated?
22
22
  end
23
+
24
+ def verify
25
+ fail NotImplementedError
26
+ end
23
27
  end
24
28
  end
25
29
  end
@@ -29,6 +29,7 @@ end
29
29
  # Enable cucumber-sshd "fast" mode (persists sshd across scenarios), and
30
30
  # register hooks for @sshd tagged scenarios.
31
31
  Before do
32
- @_sshd_fast = true
32
+ @_sshd_fast = true
33
+ @_sshd_wait_ready = true if ENV.key? 'TRAVIS'
33
34
  end
34
35
  require 'cucumber/sshd/cucumber'
@@ -1,5 +1,5 @@
1
1
  module Producer
2
2
  module Core
3
- VERSION = '0.5.11'.freeze
3
+ VERSION = '0.5.12'.freeze
4
4
  end
5
5
  end
data/lib/producer/core.rb CHANGED
@@ -45,6 +45,8 @@ require 'producer/core/remote'
45
45
  require 'producer/core/remote/environment'
46
46
  require 'producer/core/remote/fs'
47
47
  require 'producer/core/task'
48
+ require 'producer/core/template/erb_renderer'
49
+ require 'producer/core/template/yaml_renderer'
48
50
  require 'producer/core/template'
49
51
  require 'producer/core/version'
50
52
  require 'producer/core/worker'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: producer-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.11
4
+ version: 0.5.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thibault Jouan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-14 00:00:00.000000000 Z
11
+ date: 2015-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-ssh
@@ -45,6 +45,9 @@ dependencies:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0.8'
48
+ - - "<"
49
+ - !ruby/object:Gem::Version
50
+ version: '0.9'
48
51
  type: :development
49
52
  prerelease: false
50
53
  version_requirements: !ruby/object:Gem::Requirement
@@ -52,6 +55,9 @@ dependencies:
52
55
  - - "~>"
53
56
  - !ruby/object:Gem::Version
54
57
  version: '0.8'
58
+ - - "<"
59
+ - !ruby/object:Gem::Version
60
+ version: '0.9'
55
61
  - !ruby/object:Gem::Dependency
56
62
  name: cucumber
57
63
  requirement: !ruby/object:Gem::Requirement
@@ -149,6 +155,8 @@ files:
149
155
  - lib/producer/core/remote/fs.rb
150
156
  - lib/producer/core/task.rb
151
157
  - lib/producer/core/template.rb
158
+ - lib/producer/core/template/erb_renderer.rb
159
+ - lib/producer/core/template/yaml_renderer.rb
152
160
  - lib/producer/core/test.rb
153
161
  - lib/producer/core/testing.rb
154
162
  - lib/producer/core/testing/aruba_program_wrapper.rb
@@ -192,8 +200,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
192
200
  version: '0'
193
201
  requirements: []
194
202
  rubyforge_project:
195
- rubygems_version: 2.4.5
203
+ rubygems_version: 2.4.5.1
196
204
  signing_key:
197
205
  specification_version: 4
198
206
  summary: Provisioning tool
199
207
  test_files: []
208
+ has_rdoc: