Skip to content

Commit d7280ce

Browse files
committed
cmd/containerd: split package for cli.App
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
1 parent af593cf commit d7280ce

File tree

9 files changed

+209
-195
lines changed

9 files changed

+209
-195
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package command
22

33
import (
44
"io"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package command
22

33
import (
44
"github.com/containerd/containerd/defaults"

cmd/containerd/config_unsupported.go renamed to cmd/containerd/command/config_unsupported.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// +build !linux,!windows,!solaris
22

3-
package main
3+
package command
44

55
import (
66
"github.com/containerd/containerd/defaults"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package command
22

33
import (
44
"github.com/containerd/containerd/defaults"

cmd/containerd/command/main.go

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
package command
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io/ioutil"
7+
golog "log"
8+
"net"
9+
"os"
10+
"os/signal"
11+
"path/filepath"
12+
"time"
13+
14+
"github.com/containerd/containerd/log"
15+
"github.com/containerd/containerd/server"
16+
"github.com/containerd/containerd/sys"
17+
"github.com/containerd/containerd/version"
18+
"github.com/pkg/errors"
19+
"github.com/sirupsen/logrus"
20+
"github.com/urfave/cli"
21+
gocontext "golang.org/x/net/context"
22+
"google.golang.org/grpc/grpclog"
23+
)
24+
25+
const usage = `
26+
__ _ __
27+
_________ ____ / /_____ _(_)___ ___ _________/ /
28+
/ ___/ __ \/ __ \/ __/ __ ` + "`" + `/ / __ \/ _ \/ ___/ __ /
29+
/ /__/ /_/ / / / / /_/ /_/ / / / / / __/ / / /_/ /
30+
\___/\____/_/ /_/\__/\__,_/_/_/ /_/\___/_/ \__,_/
31+
32+
high performance container runtime
33+
`
34+
35+
func init() {
36+
// Discard grpc logs so that they don't mess with our stdio
37+
grpclog.SetLogger(golog.New(ioutil.Discard, "", golog.LstdFlags))
38+
39+
cli.VersionPrinter = func(c *cli.Context) {
40+
fmt.Println(c.App.Name, version.Package, c.App.Version, version.Revision)
41+
}
42+
}
43+
44+
// App returns a *cli.App instance.
45+
func App() *cli.App {
46+
app := cli.NewApp()
47+
app.Name = "containerd"
48+
app.Version = version.Version
49+
app.Usage = usage
50+
app.Flags = []cli.Flag{
51+
cli.StringFlag{
52+
Name: "config,c",
53+
Usage: "path to the configuration file",
54+
Value: defaultConfigPath,
55+
},
56+
cli.StringFlag{
57+
Name: "log-level,l",
58+
Usage: "set the logging level [trace, debug, info, warn, error, fatal, panic]",
59+
},
60+
cli.StringFlag{
61+
Name: "address,a",
62+
Usage: "address for containerd's GRPC server",
63+
},
64+
cli.StringFlag{
65+
Name: "root",
66+
Usage: "containerd root directory",
67+
},
68+
cli.StringFlag{
69+
Name: "state",
70+
Usage: "containerd state directory",
71+
},
72+
}
73+
app.Commands = []cli.Command{
74+
configCommand,
75+
publishCommand,
76+
}
77+
app.Action = func(context *cli.Context) error {
78+
var (
79+
start = time.Now()
80+
signals = make(chan os.Signal, 2048)
81+
serverC = make(chan *server.Server, 1)
82+
ctx = gocontext.Background()
83+
config = defaultConfig()
84+
)
85+
86+
done := handleSignals(ctx, signals, serverC)
87+
// start the signal handler as soon as we can to make sure that
88+
// we don't miss any signals during boot
89+
signal.Notify(signals, handledSignals...)
90+
91+
if err := server.LoadConfig(context.GlobalString("config"), config); err != nil && !os.IsNotExist(err) {
92+
return err
93+
}
94+
// apply flags to the config
95+
if err := applyFlags(context, config); err != nil {
96+
return err
97+
}
98+
address := config.GRPC.Address
99+
if address == "" {
100+
return errors.New("grpc address cannot be empty")
101+
}
102+
log.G(ctx).WithFields(logrus.Fields{
103+
"version": version.Version,
104+
"revision": version.Revision,
105+
}).Info("starting containerd")
106+
107+
server, err := server.New(ctx, config)
108+
if err != nil {
109+
return err
110+
}
111+
serverC <- server
112+
if config.Debug.Address != "" {
113+
var l net.Listener
114+
if filepath.IsAbs(config.Debug.Address) {
115+
if l, err = sys.GetLocalListener(config.Debug.Address, config.Debug.UID, config.Debug.GID); err != nil {
116+
return errors.Wrapf(err, "failed to get listener for debug endpoint")
117+
}
118+
} else {
119+
if l, err = net.Listen("tcp", config.Debug.Address); err != nil {
120+
return errors.Wrapf(err, "failed to get listener for debug endpoint")
121+
}
122+
}
123+
serve(ctx, l, server.ServeDebug)
124+
}
125+
if config.Metrics.Address != "" {
126+
l, err := net.Listen("tcp", config.Metrics.Address)
127+
if err != nil {
128+
return errors.Wrapf(err, "failed to get listener for metrics endpoint")
129+
}
130+
serve(ctx, l, server.ServeMetrics)
131+
}
132+
133+
l, err := sys.GetLocalListener(address, config.GRPC.UID, config.GRPC.GID)
134+
if err != nil {
135+
return errors.Wrapf(err, "failed to get listener for main endpoint")
136+
}
137+
serve(ctx, l, server.ServeGRPC)
138+
139+
log.G(ctx).Infof("containerd successfully booted in %fs", time.Since(start).Seconds())
140+
<-done
141+
return nil
142+
}
143+
return app
144+
}
145+
146+
func serve(ctx context.Context, l net.Listener, serveFunc func(net.Listener) error) {
147+
path := l.Addr().String()
148+
log.G(ctx).WithField("address", path).Info("serving...")
149+
go func() {
150+
defer l.Close()
151+
if err := serveFunc(l); err != nil {
152+
log.G(ctx).WithError(err).WithField("address", path).Fatal("serve failure")
153+
}
154+
}()
155+
}
156+
157+
func applyFlags(context *cli.Context, config *server.Config) error {
158+
// the order for config vs flag values is that flags will always override
159+
// the config values if they are set
160+
if err := setLevel(context, config); err != nil {
161+
return err
162+
}
163+
for _, v := range []struct {
164+
name string
165+
d *string
166+
}{
167+
{
168+
name: "root",
169+
d: &config.Root,
170+
},
171+
{
172+
name: "state",
173+
d: &config.State,
174+
},
175+
{
176+
name: "address",
177+
d: &config.GRPC.Address,
178+
},
179+
} {
180+
if s := context.GlobalString(v.name); s != "" {
181+
*v.d = s
182+
}
183+
}
184+
return nil
185+
}
186+
187+
func setLevel(context *cli.Context, config *server.Config) error {
188+
l := context.GlobalString("log-level")
189+
if l == "" {
190+
l = config.Debug.Level
191+
}
192+
if l != "" {
193+
lvl, err := log.ParseLevel(l)
194+
if err != nil {
195+
return err
196+
}
197+
logrus.SetLevel(lvl)
198+
}
199+
return nil
200+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// +build linux darwin freebsd solaris
22

3-
package main
3+
package command
44

55
import (
66
"context"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package command
22

33
import (
44
"context"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package command
22

33
import (
44
gocontext "context"

0 commit comments

Comments
 (0)