-
Notifications
You must be signed in to change notification settings - Fork 167
/
root.go
342 lines (288 loc) · 10.2 KB
/
root.go
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Copyright (c) 2019-2021. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package cmd
import (
"context"
"encoding/json"
"fmt"
"github.com/spf13/pflag"
"os"
"strconv"
"strings"
"time"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
"github.com/pydio/cells/v4/common"
"github.com/pydio/cells/v4/common/broker"
"github.com/pydio/cells/v4/common/config"
"github.com/pydio/cells/v4/common/config/migrations"
"github.com/pydio/cells/v4/common/config/revisions"
"github.com/pydio/cells/v4/common/crypto"
"github.com/pydio/cells/v4/common/log"
cw "github.com/pydio/cells/v4/common/log/context-wrapper"
log2 "github.com/pydio/cells/v4/common/proto/log"
"github.com/pydio/cells/v4/common/runtime"
// Implicit available config types
_ "github.com/pydio/cells/v4/common/config/etcd"
_ "github.com/pydio/cells/v4/common/config/service"
_ "github.com/pydio/cells/v4/common/config/vault"
)
var (
ctx context.Context
cancel context.CancelFunc
cellsViper *viper.Viper
infoCommands = []string{"version", "completion", "doc", "help", "--help", "bash", "zsh", os.Args[0]}
)
const (
EnvPrefixOld = "pydio"
EnvPrefixNew = "cells"
)
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: os.Args[0],
Short: "Secure File Sharing for business",
Long: `
DESCRIPTION
Pydio Cells is self-hosted Document Sharing & Collaboration software for organizations that need
advanced sharing without security trade-offs. Cells gives you full control of your document sharing
environment – combining fast performance, huge file transfer sizes, granular security, and advanced
workflow automations in an easy-to-set-up and easy-to-support self-hosted platform.
CONFIGURE
For the very first run, use '` + os.Args[0] + ` configure' to begin the browser-based or command-line based installation wizard.
Services will automatically start at the end of a browser-based installation.
RUN
Run '$ ` + os.Args[0] + ` start' to load all services.
WORKING DIRECTORIES
By default, application data is stored under the standard OS application dir:
- Linux: ${USER_HOME}/.config/pydio/cells
- Darwin: ${USER_HOME}/Library/Application Support/Pydio/cells
- Windows: ${USER_HOME}/ApplicationData/Roaming/Pydio/cells
You can customize the storage locations with the following ENV variables:
- CELLS_WORKING_DIR: replace the whole standard application dir
- CELLS_DATA_DIR: replace the location for storing default datasources (default CELLS_WORKING_DIR/data)
- CELLS_LOG_DIR: replace the location for storing logs (default CELLS_WORKING_DIR/logs)
- CELLS_SERVICES_DIR: replace location for services-specific data (default CELLS_WORKING_DIR/services)
LOGS LEVEL
By default, logs are outputted in console format at the Info level and appended to a CELLS_LOG_DIR/pydio.log file. You can:
- Change the level (debug, info, warn or error) with the --log flag
- Output the logs in json format with --log_json=true
- Prevent logs from being written to a file with --log_to_file=false
For backward compatibility:
- The CELLS_LOGS_LEVEL environment variable still works to define the --log flag (or CELLS_LOG env var)
but is now deprecated and will disappear in version 4.
- The --log=production flag still works and is equivalent to "--log=info --log_json=true --log_to_file=true"
`,
}
func init() {
initEnvPrefixes()
initViperRuntime()
addRootFlags(RootCmd.PersistentFlags())
RootCmd.CompletionOptions.DisableDefaultCmd = true
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
ctx, cancel = context.WithCancel(context.Background())
if err := RootCmd.ExecuteContext(ctx); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// initEnvPrefixes looks up for legacy ENV and replace them
func initEnvPrefixes() {
prefOld := strings.ToUpper(EnvPrefixOld) + "_"
prefNew := strings.ToUpper(EnvPrefixNew) + "_"
for _, pair := range os.Environ() {
if strings.HasPrefix(pair, prefOld) {
parts := strings.Split(pair, "=")
if len(parts) == 2 && parts[1] != "" {
os.Setenv(prefNew+strings.TrimPrefix(parts[0], prefOld), parts[1])
}
} else if strings.HasPrefix(pair, "CELLS_LOGS_LEVEL") {
parts := strings.Split(pair, "=")
if len(parts) == 2 && parts[1] != "" {
os.Setenv("CELLS_LOG", parts[1])
}
}
}
}
func initViperRuntime() {
cellsViper = viper.New()
cellsViper.SetEnvPrefix(EnvPrefixNew)
cellsViper.AutomaticEnv()
var fileSet bool
if flagsFile := os.Getenv("CELLS_FLAGS_FILE"); flagsFile != "" {
if _, e := os.Stat(flagsFile); e == nil {
cellsViper.SetConfigFile(flagsFile)
fileSet = true
}
}
if !fileSet {
cellsViper.AddConfigPath(runtime.ApplicationWorkingDir())
if p, er := os.Getwd(); er == nil {
cellsViper.AddConfigPath(p)
}
cellsViper.SetConfigName("flags")
}
_ = cellsViper.ReadInConfig()
runtime.SetRuntime(cellsViper)
}
func skipCoreInit() bool {
if len(os.Args) == 1 {
return true
}
arg := os.Args[1]
for _, skip := range infoCommands {
if arg == skip {
return true
}
}
return false
}
func initConfig(ctx context.Context, debounceVersions bool) (new bool, keyring crypto.Keyring, er error) {
if skipCoreInit() {
return
}
// Keyring store
keyringStore, err := config.OpenStore(ctx, runtime.KeyringURL())
if err != nil {
return false, nil, fmt.Errorf("could not init keyring store %v", err)
}
// Keyring start and creation of the master password
keyring = crypto.NewConfigKeyring(keyringStore, crypto.WithAutoCreate(true, func(s string) {
fmt.Println(promptui.IconWarn + " [Keyring] " + s)
}))
password, err := keyring.Get(common.ServiceGrpcNamespace_+common.ServiceUserKey, common.KeyringMasterKey)
if err != nil {
return false, nil, fmt.Errorf("could not get master password %v", err)
}
runtime.SetVaultMasterKey(password)
mainConfig, err := config.OpenStore(ctx, runtime.ConfigURL())
if err != nil {
return false, nil, err
}
// Init RevisionsStore if config is config.RevisionsProvider
if revProvider, ok := mainConfig.(config.RevisionsProvider); ok {
var rOpt []config.RevisionsStoreOption
if debounceVersions {
rOpt = append(rOpt, config.WithDebounce(2*time.Second))
}
var versionsStore revisions.Store
mainConfig, versionsStore = revProvider.AsRevisionsStore(rOpt...)
config.RegisterRevisionsStore(versionsStore)
}
// Wrap config with vaultConfig if set
vaultConfig, err := config.OpenStore(ctx, runtime.VaultURL())
if err != nil {
return false, nil, err
}
config.RegisterVault(vaultConfig)
mainConfig = config.NewVault(vaultConfig, mainConfig)
// Additional Proxy
mainConfig = config.Proxy(mainConfig)
// Register default now
config.Register(mainConfig)
if !runtime.IsFork() {
if config.Get("version").String() == "" && config.Get("defaults/database").String() == "" {
new = true
var data interface{}
if err := json.Unmarshal([]byte(config.SampleConfig), &data); err == nil {
if err := config.Get().Set(data); err == nil {
config.Save(common.PydioSystemUsername, "Initialize with sample config")
}
}
}
// Need to do something for the versions
if save, err := migrations.UpgradeConfigsIfRequired(config.Get(), common.Version()); err == nil && save {
if err := config.Save(common.PydioSystemUsername, "Configs upgrades applied"); err != nil {
return false, nil, fmt.Errorf("could not save config migrations %v", err)
}
}
}
return
}
func initLogLevel() {
if skipCoreInit() {
return
}
// Init log level
logLevel := runtime.LogLevel()
logJson := runtime.LogJSON()
common.LogToFile = runtime.LogToFile()
// Backward compatibility
if os.Getenv("PYDIO_LOGS_LEVEL") != "" {
logLevel = os.Getenv("PYDIO_LOGS_LEVEL")
}
if logLevel == "production" {
logLevel = "info"
logJson = true
common.LogToFile = true
}
// Making sure the log level is passed everywhere (fork processes for example)
os.Setenv("CELLS_LOG", logLevel)
os.Setenv("CELLS_LOG_TO_FILE", strconv.FormatBool(common.LogToFile))
if logJson {
os.Setenv("CELLS_LOG_JSON", "true")
common.LogConfig = common.LogConfigProduction
} else {
common.LogConfig = common.LogConfigConsole
}
switch logLevel {
case "info":
common.LogLevel = zap.InfoLevel
case "warn":
common.LogLevel = zap.WarnLevel
case "debug":
common.LogLevel = zap.DebugLevel
case "error":
common.LogLevel = zap.ErrorLevel
}
log.Init(runtime.ApplicationWorkingDir(runtime.ApplicationDirLogs), cw.RichContext)
// Using it once
log.Logger(context.Background())
}
func initLogLevelListener(ctx context.Context) {
_, er := broker.Subscribe(ctx, common.TopicLogLevelEvent, func(message broker.Message) error {
event := &log2.LogLevelEvent{}
if _, e := message.Unmarshal(event); e == nil {
log.SetDynamicDebugLevels(event.GetResetInfo(), event.GetLevelDebug(), event.GetServices()...)
} else {
return e
}
return nil
})
if er != nil {
fmt.Println("Cannot subscribe to broker for TopicLogLevelEvent", er.Error())
}
}
// bindViperFlags visits all flags in FlagSet and bind their key to the corresponding viper variable
func bindViperFlags(flags *pflag.FlagSet, replaceKeys ...map[string]string) {
flags.VisitAll(func(flag *pflag.Flag) {
key := flag.Name
for _, replaceKey := range replaceKeys {
if replace, ok := replaceKey[flag.Name]; ok {
key = replace
}
}
_ = cellsViper.BindPFlag(key, flag)
})
}