forked from mesg-foundation/engine
/
service_options.go
111 lines (101 loc) · 2.72 KB
/
service_options.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
package container
import (
"os"
"strconv"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/swarm"
)
// ServiceOptions is a simplify version of swarm.ServiceSpec that can be created it.
type ServiceOptions struct {
Image string
Namespace []string
Ports []Port
Mounts []Mount
Env []string
Args []string
NetworksID []string
Labels map[string]string
}
// Port is a simplify version of swarm.PortConfig
type Port struct {
Target uint32
Published uint32
}
// Mount is a simplify version of mount.Mount
type Mount struct {
Source string
Target string
}
func (options *ServiceOptions) toSwarmServiceSpec() (service swarm.ServiceSpec) {
namespace := Namespace(options.Namespace)
service = swarm.ServiceSpec{
Annotations: swarm.Annotations{
Name: namespace,
Labels: mergeLabels(options.Labels, map[string]string{
"com.docker.stack.namespace": namespace,
"com.docker.stack.image": options.Image,
}),
},
TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{
Image: options.Image,
Labels: map[string]string{
"com.docker.stack.namespace": namespace,
},
Env: options.Env,
Args: options.Args,
Mounts: options.swarmMounts(false),
},
Networks: options.swarmNetworks(),
},
EndpointSpec: &swarm.EndpointSpec{
Ports: options.swarmPorts(),
},
}
return
}
func (options *ServiceOptions) swarmPorts() (ports []swarm.PortConfig) {
ports = make([]swarm.PortConfig, len(options.Ports))
for i, p := range options.Ports {
ports[i] = swarm.PortConfig{
Protocol: swarm.PortConfigProtocolTCP,
PublishMode: swarm.PortConfigPublishModeIngress,
TargetPort: p.Target,
PublishedPort: p.Published,
}
}
return
}
func (options *ServiceOptions) swarmMounts(force bool) (mounts []mount.Mount) {
// TOFIX: hack to prevent mount when in CircleCI (Mount in CircleCI doesn't work). Should use CircleCi with machine to fix this.
circleCI, errCircle := strconv.ParseBool(os.Getenv("CIRCLECI"))
if force == false && errCircle == nil && circleCI {
return
}
mounts = make([]mount.Mount, len(options.Mounts))
for i, m := range options.Mounts {
mounts[i] = mount.Mount{
Source: m.Source,
Target: m.Target,
}
}
return
}
func (options *ServiceOptions) swarmNetworks() (networks []swarm.NetworkAttachmentConfig) {
networks = make([]swarm.NetworkAttachmentConfig, len(options.NetworksID))
for i, networkID := range options.NetworksID {
networks[i] = swarm.NetworkAttachmentConfig{
Target: networkID,
}
}
return
}
func mergeLabels(l1 map[string]string, l2 map[string]string) map[string]string {
if l1 == nil {
l1 = make(map[string]string)
}
for k, v := range l2 {
l1[k] = v
}
return l1
}