/
step_guestfs.go
57 lines (47 loc) · 1.52 KB
/
step_guestfs.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package images
import (
"context"
"fmt"
"os/exec"
"path"
"github.com/cilium/little-vm-helper/pkg/logcmd"
"github.com/cilium/little-vm-helper/pkg/step"
)
// VirtCustomizeStep is a step implemented a set of arguments in virt-customize
//
// NB: we can maybe merge multiple VirtCustomizeStep in a single virt-customize invocation.
// The idea here would be that virt-customize performs the actions in its
// arguments sequentially.
//
// NB: we can probably do the same with guestfish as well
type VirtCustomizeStep struct {
*StepConf
Args []string
}
func (s *VirtCustomizeStep) Do(ctx context.Context) (step.Result, error) {
imgFname := path.Join(s.imagesDir, s.imgCnf.Name)
args := []string{"-a", imgFname}
args = append(args, s.Args...)
cmd := exec.CommandContext(ctx, "virt-customize", args...)
err := logcmd.RunAndLogCommand(cmd, s.log)
if err != nil {
s.log.WithField("image", s.imgCnf.Name).WithError(err).Error("error executing command")
return step.Stop, err
}
return step.Continue, nil
}
func (s *VirtCustomizeStep) Cleanup(ctx context.Context) {
}
func (s *VirtCustomizeStep) Merge(step step.Step) error {
vcs, ok := step.(*VirtCustomizeStep)
if !ok {
return fmt.Errorf("type %T cannot be merged to a VirtCustomizeStep", step)
}
if vcs.StepConf != s.StepConf {
return fmt.Errorf("acttions with different step configurations cannnot be merged (%v vs %v)", s.StepConf, vcs.StepConf)
}
s.Args = append(s.Args, vcs.Args...)
return nil
}