Skip to content

Commit 29a4d8e

Browse files
author
Ma Shimiao
committed
validate: add Hooks validation
Signed-off-by: Ma Shimiao <mashimiao.fnst@cn.fujitsu.com>
1 parent f59e581 commit 29a4d8e

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

man/ocitools-validate.1.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ ocitools-validate - Validate a OCI bundle
88
**ocitools validate**
99
[**--help**]
1010
[**--path**[=*PATH*]
11+
[**--hooks-check**[=*true*|*false*]
1112

1213
# DESCRIPTION
1314

@@ -20,6 +21,9 @@ Validate an OCI bundle
2021
**--path="PATH"
2122
Path to bundle
2223

24+
**--hooks-check**
25+
Specify check hooks on host, default is false.
26+
2327
# SEE ALSO
2428
**ocitools**(1)
2529

validate.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
var bundleValidateFlags = []cli.Flag{
1818
cli.StringFlag{Name: "path", Usage: "path to a bundle"},
19+
cli.BoolFlag{Name: "hooks-check", Usage: "Specify check hooks on host, default is false"},
1920
}
2021

2122
var (
@@ -72,18 +73,23 @@ var bundleValidateCommand = cli.Command{
7273
logrus.Fatalf("Rootfs: %v is not a directory.", spec.Root.Path)
7374
}
7475

75-
bundleValidate(spec, rootfsPath)
76+
hooksCheck := context.Bool("hooks-check")
77+
bundleValidate(spec, rootfsPath, hooksCheck)
7678
logrus.Infof("Bundle validation succeeded.")
7779
},
7880
}
7981

80-
func bundleValidate(spec rspec.Spec, rootfs string) {
82+
func bundleValidate(spec rspec.Spec, rootfs string, hooksCheck bool) {
8183
checkMandatoryField(spec)
8284
checkSemVer(spec.Version)
8385
checkPlatform(spec.Platform)
8486
checkProcess(spec.Process, rootfs)
8587
checkMounts(spec.Mounts, rootfs)
8688
checkLinux(spec.Linux, rootfs)
89+
90+
if hooksCheck {
91+
checkHooks(spec.Hooks)
92+
}
8793
}
8894

8995
func checkSemVer(version string) {
@@ -128,6 +134,26 @@ func checkPlatform(platform rspec.Platform) {
128134
logrus.Fatalf("Operation system '%s' of the bundle is not supported yet.", platform.OS)
129135
}
130136

137+
func checkHooks(hooks rspec.Hooks) {
138+
for _, prestart := range hooks.Prestart {
139+
if _, err := os.Stat(prestart.Path); err != nil {
140+
logrus.Fatalf("Cannot find pre-start hook: %v", prestart.Path)
141+
}
142+
}
143+
144+
for _, poststart := range hooks.Poststart {
145+
if _, err := os.Stat(poststart.Path); err != nil {
146+
logrus.Fatalf("Cannot find post-start hook: %v", poststart.Path)
147+
}
148+
}
149+
150+
for _, poststop := range hooks.Poststop {
151+
if _, err := os.Stat(poststop.Path); err != nil {
152+
logrus.Fatalf("Cannot find post-stop hook: %v", poststop.Path)
153+
}
154+
}
155+
}
156+
131157
func checkProcess(process rspec.Process, rootfs string) {
132158
for index := 0; index < len(process.Capabilities); index++ {
133159
capability := process.Capabilities[index]

0 commit comments

Comments
 (0)