-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaproxy.go
130 lines (113 loc) · 3.25 KB
/
haproxy.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
package main
import (
"context"
"io/ioutil"
"log"
"path/filepath"
dockerTypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
dockerClient "github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
)
func ensureService(dockerCli *dockerClient.Client, quit chan struct{}) {
for {
select {
case <-quit:
return
default:
startService(dockerCli)
}
}
}
func startService(dockerCli *dockerClient.Client) {
alreadyRunning, _, err := isContainerRunning(dockerCli, "com.opencopilot.service."+ServiceName)
if err != nil {
log.Fatal(err)
}
if alreadyRunning {
log.Println("HAProxy already running, stopping")
stopService(dockerCli)
}
log.Println("starting HAProxy")
ctx := context.Background()
containerConfig := &container.Config{
Image: "haproxy:1.8.9",
Labels: map[string]string{
"com.opencopilot.service." + ServiceName: "haproxy",
},
ExposedPorts: nat.PortSet{
"80/tcp": struct{}{},
"8080/tcp": struct{}{},
},
}
reader, err := dockerCli.ImagePull(ctx, containerConfig.Image, dockerTypes.ImagePullOptions{})
if err != nil {
log.Fatal(err)
}
defer reader.Close()
if _, err := ioutil.ReadAll(reader); err != nil {
log.Panic(err)
}
hostConfig := &container.HostConfig{
// RestartPolicy: container.RestartPolicy{Name: "always"},
AutoRemove: true,
Binds: []string{
filepath.Join(ConfigDir, "/services/", ServiceName) + ":/usr/local/etc/haproxy",
},
PortBindings: nat.PortMap{
"80/tcp": []nat.PortBinding{
{HostIP: "0.0.0.0", HostPort: "80"},
},
"8080/tcp": []nat.PortBinding{
{HostIP: "127.0.0.1", HostPort: "8080"},
},
},
}
res, err := dockerCli.ContainerCreate(ctx, containerConfig, hostConfig, nil, "com.opencopilot.service."+ServiceName)
if err != nil {
log.Println(err)
}
startErr := dockerCli.ContainerStart(ctx, res.ID, dockerTypes.ContainerStartOptions{})
if startErr != nil {
log.Fatal(startErr)
}
log.Printf("HAProxy container started with ID: %s\n", res.ID[:10])
waitForContainerStop(dockerCli, res.ID)
}
func stopService(dockerCli *dockerClient.Client) {
log.Println("stopping HAProxy")
ctx := context.Background()
args := filters.NewArgs(
filters.Arg("name", "com.opencopilot.service."+ServiceName),
)
containers, err := dockerCli.ContainerList(ctx, dockerTypes.ContainerListOptions{
Filters: args,
})
if err != nil {
log.Fatal(err)
}
for _, container := range containers {
dockerCli.ContainerKill(ctx, container.ID, "SIGTERM")
// dockerCli.ContainerStop(ctx, container.ID, nil)
log.Printf("removing container with ID: %s\n", container.ID[:10])
}
}
func configureService(dockerCli *dockerClient.Client) {
log.Println("configuring " + ServiceName)
// Go find the docker container running the service and send a SIGHUP to have it reload the config
ctx := context.Background()
args := filters.NewArgs(
filters.Arg("label", "com.opencopilot.service="+ServiceName),
filters.Arg("name", "com.opencopilot.service."+ServiceName),
)
containers, err := dockerCli.ContainerList(ctx, dockerTypes.ContainerListOptions{
Filters: args,
})
if err != nil {
log.Fatal(err)
}
for _, container := range containers {
dockerCli.ContainerKill(ctx, container.ID, "SIGHUP")
}
}