Class: Actions::Staypuft::Hostgroup::OrderedDeploy

Inherits:
Base
  • Object
show all
Defined in:
app/lib/actions/staypuft/hostgroup/ordered_deploy.rb

Overview

deploys Hostgroups in given order

Instance Method Summary collapse

Instance Method Details

#configure_hosts_concurrent(hostgroup, hostgroup_hosts) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/lib/actions/staypuft/hostgroup/ordered_deploy.rb', line 61

def configure_hosts_concurrent(hostgroup, hostgroup_hosts)
  return if hostgroup_hosts.empty?

  # wait till all hosts are ready
  concurrence do
    hostgroup_hosts.each do |host|
      input[:hostgroups][hostgroup.id][:hosts].update host.id => host.name

      plan_action Host::WaitUntilReady, host
      plan_action Host::Update, host, :environment => nil
    end
  end

  # run puppet twice without checking for puppet success
  2.times do
    concurrence do
      hostgroup_hosts.each do |host|
        plan_action Host::Deploy, host, false
      end
    end
  end

  # run puppet once and check it succeeded
  concurrence do
    hostgroup_hosts.each do |host|
      plan_action Host::Deploy, host
    end
  end
end

#configure_hosts_leader(hostgroup, hostgroup_hosts) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'app/lib/actions/staypuft/hostgroup/ordered_deploy.rb', line 91

def configure_hosts_leader(hostgroup, hostgroup_hosts)
  return if hostgroup_hosts.empty?

  leader_host, *rest_of_hosts = hostgroup_hosts

  sequence do
    configure_hosts_concurrent(hostgroup, [leader_host])
    configure_hosts_concurrent(hostgroup, rest_of_hosts)
  end
end

#configure_hosts_serial(hostgroup, hostgroup_hosts) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'app/lib/actions/staypuft/hostgroup/ordered_deploy.rb', line 102

def configure_hosts_serial(hostgroup, hostgroup_hosts)
  return if hostgroup_hosts.empty?

  sequence do
    hostgroup_hosts.each do |host|
      configure_hosts_concurrent(hostgroup, [host])
    end
  end
end

#enable_puppet_agent(hosts) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/lib/actions/staypuft/hostgroup/ordered_deploy.rb', line 112

def enable_puppet_agent(hosts)
  lookup_key_runmode_id = Puppetclass.
      find_by_name('foreman::puppet::agent::service').
      class_params.
      where(key: 'runmode').
      first.
      tap { |v| v || raise('missing runmode LookupKey') }.
      id

  sequence do
    hosts.each do |host|
      # enable puppet agent
      plan_action(Actions::Staypuft::Host::Update, host,
                  lookup_values_attributes:
                      { nil => { lookup_key_id: lookup_key_runmode_id,
                                 value:         'service' } })

    end
  end

  puppet_runs = sequence do
    hosts.map do |host|
      plan_action Actions::Staypuft::Host::PuppetRun, host
    end
  end

  concurrence do
    hosts.zip(puppet_runs).each do |host, puppet_run|
      sequence do
        plan_action Actions::Staypuft::Host::ReportWait, host.id, puppet_run.output[:executed_at]
        plan_action Actions::Staypuft::Host::AssertReportSuccess, host.id
      end
    end
  end
end

#humanized_inputObject



148
149
150
# File 'app/lib/actions/staypuft/hostgroup/ordered_deploy.rb', line 148

def humanized_input
  planned_actions.map(&:humanized_input).join(', ')
end

#humanized_outputObject



152
153
154
# File 'app/lib/actions/staypuft/hostgroup/ordered_deploy.rb', line 152

def humanized_output
  task_output.map { |hg| "#{hg[:name]} #{(hg[:progress]*100).to_i}%" }.join(', ')
end

#plan(hostgroups, hosts_to_deploy_filter, hosts_to_provision_filter) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/lib/actions/staypuft/hostgroup/ordered_deploy.rb', line 21

def plan(hostgroups, hosts_to_deploy_filter, hosts_to_provision_filter)
  hosts_to_deploy_filter ||= hostgroups.
      map(&:hosts).
      reduce(&:+).
      select { |h| !h.open_stack_deployed? }

  hosts_to_provision_filter ||= hosts_to_deploy_filter.select(&:managed?)

  (Type! hostgroups, Array).all? { |v| Type! v, ::Hostgroup }
  (Type! hosts_to_deploy_filter, Array).all? { |v| Type! v, ::Host::Base }

  sequence do
    hosts              = hostgroups.map(&:hosts).reduce(&:+)
    hosts_to_provision = hosts & hosts_to_provision_filter
    hosts_to_deploy    = hosts & hosts_to_deploy_filter
    hosts_to_provision.each { |host| plan_action Host::TriggerProvisioning, host }

    concurrence do
      hosts_to_provision.each { |host| plan_action Host::WaitUntilProvisioned, host }
    end

    input.update hostgroups: {}
    hostgroups.each do |hostgroup|
      input[:hostgroups].update hostgroup.id => { name: hostgroup.name, hosts: {} }
      hostgroup_hosts = (hostgroup.hosts & hosts_to_deploy_filter)

      case hostgroup.role.orchestration
      when 'leader'
        configure_hosts_leader(hostgroup, hostgroup_hosts)
      when 'serial'
        configure_hosts_serial(hostgroup, hostgroup_hosts)
      else
        configure_hosts_concurrent(hostgroup, hostgroup_hosts)
      end
    end

    enable_puppet_agent hosts_to_deploy
  end
end

#task_outputObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/lib/actions/staypuft/hostgroup/ordered_deploy.rb', line 156

def task_output
  steps          = all_planned_actions.map { |a| a.steps[1..2] }.reduce(&:+).compact
  stets_by_hosts = steps.inject({}) do |hash, step|
    key       = step.action(execution_plan).input[:host_id]
    hash[key] ||= []
    hash[key] << step
    hash
  end

  progresses_by_host = stets_by_hosts.inject({}) do |hash, (host_id, steps)|
    progress = if steps.empty?
                 'done'
               else
                 total          = steps.map { |s| s.progress_done * s.progress_weight }.reduce(&:+)
                 weighted_count = steps.map(&:progress_weight).reduce(&:+)
                 total / weighted_count
               end

    hash.update host_id => progress
  end

  (input[:hostgroups] || []).map do |hostgroup_id, hostgroup|
    next if hostgroup[:hosts].size == 0
    progress = hostgroup[:hosts].map { |id, _| progresses_by_host[id.to_i] }.sum.to_f / hostgroup[:hosts].size

    { id:       hostgroup_id,
      name:     hostgroup[:name],
      progress: progress,
      hosts:    hostgroup[:hosts].map do |id, name|
        { id: id, name: name, progress: progresses_by_host[id.to_i] }
      end }
  end.compact
end