forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon.go
219 lines (178 loc) · 4.8 KB
/
daemon.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package daemon
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
"syscall"
"time"
"github.com/alibaba/pouch/test/command"
"github.com/alibaba/pouch/test/util"
"github.com/gotestyourself/gotestyourself/icmd"
)
// For pouch deamon test, we launched another pouch daemon.
const (
DaemonLog = "/tmp/pouchd.log"
PouchdBin = "pouchd"
HomeDir = "/tmp/test/pouch"
Listen = "unix:///tmp/test/pouch/pouchd.sock"
ContainerdAdd = "/tmp/test/pouch/containerd.sock"
ListenCRI = "unix:///tmp/test/pouch/pouchcri.sock"
Pidfile = "/tmp/test/pouch/pouch.pid"
)
// Config is the configuration of pouch daemon.
type Config struct {
LogPath string
LogFile *os.File
// Daemon startup arguments.
Args []string
// pouchd binary location
Bin string
// The following args are all MUST required,
// in case the new daemon conflicts with existing ones.
Listen string
HomeDir string
ContainerdAddr string
ListenCri string
Pidfile string
// pid of pouchd
Pid int
// timeout for starting daemon
timeout int64
// if Debug=true, dump daemon log when deamon failed to start
Debug bool
}
// NewConfig initialize the DConfig with default value.
func NewConfig() Config {
result := Config{}
result.Bin = PouchdBin
result.LogPath = DaemonLog
result.Args = make([]string, 0, 1)
result.Listen = Listen
result.HomeDir = HomeDir
result.ContainerdAddr = ContainerdAdd
result.ListenCri = ListenCRI
result.Pidfile = Pidfile
result.timeout = 15
result.Debug = true
return result
}
// NewArgs is used to construct args according to the struct Config and input.
func (d *Config) NewArgs(args ...string) {
// Append all default configuration to d.Args if they exists
// For the rest args in parameter, they must follow the pouchd args usage.
if len(d.Listen) != 0 {
d.Args = append(d.Args, "--listen="+d.Listen)
}
if len(d.HomeDir) != 0 {
d.Args = append(d.Args, "--home-dir="+d.HomeDir)
}
if len(d.ContainerdAddr) != 0 {
d.Args = append(d.Args, "--containerd="+d.ContainerdAddr)
}
if len(d.ListenCri) != 0 {
d.Args = append(d.Args, "--listen-cri="+d.ListenCri)
}
if len(d.Pidfile) != 0 {
d.Args = append(d.Args, "--pidfile="+d.Pidfile)
}
if len(args) != 0 {
d.Args = append(d.Args, args...)
}
}
// IsDaemonUp checks if the pouchd is launched.
func (d *Config) IsDaemonUp() bool {
// if pouchd is started with -l option, use the first listen address
var sock string
for _, v := range d.Args {
if strings.Contains(v, "-l") || strings.Contains(v, "--listen") {
if strings.Contains(v, "--listen-cri") {
continue
}
if strings.Contains(v, "=") {
sock = strings.Split(v, "=")[1]
break
} else {
sock = strings.Fields(v)[1]
break
}
}
}
for _, v := range d.Args {
if strings.Contains(v, "--tlsverify") {
// TODO: need to verify server with TLS
return true
}
}
if len(sock) != 0 {
return command.PouchRun("--host", sock, "version").ExitCode == 0
}
return command.PouchRun("version").ExitCode == 0
}
// StartDaemon starts pouchd
func (d *Config) StartDaemon() error {
cmd := exec.Command(d.Bin, d.Args...)
var err error
d.LogFile, err = os.Create(d.LogPath)
if err != nil {
return fmt.Errorf("failed to create log file %s, err %s", d.LogPath, err)
}
// Must not close the outfile
//defer outfile.Close()
mwriter := io.MultiWriter(d.LogFile)
cmd.Stderr = mwriter
cmd.Stdout = mwriter
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
err = cmd.Start()
if err != nil {
return fmt.Errorf("failed to start cmd %v, err %s", cmd, err)
}
// record the pid
d.Pid = cmd.Process.Pid
wait := make(chan error)
go func() {
wait <- cmd.Wait()
fmt.Printf("[%d] exiting daemon", d.Pid)
close(wait)
}()
if util.WaitTimeout(time.Duration(d.timeout)*time.Second, d.IsDaemonUp) == false {
if d.Debug == true {
d.DumpLog()
fmt.Printf("\nFailed to launch pouchd:%v\n", d.Args)
cmd := "ps aux |grep pouchd"
fmt.Printf("\nList pouchd process:\n%s\n", icmd.RunCommand("sh", "-c", cmd).Combined())
cmd = "ps aux |grep containerd"
fmt.Printf("\nList containerd process:\n%s\n", icmd.RunCommand("sh", "-c", cmd).Combined())
}
d.KillDaemon()
return fmt.Errorf("failed to launch pouchd:%v", d.Args)
}
return nil
}
// DumpLog prints the daemon log
func (d *Config) DumpLog() {
d.LogFile.Sync()
content, err := ioutil.ReadFile(d.LogPath)
if err != nil {
fmt.Printf("failed to read log, err: %s\n", err)
}
fmt.Printf("pouch daemon log contents:\n %s\n", content)
}
// KillDaemon kill pouchd.
func (d *Config) KillDaemon() {
if d.IsDaemonUp() == false {
return
}
if d.Pid != 0 {
// kill pouchd and all other process in its group
err := syscall.Kill(-d.Pid, syscall.SIGKILL)
if err != nil {
fmt.Printf("kill pouchd failed, err:%s", err)
return
}
d.LogFile.Close()
}
return
}