class Front::CLI::VagrantPool

Public Class Methods

new(size) click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 10
def initialize(size)
  @size = size
  @script = Script.new(get_script_path())
end

Public Instance Methods

create() click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 15
def create
  front_path = get_front_path()
  FileUtils.mkdir_p(front_path)

  get_pool_size().times do |index|
    instance_id = index + 1
    instance_path = get_instance_path(instance_id)

    FileUtils.mkdir_p(instance_path)
    FileUtils.cp get_vagrant_file(), instance_path
  end

  create_front_file
end
create_front_file() click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 173
def create_front_file
  defaults = {}
  defaults['current_id'] = 1
  defaults['pool_size'] = @size

  front_file = get_front_file()
  front_file.create(defaults)
end
get_current_id() click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 137
def get_current_id
  get_front_file().get_current_id()
end
get_front_file() click to toggle source

helpers

# File lib/front/cli/vagrant_pool.rb, line 162
def get_front_file
  if @front_file.nil?
    @front_file = Frontfile.new(get_frontfile_path())
    if @front_file.exists?
      @front_file.load()
    end
  end

  @front_file
end
get_front_path() click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 112
def get_front_path
  "#{Dir.pwd}/.front"
end
get_frontfile_path() click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 133
def get_frontfile_path
  "#{get_front_path()}/Frontfile"
end
get_instance_path(id) click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 120
def get_instance_path(id)
  "#{get_front_path()}/#{id}"
end
get_inventory_file() click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 108
def get_inventory_file
  "#{get_front_path()}/inventory.ini"
end
get_next_id() click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 141
def get_next_id
  current_id = get_current_id()
  if current_id + 1 > get_pool_size()
    next_id = 1
  else
    next_id = current_id + 1
  end

  next_id
end
get_pool_size() click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 152
def get_pool_size
  front_file = get_front_file()
  if front_file.exists?
    front_file.get_pool_size()
  else
    @size
  end
end
get_script_path() click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 116
def get_script_path
  "#{get_front_path()}/pending.sh"
end
get_vagrant(instance_id) click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 103
def get_vagrant(instance_id)
  instance_path = get_instance_path(instance_id)
  Vagrant.new(instance_id, instance_path, @script)
end
get_vagrant_file() click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 124
def get_vagrant_file
  custom_path = "#{Dir.pwd}/Vagrantfile"
  if File.exists?(custom_path)
    return custom_path
  else
    return "#{ROOT_DIR}/Vagrantfile"
  end
end
load() click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 30
def load
  loaded = false
  first_vagrant = nil
  get_pool_size().times do |index|
    vagrant = get_vagrant(index + 1)
    vagrant.wait = !loaded
    vagrant.up

    unless loaded
      first_vagrant = vagrant
    end

    loaded = true
  end

  update_inventory(first_vagrant)
  @script.run
end
next() click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 60
def next
  current_id = get_current_id()
  vagrant = get_vagrant(current_id)
  vagrant.wait = false
  vagrant.destroy
  vagrant.up

  next_id = get_next_id()
  vagrant = get_vagrant(next_id)
  puts "Switched to instance \##{next_id}"

  save_front_file(next_id)
  update_inventory(vagrant)
  @script.run()

  next_id
end
save_front_file(current_id) click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 182
def save_front_file(current_id)
  front_file = get_front_file()
  front_file.set_current_id(current_id)
  front_file.save()
end
ssh() click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 78
def ssh
  vagrant = get_vagrant(get_current_id())
  vagrant.ssh
end
ssh_config() click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 83
def ssh_config
  vagrant = get_vagrant(get_current_id())
  vagrant.ssh_config
end
status() click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 88
def status
  current_id = get_current_id()
  get_pool_size().times do |index|
    instance_id = index + 1
    if instance_id == current_id
      instance_label = "\##{instance_id}*"
    else
      instance_label = "\##{instance_id} "
    end

    vagrant = get_vagrant(instance_id)
    puts "Instance #{instance_label}: #{vagrant.status}"
  end
end
unload() click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 49
def unload
  get_pool_size().times do |index|
    instance_id = index + 1
    vagrant = get_vagrant(instance_id)
    puts "Destroying instance \##{instance_id}"
    vagrant.destroy
  end

  FileUtils.rm_rf(get_front_path())
end
update_inventory(vagrant) click to toggle source
# File lib/front/cli/vagrant_pool.rb, line 188
def update_inventory(vagrant)
  ip_address = "127.0.0.#{get_current_id()}"
  port = vagrant.ssh_port()

  item = "#{ip_address}:#{port}"
  File.open(get_inventory_file(), 'w') do |file|
    file.write(item)
  end
end