Migration to AGL gerrit (update go import)
[src/xds/xds-agent.git] / lib / xdsconfig / config.go
1 /*
2  * Copyright (C) 2017-2018 "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/golib"
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                 },
95                 Log: log,
96         }
97
98         c.Log.Infoln("Agent UUID:     ", uuid)
99
100         // config file settings overwrite default config
101         err = readGlobalConfig(&c, c.Options.ConfigFile)
102         if err != nil {
103                 return nil, err
104         }
105
106         // Handle where Logs are redirected:
107         //  default 'stdout' (logfile option default value)
108         //  else use file (or filepath) set by --logfile option
109         //  that may be overwritten by LogsDir field of config file
110         logF := c.Options.LogFile
111         logD := c.FileConf.LogsDir
112         if logF != "stdout" {
113                 if logD != "" {
114                         lf := filepath.Base(logF)
115                         if lf == "" || lf == "." {
116                                 lf = "xds-agent.log"
117                         }
118                         logF = filepath.Join(logD, lf)
119                 } else {
120                         logD = filepath.Dir(logF)
121                 }
122         }
123         if logD == "" || logD == "." {
124                 logD = "/tmp/xds/logs"
125         }
126         c.Options.LogFile = logF
127         c.FileConf.LogsDir = logD
128
129         if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
130                 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
131                         return nil, fmt.Errorf("Cannot create logs dir: %v", err)
132                 }
133         }
134
135         c.Log.Infoln("Logs file:      ", c.Options.LogFile)
136         c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)
137
138         return &c, nil
139 }