From 115a751850c0cd14104baf8f4fd2c0ffcaf3eb6c Mon Sep 17 00:00:00 2001 From: schulze Date: Wed, 15 Mar 2023 15:30:29 +0100 Subject: [PATCH 01/13] aws instance(s) sdk, config loading, list instance, gitignore --- .gitignore | 3 +++ config.yaml | 6 +++++ go.mod | 22 +++++++++++++-- go.sum | 63 ++++++++++++++++++++++--------------------- main.go | 36 ++++++++++++++++++++++++- mtd_aws/sdk.go | 72 ++++++++++++++++++++++++++++++++++++++++++++----- state/config.go | 30 +++++++++++++++++++++ 7 files changed, 192 insertions(+), 40 deletions(-) create mode 100644 config.yaml create mode 100644 state/config.go diff --git a/.gitignore b/.gitignore index 66fd13c..5115415 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ # Dependency directories (remove the comment below to include it) # vendor/ + +#protect secrets +*.env diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..b2fc771 --- /dev/null +++ b/config.yaml @@ -0,0 +1,6 @@ +aws: + regions: + - us-east-1 + - us-east-2 + - us-west-1 + diff --git a/go.mod b/go.mod index 3bfdfd1..6e3d0bd 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,24 @@ module github.com/thefeli73/polemos go 1.20 -require github.com/aws/aws-sdk-go v1.44.220 +require ( + github.com/aws/aws-sdk-go-v2 v1.17.6 + github.com/aws/aws-sdk-go-v2/config v1.18.17 + github.com/aws/aws-sdk-go-v2/service/ec2 v1.90.0 + github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d + gopkg.in/yaml.v3 v3.0.1 +) -require github.com/jmespath/go-jmespath v0.4.0 // indirect +require ( + github.com/aws/aws-sdk-go-v2/credentials v1.13.17 // indirect + github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.0 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.30 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.24 // indirect + github.com/aws/aws-sdk-go-v2/internal/ini v1.3.31 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.24 // indirect + github.com/aws/aws-sdk-go-v2/service/sso v1.12.5 // indirect + github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.5 // indirect + github.com/aws/aws-sdk-go-v2/service/sts v1.18.6 // indirect + github.com/aws/smithy-go v1.13.5 // indirect + github.com/jmespath/go-jmespath v0.4.0 // indirect +) diff --git a/go.sum b/go.sum index 37a5982..41b4534 100644 --- a/go.sum +++ b/go.sum @@ -1,42 +1,45 @@ -github.com/aws/aws-sdk-go v1.44.220 h1:yAj99qAt0Htjle9Up3DglgHfOP77lmFPrElA4jKnrBo= -github.com/aws/aws-sdk-go v1.44.220/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= +github.com/aws/aws-sdk-go-v2 v1.17.6 h1:Y773UK7OBqhzi5VDXMi1zVGsoj+CVHs2eaC2bDsLwi0= +github.com/aws/aws-sdk-go-v2 v1.17.6/go.mod h1:uzbQtefpm44goOPmdKyAlXSNcwlRgF3ePWVW6EtJvvw= +github.com/aws/aws-sdk-go-v2/config v1.18.17 h1:jwTkhULSrbr/SQA8tfdYqZxpG8YsRycmIXxJcbrqY5E= +github.com/aws/aws-sdk-go-v2/config v1.18.17/go.mod h1:Lj3E7XcxJnxMa+AYo89YiL68s1cFJRGduChynYU67VA= +github.com/aws/aws-sdk-go-v2/credentials v1.13.17 h1:IubQO/RNeIVKF5Jy77w/LfUvmmCxTnk2TP1UZZIMiF4= +github.com/aws/aws-sdk-go-v2/credentials v1.13.17/go.mod h1:K9xeFo1g/YPMguMUD69YpwB4Nyi6W/5wn706xIInJFg= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.0 h1:/2Cb3SK3xVOQA7Xfr5nCWCo5H3UiNINtsVvVdk8sQqA= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.0/go.mod h1:neYVaeKr5eT7BzwULuG2YbLhzWZ22lpjKdCybR7AXrQ= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.30 h1:y+8n9AGDjikyXoMBTRaHHHSaFEB8267ykmvyPodJfys= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.30/go.mod h1:LUBAO3zNXQjoONBKn/kR1y0Q4cj/D02Ts0uHYjcCQLM= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.24 h1:r+Kv+SEJquhAZXaJ7G4u44cIwXV3f8K+N482NNAzJZA= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.24/go.mod h1:gAuCezX/gob6BSMbItsSlMb6WZGV7K2+fWOvk8xBSto= +github.com/aws/aws-sdk-go-v2/internal/ini v1.3.31 h1:hf+Vhp5WtTdcSdE+yEcUz8L73sAzN0R+0jQv+Z51/mI= +github.com/aws/aws-sdk-go-v2/internal/ini v1.3.31/go.mod h1:5zUjguZfG5qjhG9/wqmuyHRyUftl2B5Cp6NNxNC6kRA= +github.com/aws/aws-sdk-go-v2/service/ec2 v1.90.0 h1:oRl2nzkuU/qMPvudU3qQ+GUAMV5POP3V/aJTJ7Q0lT0= +github.com/aws/aws-sdk-go-v2/service/ec2 v1.90.0/go.mod h1:zDr1uSSLVYc6KqXvrmqYkeqnfbmOOrbVloz4Eqsc83k= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.24 h1:c5qGfdbCHav6viBwiyDns3OXqhqAbGjfIB4uVu2ayhk= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.24/go.mod h1:HMA4FZG6fyib+NDo5bpIxX1EhYjrAOveZJY2YR0xrNE= +github.com/aws/aws-sdk-go-v2/service/sso v1.12.5 h1:bdKIX6SVF3nc3xJFw6Nf0igzS6Ff/louGq8Z6VP/3Hs= +github.com/aws/aws-sdk-go-v2/service/sso v1.12.5/go.mod h1:vuWiaDB30M/QTC+lI3Wj6S/zb7tpUK2MSYgy3Guh2L0= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.5 h1:xLPZMyuZ4GuqRCIec/zWuIhRFPXh2UOJdLXBSi64ZWQ= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.5/go.mod h1:QjxpHmCwAg0ESGtPQnLIVp7SedTOBMYy+Slr3IfMKeI= +github.com/aws/aws-sdk-go-v2/service/sts v1.18.6 h1:rIFn5J3yDoeuKCE9sESXqM5POTAhOP1du3bv/qTL+tE= +github.com/aws/aws-sdk-go-v2/service/sts v1.18.6/go.mod h1:48WJ9l3dwP0GSHWGc5sFGGlCkuA82Mc2xnw+T6Q8aDw= +github.com/aws/smithy-go v1.13.5 h1:hgz0X/DX0dGqTYpGALqXJoRKRj5oQ7150i5FdTePzO8= +github.com/aws/smithy-go v1.13.5/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= +github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 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/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ= +github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 89fb2df..c128074 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,42 @@ package main import ( + "fmt" + "os" + + uuid "github.com/nu7hatch/gouuid" "github.com/thefeli73/polemos/mtd_aws" + "github.com/thefeli73/polemos/state" ) func main() { - var _ = mtd_aws.Session() + fmt.Println("Starting Polemos") + + config, err := state.Load_conf("config.yaml") + if err != nil { + fmt.Println("Error loading config:", err) + os.Exit(1) + } + + //aws_config := mtd_aws.New_config(config.AWS.Region) + //mtd_aws.Instance_info(aws_config, config.AWS.InstanceID) + //mtd_aws.Instances(config.AWS.Region) + u, _ := uuid.NewV4() //blank is to send errors to the void + _=u + //fmt.Println(u) + + + for _, region := range config.AWS.Regions { + fmt.Println("Listing instances in region:", region) + aws_config := mtd_aws.New_config(region) + instances, err := mtd_aws.Instances(aws_config) + if err != nil { + fmt.Println("Error listing instances:", err) + continue + } + + for _, instance := range instances { + mtd_aws.Instance_info(aws_config, *instance.InstanceId) + } + } + } \ No newline at end of file diff --git a/mtd_aws/sdk.go b/mtd_aws/sdk.go index 8a5e883..8ceb276 100644 --- a/mtd_aws/sdk.go +++ b/mtd_aws/sdk.go @@ -1,12 +1,70 @@ package mtd_aws import ( - //"github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/session" + "context" + "fmt" + "os" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/service/ec2" + "github.com/aws/aws-sdk-go-v2/service/ec2/types" ) -func Session() *session.Session { - session := session.Must(session.NewSessionWithOptions(session.Options{ - SharedConfigState: session.SharedConfigEnable, - })) - return session +func New_config(region string) aws.Config { + // Create a new AWS config + cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region)) + if err != nil { + fmt.Println("Error creating config:", err) + fmt.Println("Configure Credentials in line with the documentation found here: https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/#specifying-credentials") + os.Exit(1) + } + return cfg +} +func Instance_info(config aws.Config, instanceID string) { + // Create a new EC2 service client + svc := ec2.NewFromConfig(config) + + input := &ec2.DescribeInstancesInput{ + InstanceIds: []string{instanceID}, + } + result, err := svc.DescribeInstances(context.TODO(), input) + if err != nil { + fmt.Println("Error describing instance:", err) + return + } + // Print instance information + instance := result.Reservations[0].Instances[0] + fmt.Println("Instance ID:", aws.ToString(instance.InstanceId)) + fmt.Println("Instance Type:", string(instance.InstanceType)) + fmt.Println("AMI ID:", aws.ToString(instance.ImageId)) + fmt.Println("State:", string(instance.State.Name)) + fmt.Println("Availability Zone:", aws.ToString(instance.Placement.AvailabilityZone)) + if instance.PublicIpAddress != nil { + fmt.Println("Public IP Address:", aws.ToString(instance.PublicIpAddress)) + } + fmt.Println("Private IP Address:", aws.ToString(instance.PrivateIpAddress)) +} + +func Instances(config aws.Config) ([]*types.Instance, error) { + svc := ec2.NewFromConfig(config) + + input := &ec2.DescribeInstancesInput{} + var instances []*types.Instance + + paginator := ec2.NewDescribeInstancesPaginator(svc, input) + + for paginator.HasMorePages() { + page, err := paginator.NextPage(context.TODO()) + if err != nil { + return nil, err + } + + for _, reservation := range page.Reservations { + for _, instance := range reservation.Instances { + instances = append(instances, &instance) + } + } + } + + return instances, nil } \ No newline at end of file diff --git a/state/config.go b/state/config.go new file mode 100644 index 0000000..f3b2b48 --- /dev/null +++ b/state/config.go @@ -0,0 +1,30 @@ +package state + +import ( + "io/ioutil" + + "gopkg.in/yaml.v3" +) + + +type Config struct { + AWS struct { + Regions []string `yaml:"regions"` + } `yaml:"aws"` +} + +func Load_conf(filename string) (Config, error) { + var config Config + + data, err := ioutil.ReadFile(filename) + if err != nil { + return config, err + } + + err = yaml.Unmarshal(data, &config) + if err != nil { + return config, err + } + + return config, nil +} \ No newline at end of file From 4103f10502d4db0196251f3402f83688f2103ac3 Mon Sep 17 00:00:00 2001 From: schulze Date: Mon, 20 Mar 2023 15:42:54 +0100 Subject: [PATCH 02/13] ignore credentials --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 5115415..bcc4d49 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ #protect secrets *.env + +mtd_aws/.credentials From e31a66a59d9f85d1f407f90298c2add49d3469f0 Mon Sep 17 00:00:00 2001 From: schulze Date: Mon, 20 Mar 2023 15:43:14 +0100 Subject: [PATCH 03/13] add manual credentials --- config.yaml | 2 +- main.go | 2 +- mtd_aws/sdk.go | 4 ++-- state/config.go | 3 ++- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/config.yaml b/config.yaml index b2fc771..db71ee2 100644 --- a/config.yaml +++ b/config.yaml @@ -3,4 +3,4 @@ aws: - us-east-1 - us-east-2 - us-west-1 - + credentials_path: ./mtd_aws/.credentials \ No newline at end of file diff --git a/main.go b/main.go index c128074..3194e7c 100644 --- a/main.go +++ b/main.go @@ -27,7 +27,7 @@ func main() { for _, region := range config.AWS.Regions { fmt.Println("Listing instances in region:", region) - aws_config := mtd_aws.New_config(region) + aws_config := mtd_aws.New_config(region, config.AWS.Credentials_path) instances, err := mtd_aws.Instances(aws_config) if err != nil { fmt.Println("Error listing instances:", err) diff --git a/mtd_aws/sdk.go b/mtd_aws/sdk.go index 8ceb276..f52774a 100644 --- a/mtd_aws/sdk.go +++ b/mtd_aws/sdk.go @@ -10,9 +10,9 @@ import ( "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/aws/aws-sdk-go-v2/service/ec2/types" ) -func New_config(region string) aws.Config { +func New_config(region string, credentials string) aws.Config { // Create a new AWS config - cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region)) + cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithSharedConfigFiles([]string{credentials}), config.WithRegion(region)) if err != nil { fmt.Println("Error creating config:", err) fmt.Println("Configure Credentials in line with the documentation found here: https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/#specifying-credentials") diff --git a/state/config.go b/state/config.go index f3b2b48..a470103 100644 --- a/state/config.go +++ b/state/config.go @@ -9,7 +9,8 @@ import ( type Config struct { AWS struct { - Regions []string `yaml:"regions"` + Regions []string `yaml:"regions"` + Credentials_path string `yaml:"credentials_path"` } `yaml:"aws"` } From c672d0043d8c9baf146ef5e0287934a49dfd6c8f Mon Sep 17 00:00:00 2001 From: schulze Date: Tue, 21 Mar 2023 13:33:37 +0100 Subject: [PATCH 04/13] protect all credentials --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index bcc4d49..ed79802 100644 --- a/.gitignore +++ b/.gitignore @@ -16,5 +16,5 @@ #protect secrets *.env +*.credentials -mtd_aws/.credentials From af47f13dcf121c5f94634f33f24230d10d4b8150 Mon Sep 17 00:00:00 2001 From: schulze Date: Tue, 21 Mar 2023 13:34:06 +0100 Subject: [PATCH 05/13] tidy --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 6e3d0bd..25d617c 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/aws/aws-sdk-go-v2 v1.17.6 github.com/aws/aws-sdk-go-v2/config v1.18.17 github.com/aws/aws-sdk-go-v2/service/ec2 v1.90.0 - github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d + github.com/google/uuid v1.3.0 gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index 41b4534..265121d 100644 --- a/go.sum +++ b/go.sum @@ -28,12 +28,12 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 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/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ= -github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= From 6c3e2acabb377f0d8679419ec1226a7470cc92db Mon Sep 17 00:00:00 2001 From: schulze Date: Tue, 21 Mar 2023 13:34:52 +0100 Subject: [PATCH 06/13] Refactor, move function to sdk --- main.go | 31 +++++-------------------------- {mtd_aws => mtdaws}/sdk.go | 32 +++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 29 deletions(-) rename {mtd_aws => mtdaws}/sdk.go (68%) diff --git a/main.go b/main.go index 3194e7c..afced4b 100644 --- a/main.go +++ b/main.go @@ -4,39 +4,18 @@ import ( "fmt" "os" - uuid "github.com/nu7hatch/gouuid" - "github.com/thefeli73/polemos/mtd_aws" + "github.com/thefeli73/polemos/mtdaws" "github.com/thefeli73/polemos/state" ) + func main() { fmt.Println("Starting Polemos") - config, err := state.Load_conf("config.yaml") + config, err := state.LoadConf("config.yaml") if err != nil { fmt.Println("Error loading config:", err) os.Exit(1) } - //aws_config := mtd_aws.New_config(config.AWS.Region) - //mtd_aws.Instance_info(aws_config, config.AWS.InstanceID) - //mtd_aws.Instances(config.AWS.Region) - u, _ := uuid.NewV4() //blank is to send errors to the void - _=u - //fmt.Println(u) - - - for _, region := range config.AWS.Regions { - fmt.Println("Listing instances in region:", region) - aws_config := mtd_aws.New_config(region, config.AWS.Credentials_path) - instances, err := mtd_aws.Instances(aws_config) - if err != nil { - fmt.Println("Error listing instances:", err) - continue - } - - for _, instance := range instances { - mtd_aws.Instance_info(aws_config, *instance.InstanceId) - } - } - -} \ No newline at end of file + mtdaws.IndexInstances(config) +} diff --git a/mtd_aws/sdk.go b/mtdaws/sdk.go similarity index 68% rename from mtd_aws/sdk.go rename to mtdaws/sdk.go index f52774a..3b055b3 100644 --- a/mtd_aws/sdk.go +++ b/mtdaws/sdk.go @@ -1,4 +1,4 @@ -package mtd_aws +package mtdaws import ( "context" @@ -9,8 +9,12 @@ import ( "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/google/uuid" + "github.com/thefeli73/polemos/state" ) -func New_config(region string, credentials string) aws.Config { + +// NewConfig creates a AWS config for a specific region +func NewConfig(region string, credentials string) aws.Config { // Create a new AWS config cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithSharedConfigFiles([]string{credentials}), config.WithRegion(region)) if err != nil { @@ -20,7 +24,28 @@ func New_config(region string, credentials string) aws.Config { } return cfg } -func Instance_info(config aws.Config, instanceID string) { + +// IndexInstances scans all configured regions for instances and add them to services +func IndexInstances(config state.Config) { + for _, region := range config.AWS.Regions { + //fmt.Println("Listing instances in region:", region) + awsConfig := NewConfig(region, config.AWS.CredentialsPath) + instances, err := Instances(awsConfig) + if err != nil { + fmt.Println("Error listing instances:", err) + continue + } + + for _, instance := range instances { + InstanceInfo(awsConfig, *instance.InstanceId) + u := uuid.New() + _ = u + } + } +} + +// InstanceInfo collects info about a specific instance in a region +func InstanceInfo(config aws.Config, instanceID string) { // Create a new EC2 service client svc := ec2.NewFromConfig(config) @@ -45,6 +70,7 @@ func Instance_info(config aws.Config, instanceID string) { fmt.Println("Private IP Address:", aws.ToString(instance.PrivateIpAddress)) } +// Instances returns all instances for a config i.e. a region func Instances(config aws.Config) ([]*types.Instance, error) { svc := ec2.NewFromConfig(config) From fc20774a0e33a778af07ed8a519376d11a0986b7 Mon Sep 17 00:00:00 2001 From: schulze Date: Tue, 21 Mar 2023 13:35:16 +0100 Subject: [PATCH 07/13] prepare config for services state --- config.yaml | 15 ++++++++++++++- state/config.go | 41 ++++++++++++++++++++++++++++++++++------- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/config.yaml b/config.yaml index db71ee2..ea835fe 100644 --- a/config.yaml +++ b/config.yaml @@ -1,6 +1,19 @@ +mtd: + services: + - id: 3213a31c-59de-4e14-7648-e9508216e4bc + entry_ip: 192.168.1.1 + entry_port: 8080 + service_ip: 10.0.1.2 + service_port: 80 + - id: 1233a31c-59de-4e14-7648-e9508216e4bc + entry_ip: 192.168.1.3 + entry_port: 8081 + service_ip: 10.0.1.4 + service_port: 81 + aws: regions: - us-east-1 - - us-east-2 +# - us-east-2 - us-west-1 credentials_path: ./mtd_aws/.credentials \ No newline at end of file diff --git a/state/config.go b/state/config.go index a470103..174f0db 100644 --- a/state/config.go +++ b/state/config.go @@ -2,19 +2,46 @@ package state import ( "io/ioutil" + "net/netip" + "github.com/google/uuid" "gopkg.in/yaml.v3" ) - +// Config contains all MTD services and cloud provider configs type Config struct { - AWS struct { - Regions []string `yaml:"regions"` - Credentials_path string `yaml:"credentials_path"` - } `yaml:"aws"` + MTD mtdconf `yaml:"mtd"` + AWS aws `yaml:"aws"` } -func Load_conf(filename string) (Config, error) { +type mtdconf struct { + Services []service `yaml:"services"` +} +type service struct { + ID customUUID `yaml:"id"` + ServiceID string `yaml:"cloud_id"` + EntryIP netip.Addr `yaml:"entry_ip"` + EntryPort uint16 `yaml:"entry_port"` + ServiceIP netip.Addr `yaml:"service_ip"` + ServicePort uint16 `yaml:"service_port"` +} +type customUUID uuid.UUID +type aws struct { + Regions []string `yaml:"regions"` + CredentialsPath string `yaml:"credentials_path"` +} + +func (u *customUUID) UnmarshalYAML(value *yaml.Node) error { + id, err := uuid.Parse(value.Value) + if err != nil { + return err + } + *u = customUUID(id) + return nil +} + +// LoadConf loads config from a yaml file +func LoadConf(filename string) (Config, error) { var config Config data, err := ioutil.ReadFile(filename) @@ -22,7 +49,7 @@ func Load_conf(filename string) (Config, error) { return config, err } - err = yaml.Unmarshal(data, &config) + err = yaml.Unmarshal([]byte(data), &config) if err != nil { return config, err } From 81c611383b25ec4a968113f967e80f23b29d6029 Mon Sep 17 00:00:00 2001 From: schulze Date: Wed, 22 Mar 2023 10:39:22 +0100 Subject: [PATCH 08/13] whitespace --- state/config.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/state/config.go b/state/config.go index 174f0db..dae2252 100644 --- a/state/config.go +++ b/state/config.go @@ -17,6 +17,7 @@ type Config struct { type mtdconf struct { Services []service `yaml:"services"` } + type service struct { ID customUUID `yaml:"id"` ServiceID string `yaml:"cloud_id"` @@ -25,7 +26,9 @@ type service struct { ServiceIP netip.Addr `yaml:"service_ip"` ServicePort uint16 `yaml:"service_port"` } + type customUUID uuid.UUID + type aws struct { Regions []string `yaml:"regions"` CredentialsPath string `yaml:"credentials_path"` From 3a554568caf9ac35b56a23837991b968954d0c11 Mon Sep 17 00:00:00 2001 From: schulze Date: Wed, 22 Mar 2023 10:47:29 +0100 Subject: [PATCH 09/13] aws sdk utils --- mtdaws/sdk.go | 59 ++++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/mtdaws/sdk.go b/mtdaws/sdk.go index 3b055b3..4b066fa 100644 --- a/mtdaws/sdk.go +++ b/mtdaws/sdk.go @@ -9,13 +9,19 @@ import ( "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/aws/aws-sdk-go-v2/service/ec2/types" - "github.com/google/uuid" "github.com/thefeli73/polemos/state" ) +// AwsInstance is basic info about a single aws instance (instance id, redion, pubIP and privIP) +type AwsInstance struct { + InstanceID string + Region string + PublicIP string + PrivateIP string +} + // NewConfig creates a AWS config for a specific region func NewConfig(region string, credentials string) aws.Config { - // Create a new AWS config cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithSharedConfigFiles([]string{credentials}), config.WithRegion(region)) if err != nil { fmt.Println("Error creating config:", err) @@ -26,9 +32,9 @@ func NewConfig(region string, credentials string) aws.Config { } // IndexInstances scans all configured regions for instances and add them to services -func IndexInstances(config state.Config) { +func IndexInstances(config state.Config) []AwsInstance { + awsInstances := []AwsInstance{} for _, region := range config.AWS.Regions { - //fmt.Println("Listing instances in region:", region) awsConfig := NewConfig(region, config.AWS.CredentialsPath) instances, err := Instances(awsConfig) if err != nil { @@ -36,38 +42,33 @@ func IndexInstances(config state.Config) { continue } + //fmt.Println("Listing instances in region:", region) for _, instance := range instances { - InstanceInfo(awsConfig, *instance.InstanceId) - u := uuid.New() - _ = u + var publicAddr string + if instance.PublicIpAddress != nil { + publicAddr = aws.ToString(instance.PublicIpAddress) + } + awsInstances = append(awsInstances, AwsInstance{ + InstanceID: aws.ToString(instance.InstanceId), + Region: region, + PublicIP: publicAddr, + PrivateIP: aws.ToString(instance.PrivateIpAddress)}) } } + return awsInstances } -// InstanceInfo collects info about a specific instance in a region -func InstanceInfo(config aws.Config, instanceID string) { - // Create a new EC2 service client - svc := ec2.NewFromConfig(config) - - input := &ec2.DescribeInstancesInput{ - InstanceIds: []string{instanceID}, - } - result, err := svc.DescribeInstances(context.TODO(), input) - if err != nil { - fmt.Println("Error describing instance:", err) - return - } - // Print instance information - instance := result.Reservations[0].Instances[0] - fmt.Println("Instance ID:", aws.ToString(instance.InstanceId)) - fmt.Println("Instance Type:", string(instance.InstanceType)) - fmt.Println("AMI ID:", aws.ToString(instance.ImageId)) - fmt.Println("State:", string(instance.State.Name)) - fmt.Println("Availability Zone:", aws.ToString(instance.Placement.AvailabilityZone)) +// PrintInstanceInfo prints info about a specific instance in a region +func PrintInstanceInfo(instance *types.Instance) { + fmt.Println("\tInstance ID:", aws.ToString(instance.InstanceId)) + fmt.Println("\t\tInstance Type:", string(instance.InstanceType)) + fmt.Println("\t\tAMI ID:", aws.ToString(instance.ImageId)) + fmt.Println("\t\tState:", string(instance.State.Name)) + fmt.Println("\t\tAvailability Zone:", aws.ToString(instance.Placement.AvailabilityZone)) if instance.PublicIpAddress != nil { - fmt.Println("Public IP Address:", aws.ToString(instance.PublicIpAddress)) + fmt.Println("\t\tPublic IP Address:", aws.ToString(instance.PublicIpAddress)) } - fmt.Println("Private IP Address:", aws.ToString(instance.PrivateIpAddress)) + fmt.Println("\t\tPrivate IP Address:", aws.ToString(instance.PrivateIpAddress)) } // Instances returns all instances for a config i.e. a region From 5d7c5b3d26cbe273c831b5aa5942990dad254da7 Mon Sep 17 00:00:00 2001 From: schulze Date: Wed, 22 Mar 2023 14:00:22 +0100 Subject: [PATCH 10/13] remove config.yaml, add default config (configs are private) --- .gitignore | 2 ++ config.default.yaml | 5 +++++ config.yaml | 19 ------------------- 3 files changed, 7 insertions(+), 19 deletions(-) create mode 100644 config.default.yaml delete mode 100644 config.yaml diff --git a/.gitignore b/.gitignore index ed79802..13da3e6 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,5 @@ *.env *.credentials + +config.yaml diff --git a/config.default.yaml b/config.default.yaml new file mode 100644 index 0000000..4420f39 --- /dev/null +++ b/config.default.yaml @@ -0,0 +1,5 @@ +mtd: + services: [] +aws: + regions: [] + credentials_path: ./mtdaws/.credentials diff --git a/config.yaml b/config.yaml deleted file mode 100644 index ea835fe..0000000 --- a/config.yaml +++ /dev/null @@ -1,19 +0,0 @@ -mtd: - services: - - id: 3213a31c-59de-4e14-7648-e9508216e4bc - entry_ip: 192.168.1.1 - entry_port: 8080 - service_ip: 10.0.1.2 - service_port: 80 - - id: 1233a31c-59de-4e14-7648-e9508216e4bc - entry_ip: 192.168.1.3 - entry_port: 8081 - service_ip: 10.0.1.4 - service_port: 81 - -aws: - regions: - - us-east-1 -# - us-east-2 - - us-west-1 - credentials_path: ./mtd_aws/.credentials \ No newline at end of file From 81dbb99017ed75c551fa20124858b88da752e6cb Mon Sep 17 00:00:00 2001 From: schulze Date: Wed, 22 Mar 2023 14:01:08 +0100 Subject: [PATCH 11/13] support for defualt configs, index aws instances --- main.go | 51 ++++++++++++++++++++++++++++++----- mtdaws/{sdk.go => utils.go} | 9 +++++-- state/config.go | 54 +++++++++++++++++++++++++++++-------- 3 files changed, 94 insertions(+), 20 deletions(-) rename mtdaws/{sdk.go => utils.go} (89%) diff --git a/main.go b/main.go index afced4b..34d4c72 100644 --- a/main.go +++ b/main.go @@ -2,20 +2,57 @@ package main import ( "fmt" - "os" + "net/netip" + "github.com/google/uuid" "github.com/thefeli73/polemos/mtdaws" "github.com/thefeli73/polemos/state" ) +// ConfigPath is a string of the location for the configfile +var ConfigPath string + func main() { fmt.Println("Starting Polemos") - config, err := state.LoadConf("config.yaml") - if err != nil { - fmt.Println("Error loading config:", err) - os.Exit(1) - } + ConfigPath = "config.yaml" + + config := state.LoadConf(ConfigPath) + state.SaveConf(ConfigPath, config) + + config = indexInstances(config) - mtdaws.IndexInstances(config) } + +func indexInstances(config state.Config) state.Config { + fmt.Println("Indexing instances") + + //index AWS instances + awsInstances := mtdaws.GetInstances(config) + for _, instance := range awsInstances { + cloudID := mtdaws.GetCloudID(instance) + ip, err := netip.ParseAddr(instance.PublicIP) + if err != nil { + fmt.Println("Error converting ip:", err) + continue + } + config = indexInstance(config, cloudID, ip) + } + return config +} + +func indexInstance(config state.Config, cloudID string, serviceIP netip.Addr) state.Config { + for _, service := range config.MTD.Services { + if service.CloudID == cloudID { + return config + } + } + u := uuid.New() + newService := state.Service{ + ID: state.CustomUUID(u), + CloudID: cloudID, + ServiceIP: serviceIP} + config.MTD.Services = append(config.MTD.Services, newService) + state.SaveConf(ConfigPath, config) + return config +} \ No newline at end of file diff --git a/mtdaws/sdk.go b/mtdaws/utils.go similarity index 89% rename from mtdaws/sdk.go rename to mtdaws/utils.go index 4b066fa..9d31ca9 100644 --- a/mtdaws/sdk.go +++ b/mtdaws/utils.go @@ -31,8 +31,13 @@ func NewConfig(region string, credentials string) aws.Config { return cfg } -// IndexInstances scans all configured regions for instances and add them to services -func IndexInstances(config state.Config) []AwsInstance { +// GetCloudID returns a string to find the instance based on information from aws +func GetCloudID(instance AwsInstance) string { + return "aws_" + instance.Region + "_" + instance.InstanceID +} + +// GetInstances scans all configured regions for instances and add them to services +func GetInstances(config state.Config) []AwsInstance { awsInstances := []AwsInstance{} for _, region := range config.AWS.Regions { awsConfig := NewConfig(region, config.AWS.CredentialsPath) diff --git a/state/config.go b/state/config.go index dae2252..b52bc52 100644 --- a/state/config.go +++ b/state/config.go @@ -1,8 +1,10 @@ package state import ( + "fmt" "io/ioutil" "net/netip" + "os" "github.com/google/uuid" "gopkg.in/yaml.v3" @@ -15,47 +17,77 @@ type Config struct { } type mtdconf struct { - Services []service `yaml:"services"` + Services []Service `yaml:"services"` } -type service struct { - ID customUUID `yaml:"id"` - ServiceID string `yaml:"cloud_id"` +// Service contains all necessary information about a service to identify it in the cloud as well as configuring a proxy for it +type Service struct { + ID CustomUUID `yaml:"id"` + CloudID string `yaml:"cloud_id"` EntryIP netip.Addr `yaml:"entry_ip"` EntryPort uint16 `yaml:"entry_port"` ServiceIP netip.Addr `yaml:"service_ip"` ServicePort uint16 `yaml:"service_port"` } -type customUUID uuid.UUID +// CustomUUID is an alias for uuid.UUID to enable custom unmarshal function +type CustomUUID uuid.UUID type aws struct { Regions []string `yaml:"regions"` CredentialsPath string `yaml:"credentials_path"` } -func (u *customUUID) UnmarshalYAML(value *yaml.Node) error { +// UnmarshalYAML parses uuid in yaml to CustomUUID type +func (u *CustomUUID) UnmarshalYAML(value *yaml.Node) error { id, err := uuid.Parse(value.Value) if err != nil { return err } - *u = customUUID(id) + *u = CustomUUID(id) return nil } +// MarshalYAML parses CustomUUID type to uuid string for yaml +func (u CustomUUID) MarshalYAML() (interface{}, error) { + return uuid.UUID(u).String(), nil +} + // LoadConf loads config from a yaml file -func LoadConf(filename string) (Config, error) { +func LoadConf(filename string) (Config) { var config Config data, err := ioutil.ReadFile(filename) if err != nil { - return config, err + fmt.Println("Error reading file:", err) + + fmt.Println("Attempting to load default config") + data, err = ioutil.ReadFile("config.default.yaml") + if err != nil { + fmt.Println("Error reading file:", err) + os.Exit(1) + } } err = yaml.Unmarshal([]byte(data), &config) if err != nil { - return config, err + fmt.Println("Error importing config:", err) + os.Exit(1) } + fmt.Println("Imported config succesfully!") + return config +} - return config, nil +// SaveConf saves config to yaml file +func SaveConf(filename string, config Config) (error) { + yamlBytes, err := yaml.Marshal(&config) + if err != nil { + return err + } + + err = ioutil.WriteFile(filename, yamlBytes, 0644) + if err != nil { + return err + } + return nil } \ No newline at end of file From 42513feedf2218c0e1bcf276d5a03e20e95559eb Mon Sep 17 00:00:00 2001 From: schulze Date: Wed, 22 Mar 2023 14:12:12 +0100 Subject: [PATCH 12/13] indexing logic --- main.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 34d4c72..cec864a 100644 --- a/main.go +++ b/main.go @@ -36,15 +36,20 @@ func indexInstances(config state.Config) state.Config { fmt.Println("Error converting ip:", err) continue } - config = indexInstance(config, cloudID, ip) + newService, found := indexInstance(config, cloudID, ip) + if !found { + config.MTD.Services = append(config.MTD.Services, newService) + state.SaveConf(ConfigPath, config) + } } return config } -func indexInstance(config state.Config, cloudID string, serviceIP netip.Addr) state.Config { +func indexInstance(config state.Config, cloudID string, serviceIP netip.Addr) (state.Service, bool) { + found := false for _, service := range config.MTD.Services { if service.CloudID == cloudID { - return config + found = true } } u := uuid.New() @@ -52,7 +57,5 @@ func indexInstance(config state.Config, cloudID string, serviceIP netip.Addr) st ID: state.CustomUUID(u), CloudID: cloudID, ServiceIP: serviceIP} - config.MTD.Services = append(config.MTD.Services, newService) - state.SaveConf(ConfigPath, config) - return config + return newService, found } \ No newline at end of file From d8bccb81a9859c74780459f2078609ac46c76caf Mon Sep 17 00:00:00 2001 From: schulze Date: Wed, 22 Mar 2023 14:24:56 +0100 Subject: [PATCH 13/13] newline at EOF --- main.go | 2 +- mtdaws/utils.go | 2 +- state/config.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index cec864a..3db5399 100644 --- a/main.go +++ b/main.go @@ -58,4 +58,4 @@ func indexInstance(config state.Config, cloudID string, serviceIP netip.Addr) (s CloudID: cloudID, ServiceIP: serviceIP} return newService, found -} \ No newline at end of file +} diff --git a/mtdaws/utils.go b/mtdaws/utils.go index 9d31ca9..b59805b 100644 --- a/mtdaws/utils.go +++ b/mtdaws/utils.go @@ -99,4 +99,4 @@ func Instances(config aws.Config) ([]*types.Instance, error) { } return instances, nil -} \ No newline at end of file +} diff --git a/state/config.go b/state/config.go index b52bc52..a74c59e 100644 --- a/state/config.go +++ b/state/config.go @@ -90,4 +90,4 @@ func SaveConf(filename string, config Config) (error) { return err } return nil -} \ No newline at end of file +}