-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathstep_handle_resolv_conf.go
61 lines (51 loc) · 1.35 KB
/
step_handle_resolv_conf.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
package builder
// taken from here: https://github.com/hashicorp/packer/blob/81522dced0b25084a824e79efda02483b12dc7cd/builder/amazon/chroot/step_chroot_provision.go
import (
"context"
"io"
"os"
"path/filepath"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
)
// stepHandleResolvConf provisions the instance within a chroot.
type stepHandleResolvConf struct {
ChrootKey string
Delete bool
}
func (s *stepHandleResolvConf) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
mountPath := state.Get(s.ChrootKey).(string)
ui := state.Get("ui").(packer.Ui)
const origResolvConf = "/etc/resolv.conf"
destResolvConf := filepath.Join(mountPath, origResolvConf)
if s.Delete {
err := os.Remove(destResolvConf)
if err != nil {
ui.Error(err.Error())
return multistep.ActionHalt
}
} else {
// copy file over:
err := copyFile(destResolvConf, origResolvConf)
if err != nil {
ui.Error(err.Error())
return multistep.ActionHalt
}
}
return multistep.ActionContinue
}
func (s *stepHandleResolvConf) Cleanup(state multistep.StateBag) {}
func copyFile(dst, src string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
return err
}