class Cucumber::Chef::Provider::Vagrant

Constants

INVALID_STATES
MSG_NO_LAB
MSG_NO_RUNNING_LAB
MSG_NO_STOPPED_LAB
RUNNING_STATES
SHUTDOWN_STATES
VALID_STATES

Public Class Methods

new(ui=ZTK::UI.new) click to toggle source
# File lib/cucumber/chef/providers/vagrant.rb, line 40
def initialize(ui=ZTK::UI.new)
  @ui = ui
end

Public Instance Methods

alive?() click to toggle source
# File lib/cucumber/chef/providers/vagrant.rb, line 128
def alive?
  (self.exists? && (RUNNING_STATES.include?(self.state) rescue false))
end
build_create_context() click to toggle source
# File lib/cucumber/chef/providers/vagrant.rb, line 65
def build_create_context
  {
    :ip => Cucumber::Chef.lab_ip,
    :cpus => Cucumber::Chef::Config.vagrant[:cpus],
    :memory => Cucumber::Chef::Config.vagrant[:memory]
  }
end
create() click to toggle source

CREATE

# File lib/cucumber/chef/providers/vagrant.rb, line 48
def create
  ZTK::Benchmark.bench(:message => "Creating #{Cucumber::Chef::Config.provider.upcase} instance", :mark => "completed in %0.4f seconds.", :stdout => @stdout) do
    context               = build_create_context
    vagrantfile_template  = File.join(Cucumber::Chef.root_dir, "lib", "cucumber", "chef", "templates", "cucumber-chef", "Vagrantfile.erb")
    vagrantfile           = File.join(Cucumber::Chef.chef_repo, "Vagrantfile")

    if !File.exists?(vagrantfile)
      IO.write(vagrantfile, ::ZTK::Template.render(vagrantfile_template, context))
    end

    self.vagrant_cli("up", id)
    ZTK::TCPSocketCheck.new(:host => self.ip, :port => self.port, :wait => 120).wait
  end

  self
end
dead?() click to toggle source
# File lib/cucumber/chef/providers/vagrant.rb, line 132
def dead?
  (self.exists? && (SHUTDOWN_STATES.include?(self.state) rescue true))
end
destroy() click to toggle source

DESTROY

# File lib/cucumber/chef/providers/vagrant.rb, line 77
def destroy
  if self.exists?
    self.vagrant_cli("destroy", "--force", id)
  else
    raise VagrantError, MSG_NO_LAB
  end
end
down() click to toggle source

HALT

# File lib/cucumber/chef/providers/vagrant.rb, line 102
def down
  if self.alive?
    self.vagrant_cli("halt", id)
  else
    raise VagrantError, MSG_NO_RUNNING_LAB
  end
end
exists?() click to toggle source
# File lib/cucumber/chef/providers/vagrant.rb, line 124
def exists?
  (self.state != :not_created)
end
id() click to toggle source
# File lib/cucumber/chef/providers/vagrant.rb, line 138
def id
  "test-lab-#{ENV['USER']}".downcase
end
ip() click to toggle source
# File lib/cucumber/chef/providers/vagrant.rb, line 158
def ip
  "192.168.33.10"
end
port() click to toggle source
# File lib/cucumber/chef/providers/vagrant.rb, line 162
def port
  22
end
reload() click to toggle source

RELOAD

# File lib/cucumber/chef/providers/vagrant.rb, line 114
def reload
  if self.alive?
    self.vagrant_cli("reload", id)
  else
    raise VagrantError, MSG_NO_RUNNING_LAB
  end
end
state() click to toggle source
# File lib/cucumber/chef/providers/vagrant.rb, line 142
def state
  output = self.vagrant_cli("status | grep '#{id}'").output
  result = :unknown
  (VALID_STATES+INVALID_STATES).each do |state|
    if output =~ /#{state}/
      result = state
      break
    end
  end
  result.to_sym
end
up() click to toggle source

UP

# File lib/cucumber/chef/providers/vagrant.rb, line 89
def up
  if self.dead?
    self.vagrant_cli("up", id)
    ZTK::TCPSocketCheck.new(:host => self.ip, :port => self.port, :wait => 120).wait
  else
    raise VagrantError, MSG_NO_STOPPED_LAB
  end
end
username() click to toggle source
# File lib/cucumber/chef/providers/vagrant.rb, line 154
def username
  "vagrant"
end
vagrant_cli(*args) click to toggle source
# File lib/cucumber/chef/providers/vagrant.rb, line 168
def vagrant_cli(*args)
  command = Cucumber::Chef.build_command("vagrant", *args)
  ZTK::Command.new.exec(command, :silence => true)
end