-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmitamae_provisioner.go
174 lines (136 loc) · 3.81 KB
/
mitamae_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
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
package main
import (
"bytes"
"errors"
"fmt"
"log"
"os"
"regexp"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/plugin"
"github.com/hashicorp/packer/template/interpolate"
)
type Config struct {
common.PackerConfig `mapstructure:",squash"`
Sudo bool
MitamaeVersion string `mapstructure:"mitamae_version"`
BinDir string `mapstructure:"bin_dir"`
Option string
RecipePath string `mapstructure:"recipe_path"`
WorkingDirectory string `mapstructure:"working_directory"`
ctx interpolate.Context
}
type MitamaeProvisioner struct {
config Config
}
func (mp *MitamaeProvisioner) Prepare(raws ...interface{}) error {
err := config.Decode(&mp.config, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &mp.config.ctx,
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{},
},
}, raws...)
if err != nil {
return err
}
if mp.config.RecipePath == "" {
return errors.New("recipe_path is required")
}
if mp.config.BinDir == "" {
mp.config.BinDir = "/usr/local/bin"
}
if mp.config.MitamaeVersion == "" {
mp.config.MitamaeVersion = "v1.4.5"
}
return nil
}
func (mp *MitamaeProvisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
ui.Say("MItamae provisioning start")
filename, err := mp.getMItamaeFileName(comm)
if err != nil {
return err
}
err = mp.downloadMItamae(ui, comm, filename)
if err != nil {
return err
}
err = mp.execRecipe(ui, comm, filename)
if err != nil {
return err
}
ui.Say("MItamae provisioning end")
return nil
}
func (mp *MitamaeProvisioner) getMItamaeFileName(comm packer.Communicator) (string, error) {
var cmd packer.RemoteCmd
cmd.Command = "uname -s -m | awk '{printf(\"%s-%s\",$2,tolower($1))}'"
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := comm.Start(&cmd); err != nil {
return "", err
}
cmd.Wait()
if stderr.String() != "" {
return "", errors.New(stderr.String())
}
osArch := stdout.String()
os := regexp.MustCompile(`^[^-]+-`).ReplaceAllString(osArch, "")
switch os {
case "linux", "darwin":
return fmt.Sprintf("mitamae-%s", osArch), nil
default:
return "", fmt.Errorf("%s is not support.", osArch)
}
}
func (mp *MitamaeProvisioner) downloadMItamae(ui packer.Ui, comm packer.Communicator, filename string) error {
downloadUrl := fmt.Sprintf("https://github.com/itamae-kitchen/mitamae/releases/download/%s/%s", mp.config.MitamaeVersion, filename)
binPath := fmt.Sprintf("%s/%s", mp.config.BinDir, filename)
log.Printf("Start MItamae Download from %s to %s", downloadUrl, binPath)
var cmd packer.RemoteCmd
cmd.Command = fmt.Sprintf("wget %s -q -O %s && chmod +x %s", downloadUrl, binPath, binPath)
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := comm.Start(&cmd); err != nil {
return err
}
cmd.Wait()
if stderr.String() != "" {
return errors.New(stderr.String())
}
log.Printf("Success MItamae Download")
return nil
}
func (mp *MitamaeProvisioner) execRecipe(ui packer.Ui, comm packer.Communicator, filename string) error {
binPath := fmt.Sprintf("%s/%s", mp.config.BinDir, filename)
var cmd packer.RemoteCmd
command := fmt.Sprintf("%s local %s %s", binPath, mp.config.Option, mp.config.RecipePath)
if mp.config.Sudo {
command = "sudo " + command
}
if mp.config.WorkingDirectory != "" {
command = fmt.Sprintf("cd %s && ", mp.config.WorkingDirectory) + command
}
cmd.Command = command
if err := cmd.StartWithUi(comm, ui); err != nil {
return err
}
if cmd.ExitStatus != 0 {
return fmt.Errorf("MItamae execution failed")
}
return nil
}
func (mp *MitamaeProvisioner) Cancel() {
os.Exit(0)
}
func main() {
server, err := plugin.Server()
if err != nil {
panic(err)
}
server.RegisterProvisioner(new(MitamaeProvisioner))
server.Serve()
}