/
main.go
215 lines (193 loc) · 5.5 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/user"
"path"
"strings"
"sync"
"code.google.com/p/gopass"
"github.com/sudharsh/henchman/henchman"
)
func currentUsername() *user.User {
u, err := user.Current()
if err != nil {
log.Printf("Couldn't get current username: %s. Assuming root" + err.Error())
u, err = user.Lookup("root")
if err != nil {
log.Print(err.Error())
}
return u
}
return u
}
func defaultKeyFile() string {
u := currentUsername()
return path.Join(u.HomeDir, ".ssh", "id_rsa")
}
// Split args from the cli that are of the form,
// "a=x b=y c=z" as a map of form { "a": "b", "b": "y", "c": "z" }
// These plan arguments override the variables that may be defined
// as part of the plan file.
func parseExtraArgs(args string) henchman.TaskVars {
extraArgs := make(henchman.TaskVars)
if args == "" {
return extraArgs
}
for _, a := range strings.Split(args, " ") {
kv := strings.Split(a, "=")
extraArgs[kv[0]] = kv[1]
}
return extraArgs
}
// TODO: Modules
func validateModulesPath() (string, error) {
_modulesDir := os.Getenv("HENCHMAN_MODULES_PATH")
if _modulesDir == "" {
cwd, _ := os.Getwd()
_modulesDir = path.Join(cwd, "modules")
}
modulesDir := flag.String("modules", _modulesDir, "Path to the modules")
_, err := os.Stat(*modulesDir)
return *modulesDir, err
}
func localhost() *henchman.Machine {
tc := make(henchman.TransportConfig)
local, _ := henchman.NewLocal(&tc)
localhost := henchman.Machine{}
localhost.Hostname = "127.0.0.1"
localhost.Transport = local
return &localhost
}
func sshTransport(_tc *henchman.TransportConfig, hostname string) *henchman.SSHTransport {
tc := *_tc
tc["hostname"] = hostname
ssht, err := henchman.NewSSH(&tc)
if err != nil {
log.Fatalf(err.Error())
}
return ssht
}
func main() {
username := flag.String("user", currentUsername().Username, "User to run as")
usePassword := flag.Bool("password", false, "Use password authentication")
keyfile := flag.String("private-keyfile", defaultKeyFile(), "Path to the keyfile")
extraArgs := flag.String("args", "", "Extra arguments for the plan")
hostsFile := flag.String("i", "", "Specify hosts file name")
modulesDir, err := validateModulesPath()
if err != nil {
log.Fatalf("Couldn't stat modules path '%s'\n", modulesDir)
}
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [args] <plan>\n\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
planFile := flag.Arg(0)
if planFile == "" {
flag.Usage()
os.Exit(1)
}
if *username == "" {
fmt.Fprintf(os.Stderr, "Missing username")
os.Exit(1)
}
// We support two SSH authentications methods for now
// password and client key bases. Both are mutually exclusive and password takes
// higher precedence
tc := make(henchman.TransportConfig)
tc["username"] = *username
if *usePassword {
var password string
if password, err = gopass.GetPass("Password:"); err != nil {
log.Fatalf("Couldn't get password: " + err.Error())
os.Exit(1)
}
tc["password"] = password
} else {
tc["keyfile"] = *keyfile
}
planBuf, err := ioutil.ReadFile(planFile)
if err != nil {
log.Fatalf("Error reading plan - %s\n", planFile)
}
var hostsFileBuf []byte = nil
if *hostsFile != "" {
hostsFileBuf, err = ioutil.ReadFile(*hostsFile)
if err != nil {
log.Fatalf("Error reading hosts file - %s\n", *hostsFile)
}
}
var plan *henchman.Plan
parsedArgs := parseExtraArgs(*extraArgs)
plan, err = henchman.NewPlanFromYAML(planBuf, hostsFileBuf, parsedArgs)
if err != nil {
log.Fatalf("Couldn't read the plan: %s", err)
os.Exit(1)
}
local := localhost()
// Execute the same plan concurrently across all the machines.
// Note the tasks themselves in plan are executed sequentially.
wg := new(sync.WaitGroup)
for _, hostname := range plan.Hosts {
machine := henchman.Machine{}
machine.Hostname = hostname
machine.Transport = sshTransport(&tc, hostname)
// initializes a map for "register" values for each host
registerMap := make(map[string]interface{})
//renders all tasks in the plan file
tasks, err := henchman.PrepareTasks(plan.Tasks, plan.Vars, machine)
if err != nil {
fmt.Println(err)
}
// for each host use the task list of the plan and run each task individually
wg.Add(1)
go func(machine *henchman.Machine) {
defer wg.Done()
// makes a temporary tasks to temper with
for ndx := 0; ndx < len(tasks); ndx++ {
var status *henchman.TaskStatus
var err error
task := tasks[ndx]
// if there is a valid include field within task
// update task list
// else
// do standard task run procedure
whenVal, err := task.ProcessWhen(registerMap)
if err != nil {
log.Println("Error at When Eval at task: " + task.Name)
log.Println("Error: " + err.Error())
}
if whenVal == true {
if task.Include != "" {
err = henchman.UpdateTasks(&tasks, task.Vars, ndx, *machine)
if err != nil {
log.Println("Error at Include Eval at task: " + task.Name)
log.Println("Error: " + err.Error())
}
} else {
if tasks[ndx].LocalAction {
log.Printf("Local action detected\n")
status, err = task.Run(local, registerMap)
} else {
status, err = task.Run(machine, registerMap)
}
plan.SaveStatus(&task, status.Status)
if err != nil {
log.Printf("Error when executing task: %s\n", err.Error())
}
if status.Status == "failure" {
log.Printf("Task was unsuccessful: %s\n", task.Id)
break
}
}
}
}
}(&machine)
}
wg.Wait()
plan.PrintReport()
}