-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconfig.go
100 lines (85 loc) · 2.81 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
package config
import (
"encoding/json"
"errors"
"os"
log "github.com/sirupsen/logrus"
"github.com/qa-dev/jsonwire-grid/pool/metrics"
)
// Config - settings of application.
type Config struct {
Logger Logger `json:"logger"`
DB DB `json:"db"`
Grid Grid `json:"grid"`
Statsd *Statsd `json:"statsd,omitempty"`
}
// Grid general settings
type Grid struct {
ClientType string `json:"client_type"`
Port int `json:"port"`
StrategyList []Strategy `json:"strategy_list"`
// duration string format ex. 12m, see time.ParseDuration()
BusyNodeDuration string `json:"busy_node_duration"`
// todo: выпилить и сделать равным дедлайну http запроса
ReservedDuration string `json:"reserved_node_duration"`
FixNodeTimeout string `json:"fix_node_timeout,omitempty"`
}
// Strategy - Describes the algorithm of node selection.
type Strategy struct {
Params json.RawMessage `json:"params"` // ex. docker config, kubernetes config, etc.
Type string `json:"type"`
Limit int `json:"limit"`
NodeList []Node `json:"node_list"`
}
// Node - Describes node properties and capabilities. Applicable only for on-demand strategies.
type Node struct {
Params json.RawMessage `json:"params"` // ex. image_name, etc.
CapabilitiesList []map[string]interface{} `json:"capabilities_list"`
}
// Logger - Configuration of logger.
type Logger struct {
Level string `json:"level"`
}
// DB - Configuration of storage.
type DB struct {
Implementation string `json:"implementation"`
Connection string `json:"connection"`
DbName string `json:"db_name"`
}
// Statsd - Settings of metrics sender.
type Statsd struct {
Host string `json:"host"`
Port int `json:"port"`
Protocol string `json:"protocol"`
Prefix string `json:"prefix"`
Enable bool `json:"enable"`
CapabilitiesList []metrics.CapabilitiesSelector `json:"selectors"`
}
// New - Constructor of config with default values
func New() *Config {
return &Config{Logger: Logger{Level: "debug"},
DB: DB{Implementation: "local"},
Grid: Grid{
ClientType: "selenium",
Port: 4444,
StrategyList: []Strategy{{Type: "persistent"}},
ReservedDuration: "5m",
BusyNodeDuration: "15m",
FixNodeTimeout: "5m",
},
}
}
// LoadFromFile - config loader from json file.
func (c *Config) LoadFromFile(path string) error {
log.Printf(path)
if path == "" {
return errors.New("empty configuration file path")
}
configFile, err := os.Open(path)
if err != nil {
return err
}
defer configFile.Close()
jsonParser := json.NewDecoder(configFile)
return jsonParser.Decode(&c)
}