1
0
mirror of https://github.com/rancher/os.git synced 2025-06-30 16:51:47 +00:00

Update to newer compose

This commit is contained in:
Darren Shepherd 2015-05-11 01:18:42 -07:00
parent c556b8b231
commit 7f4c339034
7 changed files with 213 additions and 163 deletions

8
Godeps/Godeps.json generated
View File

@ -172,13 +172,13 @@
}, },
{ {
"ImportPath": "github.com/rancherio/rancher-compose/librcompose/docker", "ImportPath": "github.com/rancherio/rancher-compose/librcompose/docker",
"Comment": "0.1.0-22-gb05516d", "Comment": "0.1.0-25-g22c2b87",
"Rev": "b05516d2ddc60579cf34ff89d4214bf3e6421e45" "Rev": "22c2b870151ab3fa7f3727cfc4504e326ea512e1"
}, },
{ {
"ImportPath": "github.com/rancherio/rancher-compose/librcompose/project", "ImportPath": "github.com/rancherio/rancher-compose/librcompose/project",
"Comment": "0.1.0-22-gb05516d", "Comment": "0.1.0-25-g22c2b87",
"Rev": "b05516d2ddc60579cf34ff89d4214bf3e6421e45" "Rev": "22c2b870151ab3fa7f3727cfc4504e326ea512e1"
}, },
{ {
"ImportPath": "github.com/ryanuber/go-glob", "ImportPath": "github.com/ryanuber/go-glob",

View File

@ -43,7 +43,7 @@ func Convert(c *project.ServiceConfig) (*runconfig.Config, *runconfig.HostConfig
Hostname: c.Hostname, Hostname: c.Hostname,
Domainname: c.DomainName, Domainname: c.DomainName,
User: c.User, User: c.User,
Env: c.Environment, Env: c.Environment.Slice(),
Cmd: runconfig.NewCommand(cmd...), Cmd: runconfig.NewCommand(cmd...),
Image: c.Image, Image: c.Image,
Labels: labels, Labels: labels,

View File

@ -5,8 +5,9 @@ import (
"errors" "errors"
"strings" "strings"
log "github.com/Sirupsen/logrus"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
log "github.com/Sirupsen/logrus"
) )
type ServiceState string type ServiceState string
@ -33,9 +34,9 @@ func NewProject(name string, factory ServiceFactory) *Project {
func (p *Project) CreateService(name string, config ServiceConfig) (Service, error) { func (p *Project) CreateService(name string, config ServiceConfig) (Service, error) {
if p.EnvironmentLookup != nil { if p.EnvironmentLookup != nil {
parsedEnv := make([]string, 0, len(config.Environment)) parsedEnv := make([]string, 0, len(config.Environment.Slice()))
for _, env := range config.Environment { for _, env := range config.Environment.Slice() {
if strings.IndexRune(env, '=') != -1 { if strings.IndexRune(env, '=') != -1 {
parsedEnv = append(parsedEnv, env) parsedEnv = append(parsedEnv, env)
continue continue
@ -46,7 +47,7 @@ func (p *Project) CreateService(name string, config ServiceConfig) (Service, err
} }
} }
config.Environment = parsedEnv config.Environment = NewMaporslice(parsedEnv)
} }
return p.factory.Create(p, name, &config) return p.factory.Create(p, name, &config)
@ -56,6 +57,7 @@ func (p *Project) AddConfig(name string, config *ServiceConfig) error {
p.Notify(SERVICE_ADD, name, nil) p.Notify(SERVICE_ADD, name, nil)
p.configs[name] = config p.configs[name] = config
p.reload = append(p.reload, name)
return nil return nil
} }
@ -77,10 +79,8 @@ func (p *Project) Load(bytes []byte) error {
return nil return nil
} }
func (p *Project) Up() error { func (p *Project) loadWrappers(wrappers map[string]*serviceWrapper) error {
wrappers := make(map[string]*serviceWrapper) for _, name := range p.reload {
for name, _ := range p.configs {
wrapper, err := newServiceWrapper(name, p) wrapper, err := newServiceWrapper(name, p)
if err != nil { if err != nil {
return err return err
@ -88,9 +88,17 @@ func (p *Project) Up() error {
wrappers[name] = wrapper wrappers[name] = wrapper
} }
p.reload = []string{}
return nil
}
func (p *Project) Up() error {
wrappers := make(map[string]*serviceWrapper)
p.Notify(PROJECT_UP_START, "", nil) p.Notify(PROJECT_UP_START, "", nil)
err := p.startAll(wrappers, 0) err := p.startAll(wrappers)
if err == nil { if err == nil {
p.Notify(PROJECT_UP_DONE, "", nil) p.Notify(PROJECT_UP_DONE, "", nil)
@ -99,17 +107,17 @@ func (p *Project) Up() error {
return err return err
} }
func (p *Project) startAll(wrappers map[string]*serviceWrapper, level int) error { func (p *Project) startAll(wrappers map[string]*serviceWrapper) error {
restart := false restart := false
if level > 0 { for _, wrapper := range wrappers {
for _, wrapper := range wrappers { if err := wrapper.Reset(); err != nil {
if err := wrapper.Reset(); err != nil { return err
return err
}
} }
} }
p.loadWrappers(wrappers)
for _, wrapper := range wrappers { for _, wrapper := range wrappers {
go wrapper.Start(wrappers) go wrapper.Start(wrappers)
} }
@ -134,7 +142,7 @@ func (p *Project) startAll(wrappers map[string]*serviceWrapper, level int) error
log.Errorf("Failed calling callback: %v", err) log.Errorf("Failed calling callback: %v", err)
} }
} }
return p.startAll(wrappers, level+1) return p.startAll(wrappers)
} else { } else {
return firstError return firstError
} }

View File

@ -1,11 +1,6 @@
package project package project
import ( import "github.com/rancherio/go-rancher/client"
"strings"
"github.com/rancherio/go-rancher/client"
"gopkg.in/yaml.v2"
)
type Event string type Event string
@ -26,137 +21,38 @@ const (
PROJECT_RELOAD_TRIGGER = Event("Triggering project reload") PROJECT_RELOAD_TRIGGER = Event("Triggering project reload")
) )
type Stringorslice struct {
parts []string
}
func (s *Stringorslice) MarshalYAML() (interface{}, error) {
if s == nil {
return nil, nil
}
bytes, err := yaml.Marshal(s.Slice())
return string(bytes), err
}
func (s *Stringorslice) UnmarshalYAML(unmarshal func(interface{}) error) error {
var sliceType []string
err := unmarshal(&sliceType)
if err == nil {
s.parts = sliceType
return nil
}
var stringType string
err = unmarshal(&stringType)
if err == nil {
sliceType = make([]string, 0, 1)
s.parts = append(sliceType, string(stringType))
return nil
}
return err
}
func (s *Stringorslice) Len() int {
if s == nil {
return 0
}
return len(s.parts)
}
func (s *Stringorslice) Slice() []string {
if s == nil {
return nil
}
return s.parts
}
func NewStringorslice(parts ...string) *Stringorslice {
return &Stringorslice{parts}
}
type SliceorMap struct {
parts map[string]string
}
func (s *SliceorMap) MarshalYAML() (interface{}, error) {
if s == nil {
return nil, nil
}
bytes, err := yaml.Marshal(s.MapParts())
return string(bytes), err
}
func (s *SliceorMap) UnmarshalYAML(unmarshal func(interface{}) error) error {
mapType := make(map[string]string)
err := unmarshal(&mapType)
if err == nil {
s.parts = mapType
return nil
}
var sliceType []string
var keyValueSlice []string
var key string
var value string
err = unmarshal(&sliceType)
if err == nil {
mapType = make(map[string]string)
for _, slice := range sliceType {
keyValueSlice = strings.Split(slice, "=") //split up key and value into []string
key = keyValueSlice[0]
value = keyValueSlice[1]
mapType[key] = value
}
s.parts = mapType
return nil
}
return err
}
func (s *SliceorMap) MapParts() map[string]string {
if s == nil {
return nil
}
return s.parts
}
func NewSliceorMap(parts map[string]string) *SliceorMap {
return &SliceorMap{parts}
}
type ServiceConfig struct { type ServiceConfig struct {
CapAdd []string `yaml:"cap_add,omitempty"` CapAdd []string `yaml:"cap_add,omitempty"`
CapDrop []string `yaml:"cap_drop,omitempty"` CapDrop []string `yaml:"cap_drop,omitempty"`
CpuShares int64 `yaml:"cpu_shares,omitempty"` CpuShares int64 `yaml:"cpu_shares,omitempty"`
Command string `yaml:"command,omitempty"` Command string `yaml:"command,omitempty"`
Detach string `yaml:"detach,omitempty"` Detach string `yaml:"detach,omitempty"`
Dns *Stringorslice `yaml:"dns,omitempty"` Dns Stringorslice `yaml:"dns,omitempty"`
DnsSearch *Stringorslice `yaml:"dns_search,omitempty"` DnsSearch Stringorslice `yaml:"dns_search,omitempty"`
DomainName string `yaml:"domainname,omitempty"` DomainName string `yaml:"domainname,omitempty"`
Entrypoint string `yaml:"entrypoint,omitempty"` Entrypoint string `yaml:"entrypoint,omitempty"`
EnvFile string `yaml:"env_file,omitempty"` EnvFile string `yaml:"env_file,omitempty"`
Environment []string `yaml:"environment,omitempty"` Environment Maporslice `yaml:"environment,omitempty"`
Hostname string `yaml:"hostname,omitempty"` Hostname string `yaml:"hostname,omitempty"`
Image string `yaml:"image,omitempty"` Image string `yaml:"image,omitempty"`
Labels *SliceorMap `yaml:"labels,omitempty"` Labels SliceorMap `yaml:"labels,omitempty"`
Links []string `yaml:"links,omitempty"` Links []string `yaml:"links,omitempty"`
LogDriver string `yaml:"log_driver,omitempty"` LogDriver string `yaml:"log_driver,omitempty"`
MemLimit int64 `yaml:"mem_limit,omitempty"` MemLimit int64 `yaml:"mem_limit,omitempty"`
Name string `yaml:"name,omitempty"` Name string `yaml:"name,omitempty"`
Net string `yaml:"net,omitempty"` Net string `yaml:"net,omitempty"`
Pid string `yaml:"pid,omitempty"` Pid string `yaml:"pid,omitempty"`
Ipc string `yaml:"ipc,omitempty"` Ipc string `yaml:"ipc,omitempty"`
Ports []string `yaml:"ports,omitempty"` Ports []string `yaml:"ports,omitempty"`
Privileged bool `yaml:"privileged,omitempty"` Privileged bool `yaml:"privileged,omitempty"`
Restart string `yaml:"restart,omitempty"` Restart string `yaml:"restart,omitempty"`
ReadOnly bool `yaml:"read_only,omitempty"` ReadOnly bool `yaml:"read_only,omitempty"`
StdinOpen bool `yaml:"stdin_open,omitempty"` StdinOpen bool `yaml:"stdin_open,omitempty"`
Tty bool `yaml:"tty,omitempty"` Tty bool `yaml:"tty,omitempty"`
User string `yaml:"user,omitempty"` User string `yaml:"user,omitempty"`
Volumes []string `yaml:"volumes,omitempty"` Volumes []string `yaml:"volumes,omitempty"`
VolumesFrom []string `yaml:"volumes_from,omitempty"` VolumesFrom []string `yaml:"volumes_from,omitempty"`
WorkingDir string `yaml:"working_dir,omitempty"` WorkingDir string `yaml:"working_dir,omitempty"`
//`yaml:"build,omitempty"` //`yaml:"build,omitempty"`
Expose []string `yaml:"expose,omitempty"` Expose []string `yaml:"expose,omitempty"`
ExternalLinks []string `yaml:"external_links,omitempty"` ExternalLinks []string `yaml:"external_links,omitempty"`

View File

@ -0,0 +1,146 @@
package project
import (
"strings"
"gopkg.in/yaml.v2"
)
type Stringorslice struct {
parts []string
}
func (s *Stringorslice) MarshalYAML() (interface{}, error) {
if s == nil {
return nil, nil
}
bytes, err := yaml.Marshal(s.Slice())
return string(bytes), err
}
func (s *Stringorslice) UnmarshalYAML(unmarshal func(interface{}) error) error {
var sliceType []string
err := unmarshal(&sliceType)
if err == nil {
s.parts = sliceType
return nil
}
var stringType string
err = unmarshal(&stringType)
if err == nil {
sliceType = make([]string, 0, 1)
s.parts = append(sliceType, string(stringType))
return nil
}
return err
}
func (s *Stringorslice) Len() int {
if s == nil {
return 0
}
return len(s.parts)
}
func (s *Stringorslice) Slice() []string {
if s == nil {
return nil
}
return s.parts
}
func NewStringorslice(parts ...string) Stringorslice {
return Stringorslice{parts}
}
type SliceorMap struct {
parts map[string]string
}
func (s *SliceorMap) MarshalYAML() (interface{}, error) {
if s == nil {
return nil, nil
}
bytes, err := yaml.Marshal(s.MapParts())
return string(bytes), err
}
func (s *SliceorMap) UnmarshalYAML(unmarshal func(interface{}) error) error {
mapType := make(map[string]string)
err := unmarshal(&mapType)
if err == nil {
s.parts = mapType
return nil
}
var sliceType []string
var keyValueSlice []string
var key string
var value string
err = unmarshal(&sliceType)
if err == nil {
mapType = make(map[string]string)
for _, slice := range sliceType {
keyValueSlice = strings.Split(slice, "=") //split up key and value into []string
key = keyValueSlice[0]
value = keyValueSlice[1]
mapType[key] = value
}
s.parts = mapType
return nil
}
return err
}
func (s *SliceorMap) MapParts() map[string]string {
if s == nil {
return nil
}
return s.parts
}
func NewSliceorMap(parts map[string]string) SliceorMap {
return SliceorMap{parts}
}
type Maporslice struct {
parts []string
}
func (s *Maporslice) MarshalYAML() (interface{}, error) {
if s == nil {
return nil, nil
}
bytes, err := yaml.Marshal(s.Slice())
return string(bytes), err
}
func (s *Maporslice) UnmarshalYAML(unmarshal func(interface{}) error) error {
err := unmarshal(&s.parts)
if err == nil {
return nil
}
var mapType map[string]string
err = unmarshal(&mapType)
if err != nil {
return err
}
for k, v := range mapType {
s.parts = append(s.parts, strings.Join([]string{k, v}, "="))
}
return nil
}
func (s *Maporslice) Slice() []string {
return s.parts
}
func NewMaporslice(parts []string) Maporslice {
return Maporslice{parts}
}

View File

@ -102,9 +102,9 @@ func NewConfig() *Config {
DETACH: "true", DETACH: "true",
SCOPE: SYSTEM, SCOPE: SYSTEM,
}), }),
Environment: []string{ Environment: project.NewMaporslice([]string{
"DAEMON=true", "DAEMON=true",
}, }),
VolumesFrom: []string{ VolumesFrom: []string{
"system-volumes", "system-volumes",
}, },
@ -211,9 +211,9 @@ func NewConfig() *Config {
DETACH: "false", DETACH: "false",
SCOPE: SYSTEM, SCOPE: SYSTEM,
}), }),
Environment: []string{ Environment: project.NewMaporslice([]string{
"CLOUD_INIT_NETWORK=false", "CLOUD_INIT_NETWORK=false",
}, }),
VolumesFrom: []string{ VolumesFrom: []string{
"command-volumes", "command-volumes",
"system-volumes", "system-volumes",

View File

@ -84,9 +84,9 @@ outer:
config.SCOPE: config.SYSTEM, config.SCOPE: config.SYSTEM,
}), }),
LogDriver: "json-file", LogDriver: "json-file",
Environment: []string{ Environment: project.NewMaporslice([]string{
"MAGIC=" + boot2dockerMagic, "MAGIC=" + boot2dockerMagic,
}, }),
}, },
"udev": &udev, "udev": &udev,
}) })