aboutsummaryrefslogtreecommitdiffstats
path: root/lib/xdsconfig/config.go
blob: ef94f0ad8848edd0847ea7e2a3663e2afb4c117c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Copyright (C) 2017-2018 "IoT.bzh"
 * Author Sebastien Douheret <sebastien@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package xdsconfig

import (
	"fmt"
	"io"
	"path/filepath"

	"os"

	common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
	"github.com/Sirupsen/logrus"
	uuid "github.com/satori/go.uuid"
	"github.com/urfave/cli"
)

// Config parameters (json format) of /config command
type Config struct {
	AgentUID      string
	Version       string
	APIVersion    string
	VersionGitTag string
	Options       Options
	FileConf      FileConfig
	Log           *logrus.Logger
	LogVerboseOut io.Writer
}

// Options set at the command line
type Options struct {
	ConfigFile string
	LogLevel   string
	LogFile    string
}

// Config default values
const (
	DefaultAPIVersion = "1"
	DefaultLogLevel   = "error"
)

// Init loads the configuration on start-up
func Init(ctx *cli.Context, log *logrus.Logger) (*Config, error) {
	var err error

	defaultWebAppDir := "${EXEPATH}/www"
	defaultSTHomeDir := "${HOME}/.xds/agent/syncthing-config"

	// TODO: allocate uuid only the first time and save+reuse it later
	uuid := uuid.NewV1().String()

	// Define default configuration
	c := Config{
		AgentUID:      uuid,
		Version:       ctx.App.Metadata["version"].(string),
		APIVersion:    DefaultAPIVersion,
		VersionGitTag: ctx.App.Metadata["git-tag"].(string),

		Options: Options{
			ConfigFile: ctx.GlobalString("config"),
			LogLevel:   ctx.GlobalString("log"),
			LogFile:    ctx.GlobalString("logfile"),
		},

		FileConf: FileConfig{
			HTTPPort:  "8800",
			WebAppDir: defaultWebAppDir,
			LogsDir:   "/tmp/logs",
			ServersConf: []XDSServerConf{
				XDSServerConf{
					URL:       "http://localhost:8000",
					ConnRetry: 10,
				},
			},
			SThgConf: &SyncThingConf{
				Home: defaultSTHomeDir,
			},
			ProfileConf: ProfileConfT{
				XDSBinder: XDSBinderConf{
					URL:       "http://localhost:8810",
					ConnRetry: 10,
				},
			},
		},
		Log: log,
	}

	c.Log.Infoln("Agent UUID:     ", uuid)

	// config file settings overwrite default config
	err = readGlobalConfig(&c, c.Options.ConfigFile)
	if err != nil {
		return nil, err
	}

	// Handle where Logs are redirected:
	//  default 'stdout' (logfile option default value)
	//  else use file (or filepath) set by --logfile option
	//  that may be overwritten by LogsDir field of config file
	logF := c.Options.LogFile
	logD := c.FileConf.LogsDir
	if logF != "stdout" {
		if logD != "" {
			lf := filepath.Base(logF)
			if lf == "" || lf == "." {
				lf = "xds-agent.log"
			}
			logF = filepath.Join(logD, lf)
		} else {
			logD = filepath.Dir(logF)
		}
	}
	if logD == "" || logD == "." {
		logD = "/tmp/xds/logs"
	}
	c.Options.LogFile = logF
	c.FileConf.LogsDir = logD

	if c.FileConf.LogsDir != "" && !common.Exists(c.FileConf.LogsDir) {
		if err := os.MkdirAll(c.FileConf.LogsDir, 0770); err != nil {
			return nil, fmt.Errorf("Cannot create logs dir: %v", err)
		}
	}

	c.Log.Infoln("Logs file:      ", c.Options.LogFile)
	c.Log.Infoln("Logs directory: ", c.FileConf.LogsDir)

	return &c, nil
}