Skip to content

Commit

Permalink
Issues/77 (#87)
Browse files Browse the repository at this point in the history
* add rebuild option

* fixes; #77
  • Loading branch information
komuw authored Feb 24, 2018
1 parent d0997de commit d0cbd5d
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 16 deletions.
15 changes: 14 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
if [ "$num_containers" != "11" ]; then
echo "wanted 11 containers, got $num_containers" && exit 500
fi
# stop the containers running from the previous command
# remove the containers running from the previous command
- run: docker ps -aq | xargs docker rm -f; docker image prune -fa; docker system prune -af
- run: cp meli testdata/ && cd testdata/ && ./meli -up -d
- run:
Expand All @@ -82,6 +82,19 @@ jobs:
if [ "$num_containers" != "11" ]; then
echo "wanted 11 containers, got $num_containers" && exit 500
fi
# test rebuild, stop (BUT DO NOT remove) containers from previous command
- run: docker ps -aq | xargs docker stop
- run: ./meli -up -d -f testdata/docker-compose.yml -build
- run:
name: echo number of running containers (expected=11), actual;
command: |
all_containers=$(docker ps -aq | wc -l)
running_containers=$(docker ps -q | wc -l)
echo "number of all containers; $all_containers"
echo "number of running containers are; $running_containers"
if [ "$running_containers" != "11" ]; then
echo "wanted 11 containers, got $running_containers" && exit 500
fi
- run:
name: go vet
command: source /etc/profile && go vet -v -shadow ./...
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Usage of meli:
-d option runs containers in the background
-f string
path to docker-compose.yml file. (default "docker-compose.yml")
-build
Rebuild services
-v Show version information.
-version
Show version information.
Expand Down
15 changes: 11 additions & 4 deletions api/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func CreateContainer(ctx context.Context, cli MeliAPiClient, dc *DockerContainer
}

// reuse container if already running
// only reuse containers if we aren't rebuilding
meliService := labelsMap["meli_service"]
filters := filters.NewArgs()
filters.Add("label", fmt.Sprintf("meli_service=%s", meliService))
Expand All @@ -36,8 +37,14 @@ func CreateContainer(ctx context.Context, cli MeliAPiClient, dc *DockerContainer
fmt.Println(" :unable to list containers")
}
if len(containers) > 0 {
dc.UpdateContainerID(containers[0].ID)
return true, containers[0].ID, nil
if !dc.Rebuild {
dc.UpdateContainerID(containers[0].ID)
return true, containers[0].ID, nil
}
shadowErr := cli.ContainerRemove(ctx, containers[0].ID, types.ContainerRemoveOptions{Force: true})
if err != nil {
fmt.Println(shadowErr, " :unable to remove existing container, ", containers[0].ID)
}
}

// 2. make ports
Expand All @@ -51,7 +58,7 @@ func CreateContainer(ctx context.Context, cli MeliAPiClient, dc *DockerContainer
myPortBinding := nat.PortBinding{HostPort: hostport}
port, shadowErr := nat.NewPort("tcp", containerport)
if shadowErr != nil {
fmt.Println(err, " :unable to create a nat.Port")
fmt.Println(shadowErr, " :unable to create a nat.Port")
}
portsMap[port] = EmptyStruct{}
portBindingMap[port] = []nat.PortBinding{myPortBinding}
Expand Down Expand Up @@ -80,7 +87,7 @@ func CreateContainer(ctx context.Context, cli MeliAPiClient, dc *DockerContainer
if dc.ComposeService.Build != (Buildstruct{}) {
imageName, shadowErr := BuildDockerImage(ctx, cli, dc)
if shadowErr != nil {
return false, "", &popagateError{originalErr: err}
return false, "", &popagateError{originalErr: shadowErr}
}
// done this way so that we can manipulate the value of the
// imageName inside this scope
Expand Down
23 changes: 18 additions & 5 deletions api/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (

func TestCreateContainer(t *testing.T) {
tt := []struct {
dc *DockerContainer
expected string
expectedErr error
dc *DockerContainer
expected string
containerAlreadycreated bool
expectedErr error
}{
{
&DockerContainer{
Expand All @@ -21,20 +22,32 @@ func TestCreateContainer(t *testing.T) {
ContainerID: "myExistingContainerId00912",
},
"myExistingContainerId00912",
true,
nil},
{
&DockerContainer{
ComposeService: ComposeService{Image: "busybox", Restart: "unless-stopped"},
ServiceName: "myservice",
NetworkName: "myNetworkName",
DockerComposeFile: "DockerFile",
ContainerID: "myContainerId001",
Rebuild: true,
},
"myContainerId001",
false,
nil},
}
var ctx = context.Background()
cli := &MockDockerClient{}
for _, v := range tt {
//CreateContainer(ctx context.Context, cli MeliAPiClient, dc *DockerContainer)
alreadyCreated, actual, err := CreateContainer(ctx, cli, v.dc)
if err != nil {
t.Errorf("\nCalled CreateContainer(%#+v) \ngot %s \nwanted %#+v", v.dc, err, v.expectedErr)
}
if actual != v.expected {
t.Errorf("\nCalled CreateContainer(%#+v) \ngot %s \nwanted %#+v", v.dc, actual, v.expected)
}
if alreadyCreated != true {
if alreadyCreated != v.containerAlreadycreated {
t.Errorf("\nCalled CreateContainer(%#+v) \ngot %#+v \nwanted %#+v", v.dc, alreadyCreated, true)
}
}
Expand Down
2 changes: 1 addition & 1 deletion api/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func BuildDockerImage(ctx context.Context, cli MeliAPiClient, dc *DockerContaine
//Squash: true, currently only supported in experimenta mode
Tags: []string{imageName},
Remove: true, //remove intermediary containers after build
ForceRemove: true,
NoCache: dc.Rebuild,
SuppressOutput: false,
Dockerfile: dockerFileName,
Context: dockerFileTarReader,
Expand Down
11 changes: 11 additions & 0 deletions api/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ func TestBuildDockerImage(t *testing.T) {
LogMedium: ioutil.Discard},
"meli_myservicename",
nil},
{
&DockerContainer{
ServiceName: "myservicename",
DockerComposeFile: "docker-compose.yml",
ComposeService: ComposeService{
Build: Buildstruct{Dockerfile: "../testdata/Dockerfile"}},
LogMedium: ioutil.Discard,
Rebuild: true,
},
"meli_myservicename",
nil},
}

var ctx = context.Background()
Expand Down
5 changes: 5 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type DockerContainer struct {
ContainerID string // this assumes that there can only be one container per docker-compose service
LogMedium io.Writer
CurentDir string
Rebuild bool
}

func (dc *DockerContainer) UpdateContainerID(containerID string) {
Expand All @@ -67,6 +68,7 @@ type MeliAPiClient interface {
NetworkConnect(ctx context.Context, networkID, containerID string, config *network.EndpointSettings) error
VolumeCreate(ctx context.Context, options volumetypes.VolumesCreateBody) (types.Volume, error)
ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error)
ContainerRemove(ctx context.Context, containerID string, options types.ContainerRemoveOptions) error
}

type MockDockerClient struct{}
Expand Down Expand Up @@ -107,6 +109,9 @@ func (m *MockDockerClient) VolumeCreate(ctx context.Context, options volumetypes
func (m *MockDockerClient) ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error) {
return []types.Container{types.Container{ID: "myExistingContainerId00912"}}, nil
}
func (m *MockDockerClient) ContainerRemove(ctx context.Context, containerID string, options types.ContainerRemoveOptions) error {
return nil
}

type ImageProgress struct {
Status string `json:"status,omitempty"`
Expand Down
16 changes: 13 additions & 3 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"os"
)

func Cli() (bool, bool, string) {
func Cli() (bool, bool, bool, string) {
// TODO; use a more sensible cli lib.
var showVersion bool
var up bool
var d bool
var build bool
var dockerComposeFile = "docker-compose.yml"
var followLogs = true
var rebuild = false

flag.BoolVar(
&showVersion,
Expand All @@ -34,6 +36,11 @@ func Cli() (bool, bool, string) {
"d",
false,
"Run containers in the background")
flag.BoolVar(
&build,
"build",
false,
"Rebuild services")
flag.StringVar(
&dockerComposeFile,
"f",
Expand All @@ -43,7 +50,7 @@ func Cli() (bool, bool, string) {
flag.Parse()

if showVersion {
return true, followLogs, ""
return false, true, followLogs, ""
}
if !up {
fmt.Println("to use Meli, run: \n\n\t meli -up")
Expand All @@ -52,6 +59,9 @@ func Cli() (bool, bool, string) {
if d {
followLogs = false
}
if build {
rebuild = true
}

return false, followLogs, dockerComposeFile
return false, followLogs, rebuild, dockerComposeFile
}
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
var version string

func main() {
showVersion, followLogs, dockerComposeFile := cli.Cli()
showVersion, followLogs, rebuild, dockerComposeFile := cli.Cli()
if showVersion {
fmt.Println("Meli version: ", version)
os.Exit(0)
Expand Down Expand Up @@ -88,7 +88,8 @@ func main() {
FollowLogs: followLogs,
DockerComposeFile: dockerComposeFile,
LogMedium: os.Stdout,
CurentDir: dotFormattedrCurentDir}
CurentDir: dotFormattedrCurentDir,
Rebuild: rebuild}
go startComposeServices(ctx, cli, &wg, dc)
}
wg.Wait()
Expand Down

0 comments on commit d0cbd5d

Please sign in to comment.