-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.go
206 lines (177 loc) · 5.95 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
package main
import (
"fmt"
"net/http"
"os"
"github.com/jmcvetta/napping"
"github.com/pr8kerl/f5er/f5"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
appliance *f5.Device
f5Host string
username string
passwd string
cfgName = "f5"
f5Input string
f5Pool string
session napping.Session
transport *http.Transport
client *http.Client
debug bool
token bool
now bool
statsPathPrefix string
statsShowZeroValues bool
version = "master"
commit = "unstable"
)
func initialiseConfig() {
viper.SetConfigName(cfgName)
viper.AddConfigPath("$HOME/.f5")
viper.AddConfigPath(".")
viper.SetDefault("username", "admin")
viper.SetDefault("debug", false)
viper.SetDefault("token", false)
viper.SetDefault("force", false)
viper.SetDefault("statsPathPrefix", "f5")
viper.SetDefault("statsShowZeroValues", false)
viper.SetEnvPrefix("f5")
viper.BindEnv("device")
viper.BindEnv("username")
viper.BindEnv("passwd")
viper.BindEnv("debug")
viper.BindEnv("token")
viper.BindPFlag("f5", f5Cmd.PersistentFlags().Lookup("f5"))
viper.BindPFlag("debug", f5Cmd.PersistentFlags().Lookup("debug"))
viper.BindPFlag("input", f5Cmd.PersistentFlags().Lookup("input"))
viper.BindPFlag("pool", onlinePoolMemberCmd.Flags().Lookup("pool"))
viper.BindPFlag("pool", offlinePoolMemberCmd.Flags().Lookup("pool"))
// ignore errors - may be using environment vars or cmdline args
viper.ReadInConfig()
}
func checkFlags(cmd *cobra.Command) {
debug = viper.GetBool("debug")
token = viper.GetBool("token")
now = viper.GetBool("now")
username = viper.GetString("username")
passwd = viper.GetString("passwd")
f5Host = viper.GetString("device")
if f5Host == "" {
// look for the f5 cmdline option
f5Host = viper.GetString("f5")
}
statsPathPrefix = viper.GetString("stats_path_prefix")
statsShowZeroValues = viper.GetBool("stats_show_zero_values")
if username == "" {
fmt.Fprint(os.Stderr, "\nerror: missing username; use config file or F5_USERNAME environment variable\n\n")
os.Exit(1)
}
if passwd == "" {
fmt.Fprint(os.Stderr, "\nerror: missing password; use config file or F5_PASSWD environment variable\n\n")
os.Exit(1)
}
if f5Host == "" {
fmt.Fprint(os.Stderr, "\nerror: missing f5 device hostname; use config file or F5_DEVICE environment variable\n\n")
os.Exit(1)
}
// this has to be done here inside cobraCommand.Execute() inc case cmd line args are passed.
// args are only parsed after cobraCommand.Run() - urgh
appliance = f5.New(f5Host, username, passwd, f5.BASIC_AUTH)
appliance.SetDebug(debug)
appliance.SetTokenAuth(token)
appliance.SetStatsPathPrefix(statsPathPrefix)
appliance.SetStatsShowZeroes(statsShowZeroValues)
}
func checkRequiredFlag(flg string) {
if !viper.IsSet(flg) {
fmt.Fprintf(os.Stdout, "\nerror: missing required option --%s\n\n", flg)
os.Exit(1)
}
}
func init() {
f5Cmd.PersistentFlags().StringVarP(&f5Host, "f5", "f", "", "IP or hostname of F5 to poke")
f5Cmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "debug output")
f5Cmd.PersistentFlags().BoolVarP(&token, "token", "t", false, "use token auth")
f5Cmd.PersistentFlags().StringVarP(&f5Input, "input", "i", "", "input json f5 configuration")
offlinePoolMemberCmd.Flags().StringVarP(&f5Pool, "pool", "p", "", "F5 pool name")
offlinePoolMemberCmd.Flags().BoolVarP(&now, "now", "n", false, "force member offline immediately")
onlinePoolMemberCmd.Flags().StringVarP(&f5Pool, "pool", "p", "", "F5 pool name")
// version
f5Cmd.AddCommand(versionCmd)
// show
f5Cmd.AddCommand(showCmd)
showCmd.AddCommand(showPoolCmd)
showCmd.AddCommand(showPoolMemberCmd)
showCmd.AddCommand(showVirtualCmd)
showCmd.AddCommand(showNodeCmd)
showCmd.AddCommand(showPolicyCmd)
showCmd.AddCommand(showDeviceCmd)
showCmd.AddCommand(showRuleCmd)
showCmd.AddCommand(showProfileCmd)
showCmd.AddCommand(showClientSslCmd)
showCmd.AddCommand(showServerSslCmd)
showCmd.AddCommand(showMonitorHttpCmd)
showCmd.AddCommand(showStackCmd)
showCmd.AddCommand(showCertCmd)
showCmd.AddCommand(showCertsCmd)
// add
f5Cmd.AddCommand(addCmd)
addCmd.AddCommand(addPoolCmd)
addCmd.AddCommand(addPoolMemberCmd)
addCmd.AddCommand(addNodeCmd)
addCmd.AddCommand(addPolicyCmd)
addCmd.AddCommand(addVirtualCmd)
addCmd.AddCommand(addRuleCmd)
addCmd.AddCommand(addClientSslCmd)
addCmd.AddCommand(addServerSslCmd)
addCmd.AddCommand(addMonitorHttpCmd)
addCmd.AddCommand(addStackCmd)
addCmd.AddCommand(addCertCmd)
addCmd.AddCommand(addKeyCmd)
// update
f5Cmd.AddCommand(updateCmd)
updateCmd.AddCommand(updatePoolCmd)
updateCmd.AddCommand(updatePoolMemberCmd)
updateCmd.AddCommand(updateNodeCmd)
updateCmd.AddCommand(updatePolicyCmd)
updateCmd.AddCommand(updateVirtualCmd)
updateCmd.AddCommand(updateRuleCmd)
updateCmd.AddCommand(updateClientSslCmd)
updateCmd.AddCommand(updateServerSslCmd)
updateCmd.AddCommand(updateMonitorHttpCmd)
updateCmd.AddCommand(updateStackCmd)
// delete
f5Cmd.AddCommand(deleteCmd)
deleteCmd.AddCommand(deletePoolCmd)
deleteCmd.AddCommand(deletePoolMemberCmd)
deleteCmd.AddCommand(deleteNodeCmd)
deleteCmd.AddCommand(deletePolicyCmd)
deleteCmd.AddCommand(deleteVirtualCmd)
deleteCmd.AddCommand(deleteRuleCmd)
deleteCmd.AddCommand(deleteClientSslCmd)
deleteCmd.AddCommand(deleteServerSslCmd)
deleteCmd.AddCommand(deleteMonitorHttpCmd)
deleteCmd.AddCommand(deleteStackCmd)
// offline
f5Cmd.AddCommand(offlineCmd)
offlineCmd.AddCommand(offlinePoolMemberCmd)
// online
f5Cmd.AddCommand(onlineCmd)
onlineCmd.AddCommand(onlinePoolMemberCmd)
f5Cmd.AddCommand(statsCmd)
statsCmd.AddCommand(statsPoolCmd)
statsCmd.AddCommand(statsPoolMembersCmd)
statsCmd.AddCommand(statsVirtualCmd)
statsCmd.AddCommand(statsNodeCmd)
statsCmd.AddCommand(statsRuleCmd)
f5Cmd.AddCommand(uploadFileCmd)
f5Cmd.AddCommand(runCmd)
// read config
initialiseConfig()
}
func main() {
// f5Cmd.DebugFlags()
f5Cmd.Execute()
}