Skip to content

Commit fdcecc6

Browse files
author
Ma Shimiao
committed
generate: add hooks related env options
Signed-off-by: Ma Shimiao <[email protected]>
1 parent c3a59f6 commit fdcecc6

File tree

4 files changed

+133
-83
lines changed

4 files changed

+133
-83
lines changed

cmd/oci-runtime-tool/generate.go

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ var generateFlags = []cli.Flag{
2121
cli.StringSliceFlag{Name: "env", Usage: "add environment variable e.g. key=value"},
2222
cli.StringSliceFlag{Name: "env-file", Usage: "read in a file of environment variables"},
2323
cli.StringSliceFlag{Name: "hooks-poststart", Usage: "set command to run in poststart hooks"},
24+
cli.StringSliceFlag{Name: "hooks-poststart-env", Usage: "set environment variables for commands to run in poststart hooks"},
25+
cli.StringSliceFlag{Name: "hooks-poststart-timeout", Usage: "set timeout for commands to run in poststart hooks"},
2426
cli.StringSliceFlag{Name: "hooks-poststop", Usage: "set command to run in poststop hooks"},
27+
cli.StringSliceFlag{Name: "hooks-poststop-env", Usage: "set environment variables for commands to run in poststop hooks"},
28+
cli.StringSliceFlag{Name: "hooks-poststop-timeout", Usage: "set timeout for commands to run in poststop hooks"},
2529
cli.StringSliceFlag{Name: "hooks-prestart", Usage: "set command to run in prestart hooks"},
30+
cli.StringSliceFlag{Name: "hooks-prestart-env", Usage: "set environment variables for commands to run in prestart hooks"},
31+
cli.StringSliceFlag{Name: "hooks-prestart-timeout", Usage: "set timeout for commands to run in prestart hooks"},
2632
cli.StringFlag{Name: "hostname", Usage: "hostname value for the container"},
2733
cli.StringSliceFlag{Name: "label", Usage: "add annotations to the configuration e.g. key=value"},
2834
cli.StringFlag{Name: "linux-apparmor", Usage: "specifies the the apparmor profile for the container"},
@@ -75,12 +81,6 @@ var generateFlags = []cli.Flag{
7581
cli.StringSliceFlag{Name: "mount-bind", Usage: "bind mount directories src:dest[:options...]"},
7682
cli.StringFlag{Name: "mount-cgroups", Value: "no", Usage: "mount cgroups (rw,ro,no)"},
7783
cli.StringFlag{Name: "output", Usage: "output file (defaults to stdout)"},
78-
cli.StringSliceFlag{Name: "poststart", Usage: "set command to run in poststart hooks"},
79-
cli.StringSliceFlag{Name: "poststart-timeout", Usage: "set timeout for commands to run in poststart hooks"},
80-
cli.StringSliceFlag{Name: "poststop", Usage: "set command to run in poststop hooks"},
81-
cli.StringSliceFlag{Name: "poststop-timeout", Usage: "set timeout for commands to run in poststop hooks"},
82-
cli.StringSliceFlag{Name: "prestart", Usage: "set command to run in prestart hooks"},
83-
cli.StringSliceFlag{Name: "prestart-timeout", Usage: "set timeout for commands to run in prestart hooks"},
8484
cli.BoolFlag{Name: "privileged", Usage: "enable privileged container settings"},
8585
cli.StringSliceFlag{Name: "process-cap-add", Usage: "add Linux capabilities"},
8686
cli.StringSliceFlag{Name: "process-cap-drop", Usage: "drop Linux capabilities"},
@@ -338,27 +338,34 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
338338
}
339339
}
340340

