-
Notifications
You must be signed in to change notification settings - Fork 35
/
create.go
81 lines (66 loc) · 1.62 KB
/
create.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
package application
import (
"context"
api "github.com/ehazlett/stellar/api/services/application/v1"
nodeapi "github.com/ehazlett/stellar/api/services/node/v1"
ptypes "github.com/gogo/protobuf/types"
"github.com/sirupsen/logrus"
)
func (s *service) Create(ctx context.Context, req *api.CreateRequest) (*ptypes.Empty, error) {
appName := getAppName(req.Name)
logrus.Debugf("creating application %s", appName)
c, err := s.client(s.agent.Self().Address)
if err != nil {
return empty, err
}
defer c.Close()
nodes, err := c.Cluster().Nodes()
if err != nil {
return empty, err
}
// only create missing services
containers, err := s.getApplicationContainers(appName)
if err != nil {
return empty, err
}
ids := map[string]struct{}{}
for _, cnt := range containers {
ids[cnt.Container.ID] = struct{}{}
}
services := []*nodeapi.Service{}
for _, service := range req.Services {
if _, ok := ids[req.Name+"."+service.Name]; ok {
continue
}
services = append(services, service)
}
nodeIdx := 0
for _, service := range services {
// get random peer for deploy
node := nodes[nodeIdx]
nc, err := s.client(node.Address)
if err != nil {
return empty, err
}
if err := nc.Node().CreateContainer(req.Name, service); err != nil {
return empty, err
}
// update proxy
if err := nc.Proxy().Reload(); err != nil {
return empty, err
}
nc.Close()
// update peer index for next deploy
nodeIdx++
if nodeIdx >= len(nodes) {
nodeIdx = 0
}
}
if err := s.publish(&UpdateEvent{
Application: req.Name,
Action: "create",
}); err != nil {
return empty, err
}
return empty, nil
}