/
hypervisor.rb
120 lines (100 loc) · 4.42 KB
/
hypervisor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
114
115
116
117
118
119
120
['host_prebuilt_steps'].each do |lib|
require "beaker/#{lib}"
end
module Beaker
# The Beaker class that interacts to all the supported hypervisors
class Hypervisor
include HostPrebuiltSteps
# Generates an array with all letters a thru z and numbers 0 thru 9
CHARMAP = ('a'..'z').to_a + ('0'..'9').to_a
# Hypervisor creator method. Creates the appropriate hypervisor class object based upon
# the provided hypervisor type selected, then provisions hosts with hypervisor.
# @param [String] type The type of hypervisor to create - one of aix, solaris, vsphere, fusion,
# blimpy, vcloud or vagrant
# @param [Array<Host>] hosts_to_provision The hosts to be provisioned with the selected hypervisor
# @param [Hash] options options Options to alter execution
# @option options [String] :host_name_prefix (nil) Prefix host name if set
def self.create(type, hosts_to_provision, options)
@logger = options[:logger]
@logger.notify("Beaker::Hypervisor, found some #{type} boxes to create")
hyper_class = case type
when /^noop$/
Beaker::Noop
when /^(default)|(none)$/
Beaker::Hypervisor
else
# Custom hypervisor
require "beaker/hypervisor/#{type}"
Beaker.const_get(type.split('_').collect(&:capitalize).join)
end
hypervisor = hyper_class.new(hosts_to_provision, options)
self.set_ssh_connection_preference(hosts_to_provision, hypervisor)
hypervisor.provision if options[:provision]
hypervisor
end
def initialize(hosts, options)
@hosts = hosts
@options = options
end
# Provisioning steps for be run for a given hypervisor. Default is nil.
def provision
nil
end
# Cleanup steps to be run for a given hypervisor. Default is nil.
def cleanup
nil
end
DEFAULT_CONNECTION_PREFERENCE = %i[ip vmhostname hostname]
# SSH connection method preference. Can be overwritten by hypervisor to change the order
def connection_preference(_host)
DEFAULT_CONNECTION_PREFERENCE
end
def self.set_ssh_connection_preference(hosts_to_provision, hypervisor)
hosts_to_provision.each do |host|
ssh_methods = hypervisor.connection_preference(host) + DEFAULT_CONNECTION_PREFERENCE
if host[:ssh_preference]
# If user has provided ssh_connection_preference in hosts file then concat the preference provided by hypervisor
# Followed by concatenating the default preference and keeping the unique once
ssh_methods = host[:ssh_preference] + ssh_methods
end
host[:ssh_connection_preference] = ssh_methods.uniq
end
end
# Proxy package managers on tests hosts created by this hypervisor, runs before validation and configuration.
def proxy_package_manager
return unless @options[:package_proxy]
package_proxy(@hosts, @options)
end
# Default configuration steps to be run for a given hypervisor. Any additional configuration to be done
# to the provided SUT for test execution to be successful.
def configure(opts = {})
begin
return unless @options[:configure]
run_in_parallel = run_in_parallel? opts, @options, 'configure'
block_on @hosts, { :run_in_parallel => run_in_parallel } do |host|
timesync(host, @options) if host[:timesync]
end
sync_root_keys(@hosts, @options) if @options[:root_keys]
set_env(@hosts, @options) if @options[:set_env]
disable_updates(@hosts, @options) if @options[:disable_updates]
rescue SignalException => e
report_and_raise(@logger, e, "configure") if e.signo == 15 # SIGTERM
raise
end
end
# Default validation steps to be run for a given hypervisor. Ensures that SUTs meet requirements to be
# beaker test nodes.
def validate
return unless @options[:validate]
validate_host(@hosts, @options)
end
# Generate a random string composted of letter and numbers
# prefixed with value of {Beaker::Hypervisor::create} option :host_name_prefix
def generate_host_name
n = CHARMAP[rand(25)] + (0...14).map { CHARMAP[rand(CHARMAP.length)] }.join
return @options[:host_name_prefix] + n if @options[:host_name_prefix]
n
end
end
end
require "beaker/hypervisor/noop"