341-
<<<<<<< HEAD
342-
if context.IsSet("hooks-prestart") {
343-
preStartHooks := context.StringSlice("hooks-prestart")
344-
for _, hook := range preStartHooks {
345-
path, args, err := parseHook(hook)
346-
=======
347-
if context.IsSet("poststart") {
348-
postStartHooks := context.StringSlice("poststart")
341+
if context.IsSet("hooks-poststart") {
342+
postStartHooks := context.StringSlice("hooks-poststart")
349343
for _, hook := range postStartHooks {
350-
path, args := parseHook(hook)
344+
path, args, err := parseHook(hook)
345+
if err != nil {
346+
return err
347+
}
351348
g.AddPostStartHook(path, args)
352349
}
353350
}
354351

355-
if context.IsSet("poststart-timeout") {
356-
postStartTimeouts := context.StringSlice("poststart-timeout")
352+
if context.IsSet("hooks-poststart-env") {
353+
postStartEnvs := context.StringSlice("hooks-poststart-env")
354+
for _, postStartEnv := range postStartEnvs {
355+
path, env, err := parseHookEnv(postStartEnv)
356+
if err != nil {
357+
return nil
358+
}
359+
g.AddPostStartHookEnv(path, env)
360+
}
361+
}
362+
363+
if context.IsSet("hooks-poststart-timeout") {
364+
postStartTimeouts := context.StringSlice("hooks-poststart-timeout")
357365
for _, postStartTimeout := range postStartTimeouts {
358366
path, timeout, err := parseHookTimeout(postStartTimeout)
359-
>>>>>>> generate: add timeout options for hooks
360367
if err != nil {
361-
return nil
368+
return err
362369
}
363370
g.AddPostStartHookTimeout(path, timeout)
364371
}
@@ -375,34 +382,52 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
375382
}
376383
}
377384

378-
<<<<<<< HEAD
379-
if context.IsSet("hooks-poststart") {
380-
postStartHooks := context.StringSlice("hooks-poststart")
381-
for _, hook := range postStartHooks {
382-
path, args, err := parseHook(hook)
383-
=======
384-
if context.IsSet("poststop-timeout") {
385-
postStopTimeouts := context.StringSlice("poststop-timeout")
385+
if context.IsSet("hooks-poststop-env") {
386+
postStopEnvs := context.StringSlice("hooks-poststop-env")
387+
for _, postStopEnv := range postStopEnvs {
388+
path, env, err := parseHookEnv(postStopEnv)
389+
if err != nil {
390+
return nil
391+
}
392+
g.AddPostStopHookEnv(path, env)
393+
}
394+
}
395+
396+
if context.IsSet("hooks-poststop-timeout") {
397+
postStopTimeouts := context.StringSlice("hooks-poststop-timeout")
386398
for _, postStopTimeout := range postStopTimeouts {
387399
path, timeout, err := parseHookTimeout(postStopTimeout)
388-
>>>>>>> generate: add timeout options for hooks
389400
if err != nil {
390401
return nil
391402
}
392403
g.AddPostStopHookTimeout(path, timeout)
393404
}
394405
}
395406

396-
if context.IsSet("prestart") {
397-
preStartHooks := context.StringSlice("prestart")
407+
if context.IsSet("hooks-prestart") {
408+
preStartHooks := context.StringSlice("hooks-prestart")
398409
for _, hook := range preStartHooks {
399-
path, args := parseHook(hook)
410+
path, args, err := parseHook(hook)
411+
if err != nil {
412+
return nil
413+
}
400414
g.AddPreStartHook(path, args)
401415
}
402416
}
403417

404-
if context.IsSet("prestart-timeout") {
405-
preStartTimeouts := context.StringSlice("prestart-timeout")
418+
if context.IsSet("hooks-prestart-env") {
419+
preStartEnvs := context.StringSlice("hooks-prestart-env")
420+
for _, preStartEnv := range preStartEnvs {
421+
path, env, err := parseHookEnv(preStartEnv)
422+
if err != nil {
423+
return nil
424+
}
425+
g.AddPreStartHookEnv(path, env)
426+
}
427+
}
428+
429+
if context.IsSet("hooks-prestart-timeout") {
430+
preStartTimeouts := context.StringSlice("hooks-prestart-timeout")
406431
for _, preStartTimeout := range preStartTimeouts {
407432
path, timeout, err := parseHookTimeout(preStartTimeout)
408433
if err != nil {
@@ -688,6 +713,17 @@ func parseHook(s string) (string, []string, error) {
688713
return path, args, nil
689714
}
690715

716+
func parseHookEnv(s string) (string, []string, error) {
717+
parts := strings.Split(s, ":")
718+
envs := []string{}
719+
if len(parts) < 2 {
720+
return "", envs, fmt.Errorf("invalid format: %s", s)
721+
}
722+
envs = parts[1:]
723+
724+
return parts[0], envs, nil
725+
}
726+
691727
func parseHookTimeout(s string) (string, int, error) {
692728
parts := strings.Split(s, ":")
693729
if len(parts) != 2 {

completions/bash/oci-runtime-tool

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,14 @@ _oci-runtime-tool_generate() {
308308
--env
309309
--env-file
310310
--hooks-poststart
311+
--hooks-poststart-env
312+
--hooks-poststart-timeout
311313
--hooks-poststop
314+
--hooks-poststop-env
315+
--hooks-poststop-timeout
312316
--hooks-prestart
317+
--hooks-prestart-env
318+
--hooks-prestart-timeout
313319
--hostname
314320
--label
315321
--linux-apparmor
@@ -357,15 +363,6 @@ _oci-runtime-tool_generate() {
357363
--mount-bind
358364
--mount-cgroups
359365
--output
360-
--poststart
361-
--poststart-env
362-
--poststart-timeout
363-
--poststop
364-
--poststop-env
365-
--poststop-timeout
366-
--prestart
367-
--prestart-env
368-
--prestart-timeout
369366
--process-cap-add
370367
--process-cap-drop
371368
--process-consolesize

generate/generate.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,17 @@ func (g *Generator) AddPreStartHook(path string, args []string) {
747747
g.spec.Hooks.Prestart = append(g.spec.Hooks.Prestart, hook)
748748
}
749749

750+
// AddPreStartHookEnv adds envs of a prestart hook into g.spec.Hooks.Prestart.
751+
func (g *Generator) AddPreStartHookEnv(path string, envs []string) {
752+
g.initSpec()
753+
for i, hook := range g.spec.Hooks.Prestart {
754+
if hook.Path == path {
755+
g.spec.Hooks.Prestart[i].Env = envs
756+
return
757+
}
758+
}
759+
}
760+
750761
// AddPreStartHookTimeout adds timeout of a prestart hook into g.spec.Hooks.Prestart.
751762
func (g *Generator) AddPreStartHookTimeout(path string, timeout int) {
752763
g.initSpec()
@@ -776,7 +787,18 @@ func (g *Generator) AddPostStopHook(path string, args []string) {
776787
g.spec.Hooks.Poststop = append(g.spec.Hooks.Poststop, hook)
777788
}
778789

779-
// AddPostStopHookTimeout adds timeout of a poststop hook into g.spec.Hooks.Postsop.
790+
// AddPostStopHookEnv adds envs of a poststop hook into g.spec.Hooks.Poststop.
791+
func (g *Generator) AddPostStopHookEnv(path string, envs []string) {
792+
g.initSpec()
793+
for i, hook := range g.spec.Hooks.Poststop {
794+
if hook.Path == path {
795+
g.spec.Hooks.Poststop[i].Env = envs
796+
return
797+
}
798+
}
799+
}
800+
801+
// AddPostStopHookTimeout adds timeout of a poststop hook into g.spec.Hooks.Poststop.
780802
func (g *Generator) AddPostStopHookTimeout(path string, timeout int) {
781803
g.initSpec()
782804
for i, hook := range g.spec.Hooks.Poststop {
@@ -805,6 +827,17 @@ func (g *Generator) AddPostStartHook(path string, args []string) {
805827
g.spec.Hooks.Poststart = append(g.spec.Hooks.Poststart, hook)
806828
}
807829

830+
// AddPostStartHookEnv adds envs of a poststart hook into g.spec.Hooks.Poststart.
831+
func (g *Generator) AddPostStartHookEnv(path string, envs []string) {
832+
g.initSpec()
833+
for i, hook := range g.spec.Hooks.Poststart {
834+
if hook.Path == path {
835+
g.spec.Hooks.Poststart[i].Env = envs
836+
return
837+
}
838+
}
839+
}
840+
808841
// AddPostStartHookTimeout adds timeout of a poststart hook into g.spec.Hooks.Poststart.
809842
func (g *Generator) AddPostStartHookTimeout(path string, timeout int) {
810843
g.initSpec()

man/oci-runtime-tool-generate.1.md

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,40 @@ read the configuration from `config.json`.
4747
gets launched but after the container environment and main process has been
4848
created.
4949

50+
**--hooks-poststart-env**=[]
51+
Set environment variables for commands in poststart hooks, format is CMD:ENV. e.g. --hooks-poststart-env=/bin/test:key=value
52+
This option can be specified multiple times. When same CMD specified over once, the last one make sense.
53+
54+
**--hooks-poststart-timeout**=[]
55+
Set timeout for commands in poststart hooks, format is CMD:TIMEOUT. e.g. --hooks-poststart-timeout=/bin/test:5
56+
This option can be specified multiple times. When same CMD specified over once, the last one make sense.
57+
5058
**--hooks-poststop**=CMD[:ARGS...]
5159
Set command to run in poststop hooks. Can be specified multiple times.
5260
The multiple commands will be run in order after the container process
5361
is stopped.
5462

63+
**--hook-poststop-env**=[]
64+
Set environment variables for commands in poststop hooks, format is CMD:ENV. e.g. --hooks-poststop-env=/bin/test:key=value
65+
This option can be specified multiple times. When same CMD specified over once, the last one make sense.
66+
67+
**--hooks-poststop-timeout**=[]
68+
Set timeout for commands in poststop hooks, format is CMD:TIMEOUT. e.g. --hooks-poststop-timeout=/bin/test:5
69+
This option can be specified multiple times. When same CMD specified over once, the last one make sense.
70+
5571
**--hooks-prestart**=CMD[:ARGS...]
5672
Set command to run in prestart hooks. Can be specified multiple times.
5773
The multiple commands will be run in order after the container process
5874
has been created but before it executes the user-configured code.
5975

76+
**--hooks-prestart-env**=[]
77+
Set environment variables for commands in prestart hooks, format is CMD:ENV. e.g. --hooks-prestart-env=/bin/test:key=value
78+
This option can be specified multiple times. When same CMD specified over once, the last one make sense.
79+
80+
**--hooks-prestart-timeout**=[]
81+
Set timeout for commands in prestart hooks, format is CMD:TIMEOUT. e.g. --hooks-prestart-timeout=/bin/test:5
82+
This option can be specified multiple times. When same CMD specified over once, the last one make sense.
83+
6084
**--label**=[]
6185
Add annotations to the configuration e.g. key=value.
6286
Currently, key containing equals sign is not supported.
@@ -266,46 +290,6 @@ read the configuration from `config.json`.
266290
file at *PATH* (overwriting the existing content if a file already
267291
exists at *PATH*).
268292

269-
**--poststart**=CMD[:ARGS...]
270-
Set command to run in poststart hooks. Can be specified multiple times.
271-
The multiple commands will be run in order before the container process
272-
gets launched but after the container environment and main process has been
273-
created.
274-
275-
**--poststart-env**=[]
276-
Set environment variables for commands in poststart hooks, format is CMD:ENV. e.g. --poststart-env=/bin/test:key=value
277-
This option can be specified multiple times. When same CMD specified over once, the last one make sense.
278-
279-
**--poststart-timeout**=[]
280-
Set timeout for commands in poststart hooks, format is CMD:TIMEOUT. e.g. --poststart-timeout=/bin/test:5
281-
This option can be specified multiple times. When same CMD specified over once, the last one make sense.
282-
283-
**--poststop**=CMD[:ARGS...]
284-
Set command to run in poststop hooks. Can be specified multiple times.
285-
The multiple commands will be run in order after the container process
286-
is stopped.
287-
288-
**--poststop-env**=[]
289-
Set environment variables for commands in poststop hooks, format is CMD:ENV. e.g. --poststop-env=/bin/test:key=value
290-
This option can be specified multiple times. When same CMD specified over once, the last one make sense.
291-
292-
**--poststop-timeout**=[]
293-
Set timeout for commands in poststop hooks, format is CMD:TIMEOUT. e.g. --poststop-timeout=/bin/test:5
294-
This option can be specified multiple times. When same CMD specified over once, the last one make sense.
295-
296-
**--prestart**=CMD[:ARGS...]
297-
Set command to run in prestart hooks. Can be specified multiple times.
298-
The multiple commands will be run in order after the container process
299-
has been created but before it executes the user-configured code.
300-
301-
**--prestart-env**=[]
302-
Set environment variables for commands in poststop hooks, format is CMD:ENV. e.g. --prestart-env=/bin/test:key=value
303-
This option can be specified multiple times. When same CMD specified over once, the last one make sense.
304-
305-
**--prestart-timeout**=[]
306-
Set timeout for commands in prestart hooks, format is CMD:TIMEOUT. e.g. --prestart-timeout=/bin/test:5
307-
This option can be specified multiple times. When same CMD specified over once, the last one make sense.
308-
309293
**--privileged**=true|false
310294
Give extended privileges to this container. The default is *false*.
311295

0 commit comments

Comments
 (0)