-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
ssh
command in beanstalk apps (#179)
- Loading branch information
Showing
12 changed files
with
143 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package beanstalk | ||
|
||
import ( | ||
"context" | ||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/elasticbeanstalk" | ||
) | ||
|
||
func GetInstances(ctx context.Context, infra Outputs) ([]string, error) { | ||
client := elasticbeanstalk.NewFromConfig(infra.AdminerConfig()) | ||
out, err := client.DescribeEnvironmentResources(ctx, &elasticbeanstalk.DescribeEnvironmentResourcesInput{ | ||
EnvironmentId: aws.String(infra.EnvironmentId), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
instanceIds := make([]string, 0) | ||
for _, instance := range out.EnvironmentResources.Instances { | ||
instanceIds = append(instanceIds, *instance.Id) | ||
} | ||
return instanceIds, nil | ||
} | ||
|
||
func GetRandomInstance(ctx context.Context, infra Outputs) (string, error) { | ||
instanceIds, err := GetInstances(ctx, infra) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
for _, instanceId := range instanceIds { | ||
return instanceId, nil | ||
} | ||
return "", nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package beanstalk | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go-v2/aws" | ||
nsaws "github.com/nullstone-io/deployment-sdk/aws" | ||
) | ||
|
||
type Outputs struct { | ||
Region string `ns:"region"` | ||
BeanstalkArn string `ns:"beanstalk_arn"` | ||
EnvironmentId string `ns:"environment_id"` | ||
Adminer nsaws.User `ns:"adminer,optional"` | ||
} | ||
|
||
func (o Outputs) AdminerConfig() aws.Config { | ||
return nsaws.NewConfig(o.Adminer, o.Region) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package beanstalk | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/nullstone-io/deployment-sdk/app" | ||
"github.com/nullstone-io/deployment-sdk/logging" | ||
"github.com/nullstone-io/deployment-sdk/outputs" | ||
"gopkg.in/nullstone-io/go-api-client.v0" | ||
"gopkg.in/nullstone-io/nullstone.v0/admin" | ||
"gopkg.in/nullstone-io/nullstone.v0/aws/ssm" | ||
) | ||
|
||
func NewRemoter(osWriters logging.OsWriters, nsConfig api.Config, appDetails app.Details) (admin.Remoter, error) { | ||
outs, err := outputs.Retrieve[Outputs](nsConfig, appDetails.Workspace) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return Remoter{ | ||
OsWriters: osWriters, | ||
Details: appDetails, | ||
Infra: outs, | ||
}, nil | ||
} | ||
|
||
type Remoter struct { | ||
OsWriters logging.OsWriters | ||
Details app.Details | ||
Infra Outputs | ||
} | ||
|
||
func (r Remoter) Exec(ctx context.Context, options admin.RemoteOptions, cmd []string) error { | ||
// TODO: Add support for cmd | ||
instanceId, err := r.getInstanceId(ctx, options) | ||
if err != nil { | ||
return err | ||
} | ||
return ssm.StartEc2Session(ctx, r.Infra.AdminerConfig(), r.Infra.Region, instanceId, nil) | ||
} | ||
|
||
func (r Remoter) Ssh(ctx context.Context, options admin.RemoteOptions) error { | ||
parameters, err := ssm.SessionParametersFromPortForwards(options.PortForwards) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
instanceId, err := r.getInstanceId(ctx, options) | ||
if err != nil { | ||
return err | ||
} | ||
return ssm.StartEc2Session(ctx, r.Infra.AdminerConfig(), r.Infra.Region, instanceId, parameters) | ||
} | ||
|
||
func (r Remoter) getInstanceId(ctx context.Context, options admin.RemoteOptions) (string, error) { | ||
if options.Instance == "" { | ||
if instanceId, err := GetRandomInstance(ctx, r.Infra); err != nil { | ||
return "", err | ||
} else if instanceId == "" { | ||
return "", fmt.Errorf("cannot exec command with no running instances") | ||
} else { | ||
return instanceId, nil | ||
} | ||
} | ||
return options.Instance, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters