Use go module as dependency tool instead of glide
[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"
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                                 XDSBinder: XDSBinderConf{
96                                         URL:       "http://localhost:8810",
97                                         ConnRetry: 10,
98                                 },
99                         },
100                 },
101                 Log: log,
102         }
103
104         c.Log.Infoln("Agent UUID:     ", uuid)
105
106         // config file settings overwrite default config
107         err = readGlobalConfig(&c, c.Options.ConfigFile)
108         if err != nil {
109                 return nil, err
110         }
111
112         // Handle where Logs are redirected:
113         //  default 'stdout' (logfile option default value)
114         //  else use file (or filepath) set by --logfile option
115         //  else use LogsDir field of config file
116         logF := c.Options.LogFile
117         logD := c.FileConf.LogsDir
118         if logF != "stdout" {
119                 if logF != "" {
120                         if common.IsDir(logF) {
121                                 logD = logF
122                                 logF = filepath.Join(logF, "xds-agent.log")
123                         } else {
124                                 logD = filepath.Dir(logF)
125                         }
126                 } else if logD != "" {
127                         lf := filepath.Base(logF)
128                         if lf == "" || lf == "." {
129                                 lf = "xds-agent.log"
130                         }
131                         logF = filepath.Join(logD, lf)
132                 } else {
133                         logD = filepath.Dir(logF)
134                 }
135         }
136         if logD == "" || logD == "." {
137                 logD = "/tmp/xds/logs"
138         }
139         c.Options.LogFile = logF
140         c.FileConf.LogsDir = logD
141
142         if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
143                 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
144                         return nil, fmt.Errorf("Cannot create logs dir: %v", err)
145                 }
146         }
147
148         c.Log.Infoln("Logs file:      ", c.Options.LogFile)
149         c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)
150
151         return &c, nil
152 }