2 * Copyright (C) 2017-2018 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
27 "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
28 "github.com/Sirupsen/logrus"
29 "github.com/codegangsta/cli"
32 // Config parameters (json format) of /config command
34 // Public APIConfig fields
37 // Private (un-exported fields in REST GET /config route)
38 Options Options `json:"-"`
39 FileConf FileConfig `json:"-"`
40 Log *logrus.Logger `json:"-"`
41 LogVerboseOut io.Writer `json:"-"`
44 // Options set at the command line
52 // Config default values
54 DefaultAPIVersion = "1"
56 DefaultShareDir = "${HOME}/.xds/server/projects"
57 DefaultSTHomeDir = "${HOME}/.xds/server/syncthing-config"
58 DefaultSdkScriptsDir = "${EXEPATH}/sdks"
59 DefaultSdkDbUpdate = "startup"
62 // Init loads the configuration on start-up
63 func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) {
66 dfltShareDir := DefaultShareDir
67 dfltSTHomeDir := DefaultSTHomeDir
68 if resDir, err := common.ResolveEnvVar(DefaultShareDir); err == nil {
71 if resDir, err := common.ResolveEnvVar(DefaultSTHomeDir); err == nil {
72 dfltSTHomeDir = resDir
75 // Retrieve Server ID (or create one the first time)
76 uuid, err := ServerIDGet()
81 // Define default configuration
83 APIConfig: xsapiv1.APIConfig{
85 Version: cliCtx.App.Metadata["version"].(string),
86 APIVersion: DefaultAPIVersion,
87 VersionGitTag: cliCtx.App.Metadata["git-tag"].(string),
88 Builder: xsapiv1.BuilderConfig{},
89 SupportedSharing: map[string]bool{xsapiv1.TypePathMap: true},
93 ConfigFile: cliCtx.GlobalString("config"),
94 LogLevel: cliCtx.GlobalString("log"),
95 LogFile: cliCtx.GlobalString("logfile"),
96 NoFolderConfig: cliCtx.GlobalBool("no-folderconfig"),
99 WebAppDir: "webapp/dist",
100 ShareRootDir: dfltShareDir,
101 SdkScriptsDir: DefaultSdkScriptsDir,
102 SdkDbUpdate: DefaultSdkDbUpdate,
103 HTTPPort: DefaultPort,
104 SThgConf: &SyncThingConf{Home: dfltSTHomeDir},
110 c.Log.Infoln("Server UUID: ", uuid)
112 // config file settings overwrite default config
113 err = readGlobalConfig(&c, c.Options.ConfigFile)
118 // Update location of shared dir if needed
119 if !common.Exists(c.FileConf.ShareRootDir) {
120 if err := os.MkdirAll(c.FileConf.ShareRootDir, 0770); err != nil {
121 return nil, fmt.Errorf("No valid shared directory found: %v", err)
124 c.Log.Infoln("Share root directory: ", c.FileConf.ShareRootDir)
126 // Where Logs are redirected:
127 // default 'stdout' (logfile option default value)
128 // else use file (or filepath) set by --logfile option
129 // that may be overwritten by LogsDir field of config file
130 logF := c.Options.LogFile
131 logD := c.FileConf.LogsDir
132 if logF != "stdout" {
134 lf := filepath.Base(logF)
135 if lf == "" || lf == "." {
136 lf = "xds-server.log"
138 logF = filepath.Join(logD, lf)
140 logD = filepath.Dir(logF)
143 if logD == "" || logD == "." {
144 logD = "/tmp/xds/logs"
146 c.Options.LogFile = logF
147 c.FileConf.LogsDir = logD
149 if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
150 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
151 return nil, fmt.Errorf("Cannot create logs dir: %v", err)
155 c.Log.Infoln("Logs file: ", c.Options.LogFile)
156 c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)