Module: Bebox::VagrantHelper
Class Method Summary collapse
-
.generate_vagrantfile(nodes) ⇒ Object
Generate the Vagrantfile.
-
.halt_vagrant_nodes(project_root) ⇒ Object
Halt the vagrant boxes running.
-
.up_vagrant_nodes(project_root) ⇒ Object
Up the vagrant boxes in Vagrantfile.
Instance Method Summary collapse
-
#add_to_local_hosts(node) ⇒ Object
Add the vagrant hosts to the local hosts file.
-
#add_vagrant_node(project_name, vagrant_box_base, node) ⇒ Object
Add the boxes to vagrant for each node.
-
#backup_local_hosts(project_name) ⇒ Object
Backup the local hosts file.
-
#configure_local_hosts(project_name, node) ⇒ Object
Backup and add the vagrant hosts to local hosts file.
-
#installed_vagrant_box_names(node) ⇒ Object
return an Array with the names of the currently installed vagrant boxes.
-
#local_hosts_path ⇒ Object
Obtain the local hosts file for the OS.
-
#prepare_vagrant(node) ⇒ Object
Prepare the vagrant nodes.
-
#remove_vagrant_box(node) ⇒ Object
Remove the specified boxes from vagrant.
-
#vagrant_box_exist?(node) ⇒ Boolean
Return the existence status of vagrant node.
-
#vagrant_box_running?(node) ⇒ Boolean
Return the running status of vagrant node.
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
Class Method Details
.generate_vagrantfile(nodes) ⇒ Object
Generate the Vagrantfile
84 85 86 87 88 89 90 |
# File 'lib/bebox/vagrant_helper.rb', line 84 def self.generate_vagrantfile(nodes) project_root = nodes.first.project_root network_interface = RUBY_PLATFORM =~ /darwin/ ? 'en0' : 'eth0' project_name = Bebox::Project.name_from_file(project_root) vagrant_box_provider = Bebox::Project.vagrant_box_provider_from_file(project_root) generate_file_from_template("#{Bebox::FilesHelper::templates_path}/node/Vagrantfile.erb", "#{project_root}/Vagrantfile", {nodes: nodes, project_name: project_name, vagrant_box_provider: vagrant_box_provider, network_interface: network_interface}) end |
.halt_vagrant_nodes(project_root) ⇒ Object
Halt the vagrant boxes running
79 80 81 |
# File 'lib/bebox/vagrant_helper.rb', line 79 def self.halt_vagrant_nodes(project_root) `cd #{project_root} && vagrant halt` end |
.up_vagrant_nodes(project_root) ⇒ Object
Up the vagrant boxes in Vagrantfile
73 74 75 76 |
# File 'lib/bebox/vagrant_helper.rb', line 73 def self.up_vagrant_nodes(project_root) pid = fork {exec("cd #{project_root} && vagrant up --provision")} Process.wait(pid) end |
Instance Method Details
#add_to_local_hosts(node) ⇒ Object
Add the vagrant hosts to the local hosts file
45 46 47 48 |
# File 'lib/bebox/vagrant_helper.rb', line 45 def add_to_local_hosts(node) host_command = "echo '#{node.ip} #{node.hostname} # Added by bebox' | sudo tee -a #{local_hosts_path}/hosts" `#{host_command}` unless (file_content_trimmed("#{local_hosts_path}/hosts") =~ /#{node.ip}\s+#{node.hostname}/) end |
#add_vagrant_node(project_name, vagrant_box_base, node) ⇒ Object
Add the boxes to vagrant for each node
64 65 66 67 68 69 70 |
# File 'lib/bebox/vagrant_helper.rb', line 64 def add_vagrant_node(project_name, vagrant_box_base, node) already_installed_boxes = installed_vagrant_box_names(node) box_name = "#{project_name}-#{node.hostname}" info "Adding server to vagrant: #{node.hostname}" info "Please enter the network interface number if asked, and wait until the machine is up." `cd #{node.project_root} && vagrant box add #{box_name} #{vagrant_box_base}` unless already_installed_boxes.include? box_name end |
#backup_local_hosts(project_name) ⇒ Object
Backup the local hosts file
39 40 41 42 |
# File 'lib/bebox/vagrant_helper.rb', line 39 def backup_local_hosts(project_name) hosts_backup_file = "#{local_hosts_path}/hosts_before_#{project_name}" `sudo cp #{local_hosts_path}/hosts #{hosts_backup_file}` unless File.exist?(hosts_backup_file) end |
#configure_local_hosts(project_name, node) ⇒ Object
Backup and add the vagrant hosts to local hosts file
32 33 34 35 36 |
# File 'lib/bebox/vagrant_helper.rb', line 32 def configure_local_hosts(project_name, node) info "\nPlease provide your local password, if asked, to configure the local hosts file." backup_local_hosts(project_name) add_to_local_hosts(node) end |
#installed_vagrant_box_names(node) ⇒ Object
return an Array with the names of the currently installed vagrant boxes
94 95 96 |
# File 'lib/bebox/vagrant_helper.rb', line 94 def installed_vagrant_box_names(node) (`cd #{node.project_root} && vagrant box list`).split("\n").map{|vagrant_box| vagrant_box.split(' ').first} end |
#local_hosts_path ⇒ Object
Obtain the local hosts file for the OS
51 52 53 |
# File 'lib/bebox/vagrant_helper.rb', line 51 def local_hosts_path RUBY_PLATFORM =~ /darwin/ ? '/private/etc' : '/etc' end |
#prepare_vagrant(node) ⇒ Object
Prepare the vagrant nodes
56 57 58 59 60 61 |
# File 'lib/bebox/vagrant_helper.rb', line 56 def prepare_vagrant(node) project_name = Bebox::Project.name_from_file(node.project_root) vagrant_box_base = Bebox::Project.vagrant_box_base_from_file(node.project_root) configure_local_hosts(project_name, node) add_vagrant_node(project_name, vagrant_box_base, node) end |
#remove_vagrant_box(node) ⇒ Object
Remove the specified boxes from vagrant
23 24 25 26 27 28 29 |
# File 'lib/bebox/vagrant_helper.rb', line 23 def remove_vagrant_box(node) return unless (node.environment == 'vagrant' && node.prepared_nodes_count > 0) project_name = Bebox::Project.name_from_file(node.project_root) vagrant_box_provider = Bebox::Project.vagrant_box_provider_from_file(node.project_root) `cd #{node.project_root} && vagrant destroy -f #{node.hostname}` `cd #{node.project_root} && vagrant box remove #{project_name}-#{node.hostname} --provider #{vagrant_box_provider}` end |
#vagrant_box_exist?(node) ⇒ Boolean
Return the existence status of vagrant node
9 10 11 12 13 14 |
# File 'lib/bebox/vagrant_helper.rb', line 9 def vagrant_box_exist?(node) vagrant_boxes = `cd #{node.project_root} && vagrant box list` project_name = Bebox::Project.name_from_file(node.project_root) vagrant_box_provider = Bebox::Project.vagrant_box_provider_from_file(node.project_root) (vagrant_boxes =~ /#{project_name}-#{node.hostname}\s+\(#{vagrant_box_provider}/) ? true : false end |
#vagrant_box_running?(node) ⇒ Boolean
Return the running status of vagrant node
17 18 19 20 |
# File 'lib/bebox/vagrant_helper.rb', line 17 def vagrant_box_running?(node) status = `cd #{node.project_root} && vagrant status` (status =~ /#{node.hostname}\s+running/) ? true : false end |