Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 2b888b7

Browse files
author
David Chung
authored
Terraform plugin improvements (#485)
Signed-off-by: David Chung <[email protected]>
1 parent 0f18046 commit 2b888b7

File tree

36 files changed

+4816
-130
lines changed

36 files changed

+4816
-130
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,7 @@ test-full:
171171
vendor-update:
172172
@echo "+ $@"
173173
@trash -u
174+
175+
terraform-linux:
176+
@echo "+ $@"
177+
wget -O tf.zip https://releases.hashicorp.com/terraform/0.9.3/terraform_0.9.3_linux_amd64.zip && unzip tf.zip && mv terraform ./build

circle.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ machine:
1111
GOPATH: "$HOME/.go_workspace"
1212
WORKDIR: "$GOPATH/src/github.com/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME"
1313
E2E_CLEANUP: "false"
14-
SKIP_TESTS: "docker,etcd"
14+
SKIP_TESTS: "docker,etcd,terraform"
1515
S3_MINIO: "s3bucket"
1616
S3_BUCKET: "s3bucket/infrakit/build/linux-amd64"
1717
S3_BUILD_RELEASE: "infrakit-$CIRCLE_BRANCH.tar.gz"

dockerfiles/Dockerfile.build

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
FROM golang:1.8-alpine
22

3-
RUN apk add --update git make gcc musl-dev
3+
RUN apk add --update git make gcc musl-dev wget ca-certificates openssl
44
RUN go get github.com/rancher/trash
55

66
WORKDIR /go/src/github.com/docker/infrakit
7+
78
VOLUME [ "/go/src/github.com/docker/infrakit/build" ]
8-
CMD make build-binaries
99

1010
COPY . ./
1111
RUN trash # Force updating the vendored sources per spec; this slows the build but is most correct.
12+
13+
CMD make build-binaries terraform-linux

dockerfiles/Dockerfile.bundle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
FROM docker:latest
22

3-
RUN mkdir -p /infrakit/plugins /infrakit/configs /infrakit/logs /infrakit/cli
3+
4+
RUN mkdir -p /infrakit/plugins /infrakit/configs /infrakit/logs /infrakit/cli /infrakit/instance/terraform
45

56
VOLUME /infrakit
67

@@ -19,6 +20,8 @@ ENV INFRAKIT_PLAYBOOKS_FILE /infrakit/playbooks
1920
ENV INFRAKIT_LEADER_FILE /infrakit/leader
2021
ENV INFRAKIT_STORE_DIR /infrakit/configs
2122

23+
# Default terraform directory
24+
ENV INFRAKIT_INSTANCE_TERRAFORM_DIR /infrakit/instance/terraform
2225

2326

2427
ADD build/* /usr/local/bin/

examples/instance/terraform/apply.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"math/rand"
5+
"os"
6+
"time"
7+
8+
log "github.com/Sirupsen/logrus"
9+
"github.com/docker/infrakit/pkg/util/exec"
10+
)
11+
12+
func (p *plugin) terraformApply() error {
13+
if p.pretend {
14+
return nil
15+
}
16+
17+
p.applyLock.Lock()
18+
defer p.applyLock.Unlock()
19+
20+
if p.applying {
21+
return nil
22+
}
23+
24+
go func() {
25+
for {
26+
if err := p.lock.TryLock(); err == nil {
27+
defer p.lock.Unlock()
28+
doTerraformApply(p.Dir)
29+
}
30+
log.Debugln("Can't acquire lock, waiting")
31+
time.Sleep(time.Duration(int64(rand.NormFloat64())%1000) * time.Millisecond)
32+
}
33+
}()
34+
p.applying = true
35+
return nil
36+
}
37+
38+
func doTerraformApply(dir string) error {
39+
log.Infoln(time.Now().Format(time.RFC850) + " Applying plan")
40+
command := exec.Command(`terraform apply`).InheritEnvs(true).WithDir(dir)
41+
err := command.WithStdout(os.Stdout).WithStderr(os.Stdout).Start()
42+
if err != nil {
43+
return err
44+
}
45+
return command.Wait()
46+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"path"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
10+
. "github.com/docker/infrakit/pkg/testing"
11+
)
12+
13+
func TestRunTerraformApply(t *testing.T) {
14+
15+
// Run this test locally only if terraform is set up
16+
if SkipTests("terraform") {
17+
t.SkipNow()
18+
}
19+
20+
dir, err := os.Getwd()
21+
require.NoError(t, err)
22+
dir = path.Join(dir, "aws-two-tier")
23+
24+
err = doTerraformApply(dir)
25+
require.NoError(t, err)
26+
}

examples/instance/terraform/main.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ func mustHaveTerraform() {
1818
}
1919
}
2020

21+
func getDir() string {
22+
dir := os.Getenv("INFRAKIT_INSTANCE_TERRAFORM_DIR")
23+
if dir != "" {
24+
return dir
25+
}
26+
return os.TempDir()
27+
}
28+
2129
func main() {
2230

2331
cmd := &cobra.Command{
@@ -26,7 +34,7 @@ func main() {
2634
}
2735
name := cmd.Flags().String("name", "instance-terraform", "Plugin name to advertise for discovery")
2836
logLevel := cmd.Flags().Int("log", cli.DefaultLogLevel, "Logging level. 0 is least verbose. Max is 5")
29-
dir := cmd.Flags().String("dir", os.TempDir(), "Dir for storing plan files")
37+
dir := cmd.Flags().String("dir", getDir(), "Dir for storing plan files")
3038
cmd.Run = func(c *cobra.Command, args []string) {
3139
mustHaveTerraform()
3240

0 commit comments

Comments
 (0)