Skip to content

Commit

Permalink
Merge pull request #18 from buildkite/lambda-support
Browse files Browse the repository at this point in the history
Add a lambda function for executing stats
  • Loading branch information
lox authored Dec 17, 2016
2 parents f181b11 + 4df3747 commit c58fd40
Show file tree
Hide file tree
Showing 14 changed files with 837 additions and 13 deletions.
11 changes: 6 additions & 5 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
steps:
-
name: "📦"
- name: ":golang:"
command: "make build"
artifact_paths: "build/*"
plugins:
- docker-compose:
run: "build"

- name: ":lambda:"
command: "make build-lambda"
artifact_paths: "build/*"

- wait

-
name: "Release to :s3:"
- name: "Release to :s3:"
command: ".buildkite/upload.sh"
branches: "master"
agents:
queue: "deploy"
9 changes: 8 additions & 1 deletion .buildkite/upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ export AWS_DEFAULT_REGION=us-east-1
export AWS_ACCESS_KEY_ID=$SANDBOX_AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY=$SANDBOX_AWS_SECRET_ACCESS_KEY

echo "~~~ :buildkite: Downloading artifacts"
mkdir -p build/
buildkite-agent artifact download "build/*" build/

make upload branch=${BUILDKITE_BRANCH}
echo "~~~ :s3: Uploading files"
make upload branch=${BUILDKITE_BRANCH}

echo "+++ :s3: Upload complete"
for f in $(ls build/) ; do
printf "https://buildkite-metrics.s3.amazonaws.com/%s\n" $f
done
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build/
handler.zip
19 changes: 14 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@
VERSION=$(shell git fetch --tags && git describe --tags --candidates=1 --dirty --always)
FLAGS=-X main.Version=$(VERSION)
BIN=build/buildkite-metrics-$(shell uname -s)-$(shell uname -m)-$(VERSION)
LATEST=build/buildkite-metrics-$(shell uname -s)-$(shell uname -m)
LAMBDA_ZIP=build/buildkite-metrics-$(VERSION)-lambda.zip
SRC=$(shell find . -name '*.go')

build: $(BIN)

build-lambda: $(LAMBDA_ZIP)

clean:
-rm build/*
-rm -f build/

$(BIN): main.go
$(BIN): $(SRC)
-mkdir -p build/
go build -o $(BIN) -ldflags="$(FLAGS)" .
cp -a $(BIN) $(LATEST)

GODIR=/go/src/github.com/buildkite/buildkite-metrics

$(LAMBDA_ZIP): $(SRC)
docker run --rm -v $(PWD):$(GODIR) -w $(GODIR) eawsy/aws-lambda-go
-mkdir -p build/
mv handler.zip $(LAMBDA_ZIP)

upload:
aws s3 sync --acl public-read build s3://buildkite-metrics/
aws s3 sync --acl public-read build s3://buildkite-metrics/
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ build:
command: "make clean build"
volumes:
- .:/go/src/github.com/buildkite/buildkite-metrics
working_dir: /go/src/github.com/buildkite/buildkite-metrics
working_dir: /go/src/github.com/buildkite/buildkite-metrics
50 changes: 50 additions & 0 deletions lambda.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"bytes"
"encoding/json"
"log"
"os"
"time"

"github.com/eawsy/aws-lambda-go/service/lambda/runtime"
"gopkg.in/buildkite/go-buildkite.v2/buildkite"
)

func handle(evt json.RawMessage, ctx *runtime.Context) (interface{}, error) {
output := &bytes.Buffer{}
log.SetOutput(output)

org := os.Getenv("BUILDKITE_ORG")
token := os.Getenv("BUILDKITE_TOKEN")

config, err := buildkite.NewTokenConfig(token, false)
if err != nil {
return nil, err
}

client := buildkite.NewClient(config.Client())
t := time.Now()

res, err := collectResults(client, collectOpts{
OrgSlug: org,
Historical: time.Hour * 24,
})
if err != nil {
return nil, err
}

dumpResults(res)

err = cloudwatchSend(res)
if err != nil {
return nil, err
}

log.Printf("Finished in %s", time.Now().Sub(t))
return output.String(), nil
}

func init() {
runtime.HandleFunc(handle)
}
202 changes: 202 additions & 0 deletions vendor/github.com/eawsy/aws-lambda-go/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions vendor/github.com/eawsy/aws-lambda-go/NOTICE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c58fd40

Please sign in to comment.