Skip to content

Commit

Permalink
Merge pull request test-kitchen#1301 from coderanger/doctor
Browse files Browse the repository at this point in the history
Basic framework for kitchen doctor
  • Loading branch information
coderanger authored Oct 19, 2017
2 parents b26fef8 + 9b75b7a commit 3a8d8fc
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/kitchen/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,16 @@ def package(*args)
perform("package", "package", args)
end

desc "doctor INSTANCE|REGEXP", "Check for common system problems"
log_options
method_option :all,
aliases: "-a",
desc: "Check all instances"
def doctor(*args)
update_config!
perform("doctor", "doctor", args)
end

desc "exec INSTANCE|REGEXP -c REMOTE_COMMAND",
"Execute command on one or more instance"
method_option :command,
Expand Down
40 changes: 40 additions & 0 deletions lib/kitchen/command/doctor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -*- encoding: utf-8 -*-
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require "kitchen/command"

module Kitchen
module Command
# Check for common system or configuration problems.
#
class Doctor < Kitchen::Command::Base
# Invoke the command.
def call
results = parse_subcommand(args.first)
if results.empty?
error("No instances configured, cannot check configuration. Please check your .kitchen.yml and confirm it has platform and suites sections.")
exit(1)
end
# By default only doctor the first instance to avoid output spam.
results = [results.first] unless options[:all]
failed = results.any? do |instance|
debug "Doctor on #{instance.name}."
instance.doctor_action
end
exit(1) if failed
end
end
end
end
8 changes: 8 additions & 0 deletions lib/kitchen/driver/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def destroy(state) # rubocop:disable Lint/UnusedMethodArgument
def package(state) # rubocop:disable Lint/UnusedMethodArgument
end

# Check system and configuration for common errors.
#
# @param state [Hash] mutable instance and driver state
# @returns [Boolean] Return true if a problem is found.
def doctor(state)
false
end

class << self
# @return [Array<Symbol>] an array of action method names that cannot
# be run concurrently and must be run in serial via a shared mutex
Expand Down
9 changes: 9 additions & 0 deletions lib/kitchen/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@ def package_action
driver.package(state_file.read)
end

# Check system and configuration for common errors.
#
def doctor_action
banner "The doctor is in"
[driver, provisioner, transport, verifier].any? do |obj|
obj.doctor(state_file.read)
end
end

# Returns a Hash of configuration and other useful diagnostic information.
#
# @return [Hash] a diagnostic hash
Expand Down
8 changes: 8 additions & 0 deletions lib/kitchen/provisioner/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ def call(state)
cleanup_sandbox
end

# Check system and configuration for common errors.
#
# @param state [Hash] mutable instance state
# @returns [Boolean] Return true if a problem is found.
def doctor(state)
false
end

# Generates a command string which will install and configure the
# provisioner software on an instance. If no work is required, then `nil`
# will be returned.
Expand Down
8 changes: 8 additions & 0 deletions lib/kitchen/transport/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ def connection(state)
raise ClientError, "#{self.class}#connection must be implemented"
end

# Check system and configuration for common errors.
#
# @param state [Hash] mutable instance state
# @returns [Boolean] Return true if a problem is found.
def doctor(state)
false
end

# Closes the connection, if it is still active.
#
# @return [void]
Expand Down
8 changes: 8 additions & 0 deletions lib/kitchen/verifier/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ def call(state)
cleanup_sandbox
end

# Check system and configuration for common errors.
#
# @param state [Hash] mutable instance state
# @returns [Boolean] Return true if a problem is found.
def doctor(state)
false
end

# Deletes the sandbox path. Without calling this method, the sandbox path
# will persist after the process terminates. In other words, cleanup is
# explicit. This method is safe to call multiple times.
Expand Down

0 comments on commit 3a8d8fc

Please sign in to comment.