-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
158 lines (142 loc) · 3.64 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
/*
Copyright (C) 2016 Eric Ziscky
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package main
import (
"bufio"
"errors"
"fmt"
"github.com/BurntSushi/toml"
"io/ioutil"
"log"
"os"
"path"
)
//ZistConfig stores the zistd configuration info
type ZistConfig struct {
Confdir string
Web bool
Protocol string
HTTPPort int
RPCPort int
Token string
}
var appConf ZistConfig
//Job represents the structure of monitored processes
type Job struct {
Name string
Path string
Args string
Workingdir string
Logfile string
Web bool
Restart bool
}
var jobs []Job
//BinaryConf reads the binary config file .conf to find out the INSTALL_DIR and BINARY_DIR
func BinaryConf() error {
_, err := os.Stat(".conf")
if err != nil {
if os.IsNotExist(err) {
fmt.Println("[*] Can't find the config installation path.")
readConfigInput(1)
createBinaryConf(1)
return nil
}
return err
}
f, err1 := os.OpenFile(".conf", os.O_RDONLY, 0777)
if err1 != nil {
return err1
}
defer f.Close()
scanner := bufio.NewScanner(f)
scanner.Scan()
INSTALL_DIR = scanner.Text()
scanner.Scan()
BINARY_DIR = scanner.Text()
return nil
}
//ZistConf Reads zist configuration options
//stored by default in /etc/zist
func ZistConf() error {
_, err := os.Stat(path.Join(INSTALL_DIR, "conf.toml"))
if err != nil {
if os.IsPermission(err) {
log.Println("Run zist with proper permissions")
return err
}
if os.IsNotExist(err) {
log.Println("Can't find", INSTALL_DIR, "/conf.toml run `./zist install`")
return err
}
return err
}
if _, err1 := toml.DecodeFile(path.Join(INSTALL_DIR, "conf.toml"), &appConf); err1 != nil {
log.Println(err1)
return err1
}
if appConf.Token == "" {
return errors.New("[*] Token cant be empty. Run `sudo zistd generate` to create a secure token. Add it to" + INSTALL_DIR + "/conf.toml")
}
if appConf.RPCPort == 0 {
return errors.New("[*] RPC port needed")
}
return nil
}
//ReadConfig reads and parses all config files
func ReadConfig() error {
info, err := os.Stat(appConf.Confdir)
if err != nil {
if os.IsPermission(err) {
log.Println("Run zist with proper permissions")
return err
}
if os.IsNotExist(err) {
log.Println("Can't find", INSTALL_DIR+"/conf.d/ run `./zist install`")
return err
}
return err
}
if !info.IsDir() {
log.Println("Can't find", INSTALL_DIR+"/conf.d/ run `./zist install`")
return err
}
dir, err1 := ioutil.ReadDir(appConf.Confdir)
if err1 != nil {
log.Println(err1)
return err1
}
if len(dir) < 1 {
fmt.Println("Nothing to run...Exiting")
os.Exit(0)
}
for _, f := range dir {
if err := ParseConfig(f); err != nil {
log.Println("Error parsing " + f.Name())
log.Println(err)
continue
}
}
return nil
}
//ParseConfig decodes job toml and adds to local []Job
func ParseConfig(f os.FileInfo) error {
var job Job
if _, err := toml.DecodeFile(path.Join(appConf.Confdir, f.Name()), &job); err != nil {
return err
}
jobs = append(jobs, job)
return nil
}