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

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

Constant Summary collapse

INVALID_STATES =

attr_accessor :env, :vm

%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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Vagrant.



37
38
39
# File 'lib/cucumber/chef/providers/vagrant.rb', line 37

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

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/cucumber/chef/providers/vagrant.rb', line 147

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

#createObject

CREATE



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cucumber/chef/providers/vagrant.rb', line 45

def create
  ZTK::Benchmark.bench(:message => "Creating #{Cucumber::Chef::Config.provider.upcase} instance", :mark => "completed in %0.4f seconds.", :stdout => @stdout) do

    context = {
      :ip => Cucumber::Chef.lab_ip,
      :cpus => Cucumber::Chef::Config.vagrant[:cpus],
      :memory => Cucumber::Chef::Config.vagrant[:memory]
    }

    vagrantfile_template = File.join(Cucumber::Chef.root_dir, "lib", "cucumber", "chef", "templates", "cucumber-chef", "Vagrantfile.erb")

    vagrantfile = File.join(Cucumber::Chef.chef_repo, "Vagrantfile")

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

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

  self

rescue Exception => e
  @ui.logger.fatal { e.message }
  @ui.logger.fatal { e.backtrace.join("\n") }
  raise VagrantError, e.message
end

#dead?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/cucumber/chef/providers/vagrant.rb', line 151

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

#destroyObject

DESTROY



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cucumber/chef/providers/vagrant.rb', line 76

def destroy
  if exists?
    self.vagrant_cli("destroy", "--force", id)
  else
    raise VagrantError, "We could not find a test lab!"
  end

rescue Exception => e
  @ui.logger.fatal { e.message }
  @ui.logger.fatal { e.backtrace.join("\n") }
  raise VagrantError, e.message
end

#downObject

HALT



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cucumber/chef/providers/vagrant.rb', line 111

def down
  if (exists? && alive?)
    self.vagrant_cli("halt", id)
  else
    raise AWSError, "We could not find a running test lab."
  end

rescue Exception => e
  @ui.logger.fatal { e.message }
  @ui.logger.fatal { e.backtrace.join("\n") }
  raise VagrantError, e.message
end

#exists?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/cucumber/chef/providers/vagrant.rb', line 143

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

#idObject



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

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

#ipObject



177
178
179
# File 'lib/cucumber/chef/providers/vagrant.rb', line 177

def ip
  "192.168.33.10"
end

#portObject



181
182
183
# File 'lib/cucumber/chef/providers/vagrant.rb', line 181

def port
  22
end

#reloadObject

RELOAD



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cucumber/chef/providers/vagrant.rb', line 128

def reload
  if (exists? && alive?)
    self.vagrant_cli("reload", id)
  else
    raise AWSError, "We could not find a running test lab."
  end

rescue Exception => e
  @ui.logger.fatal { e.message }
  @ui.logger.fatal { e.backtrace.join("\n") }
  raise VagrantError, e.message
end

#stateObject



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/cucumber/chef/providers/vagrant.rb', line 161

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



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/cucumber/chef/providers/vagrant.rb', line 93

def up
  if (exists? && dead?)
    self.vagrant_cli("up", id)
    ZTK::TCPSocketCheck.new(:host => self.ip, :port => self.port, :wait => 120).wait
  else
    raise VagrantError, "We could not find a powered off test lab."
  end

rescue Exception => e
  @ui.logger.fatal { e.message }
  @ui.logger.fatal { e.backtrace.join("\n") }
  raise VagrantError, e.message
end

#usernameObject



173
174
175
# File 'lib/cucumber/chef/providers/vagrant.rb', line 173

def username
  "vagrant"
end

#vagrant_cli(*args) ⇒ Object



187
188
189
190
# File 'lib/cucumber/chef/providers/vagrant.rb', line 187

def vagrant_cli(*args)
  command = Cucumber::Chef.build_command("vagrant", *args)
  ZTK::Command.new.exec(command, :silence => true)
end