Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions man/ocitools-validate.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Validate an OCI bundle
**--path=PATH
Path to bundle

**--hooks**
Check specified hooks exist and are executable on the host.

# SEE ALSO
**ocitools**(1)

Expand Down
32 changes: 30 additions & 2 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path"
"path/filepath"
"reflect"
"regexp"
"strings"
Expand All @@ -19,6 +20,7 @@ import (

var bundleValidateFlags = []cli.Flag{
cli.StringFlag{Name: "path", Usage: "path to a bundle"},
cli.BoolFlag{Name: "hooks", Usage: "Check specified hooks exist and are executable on the host."},
}

var (
Expand Down Expand Up @@ -76,17 +78,19 @@ var bundleValidateCommand = cli.Command{
logrus.Fatalf("root path %q is not a directory.", spec.Root.Path)
}

bundleValidate(spec, rootfsPath)
hooksCheck := context.Bool("hooks")
bundleValidate(spec, rootfsPath, hooksCheck)
logrus.Infof("Bundle validation succeeded.")
},
}

func bundleValidate(spec rspec.Spec, rootfs string) {
func bundleValidate(spec rspec.Spec, rootfs string, hooksCheck bool) {
checkMandatoryField(spec)
checkSemVer(spec.Version)
checkPlatform(spec.Platform)
checkProcess(spec.Process, rootfs)
checkLinux(spec.Linux, spec.Hostname, rootfs)
checkHooks(spec.Hooks, hooksCheck)
}

func checkSemVer(version string) {
Expand Down Expand Up @@ -120,6 +124,30 @@ func checkPlatform(platform rspec.Platform) {
logrus.Fatalf("Operation system %q of the bundle is not supported yet.", platform.OS)
}

func checkHooks(hooks rspec.Hooks, hooksCheck bool) {
checkEventHookPaths("pre-start", hooks.Prestart, hooksCheck)
checkEventHookPaths("post-start", hooks.Poststart, hooksCheck)
checkEventHookPaths("post-stop", hooks.Poststop, hooksCheck)
}

func checkEventHookPaths(hookType string, hooks []rspec.Hook, hooksCheck bool) {
for _, hook := range hooks {
if !filepath.IsAbs(hook.Path) {
logrus.Fatalf("The %s hook %v: is not absolute path", hookType, hook.Path)
}

if hooksCheck {
fi, err := os.Stat(hook.Path)
if err != nil {
logrus.Fatalf("Cannot find %s hook: %v", hookType, hook.Path)
}
if fi.Mode()&0111 == 0 {
logrus.Fatalf("The %s hook %v: is not executable", hookType, hook.Path)
}
}
}
}

func checkProcess(process rspec.Process, rootfs string) {
if !path.IsAbs(process.Cwd) {
logrus.Fatalf("cwd %q is not an absolute path", process.Cwd)
Expand Down