Class: Kitchen::Instance
- Inherits:
-
Object
- Object
- Kitchen::Instance
- Includes:
- Logging
- Defined in:
- lib/kitchen/instance.rb
Overview
An instance of a suite running on a platform. A created instance may be a local virtual machine, cloud instance, container, or even a bare metal server, which is determined by the platform's driver.
Defined Under Namespace
Classes: ActionRunner, FSM
Class Attribute Summary collapse
-
.mutexes ⇒ Hash
private
A hash of mutexes, arranged by Plugin class names.
Instance Attribute Summary collapse
-
#driver ⇒ Driver::Base
Driver object which will manage this instance's lifecycle actions.
-
#lifecycle_hooks ⇒ LifecycleHooks
Lifecycle hooks manager object.
-
#logger ⇒ Logger
readonly
The logger for this instance.
-
#name ⇒ String
readonly
Name of this instance.
-
#platform ⇒ Platform
readonly
The target platform configuration.
-
#provisioner ⇒ Provisioner::Base
Provisioner object which will provide the setup and invocation instructions for configuration management and other automation tools.
-
#suite ⇒ Suite
readonly
The test suite configuration.
-
#transport ⇒ Transport::Base
Transport object which will communicate with an instance.
-
#verifier ⇒ Verifier
Verifier object for instance to manage the verifier installation on this instance.
Class Method Summary collapse
-
.name_for(suite, platform) ⇒ String
Generates a name for an instance given a suite and platform.
Instance Method Summary collapse
-
#cleanup! ⇒ void
Clean up any per-instance resources before exiting.
-
#converge ⇒ self
Converges this running instance.
-
#create ⇒ self
Creates this instance.
-
#current_session_id ⇒ String?
Returns the current instance session identifier, if one has been established.
-
#destroy ⇒ self
Destroys this instance.
-
#diagnose ⇒ Hash
Returns a Hash of configuration and other useful diagnostic information.
-
#diagnose_plugins ⇒ Hash
Returns a Hash of configuration and other useful diagnostic information associated with plugins (such as loaded version, class name, etc.).
-
#doctor_action ⇒ Object
Check system and configuration for common errors.
-
#initialize(options = {}) ⇒ Instance
constructor
Creates a new instance, given a suite and a platform.
-
#last_action ⇒ String
Returns the last successfully completed action state of the instance.
-
#last_error ⇒ String
Returns the error encountered on the last action on the instance.
-
#log_path ⇒ String?
Returns the path to the text log file for this instance.
-
#login ⇒ Object
Logs in to this instance by invoking a system command, provided by the instance's transport.
-
#package_action ⇒ Object
Perform package.
-
#remote_exec(command) ⇒ Object
Executes an arbitrary command on this instance.
-
#setup ⇒ self
Sets up this converged instance for suite tests.
-
#state_path ⇒ String
Returns the path to the state file for this instance.
-
#status(probe: false) ⇒ Hash
Returns normalized liveness status for this instance.
-
#structured_log_path ⇒ String?
Returns the path to the structured log file for this instance.
-
#test(destroy_mode = :passing) ⇒ self
Tests this instance by creating, converging and verifying.
-
#to_str ⇒ String
Returns a displayable representation of the instance.
-
#verify ⇒ self
Verifies this set up instance by executing suite tests.
Methods included from Logging
#debug, #error, #fatal, #info, #warn
Constructor Details
#initialize(options = {}) ⇒ Instance
Creates a new instance, given a suite and a platform.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/kitchen/instance.rb', line 97 def initialize( = {}) () @suite = .fetch(:suite) @platform = .fetch(:platform) @name = self.class.name_for(@suite, @platform) @driver = .fetch(:driver) @lifecycle_hooks = .fetch(:lifecycle_hooks) @provisioner = .fetch(:provisioner) @transport = .fetch(:transport) @verifier = .fetch(:verifier) @logger = .fetch(:logger) { Kitchen.logger } @state_file = .fetch(:state_file) setup_driver setup_provisioner setup_transport setup_verifier setup_lifecycle_hooks end |
Class Attribute Details
.mutexes ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a hash of mutexes, arranged by Plugin class names.
37 38 39 |
# File 'lib/kitchen/instance.rb', line 37 def mutexes @mutexes end |
Instance Attribute Details
#driver ⇒ Driver::Base
Returns driver object which will manage this instance's lifecycle actions.
60 61 62 |
# File 'lib/kitchen/instance.rb', line 60 def driver @driver end |
#lifecycle_hooks ⇒ LifecycleHooks
Returns lifecycle hooks manager object.
63 64 65 |
# File 'lib/kitchen/instance.rb', line 63 def lifecycle_hooks @lifecycle_hooks end |
#logger ⇒ Logger (readonly)
Returns the logger for this instance.
79 80 81 |
# File 'lib/kitchen/instance.rb', line 79 def logger @logger end |
#name ⇒ String (readonly)
Returns name of this instance.
56 57 58 |
# File 'lib/kitchen/instance.rb', line 56 def name @name end |
#platform ⇒ Platform (readonly)
Returns the target platform configuration.
53 54 55 |
# File 'lib/kitchen/instance.rb', line 53 def platform @platform end |
#provisioner ⇒ Provisioner::Base
Returns provisioner object which will provide the setup and invocation instructions for configuration management and other automation tools.
68 69 70 |
# File 'lib/kitchen/instance.rb', line 68 def provisioner @provisioner end |
#suite ⇒ Suite (readonly)
Returns the test suite configuration.
50 51 52 |
# File 'lib/kitchen/instance.rb', line 50 def suite @suite end |
#transport ⇒ Transport::Base
Returns transport object which will communicate with an instance.
72 73 74 |
# File 'lib/kitchen/instance.rb', line 72 def transport @transport end |
#verifier ⇒ Verifier
Returns verifier object for instance to manage the verifier installation on this instance.
76 77 78 |
# File 'lib/kitchen/instance.rb', line 76 def verifier @verifier end |
Class Method Details
.name_for(suite, platform) ⇒ String
Generates a name for an instance given a suite and platform.
44 45 46 |
# File 'lib/kitchen/instance.rb', line 44 def name_for(suite, platform) "#{suite.name}-#{platform.name}".gsub(%r{[_,/]}, "-").delete(".") end |
Instance Method Details
#cleanup! ⇒ void
This method returns an undefined value.
Clean up any per-instance resources before exiting.
338 339 340 |
# File 'lib/kitchen/instance.rb', line 338 def cleanup! @transport.cleanup! if @transport end |
#converge ⇒ self
rescue Driver::ActionFailed and return some kind of null object to gracefully stop action chaining
Converges this running instance.
143 144 145 |
# File 'lib/kitchen/instance.rb', line 143 def converge transition_to(:converge) end |
#create ⇒ self
rescue Driver::ActionFailed and return some kind of null object to gracefully stop action chaining
Creates this instance.
132 133 134 |
# File 'lib/kitchen/instance.rb', line 132 def create transition_to(:create) end |
#current_session_id ⇒ String?
Returns the current instance session identifier, if one has been established.
299 300 301 |
# File 'lib/kitchen/instance.rb', line 299 def current_session_id state_file.read[:instance_session_id] end |
#destroy ⇒ self
rescue Driver::ActionFailed and return some kind of null object to gracefully stop action chaining
Destroys this instance.
176 177 178 |
# File 'lib/kitchen/instance.rb', line 176 def destroy transition_to(:destroy) end |
#diagnose ⇒ Hash
Returns a Hash of configuration and other useful diagnostic information.
253 254 255 256 257 258 259 260 261 262 |
# File 'lib/kitchen/instance.rb', line 253 def diagnose result = {} %i{ platform state_file driver provisioner transport verifier lifecycle_hooks }.each do |sym| obj = send(sym) result[sym] = obj.respond_to?(:diagnose) ? obj.diagnose : :unknown end result end |
#diagnose_plugins ⇒ Hash
Returns a Hash of configuration and other useful diagnostic information associated with plugins (such as loaded version, class name, etc.).
268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/kitchen/instance.rb', line 268 def diagnose_plugins result = {} %i{driver provisioner verifier transport}.each do |sym| obj = send(sym) result[sym] = if obj.respond_to?(:diagnose_plugin) obj.diagnose_plugin else :unknown end end result end |
#doctor_action ⇒ Object
Check system and configuration for common errors.
243 244 245 246 247 248 |
# File 'lib/kitchen/instance.rb', line 243 def doctor_action "The doctor is in" [driver, provisioner, transport, verifier].any? do |obj| obj.doctor(state_file.read) end end |
#last_action ⇒ String
Returns the last successfully completed action state of the instance.
284 285 286 |
# File 'lib/kitchen/instance.rb', line 284 def last_action state_file.read[:last_action] end |
#last_error ⇒ String
Returns the error encountered on the last action on the instance
291 292 293 |
# File 'lib/kitchen/instance.rb', line 291 def last_error state_file.read[:last_error] end |
#log_path ⇒ String?
Returns the path to the text log file for this instance.
306 307 308 |
# File 'lib/kitchen/instance.rb', line 306 def log_path logger.logdev_path end |
#login ⇒ Object
Logs in to this instance by invoking a system command, provided by the instance's transport. This could be an SSH command, telnet, or serial console session.
Note This method calls exec and will not return.
212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/kitchen/instance.rb', line 212 def login state = state_file.read if state[:last_action].nil? raise UserError, "Instance #{to_str} has not yet been created" end lc = transport.connection(state).login_command debug(%{Login command: #{lc.command} #{lc.arguments.join(" ")} } \ "(Options: #{lc.})") Kernel.exec(*lc.exec_args) end |
#package_action ⇒ Object
Perform package.
236 237 238 239 |
# File 'lib/kitchen/instance.rb', line 236 def package_action "Packaging remote instance" driver.package(state_file.read) end |
#remote_exec(command) ⇒ Object
Executes an arbitrary command on this instance.
228 229 230 231 232 |
# File 'lib/kitchen/instance.rb', line 228 def remote_exec(command) transport.connection(state_file.read) do |conn| conn.execute(command) end end |
#setup ⇒ self
rescue Driver::ActionFailed and return some kind of null object to gracefully stop action chaining
Sets up this converged instance for suite tests.
154 155 156 |
# File 'lib/kitchen/instance.rb', line 154 def setup transition_to(:setup) end |
#state_path ⇒ String
Returns the path to the state file for this instance.
320 321 322 |
# File 'lib/kitchen/instance.rb', line 320 def state_path state_file.path end |
#status(probe: false) ⇒ Hash
Returns normalized liveness status for this instance.
328 329 330 331 332 333 |
# File 'lib/kitchen/instance.rb', line 328 def status(probe: false) state = state_file.read result = driver_status(state) result[:transport_probe] = transport_probe(state) if probe result end |
#structured_log_path ⇒ String?
Returns the path to the structured log file for this instance.
313 314 315 |
# File 'lib/kitchen/instance.rb', line 313 def structured_log_path logger.structured_logdev_path end |
#test(destroy_mode = :passing) ⇒ self
rescue Driver::ActionFailed and return some kind of null object to gracefully stop action chaining
Tests this instance by creating, converging and verifying. If this instance is running, it will be pre-emptively destroyed to ensure a clean slate. The instance will be left post-verify in a running state.
190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/kitchen/instance.rb', line 190 def test(destroy_mode = :passing) elapsed = Benchmark.measure do "Cleaning up any prior instances of #{to_str}" destroy "Testing #{to_str}" verify destroy if destroy_mode == :passing end info "Finished testing #{to_str} #{Util.duration(elapsed.real)}." self ensure destroy if destroy_mode == :always end |
#to_str ⇒ String
Returns a displayable representation of the instance.
121 122 123 |
# File 'lib/kitchen/instance.rb', line 121 def to_str "<#{name}>" end |
#verify ⇒ self
rescue Driver::ActionFailed and return some kind of null object to gracefully stop action chaining
Verifies this set up instance by executing suite tests.
165 166 167 |
# File 'lib/kitchen/instance.rb', line 165 def verify transition_to(:verify) end |