-
Notifications
You must be signed in to change notification settings - Fork 11
/
config.go
189 lines (150 loc) · 4.66 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
package util
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"strconv"
"github.com/Sirupsen/logrus"
"github.com/gorilla/securecookie"
"golang.org/x/crypto/bcrypt"
"gopkg.in/yaml.v2"
)
var InteractiveSetup bool
var Secrets bool
type MongoDBConfig struct {
Hosts []string `yaml:"hosts"`
Username string `yaml:"user"`
Password string `yaml:"pass"`
DbName string `yaml:"name"`
ReplicaSet string `yaml:"replica_set"`
}
type RedisConfig struct {
Host string `yaml:"host"`
}
type configType struct {
MongoDB MongoDBConfig `yaml:"mongodb"`
Redis RedisConfig `yaml:"redis"`
// Format `:port_num` eg, :3000
Port string `yaml:"port"`
// Tensor stores projects here
ProjectsHome string `yaml:"projects_home"`
// cookie hashing & encryption
Salt string `yaml:"salt"`
AnsibleJobTimeOut int `yaml:"ansible_job_timeout"`
SyncJobTimeOut int `yaml:"sync_job_timeout"`
TerraformJobTimeOut int `yaml:"terraform_job_timeout"`
JWTTimeout int `yaml:"jwt_timeout"`
JWTRefreshTimeout int `yaml:"jwt_refresh_timeout"`
Debug bool `yaml:"debug"`
}
var Config *configType
func init() {
flag.BoolVar(&InteractiveSetup, "setup", false, "perform interactive setup")
flag.BoolVar(&Secrets, "secrets", false, "generate salt")
var pwd string
flag.StringVar(&pwd, "hash", "", "generate hash of given password")
flag.Parse()
if len(pwd) > 0 {
password, _ := bcrypt.GenerateFromPassword([]byte(pwd), 11)
fmt.Println("Generated password: ", string(password))
os.Exit(0)
}
if Secrets {
GenerateSalt()
os.Exit(0)
}
if _, err := os.Stat("/etc/tensor.conf"); os.IsNotExist(err) {
logrus.Println("Configuration file does not exist")
Config = &configType{} // initialize empty
} else {
conf, err := ioutil.ReadFile("/etc/tensor.conf")
if err != nil {
logrus.Fatal(errors.New("Could not find configuration!\n\n" + err.Error()))
os.Exit(5)
}
if err := yaml.Unmarshal(conf, &Config); err != nil {
logrus.Fatal("Invalid Configuration!\n\n" + err.Error())
os.Exit(6)
}
}
if len(os.Getenv("TENSOR_PORT")) > 0 {
Config.Port = os.Getenv("TENSOR_PORT")
} else if len(Config.Port) == 0 {
Config.Port = ":3000"
}
if len(os.Getenv("PROJECTS_HOME")) > 0 {
Config.ProjectsHome = os.Getenv("PROJECTS_HOME")
} else if len(Config.ProjectsHome) == 0 {
Config.ProjectsHome = "/opt/tensor/projects"
}
if len(os.Getenv("TENSOR_SALT")) > 0 {
Config.Salt = os.Getenv("TENSOR_SALT")
} else if len(Config.Salt) == 0 {
Config.Salt = "8m86pie1ef8bghbq41ru!de4"
}
if len(os.Getenv("TENSOR_ANSIBLE_JOB_TIMEOUT")) > 0 {
time, _ := strconv.Atoi(os.Getenv("TENSOR_ANSIBLE_JOB_TIMEOUT"))
Config.AnsibleJobTimeOut = time
} else if Config.AnsibleJobTimeOut == 0 {
Config.AnsibleJobTimeOut = 3600
}
if len(os.Getenv("TENSOR_TERRAFORM_JOB_TIMEOUT")) > 0 {
time, _ := strconv.Atoi(os.Getenv("TENSOR_TERRAFORM_JOB_TIMEOUT"))
Config.TerraformJobTimeOut = time
} else if Config.TerraformJobTimeOut == 0 {
Config.TerraformJobTimeOut = 3600
}
if len(os.Getenv("TENSOR_SYNC_JOB_TIMEOUT")) > 0 {
time, _ := strconv.Atoi(os.Getenv("TENSOR_SYNC_JOB_TIMEOUT"))
Config.SyncJobTimeOut = time
} else if Config.SyncJobTimeOut == 0 {
Config.SyncJobTimeOut = 3600
}
if len(os.Getenv("TENSOR_JWT_TIMEOUT")) > 0 {
time, _ := strconv.Atoi(os.Getenv("TENSOR_JWT_TIMEOUT"))
Config.JWTTimeout = time
} else if Config.JWTTimeout == 0 {
Config.JWTTimeout = 3600
}
if len(os.Getenv("TENSOR_JWT_REFRESH_TIMEOUT")) > 0 {
time, _ := strconv.Atoi(os.Getenv("TENSOR_JWT_REFRESH_TIMEOUT"))
Config.JWTRefreshTimeout = time
} else if Config.JWTRefreshTimeout == 0 {
Config.JWTRefreshTimeout = 3600
}
if len(os.Getenv("TENSOR_DB_USER")) > 0 {
Config.MongoDB.Username = os.Getenv("TENSOR_DB_USER")
}
if len(os.Getenv("TENSOR_DB_PASSWORD")) > 0 {
Config.MongoDB.Password = os.Getenv("TENSOR_DB_PASSWORD")
}
if len(os.Getenv("TENSOR_DB_NAME")) > 0 {
Config.MongoDB.DbName = os.Getenv("TENSOR_DB_NAME")
}
if len(os.Getenv("TENSOR_DB_REPLICA")) > 0 {
Config.MongoDB.ReplicaSet = os.Getenv("TENSOR_DB_REPLICA")
}
if len(os.Getenv("TENSOR_DB_HOSTS")) > 0 {
Config.MongoDB.Hosts = strings.Split(os.Getenv("TENSOR_DB_HOSTS"), ";")
}
if len(os.Getenv("TENSOR_REDIS_HOST")) > 0 {
Config.Redis.Host = os.Getenv("TENSOR_REDIS_HOST")
}
if os.Getenv("TENSOR_DEBUG") == "true" {
Config.Debug = true
}
if _, err := os.Stat(Config.ProjectsHome); os.IsNotExist(err) {
fmt.Printf(" Running: mkdir -p %v..\n", Config.ProjectsHome)
if err := os.MkdirAll(Config.ProjectsHome, 0755); err != nil {
logrus.Fatal(err)
os.Exit(7)
}
}
}
func GenerateSalt() {
salt := securecookie.GenerateRandomKey(24)
fmt.Println("Generated Salt: ", salt)
}