-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
212 lines (183 loc) · 5.56 KB
/
config.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
package config
import (
"fmt"
"os"
"sort"
"strings"
"github.com/fsnotify/fsnotify"
"github.com/robfig/cron"
"github.com/spf13/cast"
"github.com/spf13/viper"
"github.com/xops-infra/jms/core/db"
"github.com/xops-infra/multi-cloud-sdk/pkg/model"
"github.com/xops-infra/noop/log"
)
var Conf *Config
func init() {
Conf = &Config{}
}
// Config config
type Config struct {
Profiles []db.CreateProfileRequest `mapstructure:"profiles"` // 云账号配置,用来自动同步云服务器信息
Proxys []db.CreateProxyRequest `mapstructure:"proxies"` // ssh代理
Keys Keys `mapstructure:"keys"` // ssh key pair 不启用数据库时使用
WithVideo WithVideo `mapstructure:"withVideo"` // 视频存储
WithLdap WithLdap `mapstructure:"withLdap"` // 配置ldap
WithSSHCheck WithSSHCheck `mapstructure:"withSSHCheck"` // 配置服务器SSH可连接性告警
WithDB WithPolicy `mapstructure:"withDB"` // 需要进行权限管理则启用该配置,启用后会使用数据库进行权限管理
WithDingtalk WithDingtalk `mapstructure:"withDingtalk"` // 配置钉钉审批流程
}
type Keys []db.AddKeyRequest
// ToMap convert to map with keyID
func (k Keys) ToMap() map[string]db.AddKeyRequest {
m := make(map[string]db.AddKeyRequest)
for _, key := range k {
m[*key.KeyID] = key
}
return m
}
func (k Keys) ToMapWithName() map[string]db.AddKeyRequest {
m := make(map[string]db.AddKeyRequest)
for _, key := range k {
// log.Debugf("key: %v", tea.Prettify(key))
m[*key.IdentityFile] = key
}
return m
}
// ToMap convert to map with privateIp
func ServerListToMap(s []Server) map[string]Server {
m := make(map[string]Server)
for _, server := range s {
m[server.Host] = server
}
return m
}
type WithVideo struct {
Enable bool `mapstructure:"enable"`
Cron string `mapstructure:"cron"` // 定时任务默认 "0 0 3 * * *" 表示每天凌晨 3 点触发
Dir string `mapstructure:"dir"` // 日志目录,默认/opt/jms/audit/
KeepDays int `mapstructure:"keepDays"` // 保留天数,默认 3 个月
}
type WithDingtalk struct {
Enable bool `mapstructure:"enable"`
AppKey string `mapstructure:"appKey"`
AppSecret string `mapstructure:"appSecret"`
ProcessCode string `mapstructure:"processCode"` // 审批流程编码
}
type WithPolicy struct {
Enable bool `mapstructure:"enable"`
DBFile string `mapstructure:"dbFile"`
PG PGConfig `mapstructure:"pg"`
}
type PGConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Database string `mapstructure:"database"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
}
func (c *PGConfig) GetUrl() string {
return fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai",
c.Host,
c.Username,
c.Password,
c.Database,
cast.ToString(c.Port),
)
}
type WithSSHCheck struct {
Enable bool `mapstructure:"enable"`
Alert SSHAlert `mapstructure:"alert"`
IPS []string `mapstructure:"ips"`
}
// 目前只支持钉钉机器人群告警
type SSHAlert struct {
RobotToken string `mapstructure:"robotToken"`
}
type WithLdap struct {
Enable bool `mapstructure:"enable"`
BindUser string `mapstructure:"bindUser"`
BindPassword string `mapstructure:"bindPassword"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
BaseDN string `mapstructure:"baseDN"`
UserSearchFilter string `mapstructure:"userSearchFilter"`
Attributes []string `mapstructure:"attributes"`
}
// load config from file
func LoadYaml(configFile string) {
homedir := os.Getenv("HOME")
if strings.HasPrefix(configFile, "~") {
configFile = strings.Replace(configFile, "~", homedir, 1)
}
viper.SetConfigFile(configFile)
viper.SetConfigType("yml")
if err := viper.ReadInConfig(); err != nil {
panic(err)
}
err := viper.Unmarshal(Conf)
if err != nil {
panic(err)
}
configCheck()
// 使用fsnotify监视配置文件变化
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
err := viper.ReadInConfig()
if err != nil {
log.Errorf("config file changed error: %s\n", err)
} else {
Conf = &Config{}
viper.Unmarshal(Conf)
// TODO: 热加载
log.Debugf("config file %s changed", e.Name)
}
})
}
func configCheck() {
// 校验 corn配置是否正确
if Conf.WithVideo.Enable {
if _, err := cron.Parse(Conf.WithVideo.Cron); err != nil {
panic(fmt.Errorf("cron parse error: %s", err))
}
err := os.MkdirAll(Conf.WithVideo.Dir, 0755)
if err != nil {
panic(err)
}
}
}
type User struct {
Username string `yaml:"username"`
HashPasswd string `yaml:"hashPasswd"`
Admin bool `yaml:"admin"`
PublicKey string `yaml:"publickey"`
}
// Server server
type Server struct {
ID string
Name string
Host string // 默认取私有 IP 第一个
Port int
KeyPairs []*string // key pair name
// Proxy *db.CreateProxyRequest
Profile string
Region string
Tags model.Tags
Status model.InstanceStatus
SSHUsers []SSHUser
}
type Servers []Server
// 按名称排序
func (s Servers) SortByName() {
sort.Slice(s, func(i, j int) bool {
// log.Debugf("%s %s", s[i].Name, s[j].Name)
return s[i].Name < s[j].Name
})
}
// SSHUser ssh user
type SSHUser struct {
SSHUsername string
KeyName string // pem file name, 这里是支持本地读取内容的
Base64Pem string // base64 pem
Password string
}