From 0047b35b624794f50cf7bba2ff61930fe91d4310 Mon Sep 17 00:00:00 2001 From: Brandon Cook Date: Tue, 6 Sep 2016 13:25:19 -0700 Subject: [PATCH 1/4] fix ec2-bootstrap hook for multi-region --- hook/hook.go | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/hook/hook.go b/hook/hook.go index b535ed6..766f2f1 100644 --- a/hook/hook.go +++ b/hook/hook.go @@ -18,6 +18,7 @@ import ( "os/exec" "path" "strings" + "sync/atomic" "github.com/adobe-platform/porter/aws/elb" "github.com/adobe-platform/porter/aws_session" @@ -37,6 +38,10 @@ type ( } ) +// Multi-region deployment means we need an additional unique id for git clones +// and image names +var globalCounter *uint32 = new(uint32) + func newOpts() *Opts { return &Opts{ BuildStdout: os.Stdout, @@ -140,8 +145,6 @@ func Execute(log log15.Logger, return } - log := log.New("Region", region.Name) - roleARN, err := env.GetRoleARN(region.Name) if err != nil { log.Error("GetRoleARN", "Error", err) @@ -337,16 +340,16 @@ func runConfigHook(log log15.Logger, config conf.Config, hookName string, } dockerFilePath := hook.Dockerfile + hookCounter := atomic.AddUint32(globalCounter, 1) if hook.Repo != "" { - repoDir := fmt.Sprintf("%s-clone-%d", hookName, hookIndex) + repoDir := fmt.Sprintf("%s_clone_%d_%d", hookName, hookIndex, hookCounter) repoDir = path.Join(constants.TempDir, repoDir) - defer func() { - exec.Command("rm", "-fr", repoDir).Run() - }() - log.Info("Cloning", + defer exec.Command("rm", "-fr", repoDir).Run() + + log.Info("git clone", "Repo", hook.Repo, "Ref", hook.Ref, "Directory", repoDir, @@ -365,7 +368,8 @@ func runConfigHook(log log15.Logger, config conf.Config, hookName string, return } - // TODO do this in conf.SetDefaults() + // Validation ensures that local hooks have a dockerfile path + // Plugins default to Dockerfile if dockerFilePath == "" { dockerFilePath = "Dockerfile" } @@ -373,7 +377,7 @@ func runConfigHook(log log15.Logger, config conf.Config, hookName string, dockerFilePath = path.Join(repoDir, dockerFilePath) } - imageName := fmt.Sprintf("%s-%s-%d", config.ServiceName, hookName, hookIndex) + imageName := fmt.Sprintf("%s-%s-%d-%d", config.ServiceName, hookName, hookIndex, hookCounter) if !buildAndRun(log, imageName, dockerFilePath, runArgs, opts) { return @@ -386,6 +390,12 @@ func runConfigHook(log log15.Logger, config conf.Config, hookName string, func buildAndRun(log log15.Logger, imageName, dockerFilePath string, runArgs []string, opts *Opts) (success bool) { + log = log.New("Dockerfile", dockerFilePath, "ImageName", imageName) + + log.Debug("buildAndRun() BEGIN") + defer log.Debug("buildAndRun() END") + log.Info("docker build") + dockerBuildCmd := exec.Command("docker", "build", "-t", imageName, "-f", dockerFilePath, @@ -406,6 +416,8 @@ func buildAndRun(log log15.Logger, imageName, dockerFilePath string, runCmd.Stdout = opts.RunStdout runCmd.Stderr = opts.RunStderr + log.Debug("docker run", "Args", runArgs) + err = runCmd.Run() if err != nil { log.Error("docker run", "Error", err) From e479883ed7131359fd685d4e7169c8f89e4c2f0e Mon Sep 17 00:00:00 2001 From: Brandon Cook Date: Tue, 6 Sep 2016 13:25:34 -0700 Subject: [PATCH 2/4] optional -x --- files/porter_bootstrap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/porter_bootstrap b/files/porter_bootstrap index 6187d57..436df4b 100644 --- a/files/porter_bootstrap +++ b/files/porter_bootstrap @@ -1,4 +1,4 @@ -#!/bin/bash -ex +#!/bin/bash -e{{ if .LogDebug -}}x{{- end }} echo "porter bootstrap BEGIN" export PORTER_VERSION={{ .PorterVersion }} From 691863af37c806b93d9a30a5bb086489b61e1f54 Mon Sep 17 00:00:00 2001 From: Brandon Cook Date: Tue, 6 Sep 2016 13:25:45 -0700 Subject: [PATCH 3/4] relative path --- files/porter_hotswap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/porter_hotswap b/files/porter_hotswap index 86e008b..a020066 100644 --- a/files/porter_hotswap +++ b/files/porter_hotswap @@ -4,7 +4,7 @@ aws s3 cp s3://{{ .ServicePayloadBucket }}/{{ .ServicePayloadKey }} payload.tar # load all the containers in this tar echo "loading containers" {{ range $imageName := .ImageNames -}} -tar -xOf /payload.tar ./{{ $imageName }}.docker | docker load +tar -xOf payload.tar ./{{ $imageName }}.docker | docker load {{ end -}} echo "starting containers" From b64ba96a8fc96cbb9ab286392f749229c4f44e1f Mon Sep 17 00:00:00 2001 From: Brandon Cook Date: Tue, 6 Sep 2016 13:28:10 -0700 Subject: [PATCH 4/4] more hook config permutations --- testintegration/.porter/config | 9 +++++++++ testintegration/Dockerfile | 7 +++++++ testintegration/Dockerfile.cmd | 3 +++ testintegration/hooks/ec2-bootstrap.cmd | 2 +- testintegration/plugins/ec2-bootstrap | 7 +++++++ testintegration/plugins/ec2-bootstrap.cmd | 3 +++ 6 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 testintegration/Dockerfile create mode 100644 testintegration/Dockerfile.cmd create mode 100644 testintegration/plugins/ec2-bootstrap create mode 100644 testintegration/plugins/ec2-bootstrap.cmd diff --git a/testintegration/.porter/config b/testintegration/.porter/config index f194984..cae56f3 100644 --- a/testintegration/.porter/config +++ b/testintegration/.porter/config @@ -47,6 +47,15 @@ hooks: environment: DEV_MODE: FOO: bar + concurrent: true + - repo: $TEST_REPO_URL + ref: $TEST_REPO_BRANCH + dockerfile: testintegration/plugins/ec2-bootstrap + concurrent: true + - repo: $TEST_REPO_URL + ref: $TEST_REPO_BRANCH + dockerfile: testintegration/plugins/ec2-bootstrap + concurrent: true _container_base_inet: &CONTAINER_BASE_INET name: inet diff --git a/testintegration/Dockerfile b/testintegration/Dockerfile new file mode 100644 index 0000000..81948ad --- /dev/null +++ b/testintegration/Dockerfile @@ -0,0 +1,7 @@ +FROM ubuntu:16.04 + +ADD ec2-bootstrap.cmd / +RUN chmod 544 /ec2-bootstrap.cmd + +# TODO generate config +CMD /ec2-bootstrap.cmd diff --git a/testintegration/Dockerfile.cmd b/testintegration/Dockerfile.cmd new file mode 100644 index 0000000..b3088ce --- /dev/null +++ b/testintegration/Dockerfile.cmd @@ -0,0 +1,3 @@ +#!/bin/bash + +echo "DEV_MODE $DEV_MODE" 1>&2 diff --git a/testintegration/hooks/ec2-bootstrap.cmd b/testintegration/hooks/ec2-bootstrap.cmd index ffb619e..b3088ce 100644 --- a/testintegration/hooks/ec2-bootstrap.cmd +++ b/testintegration/hooks/ec2-bootstrap.cmd @@ -1,3 +1,3 @@ -#!/bin/bash -x +#!/bin/bash echo "DEV_MODE $DEV_MODE" 1>&2 diff --git a/testintegration/plugins/ec2-bootstrap b/testintegration/plugins/ec2-bootstrap new file mode 100644 index 0000000..81948ad --- /dev/null +++ b/testintegration/plugins/ec2-bootstrap @@ -0,0 +1,7 @@ +FROM ubuntu:16.04 + +ADD ec2-bootstrap.cmd / +RUN chmod 544 /ec2-bootstrap.cmd + +# TODO generate config +CMD /ec2-bootstrap.cmd diff --git a/testintegration/plugins/ec2-bootstrap.cmd b/testintegration/plugins/ec2-bootstrap.cmd new file mode 100644 index 0000000..b3088ce --- /dev/null +++ b/testintegration/plugins/ec2-bootstrap.cmd @@ -0,0 +1,3 @@ +#!/bin/bash + +echo "DEV_MODE $DEV_MODE" 1>&2