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 DefaultXdsUtilsScriptsDir = "${EXEPATH}/xds-utils"
62 DefaultSdkDbUpdate = "startup"
63 DefaultXdsSrvUpdateTime = "24h"
66 // Init loads the configuration on start-up
67 func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) {
70 dfltShareDir := path.Join(ConfigRootDir(), DefaultShareDir)
71 dfltSTHomeDir := path.Join(ConfigRootDir(), DefaultSTHomeDir)
72 if resDir, err := common.ResolveEnvVar(dfltShareDir); err == nil {
75 if resDir, err := common.ResolveEnvVar(dfltSTHomeDir); err == nil {
76 dfltSTHomeDir = resDir
79 // Retrieve Server ID (or create one the first time)
80 uuid, err := ServerIDGet()
85 // Define default configuration
87 APIConfig: xsapiv1.APIConfig{
89 Version: cliCtx.App.Metadata["version"].(string),
90 APIVersion: DefaultAPIVersion,
91 VersionGitTag: cliCtx.App.Metadata["git-tag"].(string),
92 Builder: xsapiv1.BuilderConfig{},
93 SupportedSharing: map[string]bool{xsapiv1.TypePathMap: true},
97 ConfigFile: cliCtx.GlobalString("config"),
98 LogLevel: cliCtx.GlobalString("log"),
99 LogFile: cliCtx.GlobalString("logfile"),
100 NoFolderConfig: cliCtx.GlobalBool("no-folderconfig"),
102 FileConf: FileConfig{
104 ShareRootDir: dfltShareDir,
105 SdkScriptsDir: DefaultSdkScriptsDir,
106 XdsUtilsScriptsDir: DefaultXdsUtilsScriptsDir,
107 SdkDbUpdate: DefaultSdkDbUpdate,
108 HTTPPort: DefaultPort,
109 SThgConf: &SyncThingConf{Home: dfltSTHomeDir},
111 XdsSrvUpdateTime: DefaultXdsSrvUpdateTime,
116 c.Log.Infoln("Server UUID: ", uuid)
118 // config file settings overwrite default config
119 err = readGlobalConfig(&c, c.Options.ConfigFile)
124 // Update location of shared dir if needed
125 if !common.Exists(c.FileConf.ShareRootDir) {
126 if err := os.MkdirAll(c.FileConf.ShareRootDir, 0770); err != nil {
127 return nil, fmt.Errorf("No valid shared directory found: %v", err)
130 c.Log.Infoln("Share root directory: ", c.FileConf.ShareRootDir)
132 // Where Logs are redirected:
133 // default 'stdout' (logfile option default value)
134 // else use file (or filepath) set by --logfile option
135 // that may be overwritten by LogsDir field of config file
136 logF := c.Options.LogFile
137 logD := c.FileConf.LogsDir
138 if logF != "stdout" {
140 lf := filepath.Base(logF)
141 if lf == "" || lf == "." {
142 lf = "xds-server.log"
144 logF = filepath.Join(logD, lf)
146 logD = filepath.Dir(logF)
149 if logD == "" || logD == "." {
150 logD = "/tmp/xds/logs"
152 c.Options.LogFile = logF
153 c.FileConf.LogsDir = logD
155 if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
156 if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
157 return nil, fmt.Errorf("Cannot create logs dir: %v", err)
161 c.Log.Infoln("Logs file: ", c.Options.LogFile)
162 c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)
167 // ConfigRootDir return the root directory where xds server save all config files
168 func ConfigRootDir() string {
170 if usr, err := user.Current(); err == nil {
174 // Default $HOME/.xds/server but may be changed by an env variable
175 if envVar, envDef := os.LookupEnv("XDS_SERVER_ROOT_CFG_DIR"); envDef {
179 return path.Join(root, "/.xds/server")
182 // WorkspaceRootDir return the path on server side where user xds-workspace dir is accessible
183 func WorkspaceRootDir() string {
184 // May be overloaded by an env variable
185 if envVar, envDef := os.LookupEnv("XDS_SERVER_WORKSPACE_DIR"); envDef {
190 if usr, err := user.Current(); err == nil {
194 // Default value $HOME/xds-workspace
195 return path.Join(home, "xds-workspace")