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.
28 common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
29 "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
30 "github.com/Sirupsen/logrus"
31 "github.com/codegangsta/cli"
34 // Config parameters (json format) of /config command
36 // Public APIConfig fields
39 // Private (un-exported fields in REST GET /config route)
40 Options Options `json:"-"`
41 FileConf FileConfig `json:"-"`
42 Log *logrus.Logger `json:"-"`
43 LogVerboseOut io.Writer `json:"-"`
46 // Options set at the command line
54 // Config default values
56 DefaultAPIVersion = "1"
58 DefaultShareDir = "projects"
59 DefaultSTHomeDir = "syncthing-config"
60 DefaultSdkScriptsDir = "${EXEPATH}/sdks"
61 DefaultSdkDbUpdate = "startup"
64 // Init loads the configuration on start-up
65 func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) {
68 dfltShareDir := path.Join(ConfigRootDir(), DefaultShareDir)
69 dfltSTHomeDir := path.Join(ConfigRootDir(), DefaultSTHomeDir)
70 if resDir, err := common.ResolveEnvVar(dfltShareDir); err == nil {
73 if resDir, err := common.ResolveEnvVar(dfltSTHomeDir); err == nil {
74 dfltSTHomeDir = resDir
77 // Retrieve Server ID (or create one the first time)
78 uuid, err := ServerIDGet()
83 // Define default configuration
85 APIConfig: xsapiv1.APIConfig{
87 Version: cliCtx.App.Metadata["version"].(string),
88 APIVersion: DefaultAPIVersion,
89 VersionGitTag: cliCtx.App.Metadata["git-tag"].(string),
90 Builder: xsapiv1.BuilderConfig{},
91 SupportedSharing: map[string]bool{xsapiv1.TypePathMap: true},
95 ConfigFile: cliCtx.GlobalString("config"),
96 LogLevel: cliCtx.GlobalString("log"),
97 LogFile: cliCtx.GlobalString("logfile"),
98 NoFolderConfig: cliCtx.GlobalBool("no-folderconfig"),
100 FileConf: FileConfig{
101 WebAppDir: "webapp/dist",
102 ShareRootDir: dfltShareDir,
103 SdkScriptsDir: DefaultSdkScriptsDir,
104 SdkDbUpdate: DefaultSdkDbUpdate,
105 HTTPPort: DefaultPort,
106 SThgConf: &SyncThingConf{Home: dfltSTHomeDir},
112 c.Log.Infoln("Server UUID: ", uuid)
114 // config file settings overwrite default config
115 err = readGlobalConfig(&c, c.Options.ConfigFile)
120 // Update location of shared dir if needed
121 if !common.Exists(c.FileConf.ShareRootDir) {
122 if err := os.MkdirAll(c.FileConf.ShareRootDir, 0770); err != nil {
123 return nil, fmt.Errorf("No valid shared directory found: %v", err)
126 c.Log.Infoln("Share root directory: ", c.FileConf.ShareRootDir)
128 // Where Logs are redirected:
129 // default 'stdout' (logfile option default value)
130 // else use file (or filepath) set by --logfile option
131 // that may be overwritten by LogsDir field of config file
132 logF := c.Options.LogFile
133 logD := c.FileConf.LogsDir
134 if logF != "stdout" {
136 lf := filepath.Base(logF)
137 if lf == "" || lf == "." {
138 lf = "xds-server.log"
140 logF = filepath.Join(logD, lf)
142 logD = filepath.Dir(logF)
145 if logD == "" || logD == "." {
146 logD = "/tmp/xds/logs"
148 c.Options.LogFile = logF
149 c.FileConf.LogsDir = logD
151 if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
152 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
153 return nil, fmt.Errorf("Cannot create logs dir: %v", err)
157 c.Log.Infoln("Logs file: ", c.Options.LogFile)
158 c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)
163 // ConfigRootDir return the root directory where xds server save all config files
164 func ConfigRootDir() string {
166 if usr, err := user.Current(); err == nil {
170 // Default $HOME/.xds/server but may be changed by an env variable
171 if envVar, envDef := os.LookupEnv("XDS_SERVER_ROOT_CFG_DIR"); envDef {
175 return path.Join(root, "/.xds/server")
178 // WorkspaceRootDir return the path on server side where user xds-workspace dir is accessible
179 func WorkspaceRootDir() string {
180 // May be overloaded by an env variable
181 if envVar, envDef := os.LookupEnv("XDS_SERVER_WORKSPACE_DIR"); envDef {
186 if usr, err := user.Current(); err == nil {
190 // Default value $HOME/xds-workspace
191 return path.Join(home, "xds-workspace")