Class: Beaker::Host
- Inherits:
-
Object
show all
- Defined in:
- lib/beaker/host.rb
Defined Under Namespace
Classes: CommandFailure, PuppetConfigReader
Constant Summary
collapse
- SELECT_TIMEOUT =
30
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#+(other) ⇒ Object
-
#[](k) ⇒ Object
-
#[]=(k, v) ⇒ Object
-
#close ⇒ Object
-
#connection ⇒ Object
-
#do_scp_from(source, target, options) ⇒ Object
-
#do_scp_to(source, target, options) ⇒ Object
-
#exec(command, options = {}) ⇒ Object
-
#get_ip ⇒ Object
Determine the ip address of this host.
-
#has_key?(k) ⇒ Boolean
-
#hostname ⇒ Object
Return the public name of the particular host, which may be different then the name of the host provided in the configuration file as some provisioners create random, unique hostnames.
-
#initialize(name, options) ⇒ Host
constructor
-
#ip ⇒ Object
Return the ip address of this host.
-
#is_pe? ⇒ Boolean
-
#log_prefix ⇒ Object
-
#merge_defaults_for_type(options, type) ⇒ Object
-
#node_name ⇒ Object
-
#pkg_initialize ⇒ Object
-
#port_open?(port) ⇒ Boolean
-
#puppet(command = 'agent') ⇒ Object
Returning our PuppetConfigReader here allows users of the Host class to do things like ‘host.puppet` to query the ’main’ section or, if they want the configuration for a particular run type, ‘host.puppet(’agent’)‘.
-
#reachable_name ⇒ Object
Return the preferred method to reach the host, will use IP is available and then default to #hostname.
-
#to_s ⇒ Object
-
#to_str ⇒ Object
-
#up? ⇒ Boolean
Constructor Details
#initialize(name, options) ⇒ Host
Returns a new instance of Host.
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/beaker/host.rb', line 41
def initialize name, options
@logger = options[:logger]
@name, @options = name.to_s, options.dup
type = is_pe? ? :pe : :foss
@defaults = merge_defaults_for_type @options, type
pkg_initialize
end
|
Instance Attribute Details
#defaults ⇒ Object
Returns the value of attribute defaults.
40
41
42
|
# File 'lib/beaker/host.rb', line 40
def defaults
@defaults
end
|
#logger ⇒ Object
Returns the value of attribute logger.
39
40
41
|
# File 'lib/beaker/host.rb', line 39
def logger
@logger
end
|
#name ⇒ Object
Returns the value of attribute name.
40
41
42
|
# File 'lib/beaker/host.rb', line 40
def name
@name
end
|
Class Method Details
.create(name, options) ⇒ Object
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/beaker/host.rb', line 28
def self.create name, options
case options['HOSTS'][name]['platform']
when /windows/
Windows::Host.new name, options
when /aix/
Aix::Host.new name, options
else
Unix::Host.new name, options
end
end
|
Instance Method Details
#+(other) ⇒ Object
132
133
134
|
# File 'lib/beaker/host.rb', line 132
def + other
@name + other
end
|
#[](k) ⇒ Object
108
109
110
|
# File 'lib/beaker/host.rb', line 108
def [] k
@defaults[k]
end
|
#[]=(k, v) ⇒ Object
104
105
106
|
# File 'lib/beaker/host.rb', line 104
def []= k, v
@defaults[k] = v
end
|
#close ⇒ Object
164
165
166
167
|
# File 'lib/beaker/host.rb', line 164
def close
@connection.close if @connection
@connection = nil
end
|
#connection ⇒ Object
158
159
160
161
162
|
# File 'lib/beaker/host.rb', line 158
def connection
@connection ||= SshConnection.connect( reachable_name,
self['user'],
self['ssh'], { :logger => @logger } )
end
|
#do_scp_from(source, target, options) ⇒ Object
215
216
217
218
219
220
|
# File 'lib/beaker/host.rb', line 215
def do_scp_from source, target, options
@logger.debug "localhost $ scp #{@name}:#{source} #{target} #{options.to_s}"
result = connection.scp_from(source, target, options, $dry_run)
return result
end
|
#do_scp_to(source, target, options) ⇒ Object
209
210
211
212
213
|
# File 'lib/beaker/host.rb', line 209
def do_scp_to source, target, options
@logger.debug "localhost $ scp #{source} #{@name}:#{target} #{options.to_s}"
result = connection.scp_to(source, target, options, $dry_run)
return result
end
|
#exec(command, options = {}) ⇒ Object
169
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
207
|
# File 'lib/beaker/host.rb', line 169
def exec command, options={}
cmdline = command.cmd_line(self)
if options[:silent]
output_callback = nil
else
@logger.debug "\n#{log_prefix} #{Time.new.strftime('%H:%M:%S')}$ #{cmdline}"
output_callback = logger.method(:host_output)
end
unless $dry_run
result = nil
seconds = Benchmark.realtime {
result = connection.execute(cmdline, options, output_callback)
}
if not options[:silent]
@logger.debug "\n#{log_prefix} executed in %0.2f seconds" % seconds
end
unless options[:silent]
result.log(@logger)
unless result.exit_code_in?(Array(options[:acceptable_exit_codes] || 0))
raise CommandFailure, "Host '#{self}' exited with #{result.exit_code} running:\n #{cmdline}\nLast #{@options[:trace_limit]} lines of output were:\n#{result.formatted_output(@options[:trace_limit])}"
end
end
result
end
end
|
#get_ip ⇒ Object
Determine the ip address of this host
149
150
151
|
# File 'lib/beaker/host.rb', line 149
def get_ip
@logger.warn("Uh oh, this should be handled by sub-classes but hasn't been")
end
|
#has_key?(k) ⇒ Boolean
112
113
114
|
# File 'lib/beaker/host.rb', line 112
def has_key? k
@defaults.has_key?(k)
end
|
#hostname ⇒ Object
Return the public name of the particular host, which may be different then the name of the host provided in the configuration file as some provisioners create random, unique hostnames.
128
129
130
|
# File 'lib/beaker/host.rb', line 128
def hostname
@defaults['vmhostname'] || @name
end
|
#ip ⇒ Object
Return the ip address of this host
154
155
156
|
# File 'lib/beaker/host.rb', line 154
def ip
self[:ip] ||= get_ip
end
|
#is_pe? ⇒ Boolean
136
137
138
|
# File 'lib/beaker/host.rb', line 136
def is_pe?
@options.is_pe?
end
|
#log_prefix ⇒ Object
140
141
142
143
144
145
146
|
# File 'lib/beaker/host.rb', line 140
def log_prefix
if @defaults['vmhostname']
"#{self} (#{@name})"
else
self.to_s
end
end
|
#merge_defaults_for_type(options, type) ⇒ Object
59
60
61
62
|
# File 'lib/beaker/host.rb', line 59
def merge_defaults_for_type options, type
defaults = self.class.send "#{type}_defaults".to_sym
defaults.merge(options.merge((options['HOSTS'][name])))
end
|
#node_name ⇒ Object
64
65
66
67
68
69
|
# File 'lib/beaker/host.rb', line 64
def node_name
result = puppet['node_name_value'].strip
end
|
#pkg_initialize ⇒ Object
54
55
56
57
|
# File 'lib/beaker/host.rb', line 54
def pkg_initialize
end
|
#port_open?(port) ⇒ Boolean
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/beaker/host.rb', line 71
def port_open? port
begin
Timeout.timeout SELECT_TIMEOUT do
TCPSocket.new(reachable_name, port).close
return true
end
rescue Errno::ECONNREFUSED, Timeout::Error
return false
end
end
|
#puppet(command = 'agent') ⇒ Object
Returning our PuppetConfigReader here allows users of the Host class to do things like ‘host.puppet` to query the ’main’ section or, if they want the configuration for a particular run type, ‘host.puppet(’agent’)‘
100
101
102
|
# File 'lib/beaker/host.rb', line 100
def puppet(command='agent')
PuppetConfigReader.new(self, command)
end
|
#reachable_name ⇒ Object
Return the preferred method to reach the host, will use IP is available and then default to #hostname.
92
93
94
|
# File 'lib/beaker/host.rb', line 92
def reachable_name
self['ip'] || hostname
end
|
#to_s ⇒ Object
122
123
124
|
# File 'lib/beaker/host.rb', line 122
def to_s
hostname
end
|
#to_str ⇒ Object
117
118
119
|
# File 'lib/beaker/host.rb', line 117
def to_str
hostname
end
|
#up? ⇒ Boolean
82
83
84
85
86
87
88
89
|
# File 'lib/beaker/host.rb', line 82
def up?
begin
Socket.getaddrinfo( reachable_name, nil )
return true
rescue SocketError
return false
end
end
|