sshkit-custom-dsl 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 867e0d7f0ec3e1e7cabaeb2a475cb0a8b4037ae5
4
- data.tar.gz: 9aa920c2586eefb0679bfbe71081e482aaf660fc
3
+ metadata.gz: 2fd766977dc760e757d032aeb5afba4a8ecca796
4
+ data.tar.gz: 244879dd93b7ff53dea9070b59facd6726ef2ab8
5
5
  SHA512:
6
- metadata.gz: 6506a298603b51b54aaf2ae51a1f0bab46f4b1ed0db0da2b36d3e2ffc2ed226e3d5bd8f45b66af9af620c9788f6f73c20e23bfdc6aee452ea09a16251ec1cd9b
7
- data.tar.gz: 7995878880469aee3e526afd4238e898afee55b01026c300b88a2d587a63e244fa779f0cd0a9d844197bbd9251f7879f173512e46561596212d737ff586b7ef0
6
+ metadata.gz: ed3e7881c9e5aa5d64165dd2643135b9082701a66b9abbe09e32520b6141a1dcada72a954e658086843958053a9f2b0f02412b882bcb491c52920d88d1b00251
7
+ data.tar.gz: 0b2e4323dc19009e77c73bdf7693c67dbfc85e34506abffdd442308ea59404bdf556d853a379d6c0ddd3aed68453520405ae0ed3be8ee0a8371f2fecab5bb288
data/.gitignore CHANGED
@@ -21,3 +21,4 @@ tmp
21
21
  *.a
22
22
  mkmf.log
23
23
  .idea
