Class: Bebox::ProvisionWizard

Inherits:
Object
  • Object
show all
Includes:
Logger, WizardsHelper
Defined in:
lib/bebox/wizards/provision_wizard.rb

Instance Method Summary collapse

Methods included from WizardsHelper

#choose_option, #confirm_action?, #valid_puppet_class_name?, #write_input

Methods included from Logger

#error, #highline_quest, #highline_warn, included, #info, #linebreak, #msg, #ok, #quest, #title, #warn

Instance Method Details

#apply_step(project_root, environment, step) ⇒ Object

Apply a step for the nodes in a environment



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bebox/wizards/provision_wizard.rb', line 7

def apply_step(project_root, environment, step)
  # Check if environment has configured the ssh keys
  (return warn "Please add a ssh key pair (id_rsa, id_rsa.pub) in config/keys/environments/#{environment} to do this step.") unless Bebox::Environment.check_environment_access(project_root, environment)
  nodes_to_step = Bebox::Node.nodes_in_environment(project_root, environment, previous_checkpoint(step))
  # Check if there are nodes for provisioning step-N
  (return warn "There are no nodes for provision in #{step}. No changes were made.") unless nodes_to_step.count > 0
  nodes_for_provisioning(nodes_to_step, step)
  # Apply the nodes provisioning for step-N
  in_step_nodes = Bebox::Node.list(project_root, environment, "steps/#{step}")
  outputs = []
  nodes_to_step.each do |node|
    next unless check_node_to_step(node, in_step_nodes, step)
    outputs << provision_step_in_node(project_root, environment, step, in_step_nodes, node)
  end
  return outputs
end

#check_node_to_step(node, in_step_nodes, step) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/bebox/wizards/provision_wizard.rb', line 38

def check_node_to_step(node, in_step_nodes, step)
  return true unless in_step_nodes.include?(node.hostname)
  message = "The node '#{node.hostname}' was already provisioned in #{step}"
  message += " (start: #{node.checkpoint_parameter_from_file('steps/' + step, 'started_at')} - end: #{node.checkpoint_parameter_from_file('steps/' + step, 'finished_at')})."
  message += "\nDo you want to re-provision it?"
  confirm_action?(message)
end

#nodes_for_provisioning(nodes, step) ⇒ Object



46
47
48
49
50
# File 'lib/bebox/wizards/provision_wizard.rb', line 46

def nodes_for_provisioning(nodes, step)
  title "Nodes for provisioning #{step}:"
  nodes.each{|node| msg(node.hostname)}
  linebreak
end

#previous_checkpoint(step) ⇒ Object

Obtain the previous checkpoint (step/phase) for a node



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bebox/wizards/provision_wizard.rb', line 53

def previous_checkpoint(step)
  case step
    when 'prepared_nodes'
      'nodes'
    when 'step-0'
      'prepared_nodes'
    when 'step-1'
      'steps/step-0'
    when 'step-2'
      'steps/step-1'
    when 'step-3'
      'steps/step-2'
  end
end

#provision_step_in_node(project_root, environment, step, in_step_nodes, node) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bebox/wizards/provision_wizard.rb', line 24

def provision_step_in_node(project_root, environment, step, in_step_nodes, node)
  title "Provisioning #{step} in node #{node.hostname}:"
  role = Bebox::Provision.role_from_node(project_root, step, node.hostname)
  profiles = Bebox::Provision.profiles_from_role(project_root, role) unless role.nil?
  # Before apply generate the Puppetfile with modules from all associated profiles
  Bebox::Provision.generate_puppetfile(project_root, step, profiles) unless profiles.nil?
  # Before apply generate the roles and profiles modules structure for puppet step
  Bebox::Provision.generate_roles_and_profiles(project_root, step, role, profiles)
  provision = Bebox::Provision.new(project_root, environment, node, step)
  output = provision.apply.success?
  output ? (ok "Node '#{node.hostname}' provisioned to #{step}.") : (error "An error ocurred in the provision of #{step} for node '#{node.hostname}'")
  return output
end