ae02577bacb3005527065f9483475951ea63d100
[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         "github.com/iotbzh/xds-server/lib/xsapiv1"
13 )
14
15 // Config parameters (json format) of /config command
16 type Config struct {
17         // Public APIConfig fields
18         xsapiv1.APIConfig
19
20         // Private (un-exported fields in REST GET /config route)
21         Options       Options        `json:"-"`
22         FileConf      FileConfig     `json:"-"`
23         Log           *logrus.Logger `json:"-"`
24         LogVerboseOut io.Writer      `json:"-"`
25 }
26
27 // Options set at the command line
28 type Options struct {
29         ConfigFile     string
30         LogLevel       string
31         LogFile        string
32         NoFolderConfig bool
33 }
34
35 // Config default values
36 const (
37         DefaultAPIVersion = "1"
38         DefaultPort       = "8000"
39         DefaultShareDir   = "${HOME}/.xds-server/projects"
40         DefaultSTHomeDir  = "${HOME}/.xds-server/syncthing-config"
41         DefaultSdkRootDir = "/xdt/sdk"
42 )
43
44 // Init loads the configuration on start-up
45 func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) {
46         var err error
47
48         dfltShareDir := DefaultShareDir
49         dfltSTHomeDir := DefaultSTHomeDir
50         if resDir, err := common.ResolveEnvVar(DefaultShareDir); err == nil {
51                 dfltShareDir = resDir
52         }
53         if resDir, err := common.ResolveEnvVar(DefaultSTHomeDir); err == nil {
54                 dfltSTHomeDir = resDir
55         }
56
57         // Retrieve Server ID (or create one the first time)
58         uuid, err := ServerIDGet()
59         if err != nil {
60                 return nil, err
61         }
62
63         // Define default configuration
64         c := Config{
65                 APIConfig: xsapiv1.APIConfig{
66                         ServerUID:        uuid,
67                         Version:          cliCtx.App.Metadata["version"].(string),
68                         APIVersion:       DefaultAPIVersion,
69                         VersionGitTag:    cliCtx.App.Metadata["git-tag"].(string),
70                         Builder:          xsapiv1.BuilderConfig{},
71                         SupportedSharing: map[string]bool{ /*FIXME USE folder.TypePathMap*/ "PathMap": true},
72                 },
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         c.Log.Infoln("Server UUID:          ", uuid)
92
93         // config file settings overwrite default config
94         err = readGlobalConfig(&c, c.Options.ConfigFile)
95         if err != nil {
96                 return nil, err
97         }
98
99         // Update location of shared dir if needed
100         if !common.Exists(c.FileConf.ShareRootDir) {
101                 if err := os.MkdirAll(c.FileConf.ShareRootDir, 0770); err != nil {
102                         return nil, fmt.Errorf("No valid shared directory found: %v", err)
103                 }
104         }
105         c.Log.Infoln("Share root directory: ", c.FileConf.ShareRootDir)
106
107         // Where Logs are redirected:
108         //  default 'stdout' (logfile option default value)
109         //  else use file (or filepath) set by --logfile option
110         //  that may be overwritten by LogsDir field of config file
111         logF := c.Options.LogFile
112         logD := c.FileConf.LogsDir
113         if logF != "stdout" {
114                 if logD != "" {
115                         lf := filepath.Base(logF)
116                         if lf == "" || lf == "." {
117                                 lf = "xds-server.log"
118                         }
119                         logF = filepath.Join(logD, lf)
120                 } else {
121                         logD = filepath.Dir(logF)
122                 }
123         }
124         if logD == "" || logD == "." {
125                 logD = "/tmp/xds/logs"
126         }
127         c.Options.LogFile = logF
128         c.FileConf.LogsDir = logD
129
130         if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
131                 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
132                         return nil, fmt.Errorf("Cannot create logs dir: %v", err)
133                 }
134         }
135
136         c.Log.Infoln("Logs file:            ", c.Options.LogFile)
137         c.Log.Infoln("Logs directory:       ", c.FileConf.LogsDir)
138
139         return &c, nil
140 }