|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { |
| 7 | + SSM, |
| 8 | + SSMClient, |
| 9 | + Session, |
| 10 | + StartSessionCommand, |
| 11 | + TerminateSessionCommand, |
| 12 | + TerminateSessionResponse, |
| 13 | + StartSessionCommandOutput, |
| 14 | + DescribeInstanceInformationCommand, |
| 15 | + DescribeInstanceInformationCommandInput, |
| 16 | + InstanceInformation, |
| 17 | + SendCommandCommand, |
| 18 | + SendCommandCommandOutput, |
| 19 | + waitUntilCommandExecuted, |
| 20 | + SessionState, |
| 21 | + DescribeSessionsCommand, |
| 22 | +} from '@aws-sdk/client-ssm' |
| 23 | +import { WaiterState } from '@smithy/util-waiter' |
| 24 | +import { ToolkitError } from '../errors' |
| 25 | +import { ClientWrapper } from './wrapper' |
| 26 | + |
| 27 | +export class SSMWrapper extends ClientWrapper<SSM> { |
| 28 | + public constructor(public override readonly regionCode: string) { |
| 29 | + super(regionCode, SSMClient) |
| 30 | + } |
| 31 | + |
| 32 | + public async terminateSession(session: Session): Promise<TerminateSessionResponse> { |
| 33 | + const sessionId = session.SessionId! |
| 34 | + return await this.terminateSessionFromId(sessionId) |
| 35 | + } |
| 36 | + |
| 37 | + public async terminateSessionFromId(sessionId: string): Promise<TerminateSessionResponse> { |
| 38 | + return await this.makeRequest(TerminateSessionCommand, { SessionId: sessionId }) |
| 39 | + } |
| 40 | + |
| 41 | + public async startSession( |
| 42 | + target: string, |
| 43 | + document?: string, |
| 44 | + reason?: string, |
| 45 | + parameters?: Record<string, string[]> |
| 46 | + ): Promise<StartSessionCommandOutput> { |
| 47 | + return await this.makeRequest(StartSessionCommand, { |
| 48 | + Target: target, |
| 49 | + DocumentName: document, |
| 50 | + Reason: reason, |
| 51 | + Parameters: parameters, |
| 52 | + }) |
| 53 | + } |
| 54 | + |
| 55 | + public async describeInstance(target: string): Promise<InstanceInformation> { |
| 56 | + const response2 = this.makePaginatedRequest( |
| 57 | + DescribeInstanceInformationCommand, |
| 58 | + { |
| 59 | + InstanceInformationFilterList: [ |
| 60 | + { |
| 61 | + key: 'InstanceIds', |
| 62 | + valueSet: [target], |
| 63 | + }, |
| 64 | + ], |
| 65 | + } as DescribeInstanceInformationCommandInput, |
| 66 | + 'InstanceIds' |
| 67 | + ) |
| 68 | + const resolvedResponse = await response2.flatten().flatten().promise() |
| 69 | + return resolvedResponse[0]! |
| 70 | + } |
| 71 | + |
| 72 | + public async getTargetPlatformName(target: string): Promise<string> { |
| 73 | + const instanceInformation = await this.describeInstance(target) |
| 74 | + return instanceInformation.PlatformName! |
| 75 | + } |
| 76 | + |
| 77 | + public async sendCommand( |
| 78 | + target: string, |
| 79 | + documentName: string, |
| 80 | + parameters: Record<string, string[]> |
| 81 | + ): Promise<SendCommandCommandOutput> { |
| 82 | + return await this.makeRequest(SendCommandCommand, { |
| 83 | + InstanceIds: [target], |
| 84 | + DocumentName: documentName, |
| 85 | + Parameters: parameters, |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + private async waitUntilCommandExecuted(commandId: string, target: string) { |
| 90 | + const result = await waitUntilCommandExecuted( |
| 91 | + { client: await this.getClient(), maxWaitTime: 30 }, |
| 92 | + { CommandId: commandId, InstanceId: target } |
| 93 | + ) |
| 94 | + if (result.state !== WaiterState.SUCCESS) { |
| 95 | + throw new ToolkitError(`Command ${commandId} failed to execute on target ${target}`) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public async sendCommandAndWait( |
| 100 | + target: string, |
| 101 | + documentName: string, |
| 102 | + parameters: Record<string, string[]> |
| 103 | + ): Promise<SendCommandCommandOutput> { |
| 104 | + const response = await this.sendCommand(target, documentName, parameters) |
| 105 | + try { |
| 106 | + await this.waitUntilCommandExecuted(response.Command!.CommandId!, target) |
| 107 | + return response |
| 108 | + } catch (err) { |
| 109 | + throw new ToolkitError(`Failed in sending command to target ${target}`, { cause: err as Error }) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public async getInstanceAgentPingStatus(target: string): Promise<string> { |
| 114 | + const instanceInformation = await this.describeInstance(target) |
| 115 | + return instanceInformation ? instanceInformation.PingStatus! : 'Inactive' |
| 116 | + } |
| 117 | + |
| 118 | + public async describeSessions(state: SessionState) { |
| 119 | + return await this.makePaginatedRequest(DescribeSessionsCommand, { State: state }, 'Sessions').promise() |
| 120 | + } |
| 121 | +} |
0 commit comments