-
Notifications
You must be signed in to change notification settings - Fork 206
/
install.go
122 lines (100 loc) · 2.6 KB
/
install.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
package kubernetes
import (
"fmt"
"os/exec"
"strings"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
)
type InstallAction struct {
Steps []InstallStep `yaml:"install"`
}
type InstallStep struct {
InstallArguments `yaml:"kubernetes"`
}
type InstallArguments struct {
Step `yaml:",inline"`
Namespace string `yaml:"namespace"`
Manifests []string `yaml:"manifests,omitempty"`
Record *bool `yaml:"record,omitempty"`
Selector string `yaml:"selector,omitempty"`
Validate *bool `yaml:"validate,omitempty"`
Wait *bool `yaml:"wait,omitempty"`
}
func (m *Mixin) Install() error {
payload, err := m.getPayloadData()
if err != nil {
return err
}
var action InstallAction
err = yaml.Unmarshal(payload, &action)
if err != nil {
return err
}
if len(action.Steps) != 1 {
return errors.Errorf("expected a single step, but got %d", len(action.Steps))
}
step := action.Steps[0]
var commands []*exec.Cmd
for _, manifestPath := range step.Manifests {
commandPayload, err := m.buildInstallCommand(step.InstallArguments, manifestPath)
if err != nil {
return err
}
cmd := m.NewCommand("kubectl", commandPayload...)
commands = append(commands, cmd)
}
for _, cmd := range commands {
cmd.Stdout = m.Out
cmd.Stderr = m.Err
err = cmd.Start()
if err != nil {
prettyCmd := fmt.Sprintf("%s %s", cmd.Path, strings.Join(cmd.Args, " "))
return errors.Wrap(err, fmt.Sprintf("couldn't run command %s", prettyCmd))
}
err = cmd.Wait()
if err != nil {
prettyCmd := fmt.Sprintf("%s %s", cmd.Path, strings.Join(cmd.Args, " "))
return errors.Wrap(err, fmt.Sprintf("error running command %s", prettyCmd))
}
}
err = m.handleOutputs(step.Outputs)
return err
}
func (m *Mixin) getInstallStep(payload []byte) (*InstallStep, error) {
var step InstallStep
err := yaml.Unmarshal(payload, &step)
if err != nil {
return nil, err
}
return &step, nil
}
func (m *Mixin) buildInstallCommand(step InstallArguments, manifestPath string) ([]string, error) {
command := []string{"apply", "-f", manifestPath}
if step.Namespace != "" {
command = append(command, "-n", step.Namespace)
}
if step.Record != nil {
recordIt := *step.Record
if recordIt {
command = append(command, "--record=true")
}
}
if step.Selector != "" {
command = append(command, fmt.Sprintf("--selector=%s", step.Selector))
}
if step.Validate != nil {
validateIt := *step.Validate
if !validateIt {
command = append(command, "--validate=false")
}
}
waitForIt := true
if step.Wait != nil {
waitForIt = *step.Wait
}
if waitForIt {
command = append(command, "--wait")
}
return command, nil
}