-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.go
167 lines (137 loc) · 4.17 KB
/
service.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
package main
import (
"context"
"fmt"
"net"
"os"
"strconv"
"strings"
"github.com/containerd/containerd/log"
shimapi "github.com/containerd/containerd/runtime/v2/task"
"github.com/containerd/ttrpc"
"github.com/coreos/go-systemd/v22/daemon"
"github.com/coreos/go-systemd/v22/dbus"
"github.com/cpuguy83/containerd-shim-systemd-v1/options"
)
const (
// This is the socket location that we serve the containerd shim API on.
defaultAddress = "/run/containerd/s/containerd-shim-systemd-v1.sock"
serviceName = "containerd-shim-systemd-v1"
)
func newService(ts shimapi.TaskService) (*service, error) {
s, err := ttrpc.NewServer(ttrpc.WithServerHandshaker(ttrpc.UnixSocketRequireSameUser()), ttrpc.WithUnaryServerInterceptor(UnaryServerInterceptor))
if err != nil {
return nil, err
}
shimapi.RegisterTaskService(s, ts)
return &service{
srv: s,
}, nil
}
type service struct {
srv *ttrpc.Server
}
func (s *service) Serve(ctx context.Context, l net.Listener) error {
daemon.SdNotify(false, daemon.SdNotifyReady)
log.G(ctx).Info("Serving")
return s.srv.Serve(ctx, l)
}
func (s *service) Close() error {
return s.srv.Close()
}
func serviceUnit(exe string, cfg installConfig) string {
return `
[Unit]
Description=containerd shim service that uses systemd to manage containers
[Service]
Type=notify
Environment=UNIT_NAME=%n
ExecStart=` + exe + ` --address=` + cfg.Addr + ` serve` + ` --ttrpc-address=` + cfg.TTRPCAddr + ` --debug=` + strconv.FormatBool(cfg.Debug) + ` --root=` + cfg.Root + ` --log-mode=` + strings.ToLower(cfg.LogMode.String()) + ` ` + cfg.Trace.StringFlags() + ` --no-new-namespace=` + strconv.FormatBool(cfg.NoNewNamespace) + `
ExecReload=kill -HUP $MAINPID
`
}
func socketUnit(addr string) string {
return `
[Unit]
Description=containerd shim socket for ` + serviceName + `
[Install]
WantedBy = sockets.target
[Socket]
ListenStream=` + addr + `
SocketMode=0700
PassCredentials=yes
PassSecurity=yes
`
}
type installConfig struct {
Trace TraceConfig
Root string
Addr string
TTRPCAddr string
Debug bool
LogMode options.LogMode
Socket string
NoNewNamespace bool
}
func install(ctx context.Context, cfg installConfig) error {
conn, err := dbus.NewSystemdConnectionContext(ctx)
if err != nil {
return err
}
defer conn.Close()
exe, err := os.Executable()
if err != nil {
return err
}
os.WriteFile("/etc/systemd/system/"+serviceName+".service", []byte(serviceUnit(exe, cfg)), 0644)
if err != nil {
return err
}
err = os.WriteFile("/etc/systemd/system/"+serviceName+".socket", []byte(socketUnit(cfg.Socket)), 0644)
if err != nil {
os.RemoveAll("/etc/systemd/system/" + serviceName + ".service")
return err
}
if err := conn.ReloadContext(ctx); err != nil {
return err
}
ch := make(chan string, 1)
if _, err := conn.StartUnitContext(ctx, serviceName+".socket", "replace", ch); err != nil {
return fmt.Errorf("error starting socket unit: %w", err)
}
select {
case <-ctx.Done():
return ctx.Err()
case status := <-ch:
if status != "done" {
return fmt.Errorf("error starting socket unit: %s", status)
}
}
return nil
}
func uninstall(ctx context.Context) error {
conn, err := dbus.NewSystemdConnectionContext(ctx)
if err != nil {
return err
}
defer conn.Close()
if _, err := conn.StopUnitContext(ctx, serviceName+".socket", "replace", nil); err != nil {
return fmt.Errorf("error stopping socket unit: %w", err)
}
if _, err := conn.StopUnitContext(ctx, serviceName+".service", "replace", nil); err != nil {
return fmt.Errorf("error stopping service unit: %w", err)
}
if _, err := conn.DisableUnitFilesContext(ctx, []string{serviceName + ".service", serviceName + ".socket"}, true); err != nil {
return fmt.Errorf("error disabling units: %w", err)
}
if err := os.Remove("/etc/systemd/system/" + serviceName + ".socket"); err != nil && !os.IsNotExist(err) {
log.G(ctx).WithError(err).Error("failed to remove socket unit")
}
if err := os.Remove("/etc/systemd/system/" + serviceName + ".service"); err != nil && !os.IsNotExist(err) {
log.G(ctx).WithError(err).Error("failed to remove service unit")
}
if err := conn.ReloadContext(ctx); err != nil {
return err
}
return nil
}