cmd: move cli files to cmd package

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day 2016-12-01 12:07:45 -08:00
parent c4616487fe
commit 796b609741
No known key found for this signature in database
GPG key ID: FB5F6B2905D7ECF3
3 changed files with 0 additions and 0 deletions

115
cmd/containerd/main.go Normal file
View file

@ -0,0 +1,115 @@
package main
import (
"fmt"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"google.golang.org/grpc"
"github.com/Sirupsen/logrus"
"github.com/docker/containerd"
metrics "github.com/docker/go-metrics"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "containerd"
app.Version = containerd.Version
app.Usage = `
__ _ __
_________ ____ / /_____ _(_)___ ___ _________/ /
/ ___/ __ \/ __ \/ __/ __ ` + "`" + `/ / __ \/ _ \/ ___/ __ /
/ /__/ /_/ / / / / /_/ /_/ / / / / / __/ / / /_/ /
\___/\____/_/ /_/\__/\__,_/_/_/ /_/\___/_/ \__,_/
high performance container runtime
`
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "enable debug output in logs",
},
cli.StringFlag{
Name: "socket, s",
Usage: "socket path for containerd's GRPC server",
Value: "/run/containerd/containerd.sock",
},
cli.StringFlag{
Name: "metrics-address, m",
Usage: "tcp address to serve metrics on",
Value: "127.0.0.1:7897",
},
}
app.Before = func(context *cli.Context) error {
if context.GlobalBool("debug") {
logrus.SetLevel(logrus.DebugLevel)
}
return nil
}
app.Action = func(context *cli.Context) error {
signals := make(chan os.Signal, 2048)
signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT)
if address := context.GlobalString("metrics-address"); address != "" {
go serveMetrics(address)
}
path := context.GlobalString("socket")
if path == "" {
return fmt.Errorf("--socket path cannot be empty")
}
l, err := createUnixSocket(path)
if err != nil {
return err
}
server := grpc.NewServer()
go serveGRPC(server, l)
for s := range signals {
switch s {
default:
logrus.WithField("signal", s).Info("containerd: stopping GRPC server")
server.Stop()
return nil
}
}
return nil
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "containerd: %s\n", err)
os.Exit(1)
}
}
func createUnixSocket(path string) (net.Listener, error) {
if err := os.MkdirAll(filepath.Dir(path), 0660); err != nil {
return nil, err
}
if err := syscall.Unlink(path); err != nil && !os.IsNotExist(err) {
return nil, err
}
return net.Listen("unix", path)
}
func serveMetrics(address string) {
m := http.NewServeMux()
m.Handle("/metrics", metrics.Handler())
if err := http.ListenAndServe(address, m); err != nil {
logrus.WithError(err).Fatal("containerd: metrics server failure")
}
}
func serveGRPC(server *grpc.Server, l net.Listener) {
defer l.Close()
if err := server.Serve(l); err != nil {
l.Close()
logrus.WithError(err).Fatal("containerd: GRPC server failure")
}
}

49
cmd/ctr/main.go Normal file
View file

@ -0,0 +1,49 @@
package main
import (
"fmt"
"os"
"github.com/Sirupsen/logrus"
"github.com/docker/containerd"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "ctr"
app.Version = containerd.Version
app.Usage = `
__
_____/ /______
/ ___/ __/ ___/
/ /__/ /_/ /
\___/\__/_/
containerd client
`
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "enable debug output in logs",
},
cli.StringFlag{
Name: "socket, s",
Usage: "socket path for containerd's GRPC server",
Value: "/run/containerd/containerd.sock",
},
}
app.Commands = []cli.Command{
runCommand,
}
app.Before = func(context *cli.Context) error {
if context.GlobalBool("debug") {
logrus.SetLevel(logrus.DebugLevel)
}
return nil
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "containerd: %s\n", err)
os.Exit(1)
}
}

83
cmd/ctr/run.go Normal file
View file

@ -0,0 +1,83 @@
package main
import (
"fmt"
"os"
"github.com/BurntSushi/toml"
"github.com/urfave/cli"
)
type runConfig struct {
Image string `toml:"image"`
Process struct {
Args []string `toml:"args"`
Env []string `toml:"env"`
Cwd string `toml:"cwd"`
Uid int `toml:"uid"`
Gid int `toml:"gid"`
Tty bool `toml:"tty"`
} `toml:"process"`
Network struct {
Type string `toml:"type"`
IP string `toml:"ip"`
Gateway string `toml:"gateway"`
} `toml:"network"`
}
var runCommand = cli.Command{
Name: "run",
Usage: "run a container",
Action: func(context *cli.Context) error {
var config runConfig
if _, err := toml.DecodeFile(context.Args().First(), &config); err != nil {
return err
}
id := context.Args().Get(1)
if id == "" {
return fmt.Errorf("containerd must be provided")
}
client, err := getClient(context)
if err != nil {
return err
}
clone, err := client.Images.Clone(config.Image)
if err != nil {
return err
}
container, err := client.Containers.Create(CreateRequest{
Id: id,
Mounts: clone.Mounts,
Process: Process{
Args: config.Args,
Env: config.Env,
Cwd: config.Cwd,
Uid: config.Uid,
Gid: config.Gid,
Tty: config.Tty,
Stdin: os.Stdin.Name(),
Stdout: os.Stdout.Name(),
Stderr: os.Stderr.Name(),
},
Owner: "ctr",
})
defer client.Containers.Delete(container)
if err := client.Networks.Attach(config.Network, container); err != nil {
return err
}
if err := client.Containers.Start(container); err != nil {
return err
}
go forwarSignals(client.Containers.SignalProcess, container.Process)
events, err := client.Containers.Events(container.Id)
if err != nil {
return err
}
for event := range events {
if event.Type == "exit" {
os.Exit(event.Status)
}
}
return nil
},
}