forked from go-debos/debos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_action.go
86 lines (72 loc) · 1.69 KB
/
run_action.go
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
package main
import (
"errors"
"fmt"
"github.com/debos/fakemachine"
"path"
)
type RunAction struct {
BaseAction `yaml:",inline"`
Chroot bool
PostProcess bool
Script string
Command string
}
func (run *RunAction) Verify(context *DebosContext) error {
if run.PostProcess && run.Chroot {
return errors.New("Cannot run postprocessing in the chroot")
}
return nil
}
func (run *RunAction) PreMachine(context *DebosContext, m *fakemachine.Machine,
args *[]string) error {
if run.Script == "" {
return nil
}
run.Script = CleanPathAt(run.Script, context.recipeDir)
if !run.PostProcess {
m.AddVolume(path.Dir(run.Script))
}
return nil
}
func (run *RunAction) doRun(context DebosContext) error {
run.LogStart()
var cmdline []string
var label string
var cmd Command
if run.Chroot {
cmd = NewChrootCommand(context.rootdir, context.Architecture)
} else {
cmd = Command{}
}
if run.Script != "" {
run.Script = CleanPathAt(run.Script, context.recipeDir)
if run.Chroot {
cmd.AddBindMount(path.Dir(run.Script), "/script")
cmdline = []string{fmt.Sprintf("/script/%s", path.Base(run.Script))}
} else {
cmdline = []string{run.Script}
}
label = path.Base(run.Script)
} else {
cmdline = []string{"sh", "-c", run.Command}
label = run.Command
}
if !run.Chroot && !run.PostProcess {
cmd.AddEnvKey("ROOTDIR", context.rootdir)
}
return cmd.Run(label, cmdline...)
}
func (run *RunAction) Run(context *DebosContext) error {
if run.PostProcess {
/* This runs in postprocessing instead */
return nil
}
return run.doRun(*context)
}
func (run *RunAction) PostMachine(context DebosContext) error {
if !run.PostProcess {
return nil
}
return run.doRun(context)
}