sshkit-custom-dsl 0.0.7 → 0.0.8

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: 2fd766977dc760e757d032aeb5afba4a8ecca796
4
- data.tar.gz: 244879dd93b7ff53dea9070b59facd6726ef2ab8
3
+ metadata.gz: d69e7d1a549d382002bb5f7b61505554d86cbea7
4
+ data.tar.gz: 06927a065efea837d41444920c747f56da9fe253
5
5
  SHA512:
6
- metadata.gz: ed3e7881c9e5aa5d64165dd2643135b9082701a66b9abbe09e32520b6141a1dcada72a954e658086843958053a9f2b0f02412b882bcb491c52920d88d1b00251
7
- data.tar.gz: 0b2e4323dc19009e77c73bdf7693c67dbfc85e34506abffdd442308ea59404bdf556d853a379d6c0ddd3aed68453520405ae0ed3be8ee0a8371f2fecab5bb288
6
+ metadata.gz: f49e9f43033421eba935a4db6b7b483af95262154a290da5199c9ab6d84828f15bcbe52f5d179d9a700045a129b2ba3ac77bf45b2790d61d942646f3366ab5dd
7
+ data.tar.gz: ac897cae0d3eb20962659992632d74986ef4f58686e0a468502e68f629465f29b91cbef1a30125159bd6de7d4dad0b42bf94a0de2adc6a39e60fd41de7b23a30
data/.yardopts ADDED
@@ -0,0 +1,8 @@
1
+ --tag dsl:"DSL"
2
+ --no-private
3
+ --public
4
+ --api public
5
+ --verbose
6
+ --debug
7
+ --markup-provider=redcarpet
8
+ --markup=markdown
data/Gemfile CHANGED
@@ -6,5 +6,8 @@ gemspec
6
6
  gem 'simplecov', require: false, group: :test
7
7
  gem 'coveralls', require: false
8
8
  gem 'rubocop', require: false
9
+ gem 'yard', require: false
10
+ gem 'redcarpet', require: false
11
+ gem 'github-markup', require: false
9
12
 
10
13
 
data/Rakefile CHANGED
@@ -1,10 +1,15 @@
1
1
  require 'bundler/gem_tasks'
2
2
  require 'rspec/core/rake_task'
3
3
  require 'rubocop/rake_task'
4
+ require 'yard'
4
5
 
5
6
  RSpec::Core::RakeTask.new(:spec)
6
7
  RuboCop::RakeTask.new
7
8
 
9
+ YARD::Rake::YardocTask.new do |t|
10
+ t.files = ['lib/**/*.rb'] # optional
11
+ end
12
+
8
13
  task default: :spec
9
14
 
10
15
  desc 'Run RSpec with code coverage'
@@ -1,5 +1,6 @@
1
1
  module SSHKit
2
2
  module Backend
3
+ # Extend (monkeypatch)the original class with some accessors.
3
4
  class Abstract
4
5
  attr_writer :pwd, :env, :host, :user, :group
5
6
 
@@ -4,44 +4,63 @@ module SSHKit
4
4
  module Store
5
5
  module_function
6
6
 
7
+ # @api private
7
8
  def scope_storage
8
9
  ScopedStorage::ThreadLocalStorage
9
10
  end
10
11
 
12
+ # @api private
11
13
  def config_scope
12
14
  @config_scope ||= ScopedStorage::Scope.new('sshkit_dsl_config', scope_storage)
13
15
  end
14
16
 
17
+ # Creates a new runner
18
+ # @option opts [Symbol] :in Chooses the runner to be used
19
+ # :parallel => Parallel
20
+ # :sequence => Sequential
21
+ # :groups => Group
22
+ # @option opts [Integer] :wait Amount of seconds to sleep between executions for Sequential and Parallel Runner
23
+ # @option opts [Integer] :limit Amount of hosts to use in one Batch for Group Runner
24
+ #
15
25
  def create_runner(opts)
16
26
  @runner = Runner::Abstract.create_runner opts
17
27
  end
18
28
 
29
+ # The actual runner object
19
30
  def runner
20
31
  @runner.tap { |r| r.backends = backends }
21
32
  end
22
33
 
34
+ # Sets the actual backends
23
35
  def backends=(hosts)
24
36
  @backends = hosts.map { |host| SSHKit.config.backend.new(host) }
25
37
  end
26
38
 
39
+ # Get the actual backends
27
40
  def backends
28
41
  @backends ||= []
29
42
  end
30
43
 
