Class: Vagrant_Rbapi

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant_rbapi.rb

Instance Method Summary collapse

Constructor Details

#initialize(vagrant_environment) ⇒ Vagrant_Rbapi

Returns a new instance of Vagrant_Rbapi.



8
9
10
# File 'lib/vagrant_rbapi.rb', line 8

def initialize(vagrant_environment)
	Dir.chdir(vagrant_environment)
end

Instance Method Details

#destroyObject



69
70
71
72
73
# File 'lib/vagrant_rbapi.rb', line 69

def destroy
	raise VagrantRbapi::BoxNotCreated if self.status == 'not created'
	out, err, val = vagrant_cmd(['destroy', '--force'])
	return val
end

#haltObject



63
64
65
66
67
# File 'lib/vagrant_rbapi.rb', line 63

def halt
	raise VagrantRbapi::BoxNotRunning if self.status != 'running'
	out, err, val = vagrant_cmd(['halt', '--force'])
	return val
end

#ssh(cmd) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/vagrant_rbapi.rb', line 53

def ssh(cmd)
	raise VagrantRbapi::BoxNotRunning if self.status != 'running'
	config = ssh_config
	out = Net::SSH.start(config[0], config[1], port: config[2], key_data: [File.read(config[3])]) do |ssh|
		ssh.exec!(cmd)
	end
	out.strip! unless out.nil?
	return out
end

#ssh_configObject



43
44
45
46
47
48
49
50
51
# File 'lib/vagrant_rbapi.rb', line 43

def ssh_config
	raise VagrantRbapi::BoxNotRunning if self.status != 'running'
	out, err, val = vagrant_cmd(['ssh-config'])
	hostname = out[/HostName (.*)$/, 1].strip
	user = out[/User (.*)$/, 1].strip
	port = out[/Port (.*)$/, 1].strip
	identityfile = out[/IdentityFile (.*)$/, 1].strip
	return hostname, user, port, identityfile
end

#statusObject



31
32
33
34
35
# File 'lib/vagrant_rbapi.rb', line 31

def status
	out, err, val = vagrant_cmd(['status'])
	status = out[/default(.*)\(/, 1].strip
	return status
end

#upObject



37
38
39
40
41
# File 'lib/vagrant_rbapi.rb', line 37

def up
	raise VagrantRbapi::BoxAlreadyRunning if self.status == 'running'
	out, err, val = vagrant_cmd(['up'])
	return val
end

#vagrant_binObject



12
13
14
15
16
17
18
# File 'lib/vagrant_rbapi.rb', line 12

def vagrant_bin
	ENV['PATH'].split(File::PATH_SEPARATOR).each do |dir|
		if File.executable?(File.join(dir, 'vagrant'))
			return File.join(dir, 'vagrant')
		end
	end
end

#vagrant_cmd(cmd) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/vagrant_rbapi.rb', line 20

def vagrant_cmd(cmd)
	cmd = cmd.unshift(vagrant_bin).join(' ')
	out, err, val = '', '', ''
	Open3.popen3(ENV, cmd) do |stdin, stdout, stderr, wait_thr|
		out = stdout.read.to_s
		err = stderr.read.to_s
		val = wait_thr.value.to_s.split.last
	end
	return out, err, val
end