Class: Beaker::Vagrant
Constant Summary
Constants inherited
from Hypervisor
Hypervisor::CHARMAP
HostPrebuiltSteps::APT_CFG, HostPrebuiltSteps::CUMULUS_PACKAGES, HostPrebuiltSteps::DEBIAN_PACKAGES, HostPrebuiltSteps::ETC_HOSTS_PATH, HostPrebuiltSteps::ETC_HOSTS_PATH_SOLARIS, HostPrebuiltSteps::FREEBSD_PACKAGES, HostPrebuiltSteps::IPS_PKG_REPO, HostPrebuiltSteps::NTPSERVER, HostPrebuiltSteps::OPENBSD_PACKAGES, HostPrebuiltSteps::PSWINDOWS_PACKAGES, HostPrebuiltSteps::ROOT_KEYS_SCRIPT, HostPrebuiltSteps::ROOT_KEYS_SYNC_CMD, HostPrebuiltSteps::ROOT_KEYS_SYNC_CMD_AIX, HostPrebuiltSteps::SLEEPWAIT, HostPrebuiltSteps::SLES10_PACKAGES, HostPrebuiltSteps::SLES_PACKAGES, HostPrebuiltSteps::SOLARIS10_PACKAGES, HostPrebuiltSteps::SOLARIS11_PACKAGES, HostPrebuiltSteps::TRIES, HostPrebuiltSteps::UNIX_PACKAGES, HostPrebuiltSteps::WINDOWS_PACKAGES
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Hypervisor
#configure, create, #generate_host_name, #proxy_package_manager, #validate
#add_el_extras, #additive_hash_merge, #apt_get_update, #check_and_install_packages_if_needed, #construct_env, #copy_file_to_remote, #copy_ssh_to_root, #disable_iptables, #disable_se_linux, #disable_updates, #enable_root_login, #get_domain_name, #get_ip, #hack_etc_hosts, #install_one_of_packages, #package_proxy, #proxy_config, #set_env, #set_etc_hosts, #sync_root_keys, #timesync, #validate_host
#block_on
Constructor Details
#initialize(vagrant_hosts, options) ⇒ Vagrant
Returns a new instance of Vagrant.
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/beaker/hypervisor/vagrant.rb', line 158
def initialize(vagrant_hosts, options)
require 'tempfile'
@options = options
@logger = options[:logger]
@temp_files = []
@hosts = vagrant_hosts
@vagrant_path = File.expand_path(File.join(File.basename(__FILE__), '..', '.vagrant', 'beaker_vagrant_files', File.basename(options[:hosts_file])))
FileUtils.mkdir_p(@vagrant_path)
@vagrant_file = File.expand_path(File.join(@vagrant_path, "Vagrantfile"))
@vagrant_env = { "RUBYLIB" => "" }
end
|
Class Method Details
.provider_vfile_section(host, options) ⇒ Object
Instance Method Details
#cleanup ⇒ Object
208
209
210
211
212
213
214
215
216
|
# File 'lib/beaker/hypervisor/vagrant.rb', line 208
def cleanup
@logger.debug "removing temporary ssh-config files per-vagrant box"
@temp_files.each do |f|
f.close()
end
@logger.notify "Destroying vagrant boxes"
vagrant_cmd("destroy --force")
FileUtils.rm_rf(@vagrant_path)
end
|
#get_ip_from_vagrant_file(hostname) ⇒ Object
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/beaker/hypervisor/vagrant.rb', line 141
def get_ip_from_vagrant_file(hostname)
ip = ''
if File.file?(@vagrant_file) f = File.read(@vagrant_file)
m = /'#{hostname}'.*?ip:\s*('|")\s*([^'"]+)('|")/m.match(f)
if m
ip = m[2]
@logger.debug("Determined existing vagrant box #{hostname} ip to be: #{ip} ")
else
raise("Unable to determine ip for vagrant box #{hostname}")
end
else
raise("No vagrant file found (should be located at #{@vagrant_file})")
end
ip
end
|
#make_vfile(hosts, options = {}) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/beaker/hypervisor/vagrant.rb', line 33
def make_vfile hosts, options = {}
v_file = "Vagrant.configure(\"2\") do |c|\n"
v_file << " c.ssh.forward_agent = true\n" if options[:forward_ssh_agent] == true
v_file << " c.ssh.insert_key = false\n"
hosts.each do |host|
host['ip'] ||= randip v_file << " c.vm.define '#{host.name}' do |v|\n"
v_file << " v.vm.hostname = '#{host.name}'\n"
v_file << " v.vm.box = '#{host['box']}'\n"
v_file << " v.vm.box_url = '#{host['box_url']}'\n" unless host['box_url'].nil?
v_file << " v.vm.box_version = '#{host['box_version']}'\n" unless host['box_version'].nil?
v_file << " v.vm.box_check_update = '#{host['box_check_update'] ||= 'true'}'\n"
v_file << " v.vm.synced_folder '.', '/vagrant', disabled: true\n" if host['synced_folder'] == 'disabled'
v_file << private_network_generator(host)
unless host['mount_folders'].nil?
host['mount_folders'].each do |name, folder|
v_file << " v.vm.synced_folder '#{folder[:from]}', '#{folder[:to]}', create: true\n"
end
end
unless host['forwarded_ports'].nil?
host['forwarded_ports'].each do |_name, port|
fwd = " v.vm.network :forwarded_port,"
fwd << " protocol: '#{port[:protocol]}'," unless port[:protocol].nil?
fwd << " guest_ip: '#{port[:to_ip]}'," unless port[:to_ip].nil?
fwd << " guest: #{port[:to]},"
fwd << " host_ip: '#{port[:from_ip]}'," unless port[:from_ip].nil?
fwd << " host: #{port[:from]}"
fwd << "\n"
v_file << fwd
end
end
if /windows/i.match(host['platform'])
v_file << " v.vm.network :forwarded_port, guest: 22, host: 2222, id: 'ssh', auto_correct: true\n"
v_file << " v.vm.network :forwarded_port, guest: 3389, host: 3389, id: 'rdp', auto_correct: true\n"
v_file << " v.vm.network :forwarded_port, guest: 5985, host: 5985, id: 'winrm', auto_correct: true\n"
v_file << " v.vm.guest = :windows\n"
end
if /osx/i.match(host['platform'])
v_file << " v.vm.network 'private_network', ip: '10.0.1.10'\n"
v_file << " v.vm.synced_folder '.', '/vagrant', :nfs => true\n"
end
if /freebsd/i.match(host['platform'])
v_file << " v.ssh.shell = 'sh'\n"
v_file << " v.vm.guest = :freebsd\n"
if host['vagrant_freebsd_nfs'].nil?
v_file << " v.vm.synced_folder '.', '/vagrant', type: 'rsync'\n"
else
v_file << " v.vm.synced_folder '.', '/vagrant', :nfs => true\n"
end
end
v_file << self.class.provider_vfile_section(host, options)
v_file << " end\n"
@logger.debug "created Vagrantfile for VagrantHost #{host.name}"
end
v_file << "end\n"
File.open(@vagrant_file, 'w') do |f|
f.write(v_file)
end
end
|
#private_network_generator(host) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/beaker/hypervisor/vagrant.rb', line 21
def private_network_generator(host)
private_network_string = " v.vm.network :private_network, ip: \"#{host['ip'].to_s}\", :netmask => \"#{host['netmask'] ||= "255.255.0.0"}\""
case host['network_mac']
when 'false'
private_network_string << "\n"
when nil
private_network_string << ", :mac => \"#{randmac}\"\n"
else
private_network_string << ", :mac => \"#{host['network_mac']}\"\n"
end
end
|
#provision(provider = nil) ⇒ Object
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/beaker/hypervisor/vagrant.rb', line 170
def provision(provider = nil)
if !@options[:provision] and !File.file?(@vagrant_file)
raise "Beaker is configured with provision = false but no vagrant file was found at #{@vagrant_file}. You need to enable provision"
end
if @options[:provision]
vagrant_cmd("destroy --force") if File.file?(@vagrant_file)
make_vfile @hosts, @options
vagrant_cmd("up#{" --provider #{provider}" if provider}")
else @hosts.each do |host|
host[:ip] = get_ip_from_vagrant_file(host.name)
end
end
@logger.debug "configure vagrant boxes (set ssh-config, switch to root user, hack etc/hosts)"
@hosts.each do |host|
default_user = host['user']
set_ssh_config host, 'vagrant'
copy_ssh_to_root host, @options
enable_root_login host, @options
host.close
set_ssh_config host, default_user
end
hack_etc_hosts @hosts, @options
end
|
#rand_chunk ⇒ Object
13
14
15
|
# File 'lib/beaker/hypervisor/vagrant.rb', line 13
def rand_chunk
(2 + rand(252)).to_s end
|
#randip ⇒ Object
17
18
19
|
# File 'lib/beaker/hypervisor/vagrant.rb', line 17
def randip
"10.255.#{rand_chunk}.#{rand_chunk}"
end
|
#randmac ⇒ String
Return a random mac address
9
10
11
|
# File 'lib/beaker/hypervisor/vagrant.rb', line 9
def randmac
"080027" + (1..3).map{"%0.2X"%rand(256)}.join
end
|
#set_ssh_config(host, user) ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/beaker/hypervisor/vagrant.rb', line 120
def set_ssh_config host, user
f = Tempfile.new("#{host.name}")
ssh_config = Dir.chdir(@vagrant_path) do
stdin, stdout, stderr, wait_thr = Open3.popen3(@vagrant_env, 'vagrant', 'ssh-config', host.name)
if not wait_thr.value.success?
raise "Failed to 'vagrant ssh-config' for #{host.name}"
end
stdout.read
end
ssh_config = ssh_config.gsub(/Host #{host.name}/, "Host #{host['ip']}") unless not host['ip']
ssh_config = ssh_config.gsub(/User vagrant/, "User #{user}")
f.write(ssh_config)
f.rewind
host['ssh'] = {:config => f.path()}
host['user'] = user
@temp_files << f
end
|
#vagrant_cmd(args) ⇒ Object
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/beaker/hypervisor/vagrant.rb', line 218
def vagrant_cmd(args)
Dir.chdir(@vagrant_path) do
exit_status = 1
Open3.popen3(@vagrant_env, "vagrant #{args}") {|stdin, stdout, stderr, wait_thr|
while line = stdout.gets
@logger.info(line)
end
if not wait_thr.value.success?
raise "Failed to exec 'vagrant #{args}'. Error was #{stderr.read}"
end
exit_status = wait_thr.value
}
if exit_status != 0
raise "Failed to execute vagrant_cmd ( #{args} ). Error was #{stderr.read}"
end
end
end
|