Class: Bebox::NodeWizard

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

Instance Method Summary collapse

Methods included from VagrantHelper

#add_to_local_hosts, #add_vagrant_node, #backup_local_hosts, #configure_local_hosts, generate_vagrantfile, halt_vagrant_nodes, #installed_vagrant_box_names, #local_hosts_path, #prepare_vagrant, #remove_vagrant_box, up_vagrant_nodes, #vagrant_box_exist?, #vagrant_box_running?

Methods included from FilesHelper

#file_content_trimmed, #generate_file_from_template, included, #render_erb_template, templates_path, #write_content_to_file

Methods included from Logger

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

Methods included from WizardsHelper

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

Instance Method Details

#ask_hostname(project_root, environment) ⇒ Object

Ask for the hostname



115
116
117
# File 'lib/bebox/wizards/node_wizard.rb', line 115

def ask_hostname(project_root, environment)
  write_input(_('wizard.node.ask_hostname'), nil, /\.(.*)/, _('wizard.node.valid_hostname'))
end

#ask_ip(environment) ⇒ Object

Ask for the ip until is valid



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/bebox/wizards/node_wizard.rb', line 120

def ask_ip(environment)
  ip = write_input(_('wizard.node.ask_ip'), nil, /\.(.*)/, _('wizard.node.valid_ip'))
  # If the environment is not vagrant don't check ip free
  return ip if environment != 'vagrant'
  # Check if the ip address is free
  if free_ip?(ip)
    return ip
  else
    error _('wizard.node.non_free_ip')
    ask_ip(environment)
  end
end

#ask_not_existing_hostname(project_root, environment) ⇒ Object

Keep asking for a hostname that not exist



103
104
105
106
107
108
109
110
111
112
# File 'lib/bebox/wizards/node_wizard.rb', line 103

def ask_not_existing_hostname(project_root, environment)
  hostname = ask_hostname(project_root, environment)
  # Check if the node not exist
  if node_exists?(project_root, environment, hostname)
    error _('wizard.node.hostname_exist')
    ask_hostname(project_root, environment)
  else
    return hostname
  end
end

#check_nodes_to_prepare(project_root, environment) ⇒ Object

Check the nodes already prepared and ask confirmation to re-do-it



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/bebox/wizards/node_wizard.rb', line 82

def check_nodes_to_prepare(project_root, environment)
  nodes_to_prepare = []
  nodes = Bebox::Node.nodes_in_environment(project_root, environment, 'nodes')
  prepared_nodes = Bebox::Node.list(project_root, environment, 'prepared_nodes')
  nodes.each do |node|
    if prepared_nodes.include?(node.hostname)
      message = _('wizard.node.confirm_preparation')%{hostname: node.hostname, start: node.checkpoint_parameter_from_file('prepared_nodes', 'started_at'), end: node.checkpoint_parameter_from_file('prepared_nodes', 'finished_at')}
      nodes_to_prepare << node if confirm_action?(message)
    else
      nodes_to_prepare << node
    end
  end
  nodes_to_prepare
end

#create_new_node(project_root, environment) ⇒ Object

Create a new node



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/bebox/wizards/node_wizard.rb', line 9

def create_new_node(project_root, environment)
  # Ask the hostname for node
  hostname = ask_not_existing_hostname(project_root, environment)
  # Ask IP for node
  ip = ask_ip(environment)
  # Node creation
  node = Bebox::Node.new(environment, project_root, hostname, ip)
  output = node.create
  ok _('wizard.node.creation_success')
  return output
end

#free_ip?(ip) ⇒ Boolean

Validate if the IP address is free

Returns:

  • (Boolean)


134
135
136
137
# File 'lib/bebox/wizards/node_wizard.rb', line 134

def free_ip?(ip)
  `ping -q -c 1 -W 3000 #{ip}`
  ($?.exitstatus == 0) ? false : true
end

#node_exists?(project_root, environment, node_name) ⇒ Boolean

Check if there's an existing node in a environment

Returns:

  • (Boolean)


98
99
100
# File 'lib/bebox/wizards/node_wizard.rb', line 98

def node_exists?(project_root, environment, node_name)
  File.exists?("#{project_root}/.checkpoints/environments/#{environment}/nodes/#{node_name}.yml")
end

#prepare(project_root, environment) ⇒ Object

Prepare the nodes in a environment



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bebox/wizards/node_wizard.rb', line 52

def prepare(project_root, environment)
  # Check already prepared nodes
  nodes_to_prepare = check_nodes_to_prepare(project_root, environment)
  # Output the nodes to be prepared
  if nodes_to_prepare.count > 0
    title _('wizard.node.prepare_title')
    nodes_to_prepare.each{|node| msg(node.hostname)}
    linebreak
    # For all environments regenerate the deploy file
    Bebox::Node.regenerate_deploy_file(project_root, environment, nodes_to_prepare)
    # If environment is 'vagrant' Prepare and Up the machines
    up_vagrant_machines(project_root, nodes_to_prepare) if environment == 'vagrant'
    # For all the environments do the preparation
    nodes_to_prepare.each do |node|
      node.prepare
      ok _('wizard.node.preparation_success')
    end
  else
    warn _('wizard.node.no_prepare_nodes')
  end
  return true
end

#remove_node(project_root, environment, hostname) ⇒ Object

Removes an existing node



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

def remove_node(project_root, environment, hostname)
  # Ask for a node to remove
  nodes = Bebox::Node.list(project_root, environment, 'nodes')
  if nodes.count > 0
    hostname = choose_option(nodes, _('wizard.node.choose_node'))
  else
    error _('wizard.node.no_nodes')%{environment: environment}
    return true
  end
  # Ask for deletion confirmation
  return warn(_('wizard.no_changes')) unless confirm_action?(_('wizard.node.confirm_deletion'))
  # Node deletion
  node = Bebox::Node.new(environment, project_root, hostname, nil)
  output = node.remove
  ok _('wizard.node.deletion_success')
  return output
end

#set_role(project_root, environment) ⇒ Object

Associate a role with a node in a environment



41
42
43
44
45
46
47
48
49
# File 'lib/bebox/wizards/node_wizard.rb', line 41

def set_role(project_root, environment)
  roles = Bebox::Role.list(project_root)
  nodes = Bebox::Node.list(project_root, environment, 'nodes')
  node = choose_option(nodes, _('wizard.choose_node'))
  role = choose_option(roles, _('wizard.choose_role'))
  output = Bebox::Provision.associate_node_role(project_root, environment, node, role)
  ok _('wizard.node.role_set_success')
  return output
end

#up_vagrant_machines(project_root, nodes_to_prepare) ⇒ Object



75
76
77
78
79
# File 'lib/bebox/wizards/node_wizard.rb', line 75

def up_vagrant_machines(project_root, nodes_to_prepare)
  Bebox::VagrantHelper.generate_vagrantfile(nodes_to_prepare)
  nodes_to_prepare.each{|node| prepare_vagrant(node)}
  Bebox::VagrantHelper.up_vagrant_nodes(project_root)
end