This repository has been archived by the owner on Mar 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provisioner.go
107 lines (83 loc) · 2.76 KB
/
provisioner.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
package main
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
)
// template for interpolating commands ala chef provisioner(s)
type ExecuteTemplate struct {
Sudo bool
}
type Config struct {
// print the paths of would-be-truncated files
PrintOnly bool `mapstructure:"print_only"`
// to sudo or not to sudo, that is the question
PreventSudo bool `mapstructure:"prevent_sudo"`
// // paths to exclude from truncation
ExcludePaths []string `mapstructure:"exclude_paths"`
// base `find` command
BaseCommand string `mapstructure:"base_command"`
ctx interpolate.Context
}
type Provisioner struct {
config Config
}
// as required for a Provisioner Packer plugin
func (p *Provisioner) Prepare(raws ...interface{}) error {
//
err := config.Decode(&p.config, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &p.config.ctx,
InterpolateFilter: &interpolate.RenderFilter{},
}, raws...)
if err != nil {
return err
}
return nil
}
// as required for a Provisioner Packer plugin
func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator) error {
ui.Say("Running truncate provisioner...")
var find_command strings.Builder
// if not specified in config, supply the default
if p.config.BaseCommand == "" {
find_command.WriteString("{{if .Sudo}}sudo {{end}} find / -name \"*.log\"")
} else {
find_command.WriteString(string(p.config.BaseCommand))
}
// append ' -a -not -path ' to the command once per excluded path
for _, path := range p.config.ExcludePaths {
if _, err := find_command.WriteString(fmt.Sprintf(" -a -not -path %s ", path)); err != nil {
return fmt.Errorf("Error preparing exclusion paths in shell command: %s", err)
}
}
// print message or append xargs/truncate command
if p.config.PrintOnly {
ui.Message("Printing paths of (rather than truncating) effected files...")
} else {
find_command.WriteString(" | xargs {{if .Sudo}}sudo {{end}} truncate -s 0")
}
p.config.ctx.Data = &ExecuteTemplate{
Sudo: !p.config.PreventSudo,
}
// render the command using interpolation helpers
rendered_command, err := interpolate.Render(find_command.String(), &p.config.ctx)
if err != nil {
return fmt.Errorf("Error preparing interpolated shell command: %s", err)
}
// print the interpolated command
ui.Say(fmt.Sprintf("Running: %s...", rendered_command))
// use the communicator to exec the rendered command
cmd := &packer.RemoteCmd{Command: rendered_command}
if err := cmd.RunWithUi(ctx, comm, ui); err != nil {
return err
}
// return if the rendered command tails
if cmd.ExitStatus() != 0 {
return fmt.Errorf("Non-zero exit status: %d", cmd.ExitStatus())
}
return nil
}