Upgrade libkermit/compose version

This commit is contained in:
NicoMen 2017-09-07 15:14:03 +02:00 committed by Traefiker
parent 2b863d9bc2
commit 7fd1eb3780
4 changed files with 88 additions and 16 deletions

4
glide.lock generated
View file

@ -1,4 +1,4 @@
hash: 5e9aeda91bffc11f916ff6c324b26a02a279ed1d691e7a783ca03dfab584083c hash: 68fad31b1980075ca1e93f3883e1bbeb95022824c63defbecb4eafb240fded39
updated: 2017-08-25T11:52:16.848940186+02:00 updated: 2017-08-25T11:52:16.848940186+02:00
imports: imports:
- name: cloud.google.com/go - name: cloud.google.com/go
@ -783,7 +783,7 @@ testImports:
- name: github.com/gorilla/mux - name: github.com/gorilla/mux
version: e444e69cbd2e2e3e0749a2f3c717cec491552bbf version: e444e69cbd2e2e3e0749a2f3c717cec491552bbf
- name: github.com/libkermit/compose - name: github.com/libkermit/compose
version: 6167fd37267d2dfbcbcbee0a321adaa9531f4062 version: 4a33a16f1446ba205c4da7b09105d5bdc293b432
subpackages: subpackages:
- check - check
- name: github.com/libkermit/docker - name: github.com/libkermit/docker

View file

@ -210,7 +210,7 @@ testImport:
- package: github.com/go-check/check - package: github.com/go-check/check
version: 11d3bc7aa68e238947792f30573146a3231fc0f1 version: 11d3bc7aa68e238947792f30573146a3231fc0f1
- package: github.com/libkermit/compose - package: github.com/libkermit/compose
version: 6167fd37267d2dfbcbcbee0a321adaa9531f4062 version: 4a33a16f1446ba205c4da7b09105d5bdc293b432
subpackages: subpackages:
- check - check
- package: github.com/libkermit/docker - package: github.com/libkermit/docker

View file

@ -5,8 +5,10 @@ package check
import ( import (
"github.com/go-check/check" "github.com/go-check/check"
"fmt"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/libkermit/compose" "github.com/libkermit/compose"
"strings"
) )
// Project holds compose related project attributes // Project holds compose related project attributes
@ -25,16 +27,44 @@ func CreateProject(c *check.C, name string, composeFiles ...string) *Project {
} }
} }
// Start creates and starts the compose project. // Start creates and starts services of the compose project,if no services are given, all the services are created/started.
func (p *Project) Start(c *check.C) { func (p *Project) Start(c *check.C, services ...string) {
c.Assert(p.project.Start(), check.IsNil, errString := "error while creating and starting compose project"
check.Commentf("error while starting compose project")) if services != nil && len(services) > 0 {
errString = fmt.Sprintf("error while creating and starting services %s compose project", strings.Join(services, ","))
}
c.Assert(p.project.Start(services...), check.IsNil,
check.Commentf(errString))
} }
// Stop shuts down and clean the project // StartOnly starts services of the compose project,if no services are given, all the services are started.
func (p *Project) Stop(c *check.C) { func (p *Project) StartOnly(c *check.C, services ...string) {
c.Assert(p.project.Stop(), check.IsNil, errString := "error while starting compose project"
check.Commentf("error while stopping compose project")) if services != nil && len(services) > 0 {
errString = fmt.Sprintf("error while starting services %s compose project", strings.Join(services, ","))
}
c.Assert(p.project.StartOnly(services...), check.IsNil,
check.Commentf(errString))
}
// StopOnly stops services of the compose project,if no services are given, all the services are stopped.
func (p *Project) StopOnly(c *check.C, services ...string) {
errString := "error while stopping deleting compose project"
if services != nil && len(services) > 0 {
errString = fmt.Sprintf("error while stopping deleting services %s compose project", strings.Join(services, ","))
}
c.Assert(p.project.StopOnly(services...), check.IsNil,
check.Commentf(errString))
}
// Stop shuts down and clean services of the compose project,if no services are given, all the services are stopped/deleted.
func (p *Project) Stop(c *check.C, services ...string) {
errString := "error while stopping and deleting compose project"
if services != nil && len(services) > 0 {
errString = fmt.Sprintf("error while stopping and deleting services %s compose project", strings.Join(services, ","))
}
c.Assert(p.project.Stop(services...), check.IsNil,
check.Commentf(errString))
} }
// Scale scale a service up // Scale scale a service up
@ -59,3 +89,15 @@ func (p *Project) Container(c *check.C, service string) types.ContainerJSON {
check.Commentf("error while getting the container for service '%s'", service)) check.Commentf("error while getting the container for service '%s'", service))
return container return container
} }
// NoContainer check is there is no container for the service given
// It fails if there one or more containers or if the error returned
// does not indicate an empty container list
func (p *Project) NoContainer(c *check.C, service string) {
validErr := "No container found for '" + service + "' service"
_, err := p.project.Container(service)
c.Assert(err, check.NotNil,
check.Commentf("error while getting the container for service '%s'", service))
c.Assert(err.Error(), check.Equals, validErr,
check.Commentf("error while getting the container for service '%s'", service))
}

