Class: Cucumber::Chef::Provider::Vagrant

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/chef/providers/vagrant.rb

Constant Summary collapse

INVALID_STATES =
%w(not_created aborted unknown).map(&:to_sym)
RUNNING_STATES =
%w(running).map(&:to_sym)
SHUTDOWN_STATES =
%w(paused saved poweroff).map(&:to_sym)
VALID_STATES =
(RUNNING_STATES + SHUTDOWN_STATES)
MSG_NO_RUNNING_LAB =
%(We could not find a running test lab.)
MSG_NO_STOPPED_LAB =
%(We could not find a powered off test lab.)
MSG_NO_LAB =
%(We could not find a test lab!)

Instance Method Summary collapse

Constructor Details

#initialize(ui = ZTK::UI.new) ⇒ Vagrant

Returns a new instance of Vagrant.



40
41
42
# File 'lib/cucumber/chef/providers/vagrant.rb', line 40

def initialize(ui=ZTK::UI.new)
  @ui = ui
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/cucumber/chef/providers/vagrant.rb', line 128

def alive?
  (self.exists? && (RUNNING_STATES.include?(self.state) rescue false))
end

#build_create_contextObject



65
66
67
68
69
70
71
# 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

#createObject

CREATE



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/cucumber/chef/providers/vagrant.rb', line 132

def dead?
  (self.exists? && (SHUTDOWN_STATES.include?(self.state) rescue true))
end

#destroyObject

DESTROY



77
78
79
80
81
82
83
# 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

#downObject

HALT



102
103
104
105
106
107
108
# 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?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/cucumber/chef/providers/vagrant.rb', line 124

def exists?
  (self.state != :not_created)
end

#idObject



138
139
140
# File 'lib/cucumber/chef/providers/vagrant.rb', line 138

def id
  "test-lab-#{ENV['USER']}".downcase
end

#ipObject



158
159
160
# File 'lib/cucumber/chef/providers/vagrant.rb', line 158

def ip
  "192.168.33.10"
end

#portObject



162
163
164
# File 'lib/cucumber/chef/providers/vagrant.rb', line 162

def port
  22
end

#reloadObject

RELOAD



114
115
116
117
118
119
120
# 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

#stateObject



142
143
144
145
146
147
148
149
150
151
152
# 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

#upObject

UP



89
90
91
92
93
94
95
96
# 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

#usernameObject



154
155
156
# File 'lib/cucumber/chef/providers/vagrant.rb', line 154

def username
  "vagrant"
end

#vagrant_cli(*args) ⇒ Object



168
169
170
171
# 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