forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
204 lines (173 loc) · 5.67 KB
/
main.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
package main
import (
"fmt"
"os"
"os/signal"
"path"
"strings"
"syscall"
"github.com/alibaba/pouch/daemon"
"github.com/alibaba/pouch/daemon/config"
"github.com/alibaba/pouch/lxcfs"
"github.com/alibaba/pouch/pkg/exec"
"github.com/alibaba/pouch/pkg/utils"
"github.com/alibaba/pouch/version"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
cfg config.Config
sigHandles []func() error
printVersion bool
)
func main() {
var cmdServe = &cobra.Command{
Use: "pouchd",
Args: cobra.NoArgs,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
return runDaemon()
},
}
setupFlags(cmdServe)
if err := cmdServe.Execute(); err != nil {
logrus.Error(err)
os.Exit(1)
}
}
// setupFlags setups flags for command line.
func setupFlags(cmd *cobra.Command) {
flagSet := cmd.Flags()
flagSet.StringVar(&cfg.HomeDir, "home-dir", "/var/lib/pouch", "Specify root dir of pouchd")
flagSet.StringArrayVarP(&cfg.Listen, "listen", "l", []string{"unix:///var/run/pouchd.sock"}, "Specify listening addresses of Pouchd")
flagSet.StringVar(&cfg.ListenCRI, "listen-cri", "/var/run/pouchcri.sock", "Specify listening address of CRI")
flagSet.BoolVarP(&cfg.Debug, "debug", "D", false, "Switch daemon log level to DEBUG mode")
flagSet.StringVarP(&cfg.ContainerdAddr, "containerd", "c", "/var/run/containerd.sock", "Specify listening address of containerd")
flagSet.StringVar(&cfg.ContainerdPath, "containerd-path", "/usr/local/bin/containerd", "Specify the path of containerd binary")
flagSet.StringVar(&cfg.ContainerdConfig, "containerd-config", "/etc/containerd/config.toml", "Specify the path of containerd configuration file")
flagSet.StringVar(&cfg.TLS.Key, "tlskey", "", "Specify key file of TLS")
flagSet.StringVar(&cfg.TLS.Cert, "tlscert", "", "Specify cert file of TLS")
flagSet.StringVar(&cfg.TLS.CA, "tlscacert", "", "Specify CA file of TLS")
flagSet.BoolVar(&cfg.TLS.VerifyRemote, "tlsverify", false, "Use TLS and verify remote")
flagSet.BoolVarP(&printVersion, "version", "v", false, "Print daemon version")
flagSet.StringVar(&cfg.DefaultRuntime, "default-runtime", "runc", "Default OCI Runtime")
flagSet.BoolVar(&cfg.IsLxcfsEnabled, "enable-lxcfs", false, "Enable Lxcfs to make container to isolate /proc")
flagSet.StringVar(&cfg.LxcfsBinPath, "lxcfs", "/usr/local/bin/lxcfs", "Specify the path of lxcfs binary")
flagSet.StringVar(&cfg.LxcfsHome, "lxcfs-home", "/var/lib/lxc/lxcfs", "Specify the mount dir of lxcfs")
}
// runDaemon prepares configs, setups essential details and runs pouchd daemon.
func runDaemon() error {
//user specifies --version or -v, print version and return.
if printVersion {
fmt.Println(version.Version)
return nil
}
// initialize log.
initLog()
// initialize home dir.
dir := cfg.HomeDir
if dir == "" || !path.IsAbs(dir) {
return fmt.Errorf("invalid pouchd's home dir: %s", dir)
}
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0666); err != nil {
return fmt.Errorf("failed to mkdir: %v", err)
}
}
// define and start all required processes.
if _, err := os.Stat(cfg.ContainerdAddr); err == nil {
os.RemoveAll(cfg.ContainerdAddr)
}
var processes exec.Processes = []*exec.Process{
{
Path: cfg.ContainerdPath,
Args: []string{
"-c", cfg.ContainerdConfig,
"-a", cfg.ContainerdAddr,
"--root", path.Join(cfg.HomeDir, "containerd/root"),
"--state", path.Join(cfg.HomeDir, "containerd/state"),
"-l", utils.If(cfg.Debug, "debug", "info").(string),
},
},
}
if err := checkLxcfsCfg(); err != nil {
return err
}
processes = setLxcfsProcess(processes)
defer processes.StopAll()
if err := processes.RunAll(); err != nil {
return err
}
sigHandles = append(sigHandles, processes.StopAll)
// initialize signal and handle method.
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
sig := <-signals
logrus.Warnf("received signal: %s", sig)
for _, handle := range sigHandles {
if err := handle(); err != nil {
logrus.Errorf("failed to handle signal: %v", err)
}
}
os.Exit(1)
}()
// new daemon instance, this is core.
d := daemon.NewDaemon(cfg)
if d == nil {
return fmt.Errorf("failed to new daemon")
}
sigHandles = append(sigHandles, d.Shutdown)
return d.Run()
}
// initLog initializes log Level and log format of daemon.
func initLog() {
if cfg.Debug {
logrus.SetLevel(logrus.DebugLevel)
logrus.Infof("start daemon at debug level")
}
formatter := &logrus.TextFormatter{
ForceColors: true,
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05.000000000",
}
logrus.SetFormatter(formatter)
}
// define lxcfs processe.
func setLxcfsProcess(processes exec.Processes) exec.Processes {
if !cfg.IsLxcfsEnabled {
return processes
}
p := &exec.Process{
Path: cfg.LxcfsBinPath,
Args: []string{
cfg.LxcfsHome,
},
}
processes = append(processes, p)
cfg.LxcfsHome = strings.TrimSuffix(cfg.LxcfsHome, "/")
lxcfs.IsLxcfsEnabled = cfg.IsLxcfsEnabled
lxcfs.LxcfsHomeDir = cfg.LxcfsHome
lxcfs.LxcfsParentDir = path.Dir(cfg.LxcfsHome)
return processes
}
// check lxcfs config
func checkLxcfsCfg() error {
if !cfg.IsLxcfsEnabled {
return nil
}
if !path.IsAbs(cfg.LxcfsHome) {
return fmt.Errorf("invalid lxcfs home dir: %s", cfg.LxcfsHome)
}
if _, err := os.Stat(cfg.LxcfsBinPath); err != nil {
return fmt.Errorf("invalid lxcfs bin path: %s", cfg.LxcfsBinPath)
}
if _, err := os.Stat(cfg.LxcfsHome); err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(cfg.LxcfsHome, 0755); err != nil {
return fmt.Errorf("failed to LxcfsHome %s: %v", cfg.LxcfsHome, err)
}
}
}
return nil
}