File: hooks.go

package info (click to toggle)
golang-github-opencontainers-runtime-tools 0.9.0.109.ge931285-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,212 kB
  • sloc: sh: 559; makefile: 90
file content (98 lines) | stat: -rw-r--r-- 2,631 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"time"

	"github.com/google/uuid"
	tap "github.com/mndrix/tap-go"
	rspec "github.com/opencontainers/runtime-spec/specs-go"
	"github.com/opencontainers/runtime-tools/specerror"
	"github.com/opencontainers/runtime-tools/validation/util"
)

func main() {
	t := tap.New()
	t.Header(0)

	var output string
	config := util.LifecycleConfig{
		Actions: util.LifecycleActionCreate | util.LifecycleActionStart | util.LifecycleActionDelete,
		PreCreate: func(r *util.Runtime) error {
			r.SetID(uuid.NewString())
			g, err := util.GetDefaultGenerator()
			if err != nil {
				util.Fatal(err)
			}
			output = filepath.Join(r.BundleDir, g.Config.Root.Path, "output")
			shPath := filepath.Join(r.BundleDir, g.Config.Root.Path, "/bin/sh")
			g.AddPreStartHook(rspec.Hook{
				Path: shPath,
				Args: []string{
					"sh", "-c", fmt.Sprintf("echo 'pre-start1 called' >> %s", output),
				},
			})
			g.AddPreStartHook(rspec.Hook{
				Path: shPath,
				Args: []string{
					"sh", "-c", fmt.Sprintf("echo 'pre-start2 called' >> %s", output),
				},
			})
			g.AddPostStartHook(rspec.Hook{
				Path: shPath,
				Args: []string{
					"sh", "-c", fmt.Sprintf("echo 'post-start1 called' >> %s", output),
				},
			})
			g.AddPostStartHook(rspec.Hook{
				Path: shPath,
				Args: []string{
					"sh", "-c", fmt.Sprintf("echo 'post-start2 called' >> %s", output),
				},
			})
			g.AddPostStopHook(rspec.Hook{
				Path: shPath,
				Args: []string{
					"sh", "-c", fmt.Sprintf("echo 'post-stop1 called' >> %s", output),
				},
			})
			g.AddPostStopHook(rspec.Hook{
				Path: shPath,
				Args: []string{
					"sh", "-c", fmt.Sprintf("echo 'post-stop2 called' >> %s", output),
				},
			})
			g.SetProcessArgs([]string{"true"})
			return r.SetConfig(g)
		},
		PreDelete: func(r *util.Runtime) error {
			util.WaitingForStatus(*r, util.LifecycleStatusStopped, time.Second*10, time.Second)
			return nil
		},
	}

	err := util.RuntimeLifecycleValidate(config)
	outputData, _ := os.ReadFile(output)
	if err == nil && string(outputData) != "pre-start1 called\npre-start2 called\npost-start1\npost-start2\npost-stop1\npost-stop2\n" {
		err = specerror.NewError(specerror.PosixHooksCalledInOrder, fmt.Errorf("Hooks MUST be called in the listed order"), rspec.Version)
		diagnostic := map[string]string{
			"error": err.Error(),
		}
		_ = t.YAML(diagnostic)
	} else {
		diagnostic := map[string]string{
			"error": err.Error(),
		}
		if e, ok := err.(*exec.ExitError); ok {
			if len(e.Stderr) > 0 {
				diagnostic["stderr"] = string(e.Stderr)
			}
		}
		_ = t.YAML(diagnostic)
	}

	t.AutoPlan()
}