This repository has been archived by the owner on May 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
190 lines (159 loc) · 4.55 KB
/
main.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main // import "github.com/mlafeldt/chef-runner"
import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"time"
"github.com/mlafeldt/chef-runner/chef/cookbook"
"github.com/mlafeldt/chef-runner/chef/omnibus"
"github.com/mlafeldt/chef-runner/chef/runlist"
"github.com/mlafeldt/chef-runner/cli"
"github.com/mlafeldt/chef-runner/driver"
"github.com/mlafeldt/chef-runner/driver/kitchen"
"github.com/mlafeldt/chef-runner/driver/local"
"github.com/mlafeldt/chef-runner/driver/ssh"
"github.com/mlafeldt/chef-runner/driver/vagrant"
"github.com/mlafeldt/chef-runner/log"
"github.com/mlafeldt/chef-runner/provisioner"
"github.com/mlafeldt/chef-runner/provisioner/chefsolo"
"github.com/mlafeldt/chef-runner/resolver"
)
const (
// SandboxPath is the path to the local sandbox directory where
// chef-runner stores files that will be uploaded to a machine.
SandboxPath = ".chef-runner/sandbox"
// RootPath is the path on the machine where files from SandboxPath
// will be uploaded to.
RootPath = "/tmp/chef-runner"
)
func abort(v ...interface{}) {
log.Error(v...)
os.Exit(1)
}
func findDriver(flags *cli.Flags) (driver.Driver, error) {
if flags.Host != "" {
return ssh.NewDriver(flags.Host, flags.SSHOptions, flags.RsyncOptions)
}
if flags.Kitchen != "" {
return kitchen.NewDriver(flags.Kitchen, flags.SSHOptions, flags.RsyncOptions)
}
if flags.Local {
return local.NewDriver()
}
return vagrant.NewDriver(flags.Machine, flags.SSHOptions, flags.RsyncOptions)
}
func uploadFiles(drv driver.Driver) error {
log.Info("Uploading local files to machine. This may take a while...")
log.Debugf("Uploading files from %s to %s on machine\n",
SandboxPath, RootPath)
return drv.Upload(RootPath, SandboxPath+"/")
}
func installChef(drv driver.Driver, installer omnibus.Installer) error {
installCmd := installer.Command()
if len(installCmd) == 0 {
log.Info("Skipping installation of Chef")
return nil
}
log.Info("Installing Chef")
return drv.RunCommand(installCmd)
}
func runChef(drv driver.Driver, p provisioner.Provisioner) error {
log.Infof("Running Chef using %s\n", drv)
return drv.RunCommand(p.Command())
}
func main() {
startTime := time.Now()
log.SetLevel(cli.LogLevel())
flags, err := cli.ParseFlags(os.Args[1:])
if err != nil {
abort(err)
}
log.UseColor = flags.Color
if flags.ShowVersion {
fmt.Printf("chef-runner %s %s %s\n", VersionString(),
TargetString(), GoVersionString())
os.Exit(0)
}
log.Infof("Starting chef-runner (%s %s)\n", VersionString(), TargetString())
var attributes string
if flags.JSONFile != "" {
data, err := ioutil.ReadFile(flags.JSONFile)
if err != nil {
abort(err)
}
attributes = string(data)
}
// 1) Run default recipe if no recipes are passed
// 2) Use run list from JSON file if present, overriding 1)
// 3) Use run list from command line if present, overriding 1) and 2)
recipes := flags.Recipes
if len(recipes) == 0 {
// TODO: parse actual JSON data
if strings.Contains(attributes, "run_list") {
log.Infof("Using run list from %s\n", flags.JSONFile)
} else {
recipes = []string{"::default"}
}
}
var runList []string
if len(recipes) > 0 {
cb, err := cookbook.NewCookbook(".")
if err != nil {
abort(err)
}
log.Debugf("Cookbook = %s\n", cb)
if runList, err = runlist.Build(recipes, cb.Name); err != nil {
abort(err)
}
log.Infof("Run list is %s\n", runList)
}
var p provisioner.Provisioner
p = chefsolo.Provisioner{
RunList: runList,
Attributes: attributes,
Format: flags.Format,
LogLevel: flags.LogLevel,
SandboxPath: SandboxPath,
RootPath: RootPath,
Sudo: flags.Sudo,
}
log.Debugf("Provisioner = %+v\n", p)
installer := omnibus.Installer{
ChefVersion: flags.ChefVersion,
SandboxPath: SandboxPath,
RootPath: RootPath,
Sudo: flags.Sudo,
}
log.Debugf("Installer = %+v\n", installer)
log.Info("Preparing local files")
log.Debug("Creating local sandbox in", SandboxPath)
if err := os.MkdirAll(SandboxPath, 0755); err != nil {
abort(err)
}
if err := p.PrepareFiles(); err != nil {
abort(err)
}
cookbookPath := path.Join(SandboxPath, "cookbooks")
if err := resolver.Resolve(flags.Resolver, cookbookPath); err != nil {
abort(err)
}
if err := installer.PrepareFiles(); err != nil {
abort(err)
}
drv, err := findDriver(flags)
if err != nil {
abort(err)
}
if err := uploadFiles(drv); err != nil {
abort(err)
}
if err := installChef(drv, installer); err != nil {
abort(err)
}
if err := runChef(drv, p); err != nil {
abort(err)
}
log.Info("chef-runner finished in", time.Now().Sub(startTime))
}