View file

@ -23,6 +23,7 @@ import (
// Project holds compose related project attributes // Project holds compose related project attributes
type Project struct { type Project struct {
composeFiles []string
composeProject project.APIProject composeProject project.APIProject
name string name string
listenChan chan events.Event listenChan chan events.Event
@ -30,6 +31,7 @@ type Project struct {
stopped chan struct{} stopped chan struct{}
deleted chan struct{} deleted chan struct{}
client client.APIClient client client.APIClient
hasOpenedChan bool
} }
// CreateProject creates a compose project with the given name based on the // CreateProject creates a compose project with the given name based on the
@ -58,6 +60,7 @@ func CreateProject(name string, composeFiles ...string) (*Project, error) {
return nil, err return nil, err
} }
p := &Project{ p := &Project{
composeFiles: composeFiles,
composeProject: composeProject, composeProject: composeProject,
name: name, name: name,
listenChan: make(chan events.Event), listenChan: make(chan events.Event),
@ -65,6 +68,7 @@ func CreateProject(name string, composeFiles ...string) (*Project, error) {
stopped: make(chan struct{}), stopped: make(chan struct{}),
deleted: make(chan struct{}), deleted: make(chan struct{}),
client: apiClient, client: apiClient,
hasOpenedChan: true,
} }
// Listen to compose events // Listen to compose events
@ -76,6 +80,11 @@ func CreateProject(name string, composeFiles ...string) (*Project, error) {
// Start creates and starts the compose project. // Start creates and starts the compose project.
func (p *Project) Start(services ...string) error { func (p *Project) Start(services ...string) error {
// If project chan are closed, recreate new compose project
if !p.hasOpenedChan {
newProject, _ := CreateProject(p.name, p.composeFiles...)
*p = *newProject
}
ctx := context.Background() ctx := context.Background()
err := p.composeProject.Create(ctx, options.Create{}) err := p.composeProject.Create(ctx, options.Create{})
if err != nil { if err != nil {
@ -94,7 +103,6 @@ func (p *Project) StartOnly(services ...string) error {
} }
// Wait for compose to start // Wait for compose to start
<-p.started <-p.started
close(p.started)
return nil return nil
} }
@ -106,7 +114,6 @@ func (p *Project) StopOnly(services ...string) error {
return err return err
} }
<-p.stopped <-p.stopped
close(p.stopped)
return nil return nil
} }
@ -117,16 +124,39 @@ func (p *Project) Stop(services ...string) error {
if err != nil { if err != nil {
return err return err
} }
ctx := context.Background()
err = p.composeProject.Delete(ctx, options.Delete{}, services...) err = p.composeProject.Delete(context.Background(), options.Delete{}, services...)
if err != nil { if err != nil {
return err return err
} }
<-p.deleted <-p.deleted
existingContainers, err := p.existContainers(project.AnyState)
if err != nil {
return err
}
// Close channels only if there are no running services
if !existingContainers {
p.hasOpenedChan = false
close(p.started)
close(p.stopped)
close(p.deleted) close(p.deleted)
close(p.listenChan)
}
return nil return nil
} }
// Check if containers exist in the desirated state for the given services
func (p *Project) existContainers(stateFiltered project.State, services ...string) (bool, error) {
existingContainers := false
var err error
containersFound, err := p.composeProject.Containers(context.Background(), project.Filter{stateFiltered})
if err == nil && containersFound != nil && len(containersFound) > 0 {
existingContainers = true
}
return existingContainers, err
}
// Scale scale a service up // Scale scale a service up
func (p *Project) Scale(service string, count int) error { func (p *Project) Scale(service string, count int) error {
return p.composeProject.Scale(context.Background(), 10, map[string]int{ return p.composeProject.Scale(context.Background(), 10, map[string]int{