forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
164 lines (136 loc) · 3.41 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
package main
import (
"fmt"
"os"
"os/signal"
"path"
"syscall"
"github.com/alibaba/pouch/daemon"
"github.com/alibaba/pouch/daemon/config"
"github.com/alibaba/pouch/pkg/exec"
"github.com/alibaba/pouch/pkg/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
cfg config.Config
sigHandles []func() error
)
func main() {
var cmdServe = &cobra.Command{
Use: "pouchd",
Args: cobra.MinimumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return runDaemon()
},
}
setupFlags(cmdServe)
cmdServe.Execute()
}
// runDaemon prepares configs, setups essential details and runs pouchd daemon.
func runDaemon() error {
// 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),
},
},
}
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()
}
// setupFlags setups flags for command line.
func setupFlags(cmd *cobra.Command) {
flagSet := cmd.Flags()
flagSet.StringVar(
&cfg.HomeDir,
"home-dir",
"/etc/pouchd",
"The pouchd's home directory")
flagSet.StringArrayVarP(
&cfg.Listen,
"listen",
"l",
[]string{"unix:///var/run/pouchd.sock"},
"which address to listen on")
flagSet.BoolVarP(
&cfg.Debug,
"debug",
"D",
false,
"switch debug level")
flagSet.StringVarP(
&cfg.ContainerdAddr,
"containerd",
"c",
"/var/run/containerd.sock",
"where does containerd listened on")
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 binary")
utils.SetupTLSFlag(flagSet, &cfg.TLS)
}
// 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)
}