-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathconfig.go
148 lines (122 loc) · 3.95 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
package optimus
import (
"crypto/ecdsa"
"fmt"
"time"
"github.com/jinzhu/configor"
"github.com/sonm-io/core/accounts"
"github.com/sonm-io/core/blockchain"
"github.com/sonm-io/core/insonmnia/auth"
"github.com/sonm-io/core/insonmnia/benchmarks"
"github.com/sonm-io/core/insonmnia/logging"
"github.com/sonm-io/core/proto"
"github.com/sonm-io/core/util/debug"
)
type nodeConfig struct {
PrivateKey privateKey `yaml:"ethereum" json:"-"`
Endpoint auth.Addr `yaml:"endpoint"`
}
type OptimizationConfig struct {
Model optimizationMethodFactory `yaml:"model"`
}
type Config struct {
Restrictions *RestrictionsConfig `yaml:"restrictions"`
Blockchain *blockchain.Config `yaml:"blockchain"`
PrivateKey privateKey `yaml:"ethereum" json:"-"`
Logging logging.Config `yaml:"logging"`
Node nodeConfig `yaml:"node"`
Workers map[auth.Addr]*workerConfig `yaml:"workers"`
Benchmarks benchmarks.Config `yaml:"benchmarks"`
Marketplace marketplaceConfig `yaml:"marketplace"`
Debug *debug.Config `yaml:"debug"`
}
func (m *Config) Validate() error {
for _, cfg := range m.Workers {
if err := cfg.Validate(); err != nil {
return err
}
}
return nil
}
func LoadConfig(path string) (*Config, error) {
cfg := &Config{}
if err := configor.Load(cfg, path); err != nil {
return nil, err
}
if err := cfg.Validate(); err != nil {
return nil, err
}
return cfg, nil
}
type simulationConfig struct {
Orders []*sonm.BigInt `yaml:"orders"`
}
type RestrictionsConfig struct {
Name string `yaml:"cgroup_name" default:"/optimus"`
CPUCount float64 `yaml:"cpu_count" default:"1.0"`
// Upper RSS threshold in megabytes.
//
// Zero value, which is default, means no limit.
MemoryLimit uint64 `yaml:"memory_limit"`
}
type workerConfig struct {
PrivateKey privateKey `yaml:"ethereum" json:"-"`
Epoch time.Duration `yaml:"epoch"`
OrderDuration time.Duration `yaml:"order_duration_threshold" default:"24h"`
DryRun bool `yaml:"dry_run" default:"false"`
Identity sonm.IdentityLevel `yaml:"identity" required:"true"`
PriceThreshold priceThreshold `yaml:"price_threshold" required:"true"`
StaleThreshold time.Duration `yaml:"stale_threshold" default:"5m"`
PreludeTimeout time.Duration `yaml:"prelude_timeout" default:"30s"`
Optimization OptimizationConfig `yaml:"optimization"`
Simulation *simulationConfig `yaml:"simulation"`
PlanPolicy *planPolicy `yaml:"plan_policy" default:"entire_machine"`
VerboseLog bool `yaml:"verbose"`
}
func (m *workerConfig) Validate() error {
if m.OrderDuration < 0 {
return fmt.Errorf("order duration threshold must be non-negative")
}
if m.Optimization.Model.OptimizationMethodFactory == nil {
m.Optimization.Model = optimizationMethodFactory{OptimizationMethodFactory: &defaultOptimizationMethodFactory{}}
}
if m.PlanPolicy == nil {
m.PlanPolicy = &planPolicy{Type: planPolicyEntireMachine}
}
return nil
}
type privateKey ecdsa.PrivateKey
func (m *privateKey) Unwrap() *ecdsa.PrivateKey {
key := ecdsa.PrivateKey(*m)
return &key
}
func (m *privateKey) UnmarshalYAML(unmarshal func(interface{}) error) error {
var cfg accounts.EthConfig
if err := unmarshal(&cfg); err != nil {
return err
}
key, err := cfg.LoadKey()
if err != nil {
return err
}
*m = privateKey(*key)
return nil
}
type marketplaceConfig struct {
PrivateKey privateKey `yaml:"ethereum" json:"-"`
Endpoint auth.Addr `yaml:"endpoint"`
Interval time.Duration `yaml:"interval"`
MinPrice *sonm.Price `yaml:"min_price" default:"0.0001 USD/h"`
}
func typeofInterface(unmarshal func(interface{}) error) (string, error) {
raw := struct {
Type string `yaml:"type"`
}{}
if err := unmarshal(&raw); err != nil {
return "", err
}
if raw.Type == "" {
return "", fmt.Errorf(`"type" field is required`)
}
return raw.Type, nil
}