forked from fairfaxmedia/flywheel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
114 lines (96 loc) · 2.43 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
package flywheel
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"time"
)
// Config flywheel config file
type Config struct {
Vhosts map[string]string `json:"vhosts"`
Region string `json:"aws_region"`
Endpoint string `json:"endpoint"`
Instances []string `json:"instances"`
HcInterval Duration `json:"healthcheck-interval"`
IdleTimeout Duration `json:"idle-timeout"`
AutoScaling AutoScalingConfig `json:"autoscaling"`
}
// AutoScalingConfig list of terminate/stop AWS ASG
type AutoScalingConfig struct {
Terminate map[string]int64 `json:"terminate"`
Stop []string `json:"stop"`
}
// Duration helper type to parse duration from json
type Duration time.Duration
// UnmarshalText - unmarshal duration from JSON
func (d *Duration) UnmarshalText(b []byte) error {
v, err := time.ParseDuration(string(b))
if err != nil {
return err
}
*d = Duration(v)
return nil
}
// ReadConfig - read config file from a file
func ReadConfig(filename string) (*Config, error) {
fd, err := os.Open(filename)
if err != nil {
return nil, err
}
defer fd.Close()
cfg := &Config{}
if err = cfg.Parse(fd); err != nil {
return nil, err
}
return cfg, nil
}
// Parse the config file
func (c *Config) Parse(rd io.Reader) error {
in, err := ioutil.ReadAll(rd)
if err != nil {
return err
}
err = json.Unmarshal(in, c)
if err != nil {
return fmt.Errorf("Could not decode json: %v", err)
}
err = c.Validate()
if err != nil {
return fmt.Errorf("Invalid configuration: %v", err)
}
return nil
}
// AwsInstances retrieve a list of AWS instance id as requested by AWK SDK
func (c *Config) AwsInstances() []*string {
awsIds := make([]*string, len(c.Instances))
for i := range c.Instances {
awsIds[i] = &c.Instances[i]
}
return awsIds
}
// EndpointURL get endpoint URL as an URL type
func (c *Config) EndpointURL() (*url.URL, error) {
return url.Parse(c.Endpoint)
}
// Validate config content
func (c *Config) Validate() error {
if len(c.Instances) == 0 && len(c.AutoScaling.Stop) == 0 && len(c.AutoScaling.Terminate) == 0 {
return fmt.Errorf("No instances or asg configured")
}
if len(c.Endpoint) == 0 {
return fmt.Errorf("No endpoint configured")
}
if c.HcInterval <= 0 {
c.HcInterval = Duration(30 * time.Second)
}
if c.IdleTimeout <= 0 {
c.IdleTimeout = Duration(3 * time.Hour)
}
if c.Region == "" {
c.Region = "ap-southeast-2"
}
return nil
}