Skip to content
This repository has been archived by the owner on Aug 5, 2020. It is now read-only.

Commit

Permalink
Merge pull request #77 from phylake/ec2-hook
Browse files Browse the repository at this point in the history
fix ec2-bootstrap hook naming collision
  • Loading branch information
phylake authored Sep 6, 2016
2 parents 787299d + b64ba96 commit addfbae
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 12 deletions.
2 changes: 1 addition & 1 deletion files/porter_bootstrap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash -ex
#!/bin/bash -e{{ if .LogDebug -}}x{{- end }}
echo "porter bootstrap BEGIN"

export PORTER_VERSION={{ .PorterVersion }}
Expand Down
2 changes: 1 addition & 1 deletion files/porter_hotswap
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
30 changes: 21 additions & 9 deletions hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -365,15 +368,16 @@ 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"
}

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
Expand All @@ -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,
Expand All @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions testintegration/.porter/config
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions testintegration/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions testintegration/Dockerfile.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "DEV_MODE $DEV_MODE" 1>&2
2 changes: 1 addition & 1 deletion testintegration/hooks/ec2-bootstrap.cmd
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash -x
#!/bin/bash

echo "DEV_MODE $DEV_MODE" 1>&2
7 changes: 7 additions & 0 deletions testintegration/plugins/ec2-bootstrap
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions testintegration/plugins/ec2-bootstrap.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "DEV_MODE $DEV_MODE" 1>&2

0 comments on commit addfbae

Please sign in to comment.