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



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

def ask_hostname(project_root, environment)
  write_input('Write the hostname for the node:', nil, /\.(.*)/, 'Enter valid hostname. Ex. host.server1.com')
end

#ask_ip(environment) ⇒ Object

Ask for the ip until is valid



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

def ask_ip(environment)
  ip = write_input('Write the IP address for the node:', nil, /\.(.*)/, 'Enter a valid IP address. Ex. 192.168.0.50')
  # 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 'The IP address is not free. Try a new one.'
    ask_ip(environment)
  end
end

#ask_not_existing_hostname(project_root, environment) ⇒ Object

Keep asking for a hostname that not exist



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

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 'A hostname with that name already exist. Try a new one.'
    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



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

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)
      checkpoint_status = "(start: #{node.checkpoint_parameter_from_file('prepared_nodes', 'started_at')} - end: #{node.checkpoint_parameter_from_file('prepared_nodes', 'finished_at')})"
      message = "The node '#{node.hostname}' was already prepared #{checkpoint_status}.\nDo you want to re-prepare it?"
      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 'Node created!.'
  return output
end

#free_ip?(ip) ⇒ Boolean

Validate if the IP address is free

Returns:

  • (Boolean)


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

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)


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

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
74
75
76
77
# 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 'Preparing nodes:'
    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
    if environment == 'vagrant'
      Bebox::VagrantHelper.generate_vagrantfile(nodes_to_prepare)
      nodes_to_prepare.each{|node| prepare_vagrant(node)}
      Bebox::VagrantHelper.up_vagrant_nodes(project_root)
    end
    # For all the environments do the preparation
    nodes_to_prepare.each do |node|
      node.prepare
      ok 'Node prepared!.'
    end
  else
    warn 'There are no nodes to prepare. No changes were made.'
  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, 'Choose the node to remove:')
  else
    error "There are no nodes in the '#{environment}' environment to remove. No changes were made."
    return true
  end
  # Ask for deletion confirmation
  return warn('No changes were made.') unless confirm_action?('Are you sure that you want to delete the node?')
  # Node deletion
  node = Bebox::Node.new(environment, project_root, hostname, nil)
  output = node.remove
  ok 'Node removed!.'
  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, 'Choose an existing node:')
  role = choose_option(roles, 'Choose an existing role:')
  output = Bebox::Provision.associate_node_role(project_root, environment, node, role)
  ok 'Role associated to node!.'
  return output
end