Skip to content

Commit aef4105

Browse files
Introduce unified infrastructure interface
We introduce a common interface to Docker and AWS infrastructures used to run goad and lambda function. The creation of the result channel is still handled outside the infrastucture interface but should be included aswell.
1 parent be02af6 commit aef4105

File tree

8 files changed

+545
-539
lines changed

8 files changed

+545
-539
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ site/node_modules
22
site/dist
33
data/lambda.zip
44
data/lambda/goad-lambda
5-
infrastructure/bindata.go
5+
infrastructure/aws/bindata.go
66
site/Upstatic
77
build/
88

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ lambda:
4545

4646
bindata: lambda
4747
@go get github.com/jteeuwen/go-bindata/...
48-
@go-bindata -modtime $(TIMESTAMP) -nocompress -pkg infrastructure -o infrastructure/bindata.go data/lambda.zip
48+
@go-bindata -modtime $(TIMESTAMP) -nocompress -pkg awsinfra -o infrastructure/aws/bindata.go data/lambda.zip
4949

5050
linux64: bindata
5151
@GOOS=linux GOARCH=amd64 $(GO-BUILD) -o build/linux/x86-64/$(TARGET)

cli/cli.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"io"
88
"io/ioutil"
9-
"log"
109
"math"
1110
"os"
1211
"os/signal"
@@ -271,24 +270,21 @@ func createGoadTest(config *goad.TestConfig) *goad.Test {
271270
}
272271