44
+ # Set the working directory for the current backend.
45
+ # @param directory [String] The new working directory
31
46
  def add_pwd(directory)
32
47
  active_backend.pwd ||= []
33
48
  active_backend.pwd << directory
34
49
  end
35
50
 
51
+ # Set the working directory to the previous working directory for the current backend.
36
52
  def pop_pwd
37
53
  active_backend.pwd ||= []
38
54
  active_backend.pwd.pop
39
55
  end
40
56
 
57
+ # @api private
41
58
  def _envs
42
59
  config_scope[:_envs] ||= []
43
60
  end
44
61
 
62
+ # Set the environment for the current backend.
63
+ # @param env [Hash<String, String>] The new ENV-Vars to be used.
45
64
  def add_env(env)
46
65
  old_env = active_backend.env.clone
47
66
  _envs << old_env
@@ -49,27 +68,34 @@ module SSHKit
49
68
  active_backend.env = env
50
69
  end
51
70
 
71
+ # Resets the environment variables to the previous one.
52
72
  def pop_env
53
73
  old_env = _envs.pop || {}
54
74
  active_backend.env = old_env
55
75
  end
56
76
 
77
+ # @api private
57
78
  def _user_groups
58
79
  config_scope[:_user_groups] ||= []
59
80
  end
60
81
 
82
+ # Set the user and group for the current backend.
83
+ # @param user [String] The new username
84
+ # @param group [String, nil] The new group
61
85
  def add_user_group(user, group)
62
86
  _user_groups << { user: active_backend.user, group: active_backend.group }
63
87
  active_backend.user = user
64
88
  active_backend.group = group
65
89
  end
66
90
 
91
+ # Resets user and group to the previous one.
67
92
  def pop_user_group
68
93
  old_user_group = _user_groups.pop || {}
69
94
  active_backend.user = old_user_group[:user]
70
95
  active_backend.group = old_user_group[:group]
71
96
  end
72
97
 
98
+ # Returns the active backend in the current thread
73
99
  def active_backend
74
100
  SSHKit::Custom::Runner::Abstract.active_backend
75
101
  end
@@ -2,6 +2,21 @@ module SSHKit
2
2
  module Custom
3
3
  module DSL
4
4
  module ConfigStatements
5
+ # Starts the action to be done for named hosts
6
+ #
7
+ # @param hosts [Array<String>] the DNS of the hosts to execute the following blocks
8
+ # @option options [Symbol] :in Chooses the runner to be used
9
+ #
10
+ # * :parallel => Parallel
11
+ # * :sequence => Sequential
12
+ # * :groups => Group
13
+ #
14
+ # @option options [Integer] :wait Amount of seconds to sleep between executions for Sequential and Parallel Runner
15
+ # @option options [Integer] :limit Amount of hosts to use in one Batch for Group Runner
16
+ # For a block {|host| ... }
17
+ # @yield Host for further DSL execution
18
+ # @dsl
19
+ # @see SSHKit::Custom::Config::Store#create_runner
5
20
  def on(hosts, options = {}, &block)
6
21
  hosts = Array(hosts).map { |rh| Host(rh) }.uniq
7
22
 
@@ -10,6 +25,13 @@ module SSHKit
10
25
  _runner.apply_block_to_bcks(&block) if block_given?
11
26
  end
12
27
 
28
+ # Executes all following statements within the named directory.
29
+ # Multiple call's will stack the directories together. After the block
30
+ # is executed the working directory is set back.
31
+ #
32
+ # @param directory [String] The directory within the statements are executed
33
+ # @yield Host for further DSL execution
34
+ # @dsl
13
35
  def within(directory)
14
36
  _guard_dir!(File.join(_config_store.active_backend.pwd + [directory]))
15
37
 
@@ -20,6 +42,13 @@ module SSHKit
20
42
  _config_store.pop_pwd
21
43
  end
22
44
 
45
+ # Executes all following statements with provided environment variables.
46
+ # Multiple call's will the environment variables. After the block
47
+ # is executed the working environment variables are set back.
48
+ #
49
+ # @param environment [Hash<String, String>] Environment variables to be set
50
+ # @yield Host for further DSL execution
51
+ # @dsl
23
52
  def with(environment)
24
53
  _config_store.add_env environment
25
54
  yield if block_given?
@@ -27,6 +56,13 @@ module SSHKit
27
56
  _config_store.pop_env
28
57
  end
29
58
 
