2016-11-30 00:52:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-03-06 19:07:29 +00:00
|
|
|
gocontext "context"
|
2017-02-06 22:57:43 +00:00
|
|
|
"encoding/json"
|
2017-02-20 06:44:41 +00:00
|
|
|
"io/ioutil"
|
2016-12-07 18:44:05 +00:00
|
|
|
"path/filepath"
|
2017-02-13 18:23:28 +00:00
|
|
|
"runtime"
|
|
|
|
|
2017-02-20 06:44:41 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2017-02-06 22:57:43 +00:00
|
|
|
"github.com/crosbymichael/console"
|
2017-02-09 07:31:26 +00:00
|
|
|
"github.com/docker/containerd/api/services/execution"
|
2017-02-13 18:23:28 +00:00
|
|
|
"github.com/docker/containerd/api/types/mount"
|
|
|
|
protobuf "github.com/gogo/protobuf/types"
|
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2017-02-21 12:20:18 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-03-06 19:07:29 +00:00
|
|
|
"github.com/urfave/cli"
|
2016-11-30 00:52:04 +00:00
|
|
|
)
|
|
|
|
|
2017-02-13 18:23:28 +00:00
|
|
|
var rwm = "rwm"
|
|
|
|
|
2017-02-20 06:44:41 +00:00
|
|
|
const rootfsPath = "rootfs"
|
|
|
|
|
2017-02-13 18:23:28 +00:00
|
|
|
func spec(id string, args []string, tty bool) *specs.Spec {
|
|
|
|
return &specs.Spec{
|
|
|
|
Version: specs.Version,
|
|
|
|
Platform: specs.Platform{
|
|
|
|
OS: runtime.GOOS,
|
|
|
|
Arch: runtime.GOARCH,
|
|
|
|
},
|
|
|
|
Root: specs.Root{
|
2017-02-20 06:44:41 +00:00
|
|
|
Path: rootfsPath,
|
2017-02-13 18:23:28 +00:00
|
|
|
Readonly: true,
|
|
|
|
},
|
|
|
|
Process: specs.Process{
|
|
|
|
Args: args,
|
|
|
|
Env: []string{
|
|
|
|
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
|
|
|
|
},
|
|
|
|
Terminal: tty,
|
|
|
|
Cwd: "/",
|
|
|
|
NoNewPrivileges: true,
|
|
|
|
},
|
|
|
|
Mounts: []specs.Mount{
|
|
|
|
{
|
|
|
|
Destination: "/proc",
|
|
|
|
Type: "proc",
|
|
|
|
Source: "proc",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Destination: "/dev",
|
|
|
|
Type: "tmpfs",
|
|
|
|
Source: "tmpfs",
|
|
|
|
Options: []string{"nosuid", "strictatime", "mode=755", "size=65536k"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Destination: "/dev/pts",
|
|
|
|
Type: "devpts",
|
|
|
|
Source: "devpts",
|
|
|
|
Options: []string{"nosuid", "noexec", "newinstance", "ptmxmode=0666", "mode=0620", "gid=5"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Destination: "/dev/shm",
|
|
|
|
Type: "tmpfs",
|
|
|
|
Source: "shm",
|
|
|
|
Options: []string{"nosuid", "noexec", "nodev", "mode=1777", "size=65536k"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Destination: "/dev/mqueue",
|
|
|
|
Type: "mqueue",
|
|
|
|
Source: "mqueue",
|
|
|
|
Options: []string{"nosuid", "noexec", "nodev"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Destination: "/sys",
|
|
|
|
Type: "sysfs",
|
|
|
|
Source: "sysfs",
|
|
|
|
Options: []string{"nosuid", "noexec", "nodev"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Destination: "/run",
|
|
|
|
Type: "tmpfs",
|
|
|
|
Source: "tmpfs",
|
|
|
|
Options: []string{"nosuid", "strictatime", "mode=755", "size=65536k"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Destination: "/etc/resolv.conf",
|
|
|
|
Type: "bind",
|
|
|
|
Source: "/etc/resolv.conf",
|
|
|
|
Options: []string{"rbind", "ro"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Destination: "/etc/hosts",
|
|
|
|
Type: "bind",
|
|
|
|
Source: "/etc/hosts",
|
|
|
|
Options: []string{"rbind", "ro"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Destination: "/etc/localtime",
|
|
|
|
Type: "bind",
|
|
|
|
Source: "/etc/localtime",
|
|
|
|
Options: []string{"rbind", "ro"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Hostname: id,
|
|
|
|
Linux: &specs.Linux{
|
|
|
|
Resources: &specs.LinuxResources{
|
|
|
|
Devices: []specs.LinuxDeviceCgroup{
|
|
|
|
{
|
|
|
|
Allow: false,
|
|
|
|
Access: &rwm,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Namespaces: []specs.LinuxNamespace{
|
|
|
|
{
|
|
|
|
Type: "pid",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "ipc",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "uts",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "mount",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "network",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 06:44:41 +00:00
|
|
|
func customSpec(configPath string) (*specs.Spec, error) {
|
|
|
|
b, err := ioutil.ReadFile(configPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var s specs.Spec
|
|
|
|
if err := json.Unmarshal(b, &s); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if s.Root.Path != rootfsPath {
|
|
|
|
logrus.Warnf("ignoring Root.Path %q, setting %q forcibly", s.Root.Path, rootfsPath)
|
|
|
|
s.Root.Path = rootfsPath
|
|
|
|
}
|
|
|
|
return &s, nil
|
|
|
|
}
|
|
|
|
|
2016-11-30 00:52:04 +00:00
|
|
|
var runCommand = cli.Command{
|
|
|
|
Name: "run",
|
|
|
|
Usage: "run a container",
|
2016-12-06 00:36:15 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
2017-02-13 18:23:28 +00:00
|
|
|
Name: "id",
|
|
|
|
Usage: "id of the container",
|
2016-12-06 00:36:15 +00:00
|
|
|
},
|
2016-12-09 17:17:34 +00:00
|
|
|
cli.BoolFlag{
|
2017-02-13 18:23:28 +00:00
|
|
|
Name: "tty,t",
|
2016-12-09 17:17:34 +00:00
|
|
|
Usage: "allocate a TTY for the container",
|
|
|
|
},
|
2017-02-13 18:23:28 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "rootfs,r",
|
|
|
|
Usage: "path to the container's root filesystem",
|
|
|
|
},
|
2017-02-20 06:44:41 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "runtime-config",
|
|
|
|
Usage: "custom runtime config (config.json)",
|
|
|
|
},
|
2017-03-06 19:07:29 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "runtime",
|
|
|
|
Usage: "runtime name (linux, windows, vmware-linux)",
|
|
|
|
Value: "linux",
|
|
|
|
},
|
2016-12-06 00:36:15 +00:00
|
|
|
},
|
2016-11-30 00:52:04 +00:00
|
|
|
Action: func(context *cli.Context) error {
|
2017-02-13 18:23:28 +00:00
|
|
|
id := context.String("id")
|
2016-11-30 00:52:04 +00:00
|
|
|
if id == "" {
|
2017-02-21 12:20:18 +00:00
|
|
|
return errors.New("container id must be provided")
|
2016-11-30 00:52:04 +00:00
|
|
|
}
|
2016-12-07 18:44:05 +00:00
|
|
|
|
2017-02-13 18:23:28 +00:00
|
|
|
containers, err := getExecutionService(context)
|
2016-12-11 19:07:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
tmpDir, err := getTempDir(id)
|
2016-12-11 19:07:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
events, err := containers.Events(gocontext.Background(), &execution.EventsRequest{})
|
2016-12-07 18:44:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
// for ctr right now just do a bind mount
|
|
|
|
rootfs := []*mount.Mount{
|
|
|
|
{
|
|
|
|
Type: "bind",
|
|
|
|
Source: context.String("rootfs"),
|
|
|
|
Options: []string{
|
|
|
|
"rw",
|
|
|
|
"rbind",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-02-20 06:44:41 +00:00
|
|
|
|
|
|
|
var s *specs.Spec
|
|
|
|
if config := context.String("runtime-config"); config == "" {
|
|
|
|
s = spec(id, []string(context.Args()), context.Bool("tty"))
|
|
|
|
} else {
|
|
|
|
s, err = customSpec(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
data, err := json.Marshal(s)
|
2017-01-12 18:23:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
create := &execution.CreateRequest{
|
|
|
|
ID: id,
|
|
|
|
Spec: &protobuf.Any{
|
|
|
|
TypeUrl: specs.Version,
|
|
|
|
Value: data,
|
|
|
|
},
|
|
|
|
Rootfs: rootfs,
|
2017-03-06 19:07:29 +00:00
|
|
|
Runtime: context.String("runtime"),
|
2017-02-13 18:23:28 +00:00
|
|
|
Terminal: context.Bool("tty"),
|
|
|
|
Stdin: filepath.Join(tmpDir, "stdin"),
|
|
|
|
Stdout: filepath.Join(tmpDir, "stdout"),
|
|
|
|
Stderr: filepath.Join(tmpDir, "stderr"),
|
2016-12-07 18:44:05 +00:00
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
if create.Terminal {
|
2017-02-06 22:57:43 +00:00
|
|
|
con := console.Current()
|
|
|
|
defer con.Reset()
|
|
|
|
if err := con.SetRaw(); err != nil {
|
2017-01-12 19:37:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
fwg, err := prepareStdio(create.Stdin, create.Stdout, create.Stderr, create.Terminal)
|
2016-12-06 00:36:15 +00:00
|
|
|
if err != nil {
|
2016-11-30 00:52:04 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-02-21 16:46:04 +00:00
|
|
|
|
2017-02-13 18:23:28 +00:00
|
|
|
response, err := containers.Create(gocontext.Background(), create)
|
2016-12-07 18:44:05 +00:00
|
|
|
if err != nil {
|
2017-02-13 18:23:28 +00:00
|
|
|
return err
|
2016-12-07 18:44:05 +00:00
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
if _, err := containers.Start(gocontext.Background(), &execution.StartRequest{
|
|
|
|
ID: response.ID,
|
2016-12-06 00:36:15 +00:00
|
|
|
}); err != nil {
|
2017-02-13 18:23:28 +00:00
|
|
|
return err
|
2016-11-30 00:52:04 +00:00
|
|
|
}
|
2017-03-06 19:43:31 +00:00
|
|
|
|
|
|
|
// Ensure we read all io only if container started successfully.
|
|
|
|
defer fwg.Wait()
|
|
|
|
|
2017-02-13 18:23:28 +00:00
|
|
|
status, err := waitContainer(events, response)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-12-07 18:44:05 +00:00
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
if _, err := containers.Delete(gocontext.Background(), &execution.DeleteRequest{
|
|
|
|
ID: response.ID,
|
2016-12-06 00:36:15 +00:00
|
|
|
}); err != nil {
|
2017-02-13 18:23:28 +00:00
|
|
|
return err
|
2016-11-30 00:52:04 +00:00
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
if status != 0 {
|
|
|
|
return cli.NewExitError("", int(status))
|
2017-02-06 22:57:43 +00:00
|
|
|
}
|
2016-11-30 00:52:04 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|