forked from rancher/os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices.go
136 lines (112 loc) · 3.03 KB
/
services.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package docker
import (
"fmt"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/rancherio/os/config"
"github.com/rancherio/os/util"
"github.com/rancherio/rancher-compose/librcompose/project"
)
type configEnvironment struct {
cfg *config.Config
}
func appendEnv(array []string, key, value string) []string {
parts := strings.SplitN(key, "/", 2)
if len(parts) == 2 {
key = parts[1]
}
return append(array, fmt.Sprintf("%s=%s", key, value))
}
func lookupKeys(cfg *config.Config, keys ...string) []string {
for _, key := range keys {
if strings.HasSuffix(key, "*") {
result := []string{}
for envKey, envValue := range cfg.Environment {
keyPrefix := key[:len(key)-1]
if strings.HasPrefix(envKey, keyPrefix) {
result = appendEnv(result, envKey, envValue)
}
}
if len(result) > 0 {
return result
}
} else if value, ok := cfg.Environment[key]; ok {
return appendEnv([]string{}, key, value)
}
}
return []string{}
}
func (c *configEnvironment) Lookup(key, serviceName string, serviceConfig *project.ServiceConfig) []string {
fullKey := fmt.Sprintf("%s/%s", serviceName, key)
return lookupKeys(c.cfg, fullKey, key)
}
func RunServices(name string, cfg *config.Config, configs map[string]*project.ServiceConfig) error {
network := false
projectEvents := make(chan project.ProjectEvent)
p := project.NewProject(name, NewContainerFactory(cfg))
p.EnvironmentLookup = &configEnvironment{cfg: cfg}
p.AddListener(projectEvents)
enabled := make(map[string]bool)
for name, serviceConfig := range configs {
if err := p.AddConfig(name, serviceConfig); err != nil {
log.Infof("Failed loading service %s", name)
}
}
p.ReloadCallback = func() error {
err := cfg.Reload()
if err != nil {
return err
}
for service, serviceEnabled := range cfg.ServicesInclude {
if !serviceEnabled {
continue
}
if _, ok := enabled[service]; ok {
continue
}
bytes, err := LoadServiceResource(service, network, cfg)
if err != nil {
if err == util.ErrNoNetwork {
log.Debugf("Can not load %s, networking not enabled", service)
} else {
log.Errorf("Failed to load %s : %v", service, err)
}
continue
}
err = p.Load(bytes)
if err != nil {
log.Errorf("Failed to load %s : %v", service, err)
continue
}
enabled[service] = true
}
for service, config := range cfg.Services {
if _, ok := enabled[service]; ok {
continue
}
err = p.AddConfig(service, config)
if err != nil {
log.Errorf("Failed to load %s : %v", service, err)
continue
}
enabled[service] = true
}
return nil
}
go func() {
for event := range projectEvents {
if event.Event == project.CONTAINER_STARTED && event.ServiceName == "network" {
network = true
}
}
}()
err := p.ReloadCallback()
if err != nil {
log.Errorf("Failed to reload %s : %v", name, err)
return err
}
return p.Up()
}
func LoadServiceResource(name string, network bool, cfg *config.Config) ([]byte, error) {
return util.LoadResource(name, network, cfg.Repositories.ToArray())
}