59
+ # Executes all following statements as the provided user and group (sudo).
60
+ # After the block is executed the user and group is set back.
61
+ #
62
+ # @param who [String, Hash<String, String>] User and group to be set.
63
+ # Possible Hash keys are :user and :group
64
+ # @yield Host for further DSL execution
65
+ # @dsl
30
66
  def as(who)
31
67
  if who.respond_to? :fetch
32
68
  user = who.fetch(:user) { who.fetch('user') }
@@ -46,11 +82,13 @@ module SSHKit
46
82
  _config_store.pop_user_group
47
83
  end
48
84
 
85
+ # @api private
49
86
  def _setup_runner(hosts, options)
50
87
  _config_store.backends = hosts
51
88
  _config_store.create_runner options
52
89
  end
53
90
 
91
+ # @api private
54
92
  def _runner
55
93
  _config_store.runner
56
94
  end
@@ -4,12 +4,27 @@ module SSHKit
4
4
  module ExecStatements
5
5
  EXEC_STATEMENTS = [:execute, :make, :rake, :test, :capture, :upload!, :download!].freeze
6
6
 
7
- EXEC_STATEMENTS.each do |method|
7
+ # @api private
8
+ # @!macro [attach] dsl.create_delegator
9
+ # @!method $1(*args, &block)
10
+ # @api public
11
+ # @ dsl
12
+ # Delegates $1 to the runner
13
+ def self.create_delegator(method)
8
14
  define_method method do |*args, &block|
9
15
  _config_store.runner.send_cmd method, *args, &block
10
16
  end
11
17
  end
12
18
 
19
+ create_delegator :execute
20
+ create_delegator :make
21
+ create_delegator :rake
22
+ create_delegator :test
23
+ create_delegator :capture
24
+ create_delegator :upload!
25
+ create_delegator :download!
26
+
27
+ # @api private
13
28
  def _guard_sudo_user!(user)
14
29
  execute <<-EOTEST, verbosity: Logger::DEBUG
15
30
  if ! sudo -u #{user} whoami > /dev/null
@@ -19,6 +34,7 @@ module SSHKit
19
34
  EOTEST
20
35
  end
21
36
 
37
+ # @api private
22
38
  def _guard_sudo_group!(user, group)
23
39
  execute <<-EOTEST, verbosity: Logger::DEBUG if group
24
40
  if ! sudo -u #{user} -g #{group} whoami > /dev/null
@@ -28,6 +44,7 @@ module SSHKit
28
44
  EOTEST
29
45
  end
30
46
 
47
+ # @api private
31
48
  def _guard_dir!(dir_to_check)
32
49
  execute <<-EOTEST, verbosity: Logger::DEBUG
33
50
  if test ! -d #{dir_to_check}
@@ -4,20 +4,41 @@ module SSHKit
4
4
  module Helper
5
5
  LOGGING_METHODS = [:log, :debug, :fatal, :error, :warn, :info, :debug].freeze
6
6
 
7
+ # @api private
8
+ # @!macro [attach] dsl_helper.create_backend_delegator
9
+ # @!method $1(*args, &block)
10
+ # @api public
11
+ # @dsl
12
+ # Delegates $1 to the active backend
13
+ def self.create_backend_delegator(method)
14
+ define_method method do |*args|
15
+ active_backend.send method, *args
16
+ end
17
+ end
18
+
19
+ # Returns the active backend in the current thread
20
+ # @dsl
21
+ # @see SSHKit::Custom::Config::Store#active_backend
7
22
  def active_backend
8
23
  SSHKit::Custom::Config::Store.active_backend
9
24
  end
10
25
 
26
+ # Return the host of the active backend
11
27
  def host
12
28
  active_backend.host
13
29
  end
14
30
 
15
- LOGGING_METHODS.each do |method|
16
- define_method method do |*args|
17
- active_backend.send method, *args
18
- end
19
- end
31
+ create_backend_delegator :log
32
+ create_backend_delegator :debug
33
+ create_backend_delegator :fatal
34
+ create_backend_delegator :error
35
+ create_backend_delegator :warn
36
+ create_backend_delegator :info
37
+ create_backend_delegator :debug
20
38
 
39
+ # Conversion function. Converts a host name into a Host object.
40
+ # @param rh [String, SSHKit::Host] The hostname or a SSHKit::Host object
41
+ # @dsl
21
42
  def Host(rh)
22
43
  if rh.is_a?(SSHKit::Host)
23
44
  rh
@@ -26,6 +47,7 @@ module SSHKit
26
47
  end
27
48
  end
