-
Notifications
You must be signed in to change notification settings - Fork 11
/
actions.go
213 lines (179 loc) · 4.88 KB
/
actions.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package images
import (
"fmt"
"path/filepath"
"github.com/cilium/little-vm-helper/pkg/kernels"
"github.com/cilium/little-vm-helper/pkg/step"
)
// ActionOp is the interface that actions operations need to implement.
//
// Note:
// If you create an instance of ActionOp, you need to add it to
// actionOpInstances so that JSON marshaling/unmarshaling works. Please also
// consider adding a test case in actions_json_test.go to ensure that all
// works.
type ActionOp interface {
ActionOpName() string
ToSteps(s *StepConf) ([]step.Step, error)
}
type Action struct {
Comment string
Op ActionOp
}
var actionOpInstances = []ActionOp{
&RunCommand{},
&CopyInCommand{},
&SetHostnameCommand{},
&MkdirCommand{},
&UploadCommand{},
&ChmodCommand{},
&AppendLineCommand{},
&LinkCommand{},
&InstallKernelCommand{},
}
type VirtCustomizeAction struct {
OpName string
getArgs func() []string
}
// RunCommand runs a script in a path specified by a string
type RunCommand struct {
Cmd string
}
func (rc *RunCommand) ActionOpName() string {
return "run-command"
}
func (rc *RunCommand) ToSteps(s *StepConf) ([]step.Step, error) {
return []step.Step{&VirtCustomizeStep{
StepConf: s,
Args: []string{"--run-command", rc.Cmd},
}}, nil
}
// CopyInCommand copies local files in the image (recursively)
type CopyInCommand struct {
LocalPath string
RemoteDir string
}
func (c *CopyInCommand) ActionOpName() string {
return "copy-in"
}
func (c *CopyInCommand) ToSteps(s *StepConf) ([]step.Step, error) {
return []step.Step{&VirtCustomizeStep{
StepConf: s,
Args: []string{"--copy-in", fmt.Sprintf("%s:%s", c.LocalPath, c.RemoteDir)},
}}, nil
}
// SetHostnameCommand sets the hostname
type SetHostnameCommand struct {
Hostname string
}
func (c *SetHostnameCommand) ActionOpName() string {
return "set-hostname"
}
func (c *SetHostnameCommand) ToSteps(s *StepConf) ([]step.Step, error) {
return []step.Step{&VirtCustomizeStep{
StepConf: s,
Args: []string{"--hostname", c.Hostname},
}}, nil
}
// MkdirCommand creates a directory
type MkdirCommand struct {
Dir string
}
func (c *MkdirCommand) ActionOpName() string {
return "mkdir"
}
func (c *MkdirCommand) ToSteps(s *StepConf) ([]step.Step, error) {
return []step.Step{&VirtCustomizeStep{
StepConf: s,
Args: []string{"--mkdir", c.Dir},
}}, nil
}
// UploadCommand copies a file to the vim
type UploadCommand struct {
File string
Dest string
}
func (c *UploadCommand) ActionOpName() string {
return "upload"
}
func (c *UploadCommand) ToSteps(s *StepConf) ([]step.Step, error) {
return []step.Step{&VirtCustomizeStep{
StepConf: s,
Args: []string{"--upload", fmt.Sprintf("%s:%s", c.File, c.Dest)},
}}, nil
}
// ChmodCommand
type ChmodCommand struct {
Permissions string
File string
}
func (c *ChmodCommand) ActionOpName() string {
return "chmod"
}
func (c *ChmodCommand) ToSteps(s *StepConf) ([]step.Step, error) {
return []step.Step{&VirtCustomizeStep{
StepConf: s,
Args: []string{"--chmod", fmt.Sprintf("%s:%s", c.Permissions, c.File)},
}}, nil
}
// AppendLineCommand
type AppendLineCommand struct {
File string
Line string
}
func (c *AppendLineCommand) ActionOpName() string {
return "append-line"
}
func (c *AppendLineCommand) ToSteps(s *StepConf) ([]step.Step, error) {
return []step.Step{&VirtCustomizeStep{
StepConf: s,
Args: []string{"--append-line", fmt.Sprintf("%s:%s", c.File, c.Line)},
}}, nil
}
// LinkCommand
type LinkCommand struct {
Target string
Link string
}
func (c *LinkCommand) ActionOpName() string {
return "link"
}
func (c *LinkCommand) ToSteps(s *StepConf) ([]step.Step, error) {
return []step.Step{&VirtCustomizeStep{
StepConf: s,
Args: []string{"--link", fmt.Sprintf("%s:%s", c.Target, c.Link)},
}}, nil
}
// InstallKernelCommand
type InstallKernelCommand struct {
KernelInstallDir string
}
func (c *InstallKernelCommand) ActionOpName() string {
return "install-kernel"
}
func (c *InstallKernelCommand) ToSteps(s *StepConf) ([]step.Step, error) {
installDir := c.KernelInstallDir
// NB(kkourt): quick hack for having a proper (independent of base
// directory) relative path for install dirs. Should figure out
// something cleaner.
if !filepath.IsAbs(installDir) {
d, err := filepath.Abs(filepath.Join(s.imagesDir, "..", c.KernelInstallDir))
if err == nil {
installDir = d
}
}
kernel, err := kernels.FindKernel(installDir)
if err != nil {
return nil, err
}
kernelPath := filepath.Join("/", kernel)
return []step.Step{
// boot files, configs, etc.
&VirtCustomizeStep{StepConf: s, Args: []string{"--copy-in", fmt.Sprintf("%s/boot:/", installDir)}},
// modules
&VirtCustomizeStep{StepConf: s, Args: []string{"--copy-in", fmt.Sprintf("%s/lib/modules:/lib/", installDir)}},
&VirtCustomizeStep{StepConf: s, Args: []string{"--link", fmt.Sprintf("%s:%s", kernelPath, "/vmlinuz")}},
}, nil
}