Merge pull request #160 from mrunalp/feature/hooks

Add prestart/poststop hooks to runc
This commit is contained in:
Alexander Morozov 2015-09-24 14:52:30 -07:00
commit aac9179bba
4 changed files with 36 additions and 8 deletions

View File

@ -173,6 +173,9 @@ type Config struct {
// Hooks are a collection of actions to perform at various container lifecycle events. // Hooks are a collection of actions to perform at various container lifecycle events.
// Hooks are not able to be marshaled to json but they are also not needed to. // Hooks are not able to be marshaled to json but they are also not needed to.
Hooks *Hooks `json:"-"` Hooks *Hooks `json:"-"`
// Version is the version of opencontainer specification that is supported.
Version string `json:"version"`
} }
type Hooks struct { type Hooks struct {
@ -186,6 +189,7 @@ type Hooks struct {
// HookState is the payload provided to a hook on execution. // HookState is the payload provided to a hook on execution.
type HookState struct { type HookState struct {
Version string `json:"version"`
ID string `json:"id"` ID string `json:"id"`
Pid int `json:"pid"` Pid int `json:"pid"`
Root string `json:"root"` Root string `json:"root"`

View File

@ -250,6 +250,7 @@ func (c *linuxContainer) Destroy() error {
c.initProcess = nil c.initProcess = nil
if c.config.Hooks != nil { if c.config.Hooks != nil {
s := configs.HookState{ s := configs.HookState{
Version: c.config.Version,
ID: c.id, ID: c.id,
Root: c.config.Rootfs, Root: c.config.Rootfs,
} }

View File

@ -203,6 +203,7 @@ func (p *initProcess) start() (err error) {
}() }()
if p.config.Config.Hooks != nil { if p.config.Config.Hooks != nil {
s := configs.HookState{ s := configs.HookState{
Version: p.container.config.Version,
ID: p.container.id, ID: p.container.id,
Pid: p.pid(), Pid: p.pid(),
Root: p.config.Config.Rootfs, Root: p.config.Config.Rootfs,

22
spec.go
View File

@ -391,6 +391,8 @@ func createLibcontainerConfig(cgroupName string, spec *specs.LinuxSpec, rspec *s
config.Sysctl = rspec.Linux.Sysctl config.Sysctl = rspec.Linux.Sysctl
config.ProcessLabel = rspec.Linux.SelinuxProcessLabel config.ProcessLabel = rspec.Linux.SelinuxProcessLabel
config.AppArmorProfile = rspec.Linux.ApparmorProfile config.AppArmorProfile = rspec.Linux.ApparmorProfile
createHooks(rspec, config)
config.Version = specs.Version
return config, nil return config, nil
} }
@ -653,3 +655,23 @@ func setupSeccomp(config *specs.Seccomp) (*configs.Seccomp, error) {
return newConfig, nil return newConfig, nil
} }
func createHooks(rspec *specs.LinuxRuntimeSpec, config *configs.Config) {
config.Hooks = &configs.Hooks{}
for _, h := range rspec.Hooks.Prestart {
cmd := configs.Command{
Path: h.Path,
Args: h.Args,
Env: h.Env,
}
config.Hooks.Prestart = append(config.Hooks.Prestart, configs.NewCommandHook(cmd))
}
for _, h := range rspec.Hooks.Poststop {
cmd := configs.Command{
Path: h.Path,
Args: h.Args,
Env: h.Env,
}
config.Hooks.Poststop = append(config.Hooks.Poststop, configs.NewCommandHook(cmd))
}
}