Fixed Syncthing folder status events and exec command.
[src/xds/xds-agent.git] / lib / xdsconfig / config.go
1 package xdsconfig
2
3 import (
4         "fmt"
5         "io"
6         "path/filepath"
7
8         "os"
9
10         "github.com/Sirupsen/logrus"
11         "github.com/codegangsta/cli"
12         common "github.com/iotbzh/xds-common/golib"
13         uuid "github.com/satori/go.uuid"
14 )
15
16 // Config parameters (json format) of /config command
17 type Config struct {
18         AgentUID      string
19         Version       string
20         APIVersion    string
21         VersionGitTag string
22         Options       Options
23         FileConf      FileConfig
24         Log           *logrus.Logger
25         LogVerboseOut io.Writer
26 }
27
28 // Options set at the command line
29 type Options struct {
30         ConfigFile string
31         LogLevel   string
32         LogFile    string
33 }
34
35 // Config default values
36 const (
37         DefaultAPIVersion = "1"
38         DefaultLogLevel   = "error"
39 )
40
41 // Init loads the configuration on start-up
42 func Init(ctx *cli.Context, log *logrus.Logger) (*Config, error) {
43         var err error
44
45         defaultWebAppDir := "${EXEPATH}/www"
46         defaultSTHomeDir := "${HOME}/.xds/agent/syncthing-config"
47
48         // TODO: allocate uuid only the first time and save+reuse it later
49         uuid := uuid.NewV1().String()
50
51         // Define default configuration
52         c := Config{
53                 AgentUID:      uuid,
54                 Version:       ctx.App.Metadata["version"].(string),
55                 APIVersion:    DefaultAPIVersion,
56                 VersionGitTag: ctx.App.Metadata["git-tag"].(string),
57
58                 Options: Options{
59                         ConfigFile: ctx.GlobalString("config"),
60                         LogLevel:   ctx.GlobalString("log"),
61                         LogFile:    ctx.GlobalString("logfile"),
62                 },
63
64                 FileConf: FileConfig{
65                         HTTPPort:  "8800",
66                         WebAppDir: defaultWebAppDir,
67                         LogsDir:   "/tmp/logs",
68                         ServersConf: []XDSServerConf{
69                                 XDSServerConf{
70                                         URL:       "http://localhost:8000",
71                                         ConnRetry: 10,
72                                 },
73                         },
74                         SThgConf: &SyncThingConf{
75                                 Home: defaultSTHomeDir,
76                         },
77                 },
78                 Log: log,
79         }
80
81         // config file settings overwrite default config
82         err = readGlobalConfig(&c, c.Options.ConfigFile)
83         if err != nil {
84                 return nil, err
85         }
86
87         // Handle where Logs are redirected:
88         //  default 'stdout' (logfile option default value)
89         //  else use file (or filepath) set by --logfile option
90         //  that may be overwritten by LogsDir field of config file
91         logF := c.Options.LogFile
92         logD := c.FileConf.LogsDir
93         if logF != "stdout" {
94                 if logD != "" {
95                         lf := filepath.Base(logF)
96                         if lf == "" || lf == "." {
97                                 lf = "xds-agent.log"
98                         }
99                         logF = filepath.Join(logD, lf)
100                 } else {
101                         logD = filepath.Dir(logF)
102                 }
103         }
104         if logD == "" || logD == "." {
105                 logD = "/tmp/xds/logs"
106         }
107         c.Options.LogFile = logF
108         c.FileConf.LogsDir = logD
109
110         if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
111                 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
112                         return nil, fmt.Errorf("Cannot create logs dir: %v", err)
113                 }
114         }
115
116         c.Log.Infoln("Agent UUID:     ", uuid)
117         c.Log.Infoln("Logs file:      ", c.Options.LogFile)
118         c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)
119
120         return &c, nil
121 }