Class: Kdeploy::Pipeline

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

Overview

Pipeline class for managing deployment tasks and hosts

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = 'default') ⇒ Pipeline

Returns a new instance of Pipeline.



8
9
10
11
12
13
# File 'lib/kdeploy/pipeline.rb', line 8

def initialize(name = 'default')
  @name = name
  @hosts = []
  @tasks = []
  @variables = {}
end

Instance Attribute Details

#hostsObject (readonly)

Returns the value of attribute hosts.



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

def hosts
  @hosts
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#tasksObject (readonly)

Returns the value of attribute tasks.



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

def tasks
  @tasks
end

#variablesObject (readonly)

Returns the value of attribute variables.



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

def variables
  @variables
end

Instance Method Details

#add_host(hostname, user: nil, port: nil, ssh_options: {}, roles: [], vars: {}) ⇒ Host

Add host to pipeline

Parameters:

  • hostname (String)

    Hostname or IP address

  • user (String) (defaults to: nil)

    SSH user

  • port (Integer) (defaults to: nil)

    SSH port

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

    SSH options

  • roles (Array) (defaults to: [])

    Host roles

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

    Host variables

Returns:

  • (Host)

    Created host



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kdeploy/pipeline.rb', line 23

def add_host(hostname, user: nil, port: nil, ssh_options: {}, roles: [], vars: {})
  host = Host.new(
    hostname,
    user: user,
    port: port,
    ssh_options: ssh_options,
    roles: roles,
    vars: vars
  )
  @hosts << host unless @hosts.include?(host)
  host
end

#add_hosts(hosts_config) ⇒ Object

Add multiple hosts from hash

Parameters:

  • hosts_config (Hash)

    Hosts configuration



38
39
40
41
42
43
# File 'lib/kdeploy/pipeline.rb', line 38

def add_hosts(hosts_config)
  hosts_config.each do |hostname, config|
    config ||= {}
    add_host_from_config(hostname, config)
  end
end

#add_task(name, hosts: nil, **options) ⇒ Task

Add task to pipeline

Parameters:

  • name (String)

    Task name

  • hosts (Array<Host>) (defaults to: nil)

    Target hosts (default: all hosts)

  • options (Hash)

    Task options

Returns:

  • (Task)

    Created task



57
58
59
60
61
62
# File 'lib/kdeploy/pipeline.rb', line 57

def add_task(name, hosts: nil, **options)
  target_hosts = hosts || @hosts
  task = create_task(name, target_hosts, options)
  @tasks << task
  task
end

#executeHash

Execute all tasks in pipeline

Returns:

  • (Hash)

    Execution results



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/kdeploy/pipeline.rb', line 80

def execute
  return empty_execution_result if @tasks.empty?

  log_pipeline_start
  start_time = Time.now
  results = execute_tasks
  duration = Time.now - start_time
  success_count = count_successful_tasks(results)

  log_pipeline_completion(duration, success_count)
  build_execution_result(results, duration, success_count)
end

#get_variable(key) ⇒ Object

Get global variable

Parameters:

  • key (String, Symbol)

    Variable key

Returns:

  • (Object)

    Variable value



74
75
76
# File 'lib/kdeploy/pipeline.rb', line 74

def get_variable(key)
  @variables[key.to_s] || @variables[key.to_sym]
end

#hosts_with_role(role) ⇒ Array<Host>

Get hosts by role

Parameters:

  • role (String, Symbol)

    Role to filter by

Returns:

  • (Array<Host>)

    Hosts with specified role



48
49
50
# File 'lib/kdeploy/pipeline.rb', line 48

def hosts_with_role(role)
  @hosts.select { |host| host.has_role?(role) }
end

#set_variable(key, value) ⇒ Object

Set global variable

Parameters:

  • key (String, Symbol)

    Variable key

  • value (Object)

    Variable value



67
68
69
# File 'lib/kdeploy/pipeline.rb', line 67

def set_variable(key, value)
  @variables[key.to_s] = value
end

#summaryHash

Get pipeline summary

Returns:

  • (Hash)

    Pipeline summary



95
96
97
98
99
100
101
102
103
# File 'lib/kdeploy/pipeline.rb', line 95

def summary
  {
    name: @name,
    hosts_count: @hosts.size,
    tasks_count: @tasks.size,
    hosts: @hosts.map(&:hostname),
    tasks: @tasks.map(&:name)
  }
end

#valid?Boolean

Check if pipeline is valid

Returns:

  • (Boolean)

    True if pipeline is valid



117
118
119
# File 'lib/kdeploy/pipeline.rb', line 117

def valid?
  validate.empty?
end

#validateArray<String>

Validate pipeline configuration

Returns:

  • (Array<String>)

    Validation errors



107
108
109
110
111
112
113
# File 'lib/kdeploy/pipeline.rb', line 107

def validate
  errors = []
  errors.concat(validate_pipeline_structure)
  errors.concat(validate_hosts)
  errors.concat(validate_tasks)
  errors
end