2 * Copyright (C) 2017-2019 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
33 // Config parameters (json format) of /config command
42 LogVerboseOut io.Writer
45 // Options set at the command line
52 // Config default values
54 DefaultAPIVersion = "1"
55 DefaultLogLevel = "error"
58 // Init loads the configuration on start-up
59 func Init(ctx *cli.Context, log *logrus.Logger) (*Config, error) {
62 defaultWebAppDir := "${EXEPATH}/www"
63 defaultSTHomeDir := "${HOME}/.xds/agent/syncthing-config"
65 // TODO: allocate uuid only the first time and save+reuse it later
66 uuid := uuid.NewV1().String()
68 // Define default configuration
71 Version: ctx.App.Metadata["version"].(string),
72 APIVersion: DefaultAPIVersion,
73 VersionGitTag: ctx.App.Metadata["git-tag"].(string),
76 ConfigFile: ctx.GlobalString("config"),
77 LogLevel: ctx.GlobalString("log"),
78 LogFile: ctx.GlobalString("logfile"),
83 WebAppDir: defaultWebAppDir,
85 ServersConf: []XDSServerConf{
87 URL: "http://localhost:8000",
91 SThgConf: &SyncThingConf{
92 Home: defaultSTHomeDir,
94 ProfileConf: ProfileConfT{
95 XDSMonitoring: XDSMonitoringConf{
96 URL: "http://localhost:8810",
99 XDSLowCollector: XDSLowCollectorConf{
100 URL: "http://localhost:8820",
108 c.Log.Infoln("Agent UUID: ", uuid)
110 // config file settings overwrite default config
111 err = readGlobalConfig(&c, c.Options.ConfigFile)
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" {
124 if common.IsDir(logF) {
126 logF = filepath.Join(logF, "xds-agent.log")
128 logD = filepath.Dir(logF)
130 } else if logD != "" {
131 lf := filepath.Base(logF)
132 if lf == "" || lf == "." {
135 logF = filepath.Join(logD, lf)
137 logD = filepath.Dir(logF)
140 if logD == "" || logD == "." {
141 logD = "/tmp/xds/logs"
143 c.Options.LogFile = logF
144 c.FileConf.LogsDir = logD
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)
152 c.Log.Infoln("Logs file: ", c.Options.LogFile)
153 c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)