Class: Kdeploy::Task

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

Overview

Task class for managing command execution on hosts

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, hosts = [], options = {}) ⇒ Task

Returns a new instance of Task.



9
10
11
12
13
14
15
# File 'lib/kdeploy/task.rb', line 9

def initialize(name, hosts = [], options = {})
  @name = name
  @hosts = Array(hosts)
  @commands = []
  @options = default_options.merge(options)
  @global_variables = {}
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



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

def commands
  @commands
end

#global_variablesObject

Returns the value of attribute global_variables.



7
8
9
# File 'lib/kdeploy/task.rb', line 7

def global_variables
  @global_variables
end

#hostsObject (readonly)

Returns the value of attribute hosts.



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

def hosts
  @hosts
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#add_command(name, command, options = {}) ⇒ Command

Add command to task

Parameters:

  • name (String)

    Command name

  • command (String)

    Command to execute

  • options (Hash) (defaults to: {})

    Command options

Returns:



22
23
24
25
26
27
# File 'lib/kdeploy/task.rb', line 22

def add_command(name, command, options = {})
  command_options = options.merge(global_variables: @global_variables)
  cmd = Command.new(name, command, command_options)
  @commands << cmd
  cmd
end

#add_host(host) ⇒ Host

Add host to task

Parameters:

  • host (Host)

    Host to add

Returns:

  • (Host)

    Added host



32
33
34
35
# File 'lib/kdeploy/task.rb', line 32

def add_host(host)
  @hosts << host unless @hosts.include?(host)
  host
end

#executeHash

Execute task on all hosts

Returns:

  • (Hash)

    Execution results



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/kdeploy/task.rb', line 46

def execute
  return empty_execution_result if @commands.empty? || @hosts.empty?

  log_task_start
  start_time = Time.now
  results = execute_commands
  duration = Time.now - start_time
  success_count = count_successful_hosts(results)

  log_task_completion(duration, success_count)
  build_task_result(results, duration, success_count)
end

#remove_host(host) ⇒ Host?

Remove host from task

Parameters:

  • host (Host)

    Host to remove

Returns:

  • (Host, nil)

    Removed host or nil if not found



40
41
42
# File 'lib/kdeploy/task.rb', line 40

def remove_host(host)
  @hosts.delete(host)
end