Skip to content

Commit

Permalink
Merge branch 'master' into feature/144
Browse files Browse the repository at this point in the history
Signed-off-by: Javier Romero <j.romero.1214@gmail.com>
  • Loading branch information
jromero committed Apr 10, 2019
2 parents 3f82a83 + bba5090 commit 53ef503
Show file tree
Hide file tree
Showing 68 changed files with 1,701 additions and 2,408 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM hashicorp/http-echo
CMD ["-text=\"hello world\""]
14 changes: 7 additions & 7 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ import (
"github.com/buildpack/lifecycle"
dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"

"github.com/buildpack/pack/cache"
"github.com/buildpack/pack/docker"
h "github.com/buildpack/pack/testhelpers"
)

var (
packPath string
dockerCli *docker.Client
dockerCli *client.Client
registryConfig *h.TestRegistryConfig
lifecycleVersion = "0.1.0"
runImage = "pack-test/run"
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestAcceptance(t *testing.T) {
}

var err error
dockerCli, err = docker.New()
dockerCli, err = client.NewClientWithOpts(client.FromEnv, client.WithVersion("1.38"))
h.AssertNil(t, err)
registryConfig = h.RunRegistry(t, false)
defer registryConfig.StopRegistry(t)
Expand Down Expand Up @@ -340,7 +340,7 @@ func testAcceptance(t *testing.T, when spec.G, it spec.S) {
assertMockAppRunsWithOutput(t, repoName, "Launch Dep Contents", "Cached Dep Contents")

t.Log("pulls the run image")
h.AssertContains(t, output, fmt.Sprintf("Pulling run image '%s'", runImageName))
h.AssertContains(t, output, fmt.Sprintf("Pulling image '%s'", runImageName))

t.Log("uses the run image as the base image")
assertHasBase(t, repoName, runImageName)
Expand Down Expand Up @@ -724,7 +724,7 @@ func createBuilder(t *testing.T, runImageMirror string) string {
return builder
}

func createStack(t *testing.T, dockerCli *docker.Client) {
func createStack(t *testing.T, dockerCli *client.Client) {
t.Log("create stack images")
createStackImage(t, dockerCli, runImage, filepath.Join("testdata", "mock_stack", "run"))
createStackImage(t, dockerCli, buildImage, filepath.Join("testdata", "mock_stack", "build"))
Expand All @@ -733,7 +733,7 @@ func createStack(t *testing.T, dockerCli *docker.Client) {
h.AssertNil(t, h.PushImage(dockerCli, runImageMirror, registryConfig))
}

func createStackImage(t *testing.T, dockerCli *docker.Client, repoName string, dir string) {
func createStackImage(t *testing.T, dockerCli *client.Client, repoName string, dir string) {
ctx := context.Background()
buildContext, _ := archive.CreateTarReader(dir, "/", 0, 0)

Expand Down Expand Up @@ -891,7 +891,7 @@ func terminateAtStep(t *testing.T, cmd *exec.Cmd, buf *bytes.Buffer, pattern str
}
}

func imageLabel(t *testing.T, dockerCli *docker.Client, repoName, labelName string) string {
func imageLabel(t *testing.T, dockerCli *client.Client, repoName, labelName string) string {
t.Helper()
inspect, _, err := dockerCli.ImageInspectWithRaw(context.Background(), repoName)
h.AssertNil(t, err)
Expand Down
8 changes: 8 additions & 0 deletions app/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package app

import "github.com/buildpack/pack/logging"

type Image struct {
RepoName string
Logger *logging.Logger
}
98 changes: 98 additions & 0 deletions app/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package app

import (
"context"
"fmt"
"strconv"
"strings"

"github.com/docker/docker/api/types"
dcontainer "github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/pkg/errors"

"github.com/buildpack/pack/container"
"github.com/buildpack/pack/logging"
)

func (i *Image) Run(ctx context.Context, docker *client.Client, ports []string) error {
if ports == nil {
var err error
ports, err = exposedPorts(ctx, docker, i.RepoName)
if err != nil {
return err
}
}

parsedPorts, portBindings, err := parsePorts(ports)
if err != nil {
return err
}

ctr, err := docker.ContainerCreate(ctx, &dcontainer.Config{
Image: i.RepoName,
AttachStdout: true,
AttachStderr: true,
ExposedPorts: parsedPorts,
Labels: map[string]string{"author": "pack"},
}, &dcontainer.HostConfig{
AutoRemove: true,
PortBindings: portBindings,
}, nil, "")
if err != nil {
return err
}
defer docker.ContainerRemove(context.Background(), ctr.ID, types.ContainerRemoveOptions{Force: true})

logContainerListening(i.Logger, portBindings)
if err = container.Run(ctx, docker, ctr.ID, i.Logger.VerboseWriter(), i.Logger.VerboseErrorWriter()); err != nil {
return errors.Wrap(err, "run container")
}

return nil
}

func exposedPorts(ctx context.Context, docker *client.Client, imageID string) ([]string, error) {
i, _, err := docker.ImageInspectWithRaw(ctx, imageID)
if err != nil {
return nil, err
}
var ports []string
for port := range i.Config.ExposedPorts {
ports = append(ports, port.Port())
}
return ports, nil
}

func parsePorts(ports []string) (nat.PortSet, nat.PortMap, error) {
for i, p := range ports {
p = strings.TrimSpace(p)
if _, err := strconv.Atoi(p); err == nil {
// default simple port to localhost and inside the container
p = fmt.Sprintf("127.0.0.1:%s:%s/tcp", p, p)
}
ports[i] = p
}

return nat.ParsePortSpecs(ports)
}

func logContainerListening(logger *logging.Logger, portBindings nat.PortMap) {
// TODO handle case with multiple ports, for now when there is more than
// one port we assume you know what you're doing and don't need guidance
if len(portBindings) == 1 {
for _, bindings := range portBindings {
if len(bindings) == 1 {
binding := bindings[0]
host := binding.HostIP
port := binding.HostPort
if host == "127.0.0.1" {
host = "localhost"
}
// TODO the service may not be http based
logger.Info("Starting container listening at http://%s:%s/\n", host, port)
}
}
}
}
Loading

0 comments on commit 53ef503

Please sign in to comment.