Set install dir to /opt/AGL and move conf to $HOME/.xds-server
[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         Version       string        `json:"version"`
17         APIVersion    string        `json:"apiVersion"`
18         VersionGitTag string        `json:"gitTag"`
19         Builder       BuilderConfig `json:"builder"`
20
21         // Private (un-exported fields in REST GET /config route)
22         Options       Options        `json:"-"`
23         FileConf      FileConfig     `json:"-"`
24         Log           *logrus.Logger `json:"-"`
25         LogVerboseOut io.Writer      `json:"-"`
26 }
27
28 // Options set at the command line
29 type Options struct {
30         ConfigFile     string
31         LogLevel       string
32         LogFile        string
33         NoFolderConfig bool
34 }
35
36 // Config default values
37 const (
38         DefaultAPIVersion = "1"
39         DefaultPort       = "8000"
40         DefaultShareDir   = "/mnt/share"
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         // Define default configuration
49         c := Config{
50                 Version:       cliCtx.App.Metadata["version"].(string),
51                 APIVersion:    DefaultAPIVersion,
52                 VersionGitTag: cliCtx.App.Metadata["git-tag"].(string),
53                 Builder:       BuilderConfig{},
54
55                 Options: Options{
56                         ConfigFile:     cliCtx.GlobalString("config"),
57                         LogLevel:       cliCtx.GlobalString("log"),
58                         LogFile:        cliCtx.GlobalString("logfile"),
59                         NoFolderConfig: cliCtx.GlobalBool("no-folderconfig"),
60                 },
61                 FileConf: FileConfig{
62                         WebAppDir:    "webapp/dist",
63                         ShareRootDir: DefaultShareDir,
64                         SdkRootDir:   DefaultSdkRootDir,
65                         HTTPPort:     DefaultPort,
66                         LogsDir:      "",
67                 },
68                 Log: log,
69         }
70
71         // config file settings overwrite default config
72         err = readGlobalConfig(&c, c.Options.ConfigFile)
73         if err != nil {
74                 return nil, err
75         }
76
77         // Update location of shared dir if needed
78         if !common.Exists(c.FileConf.ShareRootDir) {
79                 if err := os.MkdirAll(c.FileConf.ShareRootDir, 0770); err != nil {
80                         return nil, fmt.Errorf("No valid shared directory found: %v", err)
81                 }
82         }
83         c.Log.Infoln("Share root directory: ", c.FileConf.ShareRootDir)
84
85         // Where Logs are redirected:
86         //  default 'stdout' (logfile option default value)
87         //  else use file (or filepath) set by --logfile option
88         //  that may be overwritten by LogsDir field of config file
89         logF := c.Options.LogFile
90         logD := c.FileConf.LogsDir
91         if logF != "stdout" {
92                 if logD != "" {
93                         lf := filepath.Base(logF)
94                         if lf == "" || lf == "." {
95                                 lf = "xds-server.log"
96                         }
97                         logF = filepath.Join(logD, lf)
98                 } else {
99                         logD = filepath.Dir(logF)
100                 }
101         }
102         if logD == "" || logD == "." {
103                 logD = "/tmp/xds/logs"
104         }
105         c.Options.LogFile = logF
106         c.FileConf.LogsDir = logD
107
108         if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
109                 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
110                         return nil, fmt.Errorf("Cannot create logs dir: %v", err)
111                 }
112         }
113         c.Log.Infoln("Logs file:      ", c.Options.LogFile)
114         c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)
115
116         return &c, nil
117 }