Class: Kdeploy::Command
- Inherits:
-
Object
- Object
- Kdeploy::Command
- Defined in:
- lib/kdeploy/command.rb
Overview
Command class for executing commands on remote hosts
Direct Known Subclasses
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
-
#execute(host, connection) ⇒ Boolean
Execute command on specified host.
-
#initialize(name, command, options = {}) ⇒ Command
constructor
A new instance of Command.
-
#should_run_on?(host) ⇒ Boolean
Check if command should run on host.
Constructor Details
#initialize(name, command, options = {}) ⇒ Command
Returns a new instance of Command.
8 9 10 11 12 13 14 |
# File 'lib/kdeploy/command.rb', line 8 def initialize(name, command, = {}) @name = name @command = command @options = .merge() @global_variables = .delete(:global_variables) || {} @result = nil end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
6 7 8 |
# File 'lib/kdeploy/command.rb', line 6 def command @command end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/kdeploy/command.rb', line 6 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/kdeploy/command.rb', line 6 def @options end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
6 7 8 |
# File 'lib/kdeploy/command.rb', line 6 def result @result end |
Instance Method Details
#execute(host, connection) ⇒ Boolean
Execute command on specified host
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/kdeploy/command.rb', line 20 def execute(host, connection) start_time = Time.now processed_command = process_command_template(host) log_command_start(host, processed_command) @result = execute_with_retry(connection, processed_command) duration = Time.now - start_time log_result(host, duration) record_statistics(host.hostname, duration, @result[:success]) @result[:success] rescue StandardError => e handle_execution_error(host, e, start_time) end |
#should_run_on?(host) ⇒ Boolean
Check if command should run on host
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/kdeploy/command.rb', line 39 def should_run_on?(host) return true unless @options[:only] || @options[:except] if @options[:only] roles = Array(@options[:only]) return roles.any? { |role| host.has_role?(role) } end if @options[:except] roles = Array(@options[:except]) return roles.none? { |role| host.has_role?(role) } end true end |