|
| 1 | +package hooks |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "regexp" |
| 10 | + "strings" |
| 11 | + "syscall" |
| 12 | + |
| 13 | + spec "github.com/opencontainers/runtime-spec/specs-go" |
| 14 | + "github.com/opencontainers/runtime-tools/generate" |
| 15 | + "github.com/pkg/errors" |
| 16 | + "github.com/sirupsen/logrus" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + // DefaultHooksDir Default directory containing hooks config files |
| 21 | + DefaultHooksDir = "/usr/share/containers/oci/hooks.d" |
| 22 | + // OverrideHooksDir Directory where admin can override the default configuration |
| 23 | + OverrideHooksDir = "/etc/containers/oci/hooks.d" |
| 24 | +) |
| 25 | + |
| 26 | +// HookParams is the structure returned from read the hooks configuration |
| 27 | +type HookParams struct { |
| 28 | + Hook string `json:"hook"` |
| 29 | + Stage []string `json:"stage"` |
| 30 | + Cmds []string `json:"cmd"` |
| 31 | + Annotations []string `json:"annotation"` |
| 32 | + HasBindMounts bool `json:"hasbindmounts"` |
| 33 | + Arguments []string `json:"arguments"` |
| 34 | +} |
| 35 | + |
| 36 | +// readHook reads hooks json files, verifies it and returns the json config |
| 37 | +func readHook(hookPath string) (HookParams, error) { |
| 38 | + var hook HookParams |
| 39 | + raw, err := ioutil.ReadFile(hookPath) |
| 40 | + if err != nil { |
| 41 | + return hook, errors.Wrapf(err, "error Reading hook %q", hookPath) |
| 42 | + } |
| 43 | + if err := json.Unmarshal(raw, &hook); err != nil { |
| 44 | + return hook, errors.Wrapf(err, "error Unmarshalling JSON for %q", hookPath) |
| 45 | + } |
| 46 | + if _, err := os.Stat(hook.Hook); err != nil { |
| 47 | + return hook, errors.Wrapf(err, "unable to stat hook %q in hook config %q", hook.Hook, hookPath) |
| 48 | + } |
| 49 | + validStage := map[string]bool{"prestart": true, "poststart": true, "poststop": true} |
| 50 | + for _, cmd := range hook.Cmds { |
| 51 | + if _, err = regexp.Compile(cmd); err != nil { |
| 52 | + return hook, errors.Wrapf(err, "invalid cmd regular expression %q defined in hook config %q", cmd, hookPath) |
| 53 | + } |
| 54 | + } |
| 55 | + for _, cmd := range hook.Annotations { |
| 56 | + if _, err = regexp.Compile(cmd); err != nil { |
| 57 | + return hook, errors.Wrapf(err, "invalid cmd regular expression %q defined in hook config %q", cmd, hookPath) |
| 58 | + } |
| 59 | + } |
| 60 | + for _, stage := range hook.Stage { |
| 61 | + if !validStage[stage] { |
| 62 | + return hook, errors.Wrapf(err, "unknown stage %q defined in hook config %q", stage, hookPath) |
| 63 | + } |
| 64 | + } |
| 65 | + return hook, nil |
| 66 | +} |
| 67 | + |
| 68 | +// readHooks reads hooks json files in directory to setup OCI Hooks |
| 69 | +// adding hooks to the passedin hooks map. |
| 70 | +func readHooks(hooksPath string, hooks map[string]HookParams) error { |
| 71 | + if _, err := os.Stat(hooksPath); err != nil { |
| 72 | + if os.IsNotExist(err) { |
| 73 | + logrus.Warnf("hooks path: %q does not exist", hooksPath) |
| 74 | + return nil |
| 75 | + } |
| 76 | + return errors.Wrapf(err, "unable to stat hooks path %q", hooksPath) |
| 77 | + } |
| 78 | + |
| 79 | + files, err := ioutil.ReadDir(hooksPath) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + for _, file := range files { |
| 85 | + if !strings.HasSuffix(file.Name(), ".json") { |
| 86 | + continue |
| 87 | + } |
| 88 | + hook, err := readHook(filepath.Join(hooksPath, file.Name())) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + for key, h := range hooks { |
| 93 | + // hook.Hook can only be defined in one hook file, unless it has the |
| 94 | + // same name in the override path. |
| 95 | + if hook.Hook == h.Hook && key != file.Name() { |
| 96 | + return errors.Wrapf(syscall.EINVAL, "duplicate path, hook %q from %q already defined in %q", hook.Hook, hooksPath, key) |
| 97 | + } |
| 98 | + } |
| 99 | + hooks[file.Name()] = hook |
| 100 | + } |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +// SetupHooks takes a hookspath and reads all of the hooks in that directory. |
| 105 | +// returning a map of the configured hooks |
| 106 | +func SetupHooks(hooksPath string) (map[string]HookParams, error) { |
| 107 | + hooksMap := make(map[string]HookParams) |
| 108 | + if err := readHooks(hooksPath, hooksMap); err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + if hooksPath == DefaultHooksDir { |
| 112 | + if err := readHooks(OverrideHooksDir, hooksMap); err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return hooksMap, nil |
| 118 | +} |
| 119 | + |
| 120 | +// AddOCIHook generates OCI specification using the included hook |
| 121 | +func AddOCIHook(g *generate.Generator, hook HookParams) error { |
| 122 | + for _, stage := range hook.Stage { |
| 123 | + h := spec.Hook{ |
| 124 | + Path: hook.Hook, |
| 125 | + Args: append([]string{hook.Hook}, hook.Arguments...), |
| 126 | + Env: []string{fmt.Sprintf("stage=%s", stage)}, |
| 127 | + } |
| 128 | + logrus.Debugf("AddOCIHook", h) |
| 129 | + switch stage { |
| 130 | + case "prestart": |
| 131 | + g.AddPreStartHook(h) |
| 132 | + |
| 133 | + case "poststart": |
| 134 | + g.AddPostStartHook(h) |
| 135 | + |
| 136 | + case "poststop": |
| 137 | + g.AddPostStopHook(h) |
| 138 | + } |
| 139 | + } |
| 140 | + return nil |
| 141 | +} |
0 commit comments