273272
func start(test *goad.Test, finalResult *queue.RegionsAggData, sigChan chan os.Signal) {
274-
if test.Config.RunDocker {
275-
goad.DockerPullLambdaImage()
276-
goad.DockerPullRabbitMQImage()
277-
}
273+
resultChan, teardown := test.Start()
274+
defer teardown()
278275

279-
err := termbox.Init()
280-
if err != nil {
281-
log.Println(err)
282-
} else {
283-
defer termbox.Close()
284-
termbox.Sync()
285-
renderString(0, 0, "Launching on AWS... (be patient)", coldef, coldef)
286-
renderLogo()
287-
termbox.Flush()
276+
platform := "AWS"
277+
if test.Config.RunDocker {
278+
platform = "Docker"
288279
}
280+
launchingOn := fmt.Sprintf("Launching on %s... (be patient)", platform)
289281

290-
resultChan, teardown := test.Start()
291-
defer teardown()
282+
termbox.Init()
283+
defer termbox.Close()
284+
termbox.Sync()
285+
renderString(0, 0, launchingOn, coldef, coldef)
286+
renderLogo()
287+
termbox.Flush()
292288

293289
_, h := termbox.Size()
294290
renderString(0, h-1, "Press ctrl-c to interrupt", coldef, coldef)
@@ -367,8 +363,10 @@ func renderLogo() {
367363

368364
// Also clears loading message
369365
func clearLogo() {
370-
for i := 0; i < 8; i++ {
371-
renderString(0, i, " ", coldef, coldef)
366+
w, h := termbox.Size()
367+
clearStr := strings.Repeat(" ", w)
368+
for i := 0; i < h-1; i++ {
369+
renderString(0, i, clearStr, coldef, coldef)
372370
}
373371
}
374372

goad/goad.go

Lines changed: 6 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
11
package goad
22

33
import (
4-
"bytes"
5-
"context"
6-
"encoding/json"
74
"errors"
85
"fmt"
9-
"io"
106
"math"
11-
"os"
127
"strconv"
138
"strings"
149
"time"
1510

1611
"github.com/aws/aws-sdk-go/aws"
17-
"github.com/aws/aws-sdk-go/aws/session"
18-
"github.com/aws/aws-sdk-go/service/lambda"
19-
"github.com/docker/docker/api/types"
20-
"github.com/docker/docker/api/types/container"
21-
"github.com/docker/docker/api/types/network"
22-
"github.com/docker/docker/client"
2312
"github.com/goadapp/goad/infrastructure"
13+
"github.com/goadapp/goad/infrastructure/aws"
14+
"github.com/goadapp/goad/infrastructure/docker"
2415
"github.com/goadapp/goad/queue"
25-
"github.com/goadapp/goad/version"
2616
)
2717

2818
// TestConfig type
@@ -41,11 +31,6 @@ type TestConfig struct {
4131
RunDocker bool
4232
}
4333

44-
type invokeArgs struct {
45-
File string `json:"file"`
46-
Args []string `json:"args"`
47-
}
48-
4934
const nano = 1000000000
5035

5136
var supportedRegions = []string{
@@ -80,9 +65,9 @@ func (t *Test) Start() (<-chan queue.RegionsAggData, func()) {
8065
awsConfig := aws.NewConfig().WithRegion(t.Config.Regions[0])
8166

8267
if t.Config.RunDocker {
83-
t.infra = infrastructure.NewDockerInfrastructure()
68+
t.infra = dockerinfra.NewDockerInfrastructure()
8469
} else {
85-
t.infra = infrastructure.New(t.Config.Regions, awsConfig)
70+
t.infra = awsinfra.New(t.Config.Regions, awsConfig)
8671
}
8772
teardown, err := t.infra.Setup()
8873
handleErr(err)
@@ -130,18 +115,12 @@ func (t *Test) invokeLambdas(awsConfig *aws.Config, queueURL string) {
130115
}
131116
args = append(args, fmt.Sprintf("%s", c.URL))
132117

133-
invokeargs := invokeArgs{
118+
invokeargs := infrastructure.InvokeArgs{
134119
File: "./goad-lambda",
135120
Args: args,
136121
}
137122

138-
config := aws.NewConfig().WithRegion(region)
139-
140-
if t.Config.RunDocker {
141-
go runAsDockerContainer(queueURL, invokeargs)
142-
} else {
143-
go invokeLambda(config, invokeargs)
144-
}
123+
go t.infra.Run(invokeargs)
145124
}
146125
}
147126

@@ -151,75 +130,6 @@ func handleErr(err error) {
151130
}
152131
}
153132

154-
func DockerPullLambdaImage() {
155-
DockerPullImage("lambci/lambda")
156-
}
157-
158-
func DockerPullRabbitMQImage() {
159-
DockerPullImage("rabbitmq:3")
160-
}
161-
162-
func DockerPullImage(imageName string) {
163-
ctx := context.Background()
164-
cli, err := client.NewEnvClient()
165-
handleErr(err)
166-
167-
// Pull the image from dockerhub.
168-
out, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{})
169-
handleErr(err)
170-
defer out.Close()
171-
io.Copy(os.Stdout, out)
172-
}
173-
174-
func runAsDockerContainer(queueURL string, args invokeArgs) {
175-
ctx := context.Background()
176-
cli, err := client.NewEnvClient()
177-
handleErr(err)
178-
rabbitmqURL := fmt.Sprintf("RABBITMQ=%s", queueURL)
179-
180-
// Create container to execute lambda
181-
resp, err := cli.ContainerCreate(ctx, &container.Config{
182-
Image: "lambci/lambda",
183-
Cmd: append([]string{"index.handler"}, ToJSONString(args)),
184-
Volumes: map[string]struct{}{
185-
"/var/task": struct{}{},
186-
},
187-
Env: []string{rabbitmqURL},
188-
}, &container.HostConfig{
189-
AutoRemove: true,
190-
Binds: []string{os.ExpandEnv("${PWD}/data/lambda:/var/task:ro")},
191-
}, &network.NetworkingConfig{
192-
EndpointsConfig: map[string]*network.EndpointSettings{
193-
"goad-bridge": &network.EndpointSettings{},
194-
},
195-
}, "")
196-
handleErr(err)
197-
198-
// run container
199-
err = cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
200-
handleErr(err)
201-
}
202-
203-
func ToJSONString(args interface{}) string {
204-
b, err := json.Marshal(args)
205-
handleErr(err)
206-
return string(b[:])
207-
}
208-
func toJSONReadSeeker(args interface{}) io.ReadSeeker {
209-
j, err := json.Marshal(args)
210-
handleErr(err)
211-
return bytes.NewReader(j)
212-
}
213-
214-
func invokeLambda(awsConfig *aws.Config, args invokeArgs) {
215-
svc := lambda.New(session.New(), awsConfig)
216-
217-
svc.InvokeAsync(&lambda.InvokeAsyncInput{
218-
FunctionName: aws.String("goad:" + version.LambdaVersion()),
219-
InvokeArgs: toJSONReadSeeker(args),
220-
})
221-
}
222-
223133
func numberOfLambdas(concurrency int, numRegions int) int {
224134
if numRegions > int(concurrency) {
225135
return int(concurrency)

0 commit comments

Comments
 (0)