From 6dde730d29d2ea7e5de0484f4f8207d9a1f56d36 Mon Sep 17 00:00:00 2001 From: Alexander Neishkasha Date: Wed, 15 Jan 2020 22:10:55 +0200 Subject: [PATCH] Refactored, add aws facade --- aws_facade.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 66 ++++------------------------------------------ 2 files changed, 78 insertions(+), 61 deletions(-) create mode 100644 aws_facade.go diff --git a/aws_facade.go b/aws_facade.go new file mode 100644 index 0000000..b3e77c6 --- /dev/null +++ b/aws_facade.go @@ -0,0 +1,73 @@ +package main + +import ( + "errors" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/ec2" + "log" +) + +type AwsFacade struct { + region string + env string + session *session.Session + instances []*ec2.Instance +} + +func (awsFacade *AwsFacade) IP() (ip string, err error) { + awsFacade.initSession() + awsFacade.loadInstances() + for _, instance := range awsFacade.instances { + env := awsFacade.getEnv(instance) + if *instance.State.Code == RunningCode && env != "" { + return *instance.PrivateIpAddress, nil + } + } + return "", errors.New("IP for env '" + awsFacade.env + "' not found") +} + +func (awsFacade *AwsFacade) initSession() { + if awsFacade.session != nil { + return + } + awsSession, err := session.NewSession(&aws.Config{ + Region: aws.String(awsFacade.region), + Credentials: credentials.NewEnvCredentials(), + }) + if err != nil { + log.Fatal(err) + } + awsFacade.session = awsSession +} + +func (awsFacade *AwsFacade) loadInstances() { + ec2Client := ec2.New(awsFacade.session) + req, resp := ec2Client.DescribeInstancesRequest(&ec2.DescribeInstancesInput{}) + if err := req.Send(); err != nil { + log.Println("Error sending request") + log.Fatal(err) + } + for _, reservation := range resp.Reservations { + for _, instance := range reservation.Instances { + awsFacade.instances = append(awsFacade.instances, instance) + } + } +} + +func (awsFacade *AwsFacade) getEnv(instance *ec2.Instance) string { + role, env := false, "" + for _, tag := range instance.Tags { + if *tag.Key == "role" && *tag.Value == "PhpServer" { + role = true + } + if *tag.Key == "env" && *tag.Value == awsFacade.env { + env = *tag.Value + } + } + if role { + return env + } + return "" +} diff --git a/main.go b/main.go index 6cb79b8..9f4d3a0 100644 --- a/main.go +++ b/main.go @@ -2,10 +2,6 @@ package main import ( "errors" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/credentials" - "github.com/aws/aws-sdk-go/aws/session" - "github.com/aws/aws-sdk-go/service/ec2" "github.com/creack/pty" "github.com/joho/godotenv" "golang.org/x/crypto/ssh/terminal" @@ -23,78 +19,26 @@ func main() { if err := godotenv.Load(); err != nil { log.Fatal("Error loading .env file") } - amazonSession := initSession() - resp := loadInstances(amazonSession) - environment := os.Args[1] - ip, err := ip(environment, resp.Reservations) + env := os.Args[1] + awsFacade := AwsFacade{region: os.Getenv("AWS_DEFAULT_REGION"), env: env} + ip, err := awsFacade.IP() if err != nil { log.Fatal(err) } - pathToKey, err := pathToKey(environment) + pathToKey, err := pathToKey(env) if err != nil { log.Fatal(err) } runSsh(ip, pathToKey) } -func initSession() *session.Session { - region := os.Getenv("AWS_DEFAULT_REGION") - sess, err := session.NewSession(&aws.Config{ - Region: aws.String(region), - Credentials: credentials.NewEnvCredentials(), - }) - if err != nil { - log.Println("Error with creating aws session") - log.Fatal(err) - } - return sess -} - -func loadInstances(amazonSession *session.Session) *ec2.DescribeInstancesOutput { - ec2Client := ec2.New(amazonSession) - req, resp := ec2Client.DescribeInstancesRequest(&ec2.DescribeInstancesInput{}) - if err := req.Send(); err != nil { - log.Println("Error sending request") - log.Fatal(err) - } - return resp -} - -func env(environment string, instance *ec2.Instance) string { - role, env := false, "" - for _, tag := range instance.Tags { - if *tag.Key == "role" && *tag.Value == "PhpServer" { - role = true - } - if *tag.Key == "env" && *tag.Value == environment { - env = *tag.Value - } - } - if role { - return env - } - return "" -} - -func ip(environment string, reservations []*ec2.Reservation) (ip string, err error) { - for _, reservation := range reservations { - for _, instance := range reservation.Instances { - env := env(environment, instance) - if *instance.State.Code == RunningCode && env != "" { - return *instance.PrivateIpAddress, nil - } - } - } - return "", errors.New("IP for env '"+environment+"' not found") -} - func pathToKey(environment string) (key string, err error) { if environment == "stg" { return os.Getenv("STAGE_KEY"), nil } else if environment == "prod" { return os.Getenv("PROD_KEY"), nil } else { - return "", errors.New("Key for env '"+environment+"' not found") + return "", errors.New("Key for env '" + environment + "' not found") } }