-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcluster_service.go
90 lines (75 loc) · 2.24 KB
/
cluster_service.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
package main
import (
"fmt"
"os"
"os/user"
"path"
"strings"
"time"
"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/olivere/elastic"
"github.com/spf13/viper"
)
// NewElasticClient use cluster name / host / localhost.
func NewElasticClient(context *cli.Context) (*elastic.Client, error) {
var options []elastic.ClientOptionFunc
var addr, esAddr string
if context.GlobalString("host") != "" {
addr = context.GlobalString("host")
} else if context.GlobalString("cluster") != "" {
cluster := context.GlobalString("cluster")
addr = viper.GetString("clusters." + cluster)
if addr == "" {
return nil, fmt.Errorf("get error cluster name: %s in cfgFile:(%s)", cluster, viper.ConfigFileUsed())
}
} else {
addr = "http://127.0.0.1:9200"
}
esAddr = checkURLScheme(addr, "http")
if esAddr != "" {
options = append(options, elastic.SetURL(esAddr))
} else {
return nil, fmt.Errorf("Es addr checkURLScheme failed: %s", addr)
}
basicAuth := context.GlobalString("http-auth")
if basicAuth != "" {
auths := strings.Split(basicAuth, ":")
if len(auths) == 2 {
options = append(options, elastic.SetBasicAuth(auths[0], auths[1]))
} else {
logrus.Warnf("Set basic auth failed: %s", auths)
}
}
options = append(options, elastic.SetSniff(false))
usr, err := user.Current()
if err != nil {
fmt.Println(err)
}
// log operation record
now := time.Now()
record := fmt.Sprintf("%s %s %s %s %s %s %s", now, usr.Gid, usr.HomeDir, usr.Name, usr.Uid, usr.Username, os.Args)
logOprationRecord(record)
// Create a client and connect to addr.
return elastic.NewClient(options...)
}
// logOprationRecord
func logOprationRecord(content string) {
// Create a new instance of the logger. You can have any number of instances.
var log = logrus.New()
// The API for setting attributes is a little different than the package level
// exported logger. See Godoc.
log.Out = os.Stdout
filename := viper.GetString("authlog")
if filename == "" {
filename = "elastic-auth.log"
}
filepath := path.Join(GetCurrPath(), filename)
fd, err := os.OpenFile(filepath, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err == nil {
log.Out = fd
} else {
log.Info("Failed to log to file, using default stderr")
}
log.Info(content)
}