Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

26 implement aws ssm connection type #31

Merged
merged 26 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e2bce47
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Nov 28, 2023
7b05f6d
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Dec 5, 2023
81cb271
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Dec 5, 2023
3c0d3b4
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Dec 5, 2023
7f88afd
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Dec 14, 2023
92bc821
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Dec 15, 2023
6c418eb
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 11, 2024
681999e
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 18, 2024
9de5484
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 19, 2024
6b05fe7
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 23, 2024
b4d8d3b
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 24, 2024
84a7b50
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 24, 2024
091eae3
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 24, 2024
0fdc56c
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 24, 2024
c5ea9a4
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 24, 2024
8669a9f
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 25, 2024
e41d5d6
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 25, 2024
96d905a
Default region + error handling
krystian-panek-vmltech Jan 26, 2024
c5dcc23
Defaults upgrade
krystian-panek-vmltech Jan 26, 2024
62500c7
Private fields
krystian-panek-vmltech Jan 26, 2024
8df5428
Minor imprs
krystian-panek-vmltech Jan 26, 2024
4340256
Deps upgrade
krystian-panek-vmltech Jan 26, 2024
2d1fbf5
Performance improvement
dominik-przybyl-wttech Jan 26, 2024
d4043d4
SSM works like a charm ;)
krystian-panek-vmltech Jan 29, 2024
9f3b271
Timeouts configurable
krystian-panek-vmltech Jan 29, 2024
5f60ea8
Done threshold
krystian-panek-vmltech Jan 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/aws_ssh/aws.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ resource "aws_iam_instance_profile" "aem_ec2" {
}

