forked from mantika/whaleprint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply.go
146 lines (124 loc) · 3.98 KB
/
apply.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
package main
import (
"fmt"
"io/ioutil"
"log"
"github.com/docker/docker/api/client/bundlefile"
"github.com/docker/docker/api/client/stack"
"github.com/docker/engine-api/client"
"github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/filters"
"github.com/docker/engine-api/types/network"
"github.com/fatih/color"
"github.com/urfave/cli"
"golang.org/x/net/context"
)
func apply(c *cli.Context) error {
stacks, err := getStacks(c)
if err != nil {
return err
}
swarm, swarmErr := client.NewEnvClient()
if swarmErr != nil {
return cli.NewExitError(swarmErr.Error(), 3)
}
target := c.StringSlice("target")
targetMap := map[string]bool{}
for _, name := range target {
targetMap[name] = true
}
for _, stack := range stacks {
filter := filters.NewArgs()
filter.Add("label", "com.docker.stack.namespace="+stack.Name)
services, servicesErr := swarm.ServiceList(context.Background(), types.ServiceListOptions{Filter: filter})
if servicesErr != nil {
return cli.NewExitError(servicesErr.Error(), 3)
}
expected := getBundleServicesSpec(stack.Bundle, stack.Name)
translateNetworkToIds(&expected, swarm, stack.Name)
current := getSwarmServicesSpecForStack(services)
err = updateNetworks(context.Background(), swarm, getUniqueNetworkNames(stack.Bundle.Services), stack.Name)
if err != nil {
log.Fatal("Error updating networks when creating services", err)
}
cyan := color.New(color.FgCyan)
sp := NewServicePrinter(ioutil.Discard, false)
for name, expectedService := range expected {
if _, found := targetMap[expectedService.Spec.Name]; len(targetMap) == 0 || found {
if currentService, found := current[name]; found {
if sp.PrintServiceSpecDiff(currentService.Spec, expectedService.Spec) {
cyan.Printf("Updating service %s\n", name)
servicesErr := swarm.ServiceUpdate(context.Background(), currentService.ID, currentService.Version, expectedService.Spec, types.ServiceUpdateOptions{})
if servicesErr != nil {
return cli.NewExitError(servicesErr.Error(), 3)
}
}
} else {
// service doesn't exist, need to create a new one
cyan.Printf("Creating service %s\n", name)
_, servicesErr := swarm.ServiceCreate(context.Background(), expectedService.Spec, types.ServiceCreateOptions{})
if servicesErr != nil {
return cli.NewExitError(servicesErr.Error(), 3)
}
}
}
}
for name, cs := range current {
if _, found := targetMap[cs.Spec.Name]; len(targetMap) == 0 || found {
if _, found := expected[name]; !found {
// service exists but it's not expected, need to delete it
cyan.Printf("Removing service %s\n", name)
servicesErr := swarm.ServiceRemove(context.Background(), name)
if servicesErr != nil {
return cli.NewExitError(servicesErr.Error(), 3)
}
}
}
}
}
return nil
}
func updateNetworks(
ctx context.Context,
cli *client.Client,
networks []string,
namespace string,
) error {
existingNetworks, err := stack.GetNetworks(ctx, cli, namespace)
if err != nil {
return err
}
existingNetworkMap := make(map[string]types.NetworkResource)
for _, network := range existingNetworks {
existingNetworkMap[network.Name] = network
}
createOpts := types.NetworkCreate{
Labels: stack.GetStackLabels(namespace, nil),
Driver: "overlay",
IPAM: network.IPAM{Driver: "default"},
}
for _, internalName := range networks {
name := fmt.Sprintf("%s_%s", namespace, internalName)
if _, exists := existingNetworkMap[name]; exists {
continue
}
fmt.Printf("Creating network %s\n", name)
if _, err := cli.NetworkCreate(ctx, name, createOpts); err != nil {
return err
}
}
return nil
}
func getUniqueNetworkNames(services map[string]bundlefile.Service) []string {
networkSet := make(map[string]bool)
for _, service := range services {
for _, network := range service.Networks {
networkSet[network] = true
}
}
networks := []string{}
for network := range networkSet {
networks = append(networks, network)
}
return networks
}