Cleanup and improvements
[src/xds/xds-server.git] / lib / xdsconfig / config.go
1 package xdsconfig
2
3 import (
4         "fmt"
5         "io"
6         "os"
7         "path/filepath"
8
9         "github.com/Sirupsen/logrus"
10         "github.com/codegangsta/cli"
11         common "github.com/iotbzh/xds-common/golib"
12 )
13
14 // Config parameters (json format) of /config command
15 type Config struct {
16         ServerUID        string          `json:"id"`
17         Version          string          `json:"version"`
18         APIVersion       string          `json:"apiVersion"`
19         VersionGitTag    string          `json:"gitTag"`
20         SupportedSharing map[string]bool `json:"supportedSharing"`
21         Builder          BuilderConfig   `json:"builder"`
22
23         // Private (un-exported fields in REST GET /config route)
24         Options       Options        `json:"-"`
25         FileConf      FileConfig     `json:"-"`
26         Log           *logrus.Logger `json:"-"`
27         LogVerboseOut io.Writer      `json:"-"`
28 }
29
30 // Options set at the command line
31 type Options struct {
32         ConfigFile     string
33         LogLevel       string
34         LogFile        string
35         NoFolderConfig bool
36 }
37
38 // Config default values
39 const (
40         DefaultAPIVersion = "1"
41         DefaultPort       = "8000"
42         DefaultShareDir   = "${HOME}/.xds-server/projects"
43         DefaultSTHomeDir  = "${HOME}/.xds-server/syncthing-config"
44         DefaultSdkRootDir = "/xdt/sdk"
45 )
46
47 // Init loads the configuration on start-up
48 func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) {
49         var err error
50
51         dfltShareDir := DefaultShareDir
52         dfltSTHomeDir := DefaultSTHomeDir
53         if resDir, err := common.ResolveEnvVar(DefaultShareDir); err == nil {
54                 dfltShareDir = resDir
55         }
56         if resDir, err := common.ResolveEnvVar(DefaultSTHomeDir); err == nil {
57                 dfltSTHomeDir = resDir
58         }
59
60         uuid, err := ServerIDGet()
61         if err != nil {
62                 return nil, err
63         }
64
65         // Define default configuration
66         c := Config{
67                 ServerUID:        uuid,
68                 Version:          cliCtx.App.Metadata["version"].(string),
69                 APIVersion:       DefaultAPIVersion,
70                 VersionGitTag:    cliCtx.App.Metadata["git-tag"].(string),
71                 Builder:          BuilderConfig{},
72                 SupportedSharing: map[string]bool{ /*FIXME USE folder.TypePathMap*/ "PathMap": true},
73
74                 Options: Options{
75                         ConfigFile:     cliCtx.GlobalString("config"),
76                         LogLevel:       cliCtx.GlobalString("log"),
77                         LogFile:        cliCtx.GlobalString("logfile"),
78                         NoFolderConfig: cliCtx.GlobalBool("no-folderconfig"),
79                 },
80                 FileConf: FileConfig{
81                         WebAppDir:    "webapp/dist",
82                         ShareRootDir: dfltShareDir,
83                         SdkRootDir:   DefaultSdkRootDir,
84                         HTTPPort:     DefaultPort,
85                         SThgConf:     &SyncThingConf{Home: dfltSTHomeDir},
86                         LogsDir:      "",
87                 },
88                 Log: log,
89         }
90
91         // config file settings overwrite default config
92         err = readGlobalConfig(&c, c.Options.ConfigFile)
93         if err != nil {
94                 return nil, err
95         }
96
97         // Update location of shared dir if needed
98         if !common.Exists(c.FileConf.ShareRootDir) {
99                 if err := os.MkdirAll(c.FileConf.ShareRootDir, 0770); err != nil {
100                         return nil, fmt.Errorf("No valid shared directory found: %v", err)
101                 }
102         }
103         c.Log.Infoln("Share root directory: ", c.FileConf.ShareRootDir)
104
105         // Where Logs are redirected:
106         //  default 'stdout' (logfile option default value)
107         //  else use file (or filepath) set by --logfile option
108         //  that may be overwritten by LogsDir field of config file
109         logF := c.Options.LogFile
110         logD := c.FileConf.LogsDir
111         if logF != "stdout" {
112                 if logD != "" {
113                         lf := filepath.Base(logF)
114                         if lf == "" || lf == "." {
115                                 lf = "xds-server.log"
116                         }
117                         logF = filepath.Join(logD, lf)
118                 } else {
119                         logD = filepath.Dir(logF)
120                 }
121         }
122         if logD == "" || logD == "." {
123                 logD = "/tmp/xds/logs"
124         }
125         c.Options.LogFile = logF
126         c.FileConf.LogsDir = logD
127
128         if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
129                 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
130                         return nil, fmt.Errorf("Cannot create logs dir: %v", err)
131                 }
132         }
133         c.Log.Infoln("Logs file:      ", c.Options.LogFile)
134         c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)
135
136         return &c, nil
137 }