Class: Kdeploy::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/kdeploy/command.rb

Overview

Command class for executing commands on remote hosts

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @name = name
  @command = command
  @options = default_options.merge(options)
  @global_variables = options.delete(:global_variables) || {}
  @result = nil
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



6
7
8
# File 'lib/kdeploy/command.rb', line 6

def command
  @command
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/kdeploy/command.rb', line 6

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/kdeploy/command.rb', line 6

def options
  @options
end

#resultObject (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

Parameters:

Returns:

  • (Boolean)

    True if command succeeded



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

Parameters:

  • host (Host)

    Target host

Returns:

  • (Boolean)

    True if command should run



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