24
+ .DS_Store
data/.rubocop_todo.yml CHANGED
@@ -0,0 +1,11 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`
2
+ # on 2014-06-10 13:48:11 +0200 using RuboCop version 0.23.0.
3
+ # The point is for the user to remove these configuration records
4
+ # one by one as the offenses are removed from the code base.
5
+ # Note that changes in the inspected code, or installation of new
6
+ # versions of RuboCop, may require this file to be generated again.
7
+
8
+ # Offense count: 1
9
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
10
+ Style/MethodName:
11
+ Enabled: false
data/README.md CHANGED
@@ -1,8 +1,13 @@
1
- # Sshkit::Custom::Dsl
1
+ [![Build Status](https://travis-ci.org/faber-lotto/sshkit-custom-dsl.svg)](https://travis-ci.org/faber-lotto/sshkit-custom-dsl)
2
+ [![Coverage Status](https://coveralls.io/repos/faber-lotto/sshkit-custom-dsl/badge.png)](https://coveralls.io/r/faber-lotto/sshkit-custom-dsl)
3
+ [![Code Climate](https://codeclimate.com/github/faber-lotto/sshkit-custom-dsl.png)](https://codeclimate.com/github/faber-lotto/sshkit-custom-dsl)
4
+ [![Gem Version](https://badge.fury.io/rb/capistrano-template.svg)](http://badge.fury.io/rb/capistrano-template)
5
+
6
+ # SSHKit::Custom::DSL
2
7
 
3
8
  Exchanges original sshkit dsl against a custom dsl. This DSL does not change the scope of the blocks.
4
9
  Furthor more it uses `Rake::Threadpool`, to handle parallel requests. Keep in mind `Runner::Parallel`
5
- and Runner::Group are execute all blocks in *parallel threads*, so do it thread save.
10
+ and `Runner::Group` execute all blocks in *parallel threads*, so do it thread save.
6
11
 
7
12
  ## Installation
8
13
 
@@ -25,6 +30,8 @@ Or install it yourself as:
25
30
  require 'sshkit/custom/dsl'
26
31
 
27
32
  extend SSHKit::Custom::DSL
33
+ require 'delegate'
34
+ # require 'sshkit/dsl' <= switch to see the different
28
35
 
29
36
  SSHKit.configure do |sshkit|
30
37
  sshkit.format = :pretty
@@ -38,10 +45,32 @@ SSHKit.configure do |sshkit|
38
45
  end
39
46
  end
40
47
 
48
+ class TestScope < SimpleDelegator
49
+ def data
50
+ {msg: "12345"}
51
+ end
52
+
53
+ def call_it
54
+
55
+ on %w{localhost 127.0.0.1}, in: :sequence, wait: 0 do
56
+ within "/tmp" do
57
+ with rails_env: :production do
58
+ execute "echo", data.fetch(:msg)
59
+ end
60
+ end
61
+ end
62
+
63
+ end
64
+ end
65
+
66
+ data = {msg: "ABCD"}
67
+
68
+ TestScope.new(self).call_it
69
+
41
70
  on %w{localhost 127.0.0.1}, in: :sequence, wait: 0 do
42
71
  within "/tmp" do
43
72
  with rails_env: :production do
44
- execute "echo", "12345"
73
+ execute "echo", data.fetch(:msg)
45
74
  end
46
75
  end
47
76
  end
@@ -71,7 +100,7 @@ It should work like the original one, I hope.
71
100
 
72
101
  ## Contributing
73
102
 
74
- 1. Fork it ( https://github.com/[my-github-username]/sshkit-custom-dsl/fork )
103
+ 1. Fork it ( https://github.com/faber-lotto/sshkit-custom-dsl/fork )
75
104
  2. Create your feature branch (`git checkout -b my-new-feature`)
76
105
  3. Commit your changes (`git commit -am 'Add some feature'`)
77
106
  4. Push to the branch (`git push origin my-new-feature`)
data/Rakefile CHANGED
@@ -1,12 +1,11 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
  require 'rubocop/rake_task'
4
4
 
5
5
  RSpec::Core::RakeTask.new(:spec)
6
6
  RuboCop::RakeTask.new
7
7
 
8
- task :default => :spec
9
-
8
+ task default: :spec
10
9
 
11
10
  desc 'Run RSpec with code coverage'
12
11
  task :coverage do
@@ -11,18 +11,11 @@ module SSHKit
11
11
  @env ||= {}
12
12
  end
13
13
 
14
- def host
15
- @host
16
- end
17
-
18
- def user
19
- @user
20
- end
14
+ attr_reader :host
21
15
 
22
- def group
23
- @group
24
- end
16
+ attr_reader :user
25
17
 
18
+ attr_reader :group
26
19
  end
27
20
  end
28
- end
21
+ end
@@ -1,2 +1,2 @@
1
1
  require 'sshkit'
2
- require 'core_ext/sshkit/backend/abstract'
2
+ require 'core_ext/sshkit/backend/abstract'
@@ -2,7 +2,6 @@ module SSHKit
2
2
  module Custom
3
3
  module Config
4
4
  module Store
5
-
6
5
  module_function
7
6
 
8
7
  def scope_storage
@@ -15,10 +14,10 @@ module SSHKit
15
14
 
16
15
  def create_runner(opts)
17
16
  @runner = Runner::Abstract.create_runner opts
18
- end
17
+ end
19
18
 
20
19
  def runner
21
- @runner.tap{|r| r.backends = backends}
20
+ @runner.tap { |r| r.backends = backends }
22
21
  end
23
22
 
24
23
  def backends=(hosts)
@@ -30,11 +29,13 @@ module SSHKit
30
29
  end
31
30
 
32
31
  def add_pwd(directory)
33
- active_backend.pwd ||= []; active_backend.pwd << directory
32
+ active_backend.pwd ||= []
33
+ active_backend.pwd << directory
34
34
  end
35
35
 
36
36
  def pop_pwd
37
- active_backend.pwd ||= []; active_backend.pwd.pop
37
+ active_backend.pwd ||= []
38
+ active_backend.pwd.pop
38
39
  end
39
40
 
40
41
  def _envs
@@ -42,7 +43,7 @@ module SSHKit
42
43
  end
43
44
 
44
45
  def add_env(env)
45
- old_env = backends.first.env.clone
46
+ old_env = active_backend.env.clone
46
47
  _envs << old_env
47
48
  env = old_env.merge(env)
48
49
  active_backend.env = env
@@ -50,7 +51,7 @@ module SSHKit
50
51
 
51
52
  def pop_env
52
53
  old_env = _envs.pop || {}
53
- backends.each {|backend| backend.env = old_env}
54
+ active_backend.env = old_env
54
55
  end
55
56
 
56
57
  def _user_groups
@@ -58,7 +59,7 @@ module SSHKit
58
59
  end
59
60
 
60
61
  def add_user_group(user, group)
61
- _user_groups << {user: active_backend.user, group: active_backend.group }
62
+ _user_groups << { user: active_backend.user, group: active_backend.group }
62
63
  active_backend.user = user
63
64
  active_backend.group = group
64
65
  end
@@ -70,9 +71,9 @@ module SSHKit
70
71
  end
71
72
 
72
73
  def active_backend
73
- SSHKit::Custom::Config::Runner::Abstract.active_backend
74
+ SSHKit::Custom::Runner::Abstract.active_backend
74
75
  end
75
76
  end
76
77
  end
77
78
  end
78
- end
79
+ end
@@ -2,14 +2,12 @@ module SSHKit
2
2
  module Custom
3
3
  module DSL
4
4
  module ConfigStatements
5
-
6
- def on(hosts, options={}, &block)
5
+ def on(hosts, options = {}, &block)
7
6
  hosts = Array(hosts).map { |rh| Host(rh) }.uniq
8
7
 
9
- _config_store.backends = hosts
10
- _config_store.create_runner options
8
+ _setup_runner(hosts, options)
11
9
 
12
- _config_store.runner.apply_block_to_bcks(&block) if block_given?
10
+ _runner.apply_block_to_bcks(&block) if block_given?
13
11
  end
14
12
 
15
13
  def within(directory)
@@ -30,10 +28,9 @@ module SSHKit
30
28
  end
31
29
 
32
30
  def as(who)
33
-
34
31
  if who.respond_to? :fetch
35
- user = who.fetch(:user, who.fetch("user"))
36
- group = who.fetch(:group, who.fetch("group", nil))
32
+ user = who.fetch(:user) { who.fetch('user') }
33
+ group = who.fetch(:group) { who.fetch('group', nil) }
37
34
  else
38
35
  user = who
39
36
  group = nil
@@ -49,7 +46,15 @@ module SSHKit
49
46
  _config_store.pop_user_group
50
47
  end
51
48
 
49
+ def _setup_runner(hosts, options)
50
+ _config_store.backends = hosts
51
+ _config_store.create_runner options
52
+ end
53
+
54
+ def _runner
55
+ _config_store.runner
56
+ end
52
57
  end
53
58
  end
54
59
  end
55
- end
60
+ end
@@ -2,9 +2,9 @@ module SSHKit
2
2
  module Custom
3
3
  module DSL
4
4
  module ExecStatements
5
+ EXEC_STATEMENTS = [:execute, :make, :rake, :test, :capture, :upload!, :download!].freeze
5
6
 
6
-
7
- [:execute, :make, :rake, :test, :capture, :upload!, :download!].each do |method|
7
+ EXEC_STATEMENTS.each do |method|
8
8
  define_method method do |*args, &block|
9
9
  _config_store.runner.send_cmd method, *args, &block
10
10
  end
@@ -36,7 +36,6 @@ module SSHKit
36
36
  fi
37
37
  EOTEST
38
38
  end
39
-
40
39
  end
41
40
  end
42
41
  end
@@ -2,6 +2,7 @@ module SSHKit
2
2
  module Custom
3
3
  module DSL
4
4
  module Helper
5
+ LOGGING_METHODS = [:log, :debug, :fatal, :error, :warn, :info, :debug].freeze
5
6
 
6
7
  def active_backend
7
8
  SSHKit::Custom::Config::Store.active_backend
@@ -11,7 +12,7 @@ module SSHKit
11
12
  active_backend.host
12
13
  end
13
14
 
14
- [:log, :debug, :fatal, :error, :warn, :info, :debug].each do |method|
15
+ LOGGING_METHODS.each do |method|
15
16
  define_method method do |*args|
16
17
  active_backend.send method, *args
17
18
  end
@@ -28,8 +29,7 @@ module SSHKit
28
29
  def _config_store
29
30
  @_config_store ||= SSHKit::Custom::Config::Store
30
31
  end
31
-
32
32
  end
33
33
  end
34
34
  end
35
- end
35
+ end
@@ -1,7 +1,7 @@
1
1
  module SSHKit
2
2
  module Custom
3
3
  module DSL
4
- VERSION = "0.0.6"
4
+ VERSION = '0.0.7'
5
5
  end
6
6
  end
7
7
  end
@@ -3,10 +3,10 @@ require 'core_ext/sshkit'
3
3
  require 'scoped_storage'
4
4
 
5
5
  require 'sshkit/custom/config/store'
6
- require 'sshkit/custom/config/runner/abstract'
7
- require 'sshkit/custom/config/runner/sequential'
8
- require 'sshkit/custom/config/runner/parallel'
9
- require 'sshkit/custom/config/runner/group'
6
+ require 'sshkit/custom/runner/abstract'
7
+ require 'sshkit/custom/runner/sequential'
8
+ require 'sshkit/custom/runner/parallel'
9
+ require 'sshkit/custom/runner/group'
10
10
 
11
11
  require 'sshkit/custom/dsl/config_statements'
12
12
  require 'sshkit/custom/dsl/exec_statements'
@@ -0,0 +1,88 @@
1
+ module SSHKit
2
+ module Custom
3
+ module Runner
4
+ ExecuteError = SSHKit::Runner::ExecuteError
5
+
6
+ class Abstract
7
+ attr_accessor :backends
8
+ attr_reader :options
9
+ attr_writer :wait_interval
10
+
11
+ def self.create_runner(opts)
12
+ opts_with_defaults = { in: :parallel }.merge(opts)
13
+
14
+ case opts_with_defaults[:in]
15
+ when :parallel
16
+ Parallel
17
+ when :sequence
18
+ Sequential
19
+ when :groups
20
+ Group
21
+ else
22
+ fail "Don't know how to handle run style #{opts_with_defaults[:in].inspect}"
23
+ end.new(opts_with_defaults)
24
+ end
25
+
26
+ def self.scope_storage
27
+ ScopedStorage::ThreadLocalStorage
28
+ end
29
+
30
+ def self.scope
31
+ @scope ||= ScopedStorage::Scope.new('sshkit_runner', scope_storage)
32
+ end
33
+
34
+ def self.active_backend
35
+ scope[:active_backend] || fail(ArgumentError, 'Backend not set')
36
+ end
37
+
38
+ def self.active_backend=(new_backend)
39
+ scope[:active_backend] = new_backend
40
+ end
41
+
42
+ def initialize(options = nil)
43
+ @options = options || {}
44
+ end
45
+
46
+ def active_backend
47
+ self.class.active_backend
48
+ end
49
+
50
+ def active_backend=(new_backend)
51
+ self.class.active_backend = new_backend
52
+ end
53
+
54
+ def send_cmd(cmd, *args, &block)
55
+ args = Array(block.call(active_backend.host)) if block
56
+ active_backend.send(cmd, *args)
57
+ rescue => e
58
+ e2 = ExecuteError.new e
59
+ raise e2, "Exception while executing on host #{active_backend.host}: #{e.message}"
60
+ end
61
+
62
+ def apply_block_to_bcks(&_block)
63
+ fail SSHKit::Backend::MethodUnavailableError
64
+ end
65
+
66
+ def apply_to_bck(backend, &block)
67
+ self.active_backend = backend
68
+ block.call(backend.host)
69
+ rescue => e
70
+ e2 = ExecuteError.new e
71
+ raise e2, "Exception while executing on host #{backend.host}: #{e.message}"
72
+ ensure
73
+ self.active_backend = nil
74
+ end
75
+
76
+ def do_wait
77
+ sleep wait_interval
78
+ end
79
+
80
+ protected
81
+
82
+ def wait_interval
83
+ @wait_interval || options[:wait] || 2
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,34 @@
1
+ module SSHKit
2
+ module Custom
3
+ module Runner
4
+ class Group < Abstract
5
+ attr_writer :group_size
6
+
7
+ def apply_block_to_bcks(&block)
8
+ backends.each_slice(group_size).map do |group_backends|
9
+
10
+ exec_parallel(group_backends, &block)
11
+
12
+ do_wait
13
+
14
+ end.flatten
15
+ end
16
+
17
+ def group_size
18
+ @group_size ||= options[:limit] || 2
19
+ end
20
+
21
+ def exec_parallel(group, &block)
22
+ use_runner.call(options).tap do |runner|
23
+ runner.backends = group
24
+ runner.apply_block_to_bcks(&block)
25
+ end
26
+ end
27
+
28
+ def use_runner
29
+ ->(options) { Parallel.new(options) }
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,30 @@
1
+ module SSHKit
2
+ module Custom
3
+ module Runner
4
+ require 'rake'
5
+
6
+ class Parallel < Abstract
7
+ def thread_pool
8
+ @thread_pool ||= Rake::ThreadPool.new(thread_count)
9
+ end
10
+
11
+ def apply_block_to_bcks(&block)
12
+ futures = to_futures(&block)
13
+ futures.each { |f| f.value }
14
+ end
15
+
16
+ def thread_count
17
+ @thread_count ||= options[:thread_count] || Rake.suggested_thread_count - 1
18
+ end
19
+
20
+ def to_futures(&block)
21
+ backends.map do |b|
22
+ thread_pool.future(b) do |fb|
23
+ apply_to_bck fb, &block
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,14 @@
1
+ module SSHKit
2
+ module Custom
3
+ module Runner
4
+ class Sequential < Abstract
5
+ def apply_block_to_bcks(&block)
6
+ backends.each do |backend|
7
+ apply_to_bck backend, &block
8
+ do_wait
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
data/spec/spec_helper.rb CHANGED
@@ -44,5 +44,3 @@ RSpec.configure do |config|
44
44
  # --seed 1234
45
45
  config.order = 'random'
46
46
  end
47
-
48
-
@@ -0,0 +1,116 @@
1
+ require 'unit_spec_helper'
2
+
3
+ module SSHKit
4
+ module Custom
5
+ module Config
6
+ describe Store do
7
+
8
+ let(:mock_bck) do
9
+ SSHKit.config.backend.new(SSHKit::Host.new('localhost'))
10
+ end
11
+
12
+ let(:new_env) { { ab: :cd } }
13
+ let(:old_env) { { ab: 123, i: 5 } }
14
+
15
+ before :each do
16
+ SSHKit::Custom::Runner::Abstract.active_backend = mock_bck
17
+ end
18
+
19
+ describe '.create_runner' do
20
+ it 'creates the wanted runner' do
21
+ opts = { in: :parallel }
22
+ expect(Runner::Abstract).to receive(:create_runner).with(opts)
23
+ Store.create_runner opts
24
+ end
25
+ end
26
+
27
+ describe '.runner' do
28
+ it 'returns the actual runner' do
29
+ opts = { in: :parallel }
30
+ Store.create_runner opts
31
+
32
+ expect(Store.runner).not_to be_nil
33
+ end
34
+ end
35
+
36
+ describe '.backends=' do
37
+ it 'stores a list of backends' do
38
+ Store.backends = ['127.0.0.1', 'localhost'].map { |h| SSHKit::Host.new(h) }
39
+ expect(Store.backends.last.host.hostname).to eq('localhost')
40
+ end
41
+ end
42
+
43
+ describe '.add_pwd' do
44
+ it 'adds a directory to the stack' do
45
+ expect { Store.add_pwd('ddd') }.to change { mock_bck.pwd.count }.by(1)
46
+ end
47
+ end
48
+
49
+ describe '.pop_pwd' do
50
+ it 'removes a directory from the stack' do
51
+ Store.add_pwd('ddd')
52
+ Store.add_pwd('bbb')
53
+ Store.add_pwd('ccc')
54
+ expect { Store.pop_pwd }.to change { mock_bck.pwd.count }.by(-1)
55
+ end
56
+ end
57
+
58
+ describe '.add_env' do
59
+
60
+ it 'adds an env to the stack' do
61
+ expect(mock_bck.env).to eq({})
62
+ Store.add_env(new_env)
63
+ expect(mock_bck.env).to eq(new_env)
64
+ end
65
+
66
+ it 'merges the old env with the new one' do
67
+ Store.add_env(old_env)
68
+ Store.add_env(new_env)
69
+
70
+ expect(mock_bck.env).to eq(old_env.merge(new_env))
71
+ end
72
+ end
73
+
74
+ describe '.pop_env' do
75
+ it 'removes an env from the stack' do
76
+ Store.add_env(new_env)
77
+ Store.pop_env
78
+ expect(mock_bck.env).to eq({})
79
+ end
80
+
81
+ it 'restores pre existing envs' do
82
+ Store.add_env(old_env)
83
+ Store.add_env(new_env)
84
+
85
+ Store.pop_env
86
+
87
+ expect(mock_bck.env).to eq(old_env)
88
+ end
89
+ end
90
+
91
+ describe '.add_user_group' do
92
+ it 'stores an user and group' do
93
+ Store.add_user_group('u', 'g')
94
+ expect(mock_bck.user).to eq 'u'
95
+ expect(mock_bck.group).to eq 'g'
96
+ end
97
+ end
98
+
99
+ describe '.pop_user_group' do
100
+ it 'restores previous an user and group' do
101
+ Store.add_user_group('u', 'g')
102
+ Store.add_user_group('u1', 'g1')
103
+
104
+ expect(mock_bck.user).to eq 'u1'
105
+ expect(mock_bck.group).to eq 'g1'
106
+
107
+ Store.pop_user_group
108
+
109
+ expect(mock_bck.user).to eq 'u'
110
+ expect(mock_bck.group).to eq 'g'
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end