-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.go
129 lines (107 loc) · 3.2 KB
/
grid.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
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
. "github.com/dirtman/sitepkg"
)
type RefObject struct {
Ref string `json:"_ref,omitempty"`
}
// Implement the "grid" command.
func commandGrid(invokedAs []string) error {
commands := Commands{
"ref": invokeGetRef,
"restart": invokeRestart,
}
var args []string
var err error
if len(os.Args) < 2 {
commandHelp(invokedAs, commands, "need a subcommand", 1)
}
if strings.HasPrefix(os.Args[1], "-") || strings.EqualFold(os.Args[1], "help") {
commandHelp(invokedAs, commands, "", 0)
}
command := strings.ToLower(os.Args[1])
os.Args[0] += " " + os.Args[1]
os.Args = append(os.Args[:1], os.Args[2:]...)
function, ok := commands[command]
if !ok {
commandHelp(invokedAs, commands, fmt.Sprintf("unrecognized command \"%s\"", command), 1)
}
// Set API-related options:
if err = SetAPIOptions(); err != nil {
Warn("Failure setting API options: %v", err)
os.Exit(1)
}
// Set any other common options:
SetBoolOpt("Debug", "", false, false, "Debug mode.")
// Now that all our options have been specified, configure them, initialize
// the API, and process user input..
if args, err = ConfigureOptions(); err != nil {
return Error("Failure initializing program: %s\n", err)
} else if err = InitAPI(); err != nil {
return Error("Failure initializing API: %s", err)
} else if len(args) != 0 {
return Error("no operations supported")
}
// Run the func for the specified command:
// invokedAs = append(invokedAs, command)
// err = function(invokedAs)
// if err != nil {
// err = Error("Failure running \"%s\": %v", strings.Join(invokedAs, " "), err)
// }
// return err
// Run the func for the specified command:
invokedAs = append(invokedAs, command)
return function(invokedAs)
}
func invokeGetRef(invokedAs []string) error {
var ref string
var err error
if ref, err = gridGetRef(); err != nil {
return Error("Failure getting grid reference: %s", err)
}
Print("Grid Reference ID: %s\n", ref)
return nil
}
func invokeRestart(invokedAs []string) error {
return gridRestartServices(!Quiet)
}
// Get the Grid's reference.
func gridGetRef() (string, error) {
gridRefernce, _ := GetStringOpt("gridReference")
if gridRefernce != "" {
return gridRefernce, nil
}
body, api_err := IBAPIRequest("GET", "/grid", nil)
if api_err != nil {
return "", Error("Failure getting Grid ref: %s", api_err)
}
var refObjects []RefObject
if err := json.Unmarshal(body, &refObjects); err != nil {
return "", Error("failure unmarshing body (%s): %s", string(body), err)
}
return refObjects[0].Ref, nil
}
// Restart grid services if neccessary.
func gridRestartServices(verbose bool) error {
gridref, err := gridGetRef()
if err != nil {
return Error("failure getting grid reference id: %s", err)
}
url := "/" + gridref + "?_function=restartservices"
url += "&restart_option=RESTART_IF_NEEDED"
url += "&service_option=ALL"
url += "&member_order=SEQUENTIALLY"
url += "&sequential_delay=1"
_, api_err := IBAPIRequest("POST", url, nil)
if api_err != nil {
return Error("failure restarting services: %s", api_err)
}
if verbose {
Show("Successfully instructed Infoblox to restart services if needed")
}
return nil
}