Add LowCollector & rename Supervisor to Monitoring
[src/xds/xds-agent.git] / lib / xdsconfig / config.go
1 /*
2  * Copyright (C) 2017-2019 "IoT.bzh"
3  * Author Sebastien Douheret <sebastien@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package xdsconfig
19
20 import (
21         "fmt"
22         "io"
23         "path/filepath"
24
25         "os"
26
27         common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git"
28         "github.com/Sirupsen/logrus"
29         uuid "github.com/satori/go.uuid"
30         "github.com/urfave/cli"
31 )
32
33 // Config parameters (json format) of /config command
34 type Config struct {
35         AgentUID      string
36         Version       string
37         APIVersion    string
38         VersionGitTag string
39         Options       Options
40         FileConf      FileConfig
41         Log           *logrus.Logger
42         LogVerboseOut io.Writer
43 }
44
45 // Options set at the command line
46 type Options struct {
47         ConfigFile string
48         LogLevel   string
49         LogFile    string
50 }
51
52 // Config default values
53 const (
54         DefaultAPIVersion = "1"
55         DefaultLogLevel   = "error"
56 )
57
58 // Init loads the configuration on start-up
59 func Init(ctx *cli.Context, log *logrus.Logger) (*Config, error) {
60         var err error
61
62         defaultWebAppDir := "${EXEPATH}/www"
63         defaultSTHomeDir := "${HOME}/.xds/agent/syncthing-config"
64
65         // TODO: allocate uuid only the first time and save+reuse it later
66         uuid := uuid.NewV1().String()
67
68         // Define default configuration
69         c := Config{
70                 AgentUID:      uuid,
71                 Version:       ctx.App.Metadata["version"].(string),
72                 APIVersion:    DefaultAPIVersion,
73                 VersionGitTag: ctx.App.Metadata["git-tag"].(string),
74
75                 Options: Options{
76                         ConfigFile: ctx.GlobalString("config"),
77                         LogLevel:   ctx.GlobalString("log"),
78                         LogFile:    ctx.GlobalString("logfile"),
79                 },
80
81                 FileConf: FileConfig{
82                         HTTPPort:  "8800",
83                         WebAppDir: defaultWebAppDir,
84                         LogsDir:   "/tmp/logs",
85                         ServersConf: []XDSServerConf{
86                                 XDSServerConf{
87                                         URL:       "http://localhost:8000",
88                                         ConnRetry: 10,
89                                 },
90                         },
91                         SThgConf: &SyncThingConf{
92                                 Home: defaultSTHomeDir,
93                         },
94                         ProfileConf: ProfileConfT{
95                                 XDSMonitoring: XDSMonitoringConf{
96                                         URL:       "http://localhost:8810",
97                                         ConnRetry: 10,
98                                 },
99                                 XDSLowCollector: XDSLowCollectorConf{
100                                         URL:       "http://localhost:8820",
101                                         ConnRetry: 10,
102                                 },
103                         },
104                 },
105                 Log: log,
106         }
107
108         c.Log.Infoln("Agent UUID:     ", uuid)
109
110         // config file settings overwrite default config
111         err = readGlobalConfig(&c, c.Options.ConfigFile)
112         if err != nil {
113                 return nil, err
114         }
115
116         // Handle where Logs are redirected:
117         //  default 'stdout' (logfile option default value)
118         //  else use file (or filepath) set by --logfile option
119         //  else use LogsDir field of config file
120         logF := c.Options.LogFile
121         logD := c.FileConf.LogsDir
122         if logF != "stdout" {
123                 if logF != "" {
124                         if common.IsDir(logF) {
125                                 logD = logF
126                                 logF = filepath.Join(logF, "xds-agent.log")
127                         } else {
128                                 logD = filepath.Dir(logF)
129                         }
130                 } else if logD != "" {
131                         lf := filepath.Base(logF)
132                         if lf == "" || lf == "." {
133                                 lf = "xds-agent.log"
134                         }
135                         logF = filepath.Join(logD, lf)
136                 } else {
137                         logD = filepath.Dir(logF)
138                 }
139         }
140         if logD == "" || logD == "." {
141                 logD = "/tmp/xds/logs"
142         }
143         c.Options.LogFile = logF
144         c.FileConf.LogsDir = logD
145
146         if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
147                 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
148                         return nil, fmt.Errorf("Cannot create logs dir: %v", err)
149                 }
150         }
151
152         c.Log.Infoln("Logs file:      ", c.Options.LogFile)
153         c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)
154
155         return &c, nil
156 }