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.



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

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

Verify that the AWS CLI session manager plugin is installed

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/kitchen/driver/aws/ssm_session_manager.rb', line 66

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
rescue StandardError => e
  @logger.warn("Error checking for session-manager-plugin: #{e.message}")
  false
end

#ssm_agent_available?(instance_id) ⇒ Boolean

Check if SSM agent is running on the instance

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kitchen/driver/aws/ssm_session_manager.rb', line 36

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