-
Notifications
You must be signed in to change notification settings - Fork 11
/
config.go
140 lines (111 loc) · 3.23 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
package util
import (
"encoding/base64"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
log "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"`
}
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) {
log.Println("Configuration file does not exist")
Config = &configType{} // initialize empty
} else {
conf, err := ioutil.ReadFile("/etc/tensor.conf")
if err != nil {
log.Fatal(errors.New("Could not find configuration!\n\n" + err.Error()))
os.Exit(5)
}
if err := yaml.Unmarshal(conf, &Config); err != nil {
log.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.Port) == 0 {
Config.Salt = "8m86pie1ef8bghbq41ru!de4"
}
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 _, 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 {
log.Fatal(err)
os.Exit(7)
}
}
}
func GenerateSalt() {
salt := securecookie.GenerateRandomKey(32)
fmt.Println("Generated Salt: ", base64.StdEncoding.EncodeToString(salt))
}