forked from statping-ng/statping-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
551 lines (514 loc) · 15.2 KB
/
cli.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
package main
import (
"bufio"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/nordcloud/statping-ng/handlers"
"github.com/nordcloud/statping-ng/source"
"github.com/nordcloud/statping-ng/types/configs"
"github.com/nordcloud/statping-ng/types/core"
"github.com/nordcloud/statping-ng/types/services"
"github.com/nordcloud/statping-ng/utils"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
)
var (
importAll *bool
)
func assetsCli() error {
dir := utils.Directory
if err := utils.InitLogs(); err != nil {
return err
}
if err := source.Assets(); err != nil {
return err
}
if err := source.CreateAllAssets(dir); err != nil {
return err
}
return nil
}
func systemctlCli(dir string, uninstall bool, port int64) error {
location := "/etc/systemd/system/statping.service"
if uninstall {
fmt.Println("systemctl stop statping")
if _, _, err := utils.Command("systemctl", "stop", "statping"); err != nil {
log.Errorln(err)
}
fmt.Println("systemctl disable statping")
if _, _, err := utils.Command("systemctl", "disable", "statping"); err != nil {
log.Errorln(err)
}
fmt.Println("Deleting systemctl: ", location)
if err := utils.DeleteFile(location); err != nil {
log.Errorln(err)
}
return nil
}
if ok := utils.FolderExists(dir); !ok {
return errors.New("directory does not exist: " + dir)
}
binPath, err := os.Executable()
if err != nil {
return err
}
config := []byte(`[Unit]
Description=Statping Server
After=network.target
After=systemd-user-sessions.service
After=network-online.target
[Service]
Type=simple
Restart=always
Environment="STATPING_DIR=` + dir + `"
Environment="ALLOW_REPORTS=true"
ExecStart=` + binPath + ` --port=` + utils.ToString(port) + `
WorkingDirectory=` + dir + `
[Install]
WantedBy=multi-user.target"
`)
fmt.Println("Saving systemctl service to: ", location)
fmt.Printf("Using directory %s for Statping data\n", dir)
fmt.Printf("Running on port %d\n", port)
fmt.Printf("\n\n%s\n\n", string(config))
if err := utils.SaveFile(location, config); err != nil {
return err
}
fmt.Println("systemctl daemon-reload")
if _, _, err := utils.Command("systemctl", "daemon-reload"); err != nil {
return err
}
fmt.Println("systemctl enable statping")
if _, _, err := utils.Command("systemctl", "enable", "statping.service"); err != nil {
return err
}
fmt.Println("systemctl start statping")
if _, _, err := utils.Command("systemctl", "start", "statping"); err != nil {
return err
}
fmt.Println("Statping was will auto start on reboots")
fmt.Println("systemctl service: ", location)
return nil
}
func exportCli(args []string) error {
filename := filepath.Join(utils.Directory, time.Now().Format("01-02-2006-1504")+".json")
if len(args) == 1 {
filename = fmt.Sprintf("%s/%s", utils.Directory, args)
}
var data *handlers.ExportData
if err := utils.InitLogs(); err != nil {
return err
}
if err := source.Assets(); err != nil {
return err
}
config, err := configs.LoadConfigs(configFile)
if err != nil {
return err
}
if err = configs.ConnectConfigs(config, false); err != nil {
return err
}
if _, err := services.SelectAllServices(false); err != nil {
return err
}
if data, err = handlers.ExportSettings(); err != nil {
return fmt.Errorf("could not export settings: %v", err.Error())
}
if err = utils.SaveFile(filename, data.JSON()); err != nil {
return fmt.Errorf("could not write file statping-export.json: %v", err.Error())
}
log.Infoln("Statping export file saved to ", filename)
return nil
}
func sassCli() error {
if err := utils.InitLogs(); err != nil {
return err
}
if err := source.Assets(); err != nil {
return err
}
if err := source.CompileSASS(); err != nil {
return err
}
return nil
}
func resetCli() error {
d := utils.Directory
fmt.Println("Statping directory: ", d)
assets := d + "/assets"
if utils.FolderExists(assets) {
fmt.Printf("Deleting %s folder.\n", assets)
if err := utils.DeleteDirectory(assets); err != nil {
return err
}
} else {
fmt.Printf("Assets folder does not exist %s\n", assets)
}
logDir := d + "/logs"
if utils.FolderExists(logDir) {
fmt.Printf("Deleting %s directory.\n", logDir)
if err := utils.DeleteDirectory(logDir); err != nil {
return err
}
} else {
fmt.Printf("Logs folder does not exist %s\n", logDir)
}
c := d + "/config.yml"
if utils.FileExists(c) {
fmt.Printf("Deleting %s file.\n", c)
if err := utils.DeleteFile(c); err != nil {
return err
}
} else {
fmt.Printf("Config file does not exist %s\n", c)
}
dbFile := d + "/statping.db"
if utils.FileExists(dbFile) {
fmt.Printf("Backuping up %s file.\n", dbFile)
if err := utils.RenameDirectory(dbFile, d+"/statping.db.backup"); err != nil {
return err
}
} else {
fmt.Printf("Statping SQL Database file does not exist %s\n", dbFile)
}
fmt.Println("Statping has been reset")
return nil
}
func envCli() error {
fmt.Println("Statping Configuration")
fmt.Printf("Process ID: %d\n", os.Getpid())
fmt.Printf("Running as user id: %d\n", os.Getuid())
fmt.Printf("Running as group id: %d\n", os.Getgid())
fmt.Printf("Statping Directory: %s\n", utils.Directory)
for k, v := range utils.Params.AllSettings() {
fmt.Printf("%s=%v\n", strings.ToUpper(k), v)
}
return nil
}
func onceCli() error {
if err := utils.InitLogs(); err != nil {
return err
}
if err := source.Assets(); err != nil {
return err
}
log.Infoln("Running 1 time and saving to database...")
if err := runOnce(); err != nil {
return err
}
//core.CloseDB()
fmt.Println("Check is complete.")
return nil
}
func importCli(args []string) error {
var err error
var data []byte
if len(args) < 1 {
return errors.New("invalid command arguments")
}
if data, err = ioutil.ReadFile(args[0]); err != nil {
return err
}
var exportData handlers.ExportData
if err = json.Unmarshal(data, &exportData); err != nil {
return err
}
log.Printf("=== %s ===\n", exportData.Core.Name)
if exportData.Config != nil {
log.Printf("Configs: %s\n", exportData.Config.DbConn)
if exportData.Config.DbUser != "" {
log.Printf(" - Host: %s\n", exportData.Config.DbHost)
log.Printf(" - User: %s\n", exportData.Config.DbUser)
}
}
if len(exportData.Services) > 0 {
log.Printf("Services: %d\n", len(exportData.Services))
}
if len(exportData.Checkins) > 0 {
log.Printf("Checkins: %d\n", len(exportData.Checkins))
}
if len(exportData.Groups) > 0 {
log.Printf("Groups: %d\n", len(exportData.Groups))
}
if len(exportData.Messages) > 0 {
log.Printf("Messages: %d\n", len(exportData.Messages))
}
if len(exportData.Incidents) > 0 {
log.Printf("Incidents: %d\n", len(exportData.Incidents))
}
if len(exportData.Users) > 0 {
log.Printf("Users: %d\n", len(exportData.Users))
}
if exportData.Config != nil {
if ask("Create config.yml file from Configs?") {
log.Printf("Database Host: %s\n", exportData.Config.DbHost)
log.Printf("Database Port: %d\n", exportData.Config.DbPort)
log.Printf("Database User: %s\n", exportData.Config.DbUser)
log.Printf("Database Password: %s\n", exportData.Config.DbPass)
if err := exportData.Config.Save(utils.Directory); err != nil {
return err
}
}
}
config, err := configs.LoadConfigs(configFile)
if err != nil {
return err
}
if err = configs.ConnectConfigs(config, false); err != nil {
return err
}
if ask("Create database rows and sample data?") {
if err := config.ResetCore(); err != nil {
return err
}
}
if ask("Import Core settings?") {
c := exportData.Core
if err := c.Update(); err != nil {
log.Errorln(err)
}
}
for _, s := range exportData.Groups {
if ask(fmt.Sprintf("Import Group '%s'?", s.Name)) {
s.Id = 0
if err := s.Create(); err != nil {
log.Errorln(err)
}
}
}
for _, s := range exportData.Services {
if ask(fmt.Sprintf("Import Service '%s'?", s.Name)) {
s.Id = 0
if err := s.Create(); err != nil {
log.Errorln(err)
}
}
}
for _, s := range exportData.Checkins {
if ask(fmt.Sprintf("Import Checkin '%s'?", s.Name)) {
s.Id = 0
if err := s.Create(); err != nil {
log.Errorln(err)
}
}
}
for _, s := range exportData.Messages {
if ask(fmt.Sprintf("Import Message '%s'?", s.Title)) {
s.Id = 0
if err := s.Create(); err != nil {
log.Errorln(err)
}
}
}
for _, s := range exportData.Users {
if ask(fmt.Sprintf("Import User '%s'?", s.Username)) {
s.Id = 0
if err := s.Create(); err != nil {
log.Errorln(err)
}
}
}
log.Infof("Import complete")
return nil
}
func ask(format string) bool {
fmt.Printf(fmt.Sprintf(format + " [y/N]: "))
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
text = strings.Replace(text, "\n", "", -1)
return strings.ToLower(text) == "y"
}
func updateDisplay() error {
gitCurrent, err := checkGithubUpdates()
if err != nil {
return errors.Wrap(err, "Issue connecting to https://github.com/nordcloud/statping-ng")
}
if gitCurrent.TagName == "" {
return nil
}
if len(gitCurrent.TagName) < 2 {
return nil
}
if VERSION != gitCurrent.TagName[1:] {
fmt.Printf("New Update %v Available!\n", gitCurrent.TagName[1:])
fmt.Printf("Update Command:\n")
fmt.Printf("curl -o- -L https://statping.com/install.sh | bash\n\n")
}
return nil
}
// runOnce will initialize the Statping application and check each service 1 time, will not run HTTP server
func runOnce() error {
config, err := configs.LoadConfigs(configFile)
if err != nil {
return errors.Wrap(err, "config.yml file not found")
}
err = configs.ConnectConfigs(config, false)
if err != nil {
return errors.Wrap(err, "issue connecting to database")
}
c, err := core.Select()
if err != nil {
return errors.Wrap(err, "core database was not found or setup")
}
core.App = c
_, err = services.SelectAllServices(true)
if err != nil {
return errors.Wrap(err, "could not select all services")
}
for _, srv := range services.Services() {
srv.CheckService(true)
}
return nil
}
func checkGithubUpdates() (githubResponse, error) {
url := "https://api.github.com/repos/statping-ng/statping-ng/releases/latest"
contents, _, err := utils.HttpRequest(url, "GET", nil, nil, nil, time.Duration(2*time.Second), true, nil)
if err != nil {
return githubResponse{}, err
}
var gitResp githubResponse
err = json.Unmarshal(contents, &gitResp)
return gitResp, err
}
type githubResponse struct {
URL string `json:"url"`
AssetsURL string `json:"assets_url"`
UploadURL string `json:"upload_url"`
HTMLURL string `json:"html_url"`
ID int `json:"id"`
NodeID string `json:"node_id"`
TagName string `json:"tag_name"`
TargetCommitish string `json:"target_commitish"`
Name string `json:"name"`
Draft bool `json:"draft"`
Author gitAuthor `json:"author"`
Prerelease bool `json:"prerelease"`
CreatedAt time.Time `json:"created_at"`
PublishedAt time.Time `json:"published_at"`
Assets []gitAssets `json:"assets"`
TarballURL string `json:"tarball_url"`
ZipballURL string `json:"zipball_url"`
Body string `json:"body"`
}
type gitAuthor struct {
Login string `json:"login"`
ID int `json:"id"`
NodeID string `json:"node_id"`
AvatarURL string `json:"avatar_url"`
GravatarID string `json:"gravatar_id"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
FollowersURL string `json:"followers_url"`
FollowingURL string `json:"following_url"`
GistsURL string `json:"gists_url"`
StarredURL string `json:"starred_url"`
SubscriptionsURL string `json:"subscriptions_url"`
OrganizationsURL string `json:"organizations_url"`
ReposURL string `json:"repos_url"`
EventsURL string `json:"events_url"`
ReceivedEventsURL string `json:"received_events_url"`
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
}
type gitAssets struct {
URL string `json:"url"`
ID int `json:"id"`
NodeID string `json:"node_id"`
Name string `json:"name"`
Label string `json:"label"`
Uploader gitUploader `json:"uploader"`
ContentType string `json:"content_type"`
State string `json:"state"`
Size int `json:"size"`
DownloadCount int `json:"download_count"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
BrowserDownloadURL string `json:"browser_download_url"`
}
type gitUploader struct {
Login string `json:"login"`
ID int `json:"id"`
NodeID string `json:"node_id"`
AvatarURL string `json:"avatar_url"`
GravatarID string `json:"gravatar_id"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
FollowersURL string `json:"followers_url"`
FollowingURL string `json:"following_url"`
GistsURL string `json:"gists_url"`
StarredURL string `json:"starred_url"`
SubscriptionsURL string `json:"subscriptions_url"`
OrganizationsURL string `json:"organizations_url"`
ReposURL string `json:"repos_url"`
EventsURL string `json:"events_url"`
ReceivedEventsURL string `json:"received_events_url"`
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
}
// ExportChartsJs renders the charts for the index page
//type ExportData struct {
// Config *configs.DbConfig `json:"config"`
// Core *core.Core `json:"core"`
// Services []services.Service `json:"services"`
// Messages []*messages.Message `json:"messages"`
// Checkins []*checkins.Checkin `json:"checkins"`
// Users []*users.User `json:"users"`
// Groups []*groups.Group `json:"groups"`
// Notifiers []core.AllNotifiers `json:"notifiers"`
//}
// ExportSettings will export a JSON file containing all of the settings below:
// - Core
// - Notifiers
// - Checkins
// - Users
// - Services
// - Groups
// - Messages
//func ExportSettings() ([]byte, error) {
// c, err := core.Select()
// if err != nil {
// return nil, err
// }
// var srvs []services.Service
// for _, s := range services.AllInOrder() {
// s.Failures = nil
// srvs = append(srvs, s)
// }
//
// cfg, err := configs.LoadConfigs(configFile)
// if err != nil {
// return nil, err
// }
//
// data := ExportData{
// Config: cfg,
// Core: c,
// Notifiers: core.App.Notifications,
// Checkins: checkins.All(),
// Users: users.All(),
// Services: srvs,
// Groups: groups.All(),
// Messages: messages.All(),
// }
// export, err := json.Marshal(data)
// return export, err
//}
// ExportIndexHTML returns the HTML of the index page as a string
//func ExportIndexHTML() []byte {
// source.Assets()
// core.CoreApp.Connect(core.CoreApp., utils.Directory)
// core.SelectAllServices(false)
// core.CoreApp.UseCdn = types.NewNullBool(true)
// for _, srv := range core.Services() {
// core.CheckService(srv, true)
// }
// w := httptest.NewRecorder()
// r := httptest.NewRequest("GET", "/", nil)
// handlers.ExecuteResponse(w, r, "index.gohtml", nil, nil)
// return w.Body.Bytes()
//}