-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
212 lines (186 loc) · 4.92 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
// f5-auto-uploader is a service that watches directories and automatically
// updates or creates iFiles for the LTM module of an F5 BigIP instance.
//
// For usage information, please see:
// f5-auto-uploader -h
// f5-auto-uploader -help
//
package main
import (
"errors"
"flag"
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"github.com/e-XpertSolutions/f5-rest-client/f5"
"github.com/e-XpertSolutions/go-secret/secret"
"github.com/howeyc/gopass"
)
const (
major = "1"
minor = "1"
bugfix = "0"
)
// Print usage and exit with status 1.
func usage() {
fmt.Fprintf(os.Stderr, "usage: %s\n", filepath.Base(os.Args[0]))
flag.PrintDefaults()
os.Exit(1)
}
// Print version number and exit with status 0.
func version() {
fmt.Printf("%s - %s.%s.%s\n", filepath.Base(os.Args[0]), major, minor, bugfix)
os.Exit(0)
}
// Hide stderr and stout behind an io.Writer in order to ease testing.
var (
stderr io.Writer = os.Stderr
stdout io.Writer = os.Stdout
)
// Wrap os.Exit call into a variable function in order to ease testing.
var exit = func(status int) {
os.Exit(status)
}
// fatal prints to standard error output (stderr) and then exit the program with
// a status 1. The prefix "fatal:" is prepended to the message. Arguments are
// handled in the manner of fmt.Print.
func fatal(v ...interface{}) {
fmt.Fprintln(stderr, "fatal:", fmt.Sprint(v...))
exit(1)
}
// verbose prints to standard output (stdout) only when the verboseMode is
// enabled. Otherwise it does nothing. The prefix "verbose:" is prepended to the
// message. Arguments are handled in the manner of fmt.Print.
func verbose(v ...interface{}) {
if *verboseMode {
fmt.Fprintln(stdout, "verbose:", fmt.Sprint(v...))
}
}
// info prints to standard output (stdout). The prefix "info:" is prepended to
// the message. Arguments are handled in the manner of fmt.Print.
func info(v ...interface{}) {
fmt.Fprintln(stdout, "info:", fmt.Sprint(v...))
}
// initF5Client initializes a new f5.Client with the provided configuration.
func initF5Client(cfg f5Config) (*f5.Client, error) {
var (
f5Client *f5.Client
err error
)
switch authMethod := cfg.AuthMethod; authMethod {
case "basic":
f5Client, err = f5.NewBasicClient(cfg.URL, cfg.User, cfg.Password)
case "token":
f5Client, err = f5.NewTokenClient(
cfg.URL,
cfg.User,
cfg.Password,
cfg.LoginProviderName,
)
default:
err = errors.New("unsupported auth method \"" + authMethod + "\"")
}
if err != nil {
return nil, err
}
if !cfg.SSLCheck {
f5Client.DisableCertCheck()
}
return f5Client, nil
}
// readUserCredentials reads a pair of username/passphrase from a gosecret
// secret store.
func readUserCredentials(path, passphrase string) (username, password string, err error) {
if _, err = os.Stat(path); err != nil {
return
}
var store *secret.Store
store, err = secret.OpenStore(path, passphrase)
if err != nil {
return
}
var value []byte
value, err = store.Get("username")
if err != nil {
return
}
username = string(value)
value, err = store.Get("password")
if err != nil {
return
}
password = string(value)
return
}
var (
configPath = flag.String("config", "config.toml", "path to configuration file")
verboseMode = flag.Bool("verbose", false, "enable verbose mode")
printVersion = flag.Bool("version", false, "print current version and exit")
)
func main() {
flag.Usage = usage
flag.Parse()
if *printVersion {
version()
}
cfg, err := readConfig(*configPath)
if err != nil {
fatal(err)
}
switch cs := cfg.CredentialStorage; cs {
case "plain":
if cfg.F5.Password == "" {
fmt.Print("Password: ")
pass, err := gopass.GetPasswd()
if err != nil {
fatal("cannot read password: ", err)
}
fmt.Println("")
cfg.F5.Password = string(pass)
}
case "secret":
cfg.F5.User, cfg.F5.Password, err = readUserCredentials(cfg.SecretStorePath, cfg.Passphrase)
if err != nil {
fatal("cannot read username/password from secret store: ", err)
}
default:
fatal(fmt.Sprintf("unsupported credential storage %q", cs))
}
f5Client, err := initF5Client(cfg.F5)
if err != nil {
fatal(err)
}
if !f5Client.IsActive() {
fatal(fmt.Sprintf("big-ip instance %q is not available at the moment", cfg.F5.URL))
} else {
verbose("big-ip instance is available and rest client has been successfully configured")
}
l := newLogger(os.Stderr)
var routines []*watchRoutine
defer func() {
for i, r := range routines {
l.Noticef("stopping routine %d", i)
if err := r.stop(); err != nil {
l.Errorf("cannot stop routine %d: %v", i, err)
}
}
}()
for _, watchCfg := range cfg.Watch {
if err := scanDir(watchCfg.Dir, watchCfg.Exclude, f5Client); err != nil {
l.Errorf("cannot scan directory %q: %v", watchCfg.Dir, err)
return
}
routine, err := watchDir(f5Client, l, watchCfg)
if err != nil {
l.Error(err)
return
}
routines = append(routines, routine)
}
sig := make(chan os.Signal, 2)
signal.Notify(sig, os.Kill, os.Interrupt)
<-sig
info("bye.")
}