Class: Beaker::Host
- Inherits:
-
Object
show all
- Defined in:
- lib/beaker/host.rb
Defined Under Namespace
Classes: PuppetConfigReader
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
-
#has_key?(k) ⇒ Boolean
-
#initialize(name, options, config) ⇒ Host
constructor
-
#is_pe? ⇒ Boolean
-
#merge_defaults_for_type(config, type) ⇒ Object
-
#node_name ⇒ 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
-
#to_s ⇒ Object
-
#to_str ⇒ Object
-
#up? ⇒ Boolean
Constructor Details
#initialize(name, options, config) ⇒ Host
Returns a new instance of Host.
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/beaker/host.rb', line 41
def initialize name, options, config
@logger = options[:logger]
@name, @options, @config = name, options.dup, config
type = is_pe? ? :pe : :foss
@defaults = merge_defaults_for_type @config, type
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, config) ⇒ Object
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/beaker/host.rb', line 28
def self.create name, options, config
case config['HOSTS'][name]['platform']
when /windows/
Windows::Host.new name, options, config
when /aix/
Aix::Host.new name, options, config
else
Unix::Host.new name, options, config
end
end
|
Instance Method Details
#+(other) ⇒ Object
118
119
120
|
# File 'lib/beaker/host.rb', line 118
def + other
@name + other
end
|
#[](k) ⇒ Object
102
103
104
|
# File 'lib/beaker/host.rb', line 102
def [] k
@defaults[k]
end
|
#[]=(k, v) ⇒ Object
98
99
100
|
# File 'lib/beaker/host.rb', line 98
def []= k, v
@defaults[k] = v
end
|
#close ⇒ Object
132
133
134
135
|
# File 'lib/beaker/host.rb', line 132
def close
@connection.close if @connection
@connection = nil
end
|
#connection ⇒ Object
126
127
128
129
130
|
# File 'lib/beaker/host.rb', line 126
def connection
@connection ||= SshConnection.connect( reachable_name,
self['user'],
self['ssh'] )
end
|
#do_scp_from(source, target, options) ⇒ Object
181
182
183
184
185
186
|
# File 'lib/beaker/host.rb', line 181
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
174
175
176
177
178
179
|
# File 'lib/beaker/host.rb', line 174
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/beaker/host.rb', line 137
def exec command, options={}
cmdline = command.cmd_line(self)
if options[:silent]
output_callback = nil
else
if @defaults['vmhostname']
@logger.debug "\n#{self} (#{@name}) $ #{cmdline}"
else
@logger.debug "\n#{self} $ #{cmdline}"
end
output_callback = logger.method(:host_output)
end
unless $dry_run
result = connection.execute(cmdline, options, output_callback)
unless options[:silent]
result.log(@logger)
unless result.exit_code_in?(options[:acceptable_exit_codes] || [0])
limit = 10
raise "Host '#{self}' exited with #{result.exit_code} running:\n #{cmdline}\nLast #{limit} lines of output were:\n#{result.formatted_output(limit)}"
end
end
result
end
end
|
#has_key?(k) ⇒ Boolean
106
107
108
|
# File 'lib/beaker/host.rb', line 106
def has_key? k
@defaults.has_key?(k)
end
|
#is_pe? ⇒ Boolean
122
123
124
|
# File 'lib/beaker/host.rb', line 122
def is_pe?
@config.is_pe?
end
|
#merge_defaults_for_type(config, type) ⇒ Object
53
54
55
56
|
# File 'lib/beaker/host.rb', line 53
def merge_defaults_for_type config, type
defaults = self.class.send "#{type}_defaults".to_sym
defaults.merge(config['CONFIG']).merge(config['HOSTS'][name])
end
|
#node_name ⇒ Object
58
59
60
61
62
63
|
# File 'lib/beaker/host.rb', line 58
def node_name
result = puppet['node_name_value'].strip
end
|
#port_open?(port) ⇒ Boolean
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/beaker/host.rb', line 65
def port_open? port
Timeout.timeout 1 do
begin
TCPSocket.new(reachable_name, port).close
return true
rescue Errno::ECONNREFUSED
return false
end
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’)`
94
95
96
|
# File 'lib/beaker/host.rb', line 94
def puppet(command='agent')
PuppetConfigReader.new(self, command)
end
|
#reachable_name ⇒ Object
86
87
88
|
# File 'lib/beaker/host.rb', line 86
def reachable_name
self['ip'] || self['vmhostname'] || name
end
|
#to_s ⇒ Object
114
115
116
|
# File 'lib/beaker/host.rb', line 114
def to_s
@defaults['vmhostname'] || @name
end
|
#to_str ⇒ Object
110
111
112
|
# File 'lib/beaker/host.rb', line 110
def to_str
@defaults['vmhostname'] || @name
end
|
#up? ⇒ Boolean
76
77
78
79
80
81
82
83
84
|
# File 'lib/beaker/host.rb', line 76
def up?
require 'socket'
begin
Socket.getaddrinfo( reachable_name, nil )
return true
rescue SocketError
return false
end
end
|