2016-05-16 18:50:55 +03:00
|
|
|
// +build linux,experimental
|
|
|
|
|
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/libcontainerd"
|
|
|
|
"github.com/docker/docker/oci"
|
|
|
|
"github.com/docker/docker/pkg/plugins"
|
|
|
|
"github.com/docker/docker/pkg/system"
|
|
|
|
"github.com/docker/docker/restartmanager"
|
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
"github.com/docker/engine-api/types/container"
|
|
|
|
"github.com/opencontainers/specs/specs-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (pm *Manager) enable(p *plugin) error {
|
|
|
|
spec, err := pm.initSpec(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.restartManager = restartmanager.New(container.RestartPolicy{Name: "always"}, 0)
|
2016-06-15 20:39:33 +03:00
|
|
|
if err := pm.containerdClient.Create(p.P.ID, libcontainerd.Spec(*spec), libcontainerd.WithRestartManager(p.restartManager)); err != nil { // POC-only
|
2016-05-16 18:50:55 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-15 20:39:33 +03:00
|
|
|
socket := p.P.Manifest.Interface.Socket
|
2016-05-16 18:50:55 +03:00
|
|
|
p.client, err = plugins.NewClient("unix://"+filepath.Join(p.runtimeSourcePath, socket), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: check net.Dial
|
|
|
|
|
|
|
|
pm.Lock() // fixme: lock single record
|
2016-06-15 20:39:33 +03:00
|
|
|
p.P.Active = true
|
2016-05-16 18:50:55 +03:00
|
|
|
pm.save()
|
|
|
|
pm.Unlock()
|
|
|
|
|
2016-06-15 20:39:33 +03:00
|
|
|
for _, typ := range p.P.Manifest.Interface.Types {
|
2016-05-16 18:50:55 +03:00
|
|
|
if handler := pm.handlers[typ.String()]; handler != nil {
|
|
|
|
handler(p.Name(), p.Client())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-15 20:39:33 +03:00
|
|
|
func (pm *Manager) restore(p *plugin) error {
|
|
|
|
p.restartManager = restartmanager.New(container.RestartPolicy{Name: "always"}, 0)
|
|
|
|
return pm.containerdClient.Restore(p.P.ID, libcontainerd.WithRestartManager(p.restartManager))
|
|
|
|
}
|
|
|
|
|
2016-05-16 18:50:55 +03:00
|
|
|
func (pm *Manager) initSpec(p *plugin) (*specs.Spec, error) {
|
|
|
|
s := oci.DefaultSpec()
|
|
|
|
|
2016-06-15 20:39:33 +03:00
|
|
|
rootfs := filepath.Join(pm.libRoot, p.P.ID, "rootfs")
|
2016-05-16 18:50:55 +03:00
|
|
|
s.Root = specs.Root{
|
|
|
|
Path: rootfs,
|
|
|
|
Readonly: false, // TODO: all plugins should be readonly? settable in manifest?
|
|
|
|
}
|
|
|
|
|
2016-06-15 20:39:33 +03:00
|
|
|
mounts := append(p.P.Config.Mounts, types.PluginMount{
|
2016-05-16 18:50:55 +03:00
|
|
|
Source: &p.runtimeSourcePath,
|
|
|
|
Destination: defaultPluginRuntimeDestination,
|
|
|
|
Type: "bind",
|
|
|
|
Options: []string{"rbind", "rshared"},
|
|
|
|
})
|
|
|
|
for _, mount := range mounts {
|
|
|
|
m := specs.Mount{
|
|
|
|
Destination: mount.Destination,
|
|
|
|
Type: mount.Type,
|
|
|
|
Options: mount.Options,
|
|
|
|
}
|
|
|
|
// TODO: if nil, then it's required and user didn't set it
|
|
|
|
if mount.Source != nil {
|
|
|
|
m.Source = *mount.Source
|
|
|
|
}
|
|
|
|
if m.Source != "" && m.Type == "bind" {
|
|
|
|
fi, err := os.Lstat(filepath.Join(rootfs, string(os.PathSeparator), m.Destination)) // TODO: followsymlinks
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if fi.IsDir() {
|
|
|
|
if err := os.MkdirAll(m.Source, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.Mounts = append(s.Mounts, m)
|
|
|
|
}
|
|
|
|
|
2016-06-15 20:39:33 +03:00
|
|
|
envs := make([]string, 1, len(p.P.Config.Env)+1)
|
2016-05-16 18:50:55 +03:00
|
|
|
envs[0] = "PATH=" + system.DefaultPathEnv
|
2016-06-15 20:39:33 +03:00
|
|
|
envs = append(envs, p.P.Config.Env...)
|
2016-05-16 18:50:55 +03:00
|
|
|
|
2016-06-15 20:39:33 +03:00
|
|
|
args := append(p.P.Manifest.Entrypoint, p.P.Config.Args...)
|
2016-06-17 19:13:35 +03:00
|
|
|
cwd := p.P.Manifest.Workdir
|
|
|
|
if len(cwd) == 0 {
|
|
|
|
cwd = "/"
|
|
|
|
}
|
2016-05-16 18:50:55 +03:00
|
|
|
s.Process = specs.Process{
|
|
|
|
Terminal: false,
|
|
|
|
Args: args,
|
2016-06-17 19:13:35 +03:00
|
|
|
Cwd: cwd,
|
2016-05-16 18:50:55 +03:00
|
|
|
Env: envs,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) disable(p *plugin) error {
|
|
|
|
if err := p.restartManager.Cancel(); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
2016-06-15 20:39:33 +03:00
|
|
|
if err := pm.containerdClient.Signal(p.P.ID, int(syscall.SIGKILL)); err != nil {
|
2016-05-16 18:50:55 +03:00
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(p.runtimeSourcePath)
|
|
|
|
pm.Lock() // fixme: lock single record
|
|
|
|
defer pm.Unlock()
|
2016-06-15 20:39:33 +03:00
|
|
|
p.P.Active = false
|
2016-05-16 18:50:55 +03:00
|
|
|
pm.save()
|
|
|
|
return nil
|
|
|
|
}
|