Skip to content

Commit

Permalink
run implemented
Browse files Browse the repository at this point in the history
terraform + go.rice and a temporary dir
  • Loading branch information
alephnull committed Jul 31, 2020
1 parent aabebdd commit acf6e62
Show file tree
Hide file tree
Showing 20 changed files with 874 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
# Dependency directories (remove the comment below to include it)
# vendor/
/gromit
.terraform/
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
serve: gromit
GROMIT_TABLENAME=DeveloperEnvironments GROMIT_REPOS=tyk,tyk-analytics,tyk-pump GROMIT_REGISTRYID=046805072452 ./gromit serve --certpath scerts

gromit: *.go cmd/*.go devenv/*.go
gromit: *.go cmd/*.go devenv/*.go terraform/*.go server/*.go
rice embed-go
go build
go mod tidy
sudo setcap 'cap_net_bind_service=+ep' $(@)

serve: gromit
GROMIT_TABLENAME=DeveloperEnvironments GROMIT_REPOS=tyk,tyk-analytics,tyk-pump GROMIT_REGISTRYID=046805072452 ./gromit serve --certpath scerts

2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
Generated from lines with TODO in the repo , use the pre-commit hook in .
Binary file gromit matches
server/app.go:// TODO Implement listing of all environments
Binary file terraform/manifests/.terraform/plugins/registry.terraform.io/hashicorp/aws/2.70.0/linux_amd64/terraform-provider-aws_v2.70.0_x4 matches
Binary file terraform/manifests/.terraform/plugins/registry.terraform.io/hashicorp/template/2.1.2/linux_amd64/terraform-provider-template_v2.1.2_x4 matches
66 changes: 66 additions & 0 deletions cmd/expose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

/*
Copyright © 2020 Tyk Technologies
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import (
"github.com/TykTechnologies/gromit/devenv"
"github.com/kelseyhightower/envconfig"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

// EnvConfig holds global environment variables
type EnvConfig struct {
ZoneID string
Domain string
}

// exposeCmd represents the expose command
var exposeCmd = &cobra.Command{
Use: "expose",
Short: "Upsert a record in Route53 for an ECS task/cluster",
Long: `When exposing a whole cluster, will make the entries in Route53 as <task_name>.$R53_DOMAIN
Currently it creates only A records.`,
Run: func(cmd *cobra.Command, args []string) {
var e EnvConfig

err := envconfig.Process("r53", &e)
if err != nil {
log.Fatal().Err(err).Msg("Could not get env")
}
log.Info().Interface("env", e).Msg("loaded env")
err = devenv.UpdateClusterIPs(args[0], e.ZoneID, e.Domain)
if err != nil {
log.Fatal().Err(err).Msgf("Failed to update cluster IPs for 5s", args[0])
}
},
}

func init() {
rootCmd.AddCommand(exposeCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// exposeCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// exposeCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
42 changes: 42 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cmd

/*
Copyright © 2020 Tyk Technologies
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import (
"github.com/TykTechnologies/gromit/terraform"
"github.com/spf13/cobra"
)

// runCmd represents the run command
var runCmd = &cobra.Command{
Use: "run",
Short: "Update envs from $GROMIT_TABLENAME",
Long: `Read state and called the embedded terraform manifest with the new tags. This component is meant to run in a scheduled task.`,
Run: func(cmd *cobra.Command, args []string) {
terraform.Run(args[0])
},
}

func init() {
rootCmd.AddCommand(runCmd)

runCmd.PersistentFlags().StringP("table", "t", "DeveloperEnvironments", "DynamoDB table to read")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// runCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
46 changes: 19 additions & 27 deletions devenv/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package devenv
import (
"context"
"errors"
"os"
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/external"
Expand All @@ -12,19 +12,9 @@ import (
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/ecs/ecsiface"
"github.com/aws/aws-sdk-go-v2/service/route53"
"github.com/kelseyhightower/envconfig"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

// EnvConfig holds global environment variables
type EnvConfig struct {
ZoneID string
Domain string
}

var e EnvConfig

// Will only fetch fargate tasks that want to be or are RUNNING
func getClusterTasks(svc ecsiface.ClientAPI, cluster string) ([]string, error) {
input := &ecs.ListTasksInput{
Expand Down Expand Up @@ -87,18 +77,27 @@ func getPublicIP(svc ec2iface.ClientAPI, eni string) (string, error) {
}
log.Trace().Interface("netifaces", result)

return *result.NetworkInterfaces[0].Association.PublicIp, nil
if len(result.NetworkInterfaces) > 0 {
assoc := result.NetworkInterfaces[0].Association
if assoc != nil && assoc.PublicIp != nil {
return *assoc.PublicIp, nil
}
}
return "", errors.New("No public IP")
}

func updateClusterIPs(cluster string) {
// UpdateClusterIPs is the entrypoint for CLI
func UpdateClusterIPs(cluster string, zoneid string, domain string) error {
cfg, err := external.LoadDefaultAWSConfig()
if err != nil {
log.Fatal().Err(err).Msg("unable to load SDK config,")
log.Error().Err(err).Msg("unable to load SDK config,")
return err
}
region, flag, err := external.GetRegion(external.Configs{cfg})
log.Debug().Msgf("getting region flag: %t", flag)
log.Trace().Msgf("got region flag: %v, not sure what this is supposed to indicate", flag)
if err != nil {
log.Fatal().Err(err).Msg("unable to find region,")
log.Error().Err(err).Msg("unable to find region,")
return err
}

fargate := ecs.New(cfg)
Expand All @@ -116,21 +115,14 @@ func updateClusterIPs(cluster string) {
continue
}
log.Debug().Msgf("Found ip %s for task %s.%s", ip, cluster, taskName)
fqdn, err := UpsertTaskDNS(route53.New(cfg), region, taskName, ip)
fqdn := fmt.Sprintf("%s.%s", taskName, domain)

err = UpsertTaskDNS(route53.New(cfg), region, zoneid, fqdn, ip)
if err != nil {
log.Warn().Err(err).Msgf("could not bind %s", ip)
continue
}
log.Info().Msgf("Bound %s to %s", ip, fqdn)
}
}

func entrypoint() {
zerolog.SetGlobalLevel(zerolog.TraceLevel)
err := envconfig.Process("gromit", &e)
if err != nil {
log.Fatal().Err(err)
}
log.Info().Interface("env", e).Msg("loaded env")
updateClusterIPs(os.Args[1])
return nil
}
20 changes: 12 additions & 8 deletions devenv/r53.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package devenv

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/route53"
Expand All @@ -11,17 +10,22 @@ import (
)

// UpsertTaskDNS will create or update the given record
func UpsertTaskDNS(r53 route53iface.ClientAPI, region string, name string, ip string) (string, error) {
fqdn := fmt.Sprintf("%s.%s", name, e.Domain)
func UpsertTaskDNS(
r53 route53iface.ClientAPI,
region string,
zoneid string,
name string,
ip string,
) error {
input := &route53.ChangeResourceRecordSetsInput{
ChangeBatch: &route53.ChangeBatch{
Changes: []route53.Change{
{
Action: route53.ChangeActionUpsert,
ResourceRecordSet: &route53.ResourceRecordSet{
Name: aws.String(fqdn),
Name: aws.String(name),
Region: route53.ResourceRecordSetRegion(region),
TTL: func() *int64 { i := int64(300); return &i }(),
TTL: aws.Int64(10),
Type: route53.RRType("A"),
ResourceRecords: []route53.ResourceRecord{
route53.ResourceRecord{
Expand All @@ -33,14 +37,14 @@ func UpsertTaskDNS(r53 route53iface.ClientAPI, region string, name string, ip st
},
Comment: aws.String("[CI] update from gromit"),
},
HostedZoneId: aws.String(e.ZoneID),
HostedZoneId: aws.String(zoneid),
}

req := r53.ChangeResourceRecordSetsRequest(input)
result, err := req.Send(context.Background())
log.Trace().Interface("r53upsert", result)
if err != nil {
return "", err
return err
}
return fqdn, nil
return nil
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module github.com/TykTechnologies/gromit
go 1.14

require (
github.com/GeertJohan/go.rice v1.0.0
github.com/aws/aws-sdk-go-v2 v0.24.0
github.com/daaku/go.zipexe v1.0.1 // indirect
github.com/gorilla/mux v1.7.4
github.com/kelseyhightower/envconfig v1.4.0
github.com/mitchellh/go-homedir v1.1.0
Expand Down
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/GeertJohan/go.incremental v1.0.0 h1:7AH+pY1XUgQE4Y1HcXYaMqAI0m9yrFqo/jt0CW30vsg=
github.com/GeertJohan/go.incremental v1.0.0/go.mod h1:6fAjUhbVuX1KcMD3c8TEgVUqmo4seqhv0i0kdATSkM0=
github.com/GeertJohan/go.rice v1.0.0 h1:KkI6O9uMaQU3VEKaj01ulavtF7o1fWT7+pk/4voiMLQ=
github.com/GeertJohan/go.rice v1.0.0/go.mod h1:eH6gbSOAUv07dQuZVnBmoDP8mgsM1rtixis4Tib9if0=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/akavel/rsrc v0.8.0 h1:zjWn7ukO9Kc5Q62DOJCcxGpXC18RawVtYAGdz2aLlfw=
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
Expand All @@ -17,6 +23,10 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/daaku/go.zipexe v1.0.0 h1:VSOgZtH418pH9L16hC/JrgSNJbbAL26pj7lmD1+CGdY=
github.com/daaku/go.zipexe v1.0.0/go.mod h1:z8IiR6TsVLEYKwXAoE/I+8ys/sDkgTzSL0CLnGVd57E=
github.com/daaku/go.zipexe v1.0.1 h1:wV4zMsDOI2SZ2m7Tdz1Ps96Zrx+TzaK15VbUaGozw0M=
github.com/daaku/go.zipexe v1.0.1/go.mod h1:5xWogtqlYnfBXkSB1o9xysukNP9GTvaNkqzUZbt3Bw8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -51,6 +61,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
Expand All @@ -74,6 +86,8 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229 h1:E2B8qYyeSgv5MXpmzZXRNp8IAQ4vjxIjhpAf5hv/tAg=
github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229/go.mod h1:0aYXnNPJ8l7uZxf45rWW1a/uME32OF0rhiYGNQ2oF2E=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
Expand Down Expand Up @@ -119,6 +133,10 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8WdUSz8=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
Expand Down
Loading

0 comments on commit acf6e62

Please sign in to comment.