forked from chef/chef-load
/
node.go
79 lines (71 loc) · 1.97 KB
/
node.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
package main
import (
"fmt"
"time"
"github.com/go-chef/chef"
)
func startNode(nodeName string, config chefLoadConfig) {
adminClient := getApiClient(config.ClientName, config.ClientKey, config.ChefServerUrl)
adminClient.Clients.Delete(nodeName)
adminClient.Nodes.Delete(nodeName)
createClient(adminClient, nodeName, getPublicKey(config.ClientKey))
nodeClient := getApiClient(nodeName, config.ClientKey, config.ChefServerUrl)
switch config.Runs {
case 0:
for run := 1; true; run++ {
manageChefClientRun(nodeName, config, nodeClient, run)
}
default:
for run := 1; run <= config.Runs; run++ {
manageChefClientRun(nodeName, config, nodeClient, run)
}
}
quit <- 1
}
func manageChefClientRun(nodeName string, config chefLoadConfig, nodeClient chef.Client, run int) {
fmt.Println(nodeName, "Started")
var getCookbooks bool
switch config.DownloadCookbooks {
case "always":
getCookbooks = true
case "first":
if run == 1 {
getCookbooks = true
} else {
getCookbooks = false
}
case "never":
getCookbooks = false
default:
getCookbooks = true
}
chefClientRun(nodeClient, nodeName, config.RunList, getCookbooks, config.ApiGetRequests, config.SleepDuration)
fmt.Println(nodeName, "Finished")
if config.Runs == 0 || (config.Runs > 1 && run < config.Runs) {
fmt.Printf("%v Sleeping %v seconds\n", nodeName, config.Interval)
time.Sleep(time.Duration(config.Interval) * time.Second)
}
}
func createClient(adminClient chef.Client, clientName, publicKey string) {
apiClient := chef.ApiClient{
Name: clientName,
ClientName: clientName,
PublicKey: publicKey,
Admin: false,
Validator: false,
}
data, err := chef.JSONReader(apiClient)
if err != nil {
return
}
req, err := adminClient.NewRequest("POST", "clients", data)
res, err := adminClient.Do(req, nil)
if err != nil {
// can't print res here if it is nil
// fmt.Println(res.StatusCode)
// TODO: need to handle errors better
fmt.Println(err)
return
}
defer res.Body.Close()
}