forked from flynn/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.go
235 lines (207 loc) · 5.34 KB
/
manifest.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"strings"
"text/template"
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/technoweenie/grohl"
"github.com/flynn/flynn/host/ports"
"github.com/flynn/flynn/host/types"
"github.com/flynn/flynn/pkg/cluster"
"github.com/flynn/flynn/pkg/random"
)
func parseEnviron() map[string]string {
env := os.Environ()
res := make(map[string]string, len(env))
for _, v := range env {
kv := strings.SplitN(v, "=", 2)
res[kv[0]] = kv[1]
}
if _, ok := res["ETCD_NAME"]; !ok {
res["ETCD_NAME"] = random.String(8)
}
return res
}
type ManifestData struct {
ExternalIP string
InternalIP string
TCPPorts []int
Volumes map[string]struct{}
Env map[string]string
Services map[string]*ManifestData
ports *ports.Allocator
readonly bool
}
func (m *ManifestData) TCPPort(id int) (int, error) {
if m.readonly {
return 0, fmt.Errorf("host: invalid TCPPort(%d), ManifestData is read-only", id)
}
if id < len(m.TCPPorts) {
return m.TCPPorts[id], nil
} else if id > len(m.TCPPorts) {
return 0, fmt.Errorf("host: invalid TCPPort(%d), expecting id <= %d", id, len(m.TCPPorts))
}
port, err := m.ports.Get()
if err != nil {
return 0, err
}
m.TCPPorts = append(m.TCPPorts, int(port))
return int(port), nil
}
func (m *ManifestData) Volume(v string) string {
if m.Volumes == nil {
m.Volumes = make(map[string]struct{})
}
m.Volumes[v] = struct{}{}
return v
}
type manifestRunner struct {
env map[string]string
externalAddr string
bindAddr string
backend Backend
state *State
ports map[string]*ports.Allocator
}
type manifestService struct {
ID string `json:"id"`
Image string `json:"image"`
ImageID string `json:"image_id"`
Entrypoint []string `json:"entrypoint"`
Args []string `json:"args"`
Env map[string]string `json:"env"`
TCPPorts []string `json:"tcp_ports"`
}
func (m *manifestRunner) runManifest(r io.Reader) (map[string]*ManifestData, error) {
g := grohl.NewContext(grohl.Data{"fn": "run_manifest"})
var services []*manifestService
if err := json.NewDecoder(r).Decode(&services); err != nil {
return nil, err
}
serviceData := make(map[string]*ManifestData, len(services))
m.state.mtx.Lock()
for _, job := range m.state.jobs {
if job.ManifestID == "" || job.Status != host.StatusRunning {
continue
}
var service *manifestService
for _, service = range services {
if service.ID == job.ManifestID {
break
}
}
if service == nil {
continue
}
g.Log(grohl.Data{"at": "restore", "service": service.ID, "job.id": job.Job.ID})
data := &ManifestData{
ExternalIP: m.externalAddr,
InternalIP: job.InternalIP,
Env: job.Job.Config.Env,
Services: serviceData,
ports: m.ports["tcp"],
readonly: true,
}
data.TCPPorts = make([]int, 0, len(job.Job.Config.Ports))
for _, p := range job.Job.Config.Ports {
if p.Proto != "tcp" {
continue
}
data.TCPPorts = append(data.TCPPorts, p.Port)
}
serviceData[service.ID] = data
}
m.state.mtx.Unlock()
for _, service := range services {
if _, exists := serviceData[service.ID]; exists {
continue
}
data := &ManifestData{
Env: parseEnviron(),
Services: serviceData,
ExternalIP: m.externalAddr,
ports: m.ports["tcp"],
}
// Add explicit tcp ports to data.TCPPorts
for _, port := range service.TCPPorts {
port, err := strconv.Atoi(port)
if err != nil {
return nil, err
}
data.TCPPorts = append(data.TCPPorts, port)
}
var buf bytes.Buffer
interp := func(s string) (string, error) {
t, err := template.New("arg").Parse(s)
if err != nil {
return "", err
}
if err := t.Execute(&buf, data); err != nil {
return "", err
}
defer buf.Reset()
return buf.String(), nil
}
args := make([]string, 0, len(service.Args))
for _, arg := range service.Args {
arg, err := interp(arg)
if err != nil {
return nil, err
}
if strings.TrimSpace(arg) == "" {
continue
}
args = append(args, arg)
}
var err error
for k, v := range service.Env {
service.Env[k], err = interp(v)
if err != nil {
return nil, err
}
}
data.Env = service.Env
if service.Image == "" {
service.Image = "https://registry.hub.docker.com/flynn/" + service.ID
}
if service.ImageID != "" {
service.Image += "?id=" + service.ImageID
}
job := &host.Job{
ID: cluster.RandomJobID("flynn-" + service.ID + "-"),
Artifact: host.Artifact{
Type: "docker",
URI: service.Image,
},
Config: host.ContainerConfig{
Entrypoint: service.Entrypoint,
Cmd: args,
Env: data.Env,
},
}
if job.Config.Env == nil {
job.Config.Env = make(map[string]string)
}
job.Config.Env["EXTERNAL_IP"] = m.externalAddr
job.Config.Ports = make([]host.Port, len(data.TCPPorts))
for i, port := range data.TCPPorts {
job.Config.Ports[i] = host.Port{Proto: "tcp", Port: port}
}
if len(job.Config.Ports) == 0 {
job.Config.Ports = []host.Port{{Proto: "tcp"}}
}
if err := m.backend.Run(job); err != nil {
return nil, err
}
m.state.SetManifestID(job.ID, service.ID)
activeJob := m.state.GetJob(job.ID)
data.InternalIP = activeJob.InternalIP
data.readonly = true
serviceData[service.ID] = data
}
return serviceData, nil
}