forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
301 lines (236 loc) · 8.87 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
/*
* Copyright (c) 2018. 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 implements commands for running pydio services
package cmd
import (
"fmt"
log2 "log"
"os"
"regexp"
"strings"
microregistry "github.com/micro/go-micro/registry"
"github.com/micro/go-micro/server"
"github.com/micro/go-web"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/plugins"
"github.com/pydio/cells/common/registry"
"github.com/pydio/cells/common/utils/net"
"github.com/pydio/cells/discovery/nats"
// All brokers
httpbroker "github.com/pydio/cells/common/micro/broker/http"
natsbroker "github.com/pydio/cells/common/micro/broker/nats"
// All registries
natsregistry "github.com/pydio/cells/common/micro/registry/nats"
// All transports
grpctransport "github.com/pydio/cells/common/micro/transport/grpc"
"github.com/pydio/cells/common/service/metrics"
)
var (
allServices []registry.Service
runningServices []*microregistry.Service
profiling bool
profile *os.File
IsFork bool
)
// 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: `Thank you for using Pydio Cells.
Comprehensive sync & share solution for your collaborators. Open-source software deployed on-premise or in a private cloud.
### Installation
For the very first run, use '` + os.Args[0] + ` install' to load browser-based or command-line based installation wizard. Services
will start at the end of the installation.
### Run
Run '` + os.Args[0] + ` start' to load all services.
### Logs level
By default, logs are outputted in console format at the Info level. You can set the --log flag or set the PYDIO_LOGS_LEVEL environment
variable to one of the following values:
- debug, info, error : logs are written in console format with the according level
- production : logs are written in json format, for usage with a log aggregator tool.
### Services Discovery
Micro services need a registry mechanism to discover each other. You don't need to install any dependency.
Cells currently only supports NATS (nats.io) implementation. If a gnatsd service is already running, it will be detected.
### Cells 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 various 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)
`,
PreRun: func(cmd *cobra.Command, args []string) {},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Special case
if cmd.Long == StartCmd.Long {
common.LogCaptureStdOut = true
}
// Initialise the default registry
handleRegistry()
// Initialise the default broker
handleBroker()
// Initialise the default transport
handleTransport()
// Making sure we capture the signals
handleSignals()
plugins.Init()
// Filtering out services by exclusion
registry.Default.Filter(func(s registry.Service) bool {
for _, exclude := range FilterStartExclude {
re := regexp.MustCompile(exclude)
if strings.HasPrefix(s.Name(), exclude) || re.MatchString(s.Name()) {
return true
}
}
return false
})
initServices()
if s, err := registry.Default.ListServices(); err != nil {
cmd.Print("Could not retrieve list of services")
os.Exit(0)
} else {
allServices = s
}
},
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
func init() {
cobra.OnInitialize(
initLogLevel,
)
viper.SetEnvPrefix("pydio")
viper.AutomaticEnv()
flags := RootCmd.PersistentFlags()
flags.String("registry", "nats", "Registry used to manage services (currently nats only)")
flags.String("registry_address", ":4222", "Registry connection address")
flags.String("registry_cluster_address", ":5222", "Registry cluster address")
flags.String("registry_cluster_routes", "", "Registry cluster routes")
flags.String("broker", "nats", "Pub/sub service for events between services (currently nats only)")
flags.String("broker_address", ":4222", "Broker port")
flags.String("transport", "grpc", "Transport protocol for RPC")
flags.String("transport_address", ":4222", "Transport protocol port")
flags.String("grpc_external", "", "External port exposed for gRPC (may be fixed if no SSL is configured or a reverse proxy is used)")
flags.String("log", "info", "Sets the log level mode")
flags.String("grpc_cert", "", "Certificates used for communication via grpc")
flags.String("grpc_key", "", "Certificates used for communication via grpc")
flags.BoolVar(&IsFork, "fork", false, "Used internally by application when forking processes")
flags.Bool("enable_metrics", false, "Instrument code to expose internal metrics")
flags.Bool("enable_pprof", false, "Enable pprof remote debugging")
viper.BindPFlag("registry", flags.Lookup("registry"))
viper.BindPFlag("registry_address", flags.Lookup("registry_address"))
viper.BindPFlag("registry_cluster_address", flags.Lookup("registry_cluster_address"))
viper.BindPFlag("registry_cluster_routes", flags.Lookup("registry_cluster_routes"))
viper.BindPFlag("broker", flags.Lookup("broker"))
viper.BindPFlag("broker_address", flags.Lookup("broker_address"))
viper.BindPFlag("transport", flags.Lookup("transport"))
viper.BindPFlag("transport_address", flags.Lookup("transport_address"))
viper.BindPFlag("logs_level", flags.Lookup("log"))
viper.BindPFlag("grpc_cert", flags.Lookup("grpc_cert"))
viper.BindPFlag("grpc_key", flags.Lookup("grpc_key"))
viper.BindPFlag("grpc_external", flags.Lookup("grpc_external"))
viper.BindPFlag("enable_metrics", flags.Lookup("enable_metrics"))
viper.BindPFlag("enable_pprof", flags.Lookup("enable_pprof"))
}
// 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() {
// Check PrivateIP and setup Advertise
initAdvertiseIP()
nats.Init()
metrics.Init()
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func initLogLevel() {
// Init log level
logLevel := viper.GetString("logs_level")
// Making sure the log level is passed everywhere (fork processes for example)
os.Setenv("PYDIO_LOGS_LEVEL", logLevel)
if logLevel == "production" {
common.LogConfig = common.LogConfigProduction
} else {
common.LogConfig = common.LogConfigConsole
switch logLevel {
case "info":
common.LogLevel = zap.InfoLevel
case "debug":
common.LogLevel = zap.DebugLevel
case "error":
common.LogLevel = zap.ErrorLevel
}
}
log.Init()
}
func initServices() {
registry.Default.BeforeInit()
registry.Init()
registry.Default.AfterInit()
}
func initAdvertiseIP() {
ok, advertise, err := net.DetectHasPrivateIP()
if err != nil {
log2.Fatal(err.Error())
}
if !ok {
net.DefaultAdvertiseAddress = advertise
web.DefaultAddress = advertise + ":0"
server.DefaultAddress = advertise + ":0"
if advertise != "127.0.0.1" {
fmt.Println("Warning: no private IP detected for binding broker. Will bind to " + net.DefaultAdvertiseAddress + ", which may give public access to the broker.")
}
}
}
func handleRegistry() {
switch viper.Get("registry") {
case "nats":
natsregistry.Enable()
default:
log.Fatal("registry not supported - currently only nats is supported")
}
}
func handleBroker() {
switch viper.Get("broker") {
case "nats":
natsbroker.Enable()
case "http":
httpbroker.Enable()
default:
log.Fatal("broker not supported")
}
}
func handleTransport() {
switch viper.Get("transport") {
case "grpc":
grpctransport.Enable()
default:
log.Fatal("transport not supported")
}
}