forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/144
Signed-off-by: Javier Romero <j.romero.1214@gmail.com>
- Loading branch information
Showing
68 changed files
with
1,701 additions
and
2,408 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
FROM hashicorp/http-echo | ||
CMD ["-text=\"hello world\""] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.