summaryrefslogtreecommitdiff
path: root/libpod/container_internal.go
diff options
context:
space:
mode:
authorDaniel J Walsh <dwalsh@redhat.com>2018-03-29 11:01:47 -0400
committerAtomic Bot <atomic-devel@projectatomic.io>2018-04-05 14:13:49 +0000
commitfdcf633a33bbbfbc99268965ef5da03a4770619b (patch)
tree59be1ab815261e106220a4691766830b58657ac0 /libpod/container_internal.go
parentca3b2414516c04125f986775c0cbce27f0f1e505 (diff)
downloadpodman-fdcf633a33bbbfbc99268965ef5da03a4770619b.tar.gz
podman-fdcf633a33bbbfbc99268965ef5da03a4770619b.tar.bz2
podman-fdcf633a33bbbfbc99268965ef5da03a4770619b.zip
Add hooks support to podman
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com> Closes: #155 Approved by: mheon
Diffstat (limited to 'libpod/container_internal.go')
-rw-r--r--libpod/container_internal.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/libpod/container_internal.go b/libpod/container_internal.go
index f75df8c28..f3247b1c0 100644
--- a/libpod/container_internal.go
+++ b/libpod/container_internal.go
@@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
+ "regexp"
"strings"
"syscall"
"time"
@@ -22,6 +23,7 @@ import (
"github.com/pkg/errors"
crioAnnotations "github.com/projectatomic/libpod/pkg/annotations"
"github.com/projectatomic/libpod/pkg/chrootuser"
+ "github.com/projectatomic/libpod/pkg/hooks"
"github.com/projectatomic/libpod/pkg/secrets"
"github.com/projectatomic/libpod/pkg/util"
"github.com/sirupsen/logrus"
@@ -931,6 +933,9 @@ func (c *Container) generateSpec() (*spec.Spec, error) {
}
}
+ if err := c.setupOCIHooks(&g); err != nil {
+ return nil, errors.Wrapf(err, "error setting up OCI Hooks")
+ }
// Bind builtin image volumes
if c.config.ImageVolumes {
if err := c.addImageVolumes(&g); err != nil {
@@ -1103,3 +1108,58 @@ func (c *Container) saveSpec(spec *spec.Spec) error {
return nil
}
+
+func (c *Container) setupOCIHooks(g *generate.Generator) error {
+ addedHooks := map[string]struct{}{}
+ ocihooks, err := hooks.SetupHooks(c.runtime.config.HooksDir)
+ if err != nil {
+ return err
+ }
+ addHook := func(hook hooks.HookParams) error {
+ // Only add a hook once
+ if _, ok := addedHooks[hook.Hook]; !ok {
+ if err := hooks.AddOCIHook(g, hook); err != nil {
+ return err
+ }
+ addedHooks[hook.Hook] = struct{}{}
+ }
+ return nil
+ }
+ for _, hook := range ocihooks {
+ logrus.Debugf("SetupOCIHooks", hook)
+ if hook.HasBindMounts && len(c.config.Spec.Mounts) > 0 {
+ if err := addHook(hook); err != nil {
+ return err
+ }
+ continue
+ }
+ for _, cmd := range hook.Cmds {
+ match, err := regexp.MatchString(cmd, c.config.Spec.Process.Args[0])
+ if err != nil {
+ logrus.Errorf("Invalid regex %q:%q", cmd, err)
+ continue
+ }
+ if match {
+ if err := addHook(hook); err != nil {
+ return err
+ }
+ }
+ }
+ annotations := c.Spec().Annotations
+ for _, annotationRegex := range hook.Annotations {
+ for _, annotation := range annotations {
+ match, err := regexp.MatchString(annotationRegex, annotation)
+ if err != nil {
+ logrus.Errorf("Invalid regex %q:%q", annotationRegex, err)
+ continue
+ }
+ if match {
+ if err := addHook(hook); err != nil {
+ return err
+ }
+ }
+ }
+ }
+ }
+ return nil
+}