Class: Kitchen::Driver::Aws::SsmSessionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/kitchen/driver/aws/ssm_session_manager.rb

Overview

Manages AWS Systems Manager Session Manager connections for Test Kitchen

Instance Method Summary collapse

Constructor Details

#initialize(config, logger) ⇒ SsmSessionManager

Returns a new instance of SsmSessionManager.

Parameters:

  • config (Hash)

    the driver config

  • logger (Kitchen::Logger)

    the logger to report through



28
29
30
31
32
33
34
35
# File 'lib/kitchen/driver/aws/ssm_session_manager.rb', line 28

def initialize(config, logger)
  @config = config
  @logger = logger
  @ssm_client = ::Aws::SSM::Client.new(
    region: config[:region],
    profile: config[:shared_credentials_profile]
  )
end

Instance Method Details

#session_manager_plugin_installed?Boolean

Whether the AWS CLI Session Manager plugin is installed locally.

The plugin is a separate download from the AWS CLI itself and is required to open a session.

Returns:

  • (Boolean)

    true when the plugin responds to --version



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/kitchen/driver/aws/ssm_session_manager.rb', line 79

def session_manager_plugin_installed?
  _output, status = Open3.capture2e("session-manager-plugin", "--version")
  installed = status.success?

  if installed
    @logger.debug("Session Manager plugin is installed")
  else
    @logger.warn("Session Manager plugin is not installed. Install it from: " \
                "https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html")
  end

  installed
# ::StandardError, not StandardError: this file is nested inside
# `module Kitchen`, which defines Kitchen::StandardError. An unqualified
# constant resolves to that one, which would let the Errno::ENOENT
# raised by a missing plugin escape the check meant to detect it.
rescue ::StandardError => e
  @logger.warn("Error checking for session-manager-plugin: #{e.message}")
  false
end

#ssm_agent_available?(instance_id) ⇒ Boolean

Whether the SSM agent on an instance has checked in and is reachable.

Polled while an instance boots, so an API error is reported as "not ready" rather than raised.

Parameters:

  • instance_id (String)

    the instance to check

Returns:

  • (Boolean)

    true when the agent is registered and online



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
# File 'lib/kitchen/driver/aws/ssm_session_manager.rb', line 44

def ssm_agent_available?(instance_id)
  @logger.debug("Checking if SSM agent is available on instance #{instance_id}")

  begin
    resp = @ssm_client.describe_instance_information(
      filters: [
        {
          key: "InstanceIds",
          values: [instance_id],
        },
      ]
    )

    available = !resp.instance_information_list.empty? &&
      resp.instance_information_list.first.ping_status == "Online"

    if available
      @logger.info("SSM agent is available on instance #{instance_id}")
    else
      @logger.warn("SSM agent is not available on instance #{instance_id}")
    end

    available
  rescue ::Aws::SSM::Errors::ServiceError => e
    @logger.warn("Error checking SSM agent status: #{e.message}")
    false
  end
end