resource "aws_iam_role" "aem_ec2" {
name = "${local.workspace}_aem_ec2"
name = "${local.workspace}_aem_ec2"
assume_role_policy = trimspace(<<EOF
{
"Version": "2012-10-17",
Expand All @@ -49,7 +49,7 @@ resource "aws_iam_role" "aem_ec2" {
}
EOF
)
tags = local.tags
tags = local.tags
}

resource "aws_iam_role_policy_attachment" "s3" {
Expand Down
13 changes: 8 additions & 5 deletions examples/aws_ssm/aem.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ resource "aem_instance" "single" {
depends_on = [aws_instance.aem_single, aws_volume_attachment.aem_single_data]

client {
type = "aws_ssm"
type = "aws-ssm"
settings = {
instance_id = aws_instance.aem_single.id
region = "eu-central-1" // TODO infer from AWS provider config
Expand All @@ -17,21 +17,17 @@ resource "aem_instance" "single" {
"sudo mkfs -t ext4 ${local.aem_single_data_device}",
"sudo mkdir -p ${local.aem_single_data_dir}",
"sudo mount ${local.aem_single_data_device} ${local.aem_single_data_dir}",
"sudo chown -R ${local.ssh_user} ${local.aem_single_data_dir}",
"echo '${local.aem_single_data_device} ${local.aem_single_data_dir} ext4 defaults 0 0' | sudo tee -a /etc/fstab",
// installing AWS CLI: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
"sudo yum install -y unzip",
"curl 'https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip' -o 'awscliv2.zip'",
"unzip -q awscliv2.zip",
"sudo ./aws/install --update",
// installing AWS SSM agent: https://docs.aws.amazon.com/systems-manager/latest/userguide/agent-install-rhel-8-9.html
"sudo dnf install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm",
]
}
}

compose {
config = file("aem.yml") // use templating here if needed: https://developer.hashicorp.com/terraform/language/functions/templatefile
create = {
inline = [
"mkdir -p '${local.aem_single_compose_dir}/aem/home/lib'",
Expand All @@ -40,6 +36,13 @@ resource "aem_instance" "single" {
"sh aemw instance create",
]
}
configure = {
inline = [
"sh aemw osgi config save --pid 'org.apache.sling.jcr.davex.impl.servlets.SlingDavExServlet' --input-string 'alias: /crx/server'",
"sh aemw repl agent setup -A --location 'author' --name 'publish' --input-string '{enabled: true, transportUri: \"http://localhost:4503/bin/receive?sling:authRequestLogin=1\", transportUser: admin, transportPassword: admin, userId: admin}'",
"sh aemw package deploy --file 'aem/home/lib/aem-service-pkg-6.5.*.0.zip'",
]
}
}
}

Expand Down
9 changes: 7 additions & 2 deletions examples/aws_ssm/aws.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ resource "aws_instance" "aem_single" {
instance_type = "m5.xlarge"
iam_instance_profile = aws_iam_instance_profile.aem_ec2.name
tags = local.tags
user_data = trimspace(<<EOF
#!/bin/bash
sudo dnf install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
EOF
)
}

resource "aws_ebs_volume" "aem_single_data" {
Expand All @@ -25,7 +30,7 @@ resource "aws_iam_instance_profile" "aem_ec2" {
}

resource "aws_iam_role" "aem_ec2" {
name = "${local.workspace}_aem_ec2"
name = "${local.workspace}_aem_ec2"
assume_role_policy = trimspace(<<EOF
{
"Version": "2012-10-17",
Expand All @@ -37,7 +42,7 @@ resource "aws_iam_role" "aem_ec2" {
}
EOF
)
tags = local.tags
tags = local.tags
}

resource "aws_iam_role_policy_attachment" "ssm" {
Expand Down
3 changes: 0 additions & 3 deletions examples/aws_ssm/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ locals {
env_type = "aem-single"
host = "aem_single"

ssh_user = "ec2-user"
ssh_private_key = abspath("ec2-key.cer")

tags = {
Workspace = "aemc"
Env = "tf-minimal"
Expand Down
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module github.com/wttech/terraform-provider-aem
go 1.19

require (
github.com/aws/aws-sdk-go-v2 v1.24.1
github.com/aws/aws-sdk-go-v2/config v1.26.6
github.com/aws/aws-sdk-go-v2/service/ssm v1.44.7
github.com/hashicorp/terraform-plugin-docs v0.16.0
github.com/hashicorp/terraform-plugin-framework v1.4.2
github.com/hashicorp/terraform-plugin-go v0.19.0
Expand All @@ -21,6 +24,17 @@ require (
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.16.16 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.11 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.10 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.10 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.3 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.4 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.10 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.18.7 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.7 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.26.7 // indirect
github.com/aws/smithy-go v1.19.0 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/fatih/color v1.16.0 // indirect
Expand All @@ -42,6 +56,7 @@ require (
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
34 changes: 34 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,34 @@ github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkE
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/aws/aws-sdk-go-v2 v1.24.1 h1:xAojnj+ktS95YZlDf0zxWBkbFtymPeDP+rvUQIH3uAU=
github.com/aws/aws-sdk-go-v2 v1.24.1/go.mod h1:LNh45Br1YAkEKaAqvmE1m8FUx6a5b/V0oAKV7of29b4=
github.com/aws/aws-sdk-go-v2/config v1.26.6 h1:Z/7w9bUqlRI0FFQpetVuFYEsjzE3h7fpU6HuGmfPL/o=
github.com/aws/aws-sdk-go-v2/config v1.26.6/go.mod h1:uKU6cnDmYCvJ+pxO9S4cWDb2yWWIH5hra+32hVh1MI4=
github.com/aws/aws-sdk-go-v2/credentials v1.16.16 h1:8q6Rliyv0aUFAVtzaldUEcS+T5gbadPbWdV1WcAddK8=
github.com/aws/aws-sdk-go-v2/credentials v1.16.16/go.mod h1:UHVZrdUsv63hPXFo1H7c5fEneoVo9UXiz36QG1GEPi0=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.11 h1:c5I5iH+DZcH3xOIMlz3/tCKJDaHFwYEmxvlh2fAcFo8=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.11/go.mod h1:cRrYDYAMUohBJUtUnOhydaMHtiK/1NZ0Otc9lIb6O0Y=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.10 h1:vF+Zgd9s+H4vOXd5BMaPWykta2a6Ih0AKLq/X6NYKn4=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.10/go.mod h1:6BkRjejp/GR4411UGqkX8+wFMbFbqsUIimfK4XjOKR4=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.10 h1:nYPe006ktcqUji8S2mqXf9c/7NdiKriOwMvWQHgYztw=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.10/go.mod h1:6UV4SZkVvmODfXKql4LCbaZUpF7HO2BX38FgBf9ZOLw=
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.3 h1:n3GDfwqF2tzEkXlv5cuy4iy7LpKDtqDMcNLfZDu9rls=
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.3/go.mod h1:6fQQgfuGmw8Al/3M2IgIllycxV7ZW7WCdVSqfBeUiCY=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.4 h1:/b31bi3YVNlkzkBrm9LfpaKoaYZUxIAj4sHfOTmLfqw=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.4/go.mod h1:2aGXHFmbInwgP9ZfpmdIfOELL79zhdNYNmReK8qDfdQ=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.10 h1:DBYTXwIGQSGs9w4jKm60F5dmCQ3EEruxdc0MFh+3EY4=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.10/go.mod h1:wohMUQiFdzo0NtxbBg0mSRGZ4vL3n0dKjLTINdcIino=
github.com/aws/aws-sdk-go-v2/service/ssm v1.44.7 h1:a8HvP/+ew3tKwSXqL3BCSjiuicr+XTU2eFYeogV9GJE=
github.com/aws/aws-sdk-go-v2/service/ssm v1.44.7/go.mod h1:Q7XIWsMo0JcMpI/6TGD6XXcXcV1DbTj6e9BKNntIMIM=
github.com/aws/aws-sdk-go-v2/service/sso v1.18.7 h1:eajuO3nykDPdYicLlP3AGgOyVN3MOlFmZv7WGTuJPow=
github.com/aws/aws-sdk-go-v2/service/sso v1.18.7/go.mod h1:+mJNDdF+qiUlNKNC3fxn74WWNN+sOiGOEImje+3ScPM=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.7 h1:QPMJf+Jw8E1l7zqhZmMlFw6w1NmfkfiSK8mS4zOx3BA=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.7/go.mod h1:ykf3COxYI0UJmxcfcxcVuz7b6uADi1FkiUz6Eb7AgM8=
github.com/aws/aws-sdk-go-v2/service/sts v1.26.7 h1:NzO4Vrau795RkUdSHKEwiR01FaGzGOH1EETJ+5QHnm0=
github.com/aws/aws-sdk-go-v2/service/sts v1.26.7/go.mod h1:6h2YuIoxaMSCFf5fi1EgZAwdfkGMgDY+DVfa61uLe4U=
github.com/aws/smithy-go v1.19.0 h1:KWFKQV80DpP3vJrrA9sVAHQ5gc2z8i4EzrLhLlWXcBM=
github.com/aws/smithy-go v1.19.0/go.mod h1:NukqUGpCZIILqqiV0NIjeFh24kd/FAa4beRb6nbIUPE=
github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQkY=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
Expand Down Expand Up @@ -91,6 +119,10 @@ github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jhump/protoreflect v1.15.1 h1:HUMERORf3I3ZdX05WaQ6MIpd/NJ434hTp5YiKgfCL6c=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
Expand Down Expand Up @@ -237,6 +269,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
12 changes: 2 additions & 10 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client
import (
"context"
"fmt"
"github.com/melbahja/goph"
"github.com/wttech/terraform-provider-aem/internal/utils"
"os"
"path/filepath"
Expand Down Expand Up @@ -68,7 +67,7 @@ func (c Client) Connection() Connection {
return c.connection
}

func (c Client) Command(cmdLine []string) (*goph.Cmd, error) {
func (c Client) Command(cmdLine []string) ([]byte, error) {
return c.connection.Command(cmdLine)
}

Expand Down Expand Up @@ -110,17 +109,10 @@ func (c Client) RunShellPurely(cmd string) ([]byte, error) {
} else {
cmdLine = []string{"sh", "-c", "\"" + cmd + "\""}
}
cmdObj, err := c.connection.Command(cmdLine)
out, err := c.connection.Command(cmdLine)
if err != nil {
return nil, fmt.Errorf("cannot create command '%s': %w", cmd, err)
}
out, err := cmdObj.CombinedOutput()
if err != nil {
if len(out) > 0 {
return nil, fmt.Errorf("cannot run command '%s': %w\n\n%s", cmdObj, err, string(out))
}
return nil, err
}
return out, nil
}

Expand Down
6 changes: 1 addition & 5 deletions internal/client/connection.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package client

import (
"github.com/melbahja/goph"
)

type Connection interface {
Info() string
User() string
Connect() error
Disconnect() error
Command(cmdLine []string) (*goph.Cmd, error)
Command(cmdLine []string) ([]byte, error)
CopyFile(localPath string, remotePath string) error
}
120 changes: 101 additions & 19 deletions internal/client/connection_aws_ssm.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,120 @@
package client

import "github.com/melbahja/goph"
import (
"context"
"encoding/base64"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"os"
"strings"
"time"
)

type AWSSSMConnection struct {
InstanceID string
Region string
Client *ssm.Client
SessionId *string
}

func (A AWSSSMConnection) Info() string {
//TODO implement me
panic("implement me")
func (a *AWSSSMConnection) Info() string {
return fmt.Sprintf("ssm: instance='%s', region='%s'", a.InstanceID, a.Region)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instance_id

}

func (A AWSSSMConnection) User() string {
//TODO implement me
panic("implement me")
func (a *AWSSSMConnection) User() string {
return "root"
}

func (A AWSSSMConnection) Connect() error {
//TODO implement me
panic("implement me")
func (a *AWSSSMConnection) Connect() error {
// Specify the AWS region
cfg, err := config.LoadDefaultConfig(context.Background(), config.WithRegion(a.Region))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whole connection should use same context

if err != nil {
return err
}

// Create an SSM client
client := ssm.NewFromConfig(cfg)
if err != nil {
return fmt.Errorf("ssm: error creating AWS session: %v", err)
}

startSessionInput := &ssm.StartSessionInput{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 lines to 1; fmt

Target: aws.String(a.InstanceID),
}

startSessionOutput, err := client.StartSession(context.Background(), startSessionInput)
if err != nil {
return fmt.Errorf("ssm: error starting session: %v", err)
}

a.Client = client
a.SessionId = startSessionOutput.SessionId

return nil
}

func (A AWSSSMConnection) Disconnect() error {
//TODO implement me
panic("implement me")
func (a *AWSSSMConnection) Disconnect() error {
// Disconnect from the session
terminateSessionInput := &ssm.TerminateSessionInput{
SessionId: a.SessionId,
}

_, err := a.Client.TerminateSession(context.Background(), terminateSessionInput)
if err != nil {
return fmt.Errorf("ssm: error terminating session: %v", err)
}

return nil
}

func (A AWSSSMConnection) Command(cmdLine []string) (*goph.Cmd, error) {
//TODO implement me
panic("implement me")
func (a *AWSSSMConnection) Command(cmdLine []string) ([]byte, error) {
// Execute command on the remote instance
command := strings.Join(cmdLine, " ")
runCommandInput := &ssm.SendCommandInput{
DocumentName: aws.String("AWS-RunShellScript"),
InstanceIds: []string{a.InstanceID},
Parameters: map[string][]string{
"commands": {command},
},
}

runCommandOutput, err := a.Client.SendCommand(context.Background(), runCommandInput)
if err != nil {
return nil, fmt.Errorf("ssm: error executing command: %v", err)
}

commandId := runCommandOutput.Command.CommandId

commandInvocationInput := &ssm.GetCommandInvocationInput{
CommandId: commandId,
InstanceId: aws.String(a.InstanceID),
}

waiter := ssm.NewCommandExecutedWaiter(a.Client)
_, err = waiter.WaitForOutput(context.Background(), commandInvocationInput, time.Hour)
if err != nil {
return nil, fmt.Errorf("ssm: error executing command: %v", err)
}

getCommandOutput, err := a.Client.GetCommandInvocation(context.Background(), commandInvocationInput)
if err != nil {
return nil, fmt.Errorf("ssm: error executing command: %v", err)
}

return []byte(*getCommandOutput.StandardOutputContent), nil
}

func (A AWSSSMConnection) CopyFile(localPath string, remotePath string) error {
//TODO implement me
panic("implement me")
func (a *AWSSSMConnection) CopyFile(localPath string, remotePath string) error {
fileContent, err := os.ReadFile(localPath)
if err != nil {
return fmt.Errorf("ssm: error reading local file: %v", err)
}
encodedContent := base64.StdEncoding.EncodeToString(fileContent)

cmd := fmt.Sprintf("echo -n %s | base64 -d > %s", encodedContent, remotePath)
cmdLine := []string{"sh", "-c", "\"" + cmd + "\""}
_, err = a.Command(cmdLine)
return err
}
Loading
Loading