28
49
 
50
+ # @api private
29
51
  def _config_store
30
52
  @_config_store ||= SSHKit::Custom::Config::Store
31
53
  end
@@ -1,7 +1,7 @@
1
1
  module SSHKit
2
2
  module Custom
3
3
  module DSL
4
- VERSION = '0.0.7'
4
+ VERSION = '0.0.8'
5
5
  end
6
6
  end
7
7
  end
@@ -12,6 +12,8 @@ require 'sshkit/custom/dsl/config_statements'
12
12
  require 'sshkit/custom/dsl/exec_statements'
13
13
  require 'sshkit/custom/dsl/helper'
14
14
 
15
+ # @api public
16
+ # @public
15
17
  module SSHKit
16
18
  module Custom
17
19
  module DSL
@@ -1,13 +1,19 @@
1
1
  module SSHKit
2
2
  module Custom
3
+ # @api public
4
+ # @public
3
5
  module Runner
4
6
  ExecuteError = SSHKit::Runner::ExecuteError
5
7
 
8
+ # Base class for all runners
9
+ # @abstract Subclass and override {#apply_block_to_bcks} to implement
10
+ # @public
6
11
  class Abstract
7
12
  attr_accessor :backends
8
13
  attr_reader :options
9
14
  attr_writer :wait_interval
10
15
 
16
+ # Factory method to create a new runner.
11
17
  def self.create_runner(opts)
12
18
  opts_with_defaults = { in: :parallel }.merge(opts)
13
19
 
@@ -23,18 +29,22 @@ module SSHKit
23
29
  end.new(opts_with_defaults)
24
30
  end
25
31
 
32
+ # @api private
26
33
  def self.scope_storage
27
34
  ScopedStorage::ThreadLocalStorage
28
35
  end
29
36
 
37
+ # @api private
30
38
  def self.scope
31
39
  @scope ||= ScopedStorage::Scope.new('sshkit_runner', scope_storage)
32
40
  end
33
41
 
42
+ # @api private
34
43
  def self.active_backend
35
44
  scope[:active_backend] || fail(ArgumentError, 'Backend not set')
36
45
  end
37
46
 
47
+ # @api private
38
48
  def self.active_backend=(new_backend)
39
49
  scope[:active_backend] = new_backend
40
50
  end
@@ -43,14 +53,20 @@ module SSHKit
43
53
  @options = options || {}
44
54
  end
45
55
 
56
+ # @api private
46
57
  def active_backend
47
58
  self.class.active_backend
48
59
  end
49
60
 
61
+ # @api private
50
62
  def active_backend=(new_backend)
51
63
  self.class.active_backend = new_backend
52
64
  end
53
65
 
66
+ # Sends the given command to the backend.
67
+ # @param cmd [Symbol] A command that the sshkit backend supports
68
+ # @param args [Array] Arguments for the backend command
69
+ #
54
70
  def send_cmd(cmd, *args, &block)
55
71
  args = Array(block.call(active_backend.host)) if block
56
72
  active_backend.send(cmd, *args)
@@ -59,10 +75,12 @@ module SSHKit
59
75
  raise e2, "Exception while executing on host #{active_backend.host}: #{e.message}"
60
76
  end
61
77
 
78
+ # @abstract
62
79
  def apply_block_to_bcks(&_block)
63
80
  fail SSHKit::Backend::MethodUnavailableError
64
81
  end
65
82
 
83
+ # @api private
66
84
  def apply_to_bck(backend, &block)
67
85
  self.active_backend = backend
68
86
  block.call(backend.host)
@@ -73,6 +91,7 @@ module SSHKit
73
91
  self.active_backend = nil
74
92
  end
75
93
 
94
+ # @api private
76
95
  def do_wait
77
96
  sleep wait_interval
78
97
  end
@@ -1,9 +1,13 @@
1
1
  module SSHKit
2
2
  module Custom
3
3
  module Runner
4
+ # A runner which executes all commands in groups.
5
+ #
4
6
  class Group < Abstract
5
7
  attr_writer :group_size
6
8
 
9
+ # Executes all commands in batches of size :group_size
10
+ # @yields the actual host
7
11
  def apply_block_to_bcks(&block)
8
12
  backends.each_slice(group_size).map do |group_backends|
9
13
 
@@ -14,10 +18,12 @@ module SSHKit
14
18
  end.flatten
15
19
  end
16
20
 
21
+ # @api private
17
22
  def group_size
18
23
  @group_size ||= options[:limit] || 2
19
24
  end
20
25
 
26
+ # @api private
21
27
  def exec_parallel(group, &block)
22
28
  use_runner.call(options).tap do |runner|
23
29
  runner.backends = group
@@ -25,6 +31,7 @@ module SSHKit
25
31
  end
26
32
  end
27
33
 
34
+ # @api private
28
35
  def use_runner
29
36
  ->(options) { Parallel.new(options) }
30
37
  end
@@ -3,20 +3,26 @@ module SSHKit
3
3
  module Runner
4
4
  require 'rake'
5
5
 
6
+ # A runner which executes all commands in prallel (different threads).
6
7
  class Parallel < Abstract
8
+ # @api private
7
9
  def thread_pool
8
10
  @thread_pool ||= Rake::ThreadPool.new(thread_count)
9
11
  end
10
12
 
13
+ # Executes all commands parallel
14
+ # @yields the actual host
11
15
  def apply_block_to_bcks(&block)
12
16
  futures = to_futures(&block)
13
17
  futures.each { |f| f.value }
14
18
  end
15
19
 
20
+ # @api private
16
21
  def thread_count
17
22
  @thread_count ||= options[:thread_count] || Rake.suggested_thread_count - 1
18
23
  end
19
24
 
25
+ # @api private
20
26
  def to_futures(&block)
21
27
  backends.map do |b|
22
28
  thread_pool.future(b) do |fb|
@@ -1,7 +1,10 @@
1
1
  module SSHKit
2
2
  module Custom
3
3
  module Runner
4
+ # A runner which executes all commands in sequence.
4
5
  class Sequential < Abstract
6
+ # Executes all commands in sequence
7
+ # @yields the actual host
5
8
  def apply_block_to_bcks(&block)
6
9
  backends.each do |backend|
7
10
  apply_to_bck backend, &block
data/spec/spec_helper.rb CHANGED
@@ -34,7 +34,6 @@ Dir[File.join(File.expand_path(__dir__), 'support/**/*.rb')].each { |f| require
34
34
  #
35
35
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
36
36
  RSpec.configure do |config|
37
- config.treat_symbols_as_metadata_keys_with_true_values = true
38
37
  config.run_all_when_everything_filtered = true
39
38
  config.filter_run :focus
40
39
 
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ['d.spaeth@faber.de']
11
11
  spec.summary = %q(Exchanges original sshkit dsl against a custom dsl)
12
12
  spec.description = %q(Exchanges original sshkit dsl against a custom dsl. This DSL does not change the scope of the blocks.)
13
- spec.homepage = ''
13
+ spec.homepage = 'https://github.com/faber-lotto/sshkit-custom-dsl'
14
14
  spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency 'bundler'
26
26
  spec.add_development_dependency 'rspec'
27
27
 
28
- spec.add_development_dependency 'rspec', '2.99.0.rc1'
28
+ spec.add_development_dependency 'rspec', '3.0.0'
29
29
  # show nicely how many specs have to be run
30
30
  spec.add_development_dependency 'fuubar'
31
31
  # extended console
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sshkit-custom-dsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dieter Späth
@@ -86,14 +86,14 @@ dependencies:
86
86
  requirements:
87
87
  - - '='
88
88
  - !ruby/object:Gem::Version
89
- version: 2.99.0.rc1
89
+ version: 3.0.0
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - '='
95
95
  - !ruby/object:Gem::Version
96
- version: 2.99.0.rc1
96
+ version: 3.0.0
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: fuubar
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -151,6 +151,7 @@ files:
151
151
  - .rubocop_todo.yml
152
152
  - .travis.yml
153
153
  - .versions.conf
154
+ - .yardopts
154
155
  - Gemfile
155
156
  - LICENSE.txt
156
157
  - README.md
@@ -184,7 +185,7 @@ files:
184
185
  - spec/unit/sshkit/custom/runner/sequential_spec.rb
185
186
  - spec/unit_spec_helper.rb
186
187
  - sshkit-custom-dsl.gemspec
187
- homepage: ''
188
+ homepage: https://github.com/faber-lotto/sshkit-custom-dsl
188
189
  licenses:
189
190
  - MIT
190
191
  metadata: {}
@@ -225,3 +226,4 @@ test_files:
225
226
  - spec/unit/sshkit/custom/runner/parallel_spec.rb
226
227
  - spec/unit/sshkit/custom/runner/sequential_spec.rb
227
228
  - spec/unit_spec_helper.rb
229
+ has_rdoc: