forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-cli.go
388 lines (348 loc) · 10.9 KB
/
install-cli.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* 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
import (
"context"
"encoding/json"
"fmt"
"net/url"
"os"
"strconv"
"strings"
p "github.com/manifoldco/promptui"
_ "github.com/mholt/caddy/caddyhttp"
"github.com/pydio/go-phpfpm-detect/fpm"
"github.com/spf13/cobra"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/install"
"github.com/pydio/cells/common/utils"
"github.com/pydio/cells/discovery/install/lib"
)
func notEmpty(input string) error {
if len(input) > 0 {
return nil
} else {
return fmt.Errorf("Field cannot be empty!")
}
}
func validUrl(input string) error {
if e := notEmpty(input); e != nil {
return e
}
u, e := url.ParseRequestURI(input)
if u == nil || u.Scheme == "" || u.Host == "" || u.Port() == "" {
return fmt.Errorf("Please use a valid URL like http://host:port")
}
return e
}
func validPortNumber(input string) error {
port, e := strconv.ParseInt(input, 10, 64)
if e == nil && port == 0 {
return fmt.Errorf("Please use a non empty port!")
}
return e
}
func promptAndSaveInstallUrls() (internal *url.URL, external *url.URL, e error) {
defaultPort := "8080"
var internalHost, externalHost string
defaultIps, e := utils.GetAvailableIPs()
if e != nil {
return
}
var items []string
testExt, eExt := utils.GetOutboundIP()
if eExt == nil {
items = append(items, fmt.Sprintf("%s:%s", testExt.String(), defaultPort))
}
for _, ip := range defaultIps {
if testExt != nil && testExt.String() == ip.String() {
continue
}
items = append(items, fmt.Sprintf("%s:%s", ip.String(), defaultPort))
}
items = append(items, "localhost:"+defaultPort, "0.0.0.0:"+defaultPort)
prompt := p.SelectWithAdd{
Label: "Binding Host (ip:port or yourdomain.tld that the webserver will listen. If internal and external urls differ, use internal here)",
Items: items,
AddLabel: "Other",
Validate: notEmpty,
}
_, internalHost, e = prompt.Run()
if e != nil {
return
}
internalHost = strings.TrimSuffix(internalHost, "/")
internalHost = strings.TrimPrefix(internalHost, "http://")
internalHost = strings.TrimPrefix(internalHost, "https://")
extPrompt := p.Prompt{
Label: "External Host (used to access this machine from outside world, if different from Bind Host)",
Validate: notEmpty,
Default: internalHost,
}
externalHost, e = extPrompt.Run()
if e != nil {
return
}
externalHost = strings.TrimSuffix(externalHost, "/")
externalHost = strings.TrimPrefix(externalHost, "http://")
externalHost = strings.TrimPrefix(externalHost, "https://")
_, e = promptSslMode()
if e != nil {
return
}
scheme := "http"
if config.Get("cert", "proxy", "ssl").Bool(false) {
scheme = "https"
}
externalUrl := fmt.Sprintf("%s://%s", scheme, externalHost)
internalUrl := fmt.Sprintf("%s://%s", scheme, internalHost)
internal, e = url.Parse(internalUrl)
if e != nil {
return
}
external, e = url.Parse(externalUrl)
if e != nil {
return
}
config.Set(externalUrl, "defaults", "url")
config.Set(internalUrl, "defaults", "urlInternal")
utils.SaveConfigs()
return
}
// installCmd represents the install command
var installCliCmd = &cobra.Command{
Use: "install-cli",
Short: "Pydio Cells Command-Line Installer",
Long: `This command launch the installation process of Pydio Cells in the command line instead of a browser.
`,
Run: func(cmd *cobra.Command, args []string) {
micro := config.Get("ports", common.SERVICE_MICRO_API).Int(0)
if micro == 0 {
micro = utils.GetAvailablePort()
config.Set(micro, "ports", common.SERVICE_MICRO_API)
utils.SaveConfigs()
}
internalUrl, _, err := promptAndSaveInstallUrls()
if err != nil {
log.Fatal(err.Error())
}
installConfig := lib.GenerateDefaultConfig()
installConfig.InternalUrl = internalUrl.String()
fmt.Println("")
fmt.Println("\033[1m## Database Connection\033[0m")
if e := promptDB(installConfig); e != nil {
os.Exit(1)
}
fmt.Println("")
fmt.Println("\033[1m## Frontend Configuration\033[0m")
if e := promptFPM(installConfig); e != nil {
os.Exit(1)
}
if e := promptFrontendAdmin(installConfig); e != nil {
os.Exit(1)
}
fmt.Println("")
fmt.Println("\033[1m## Advanced Settings\033[0m")
if e := promptAdvanced(installConfig); e != nil {
os.Exit(1)
}
fmt.Println("")
fmt.Println("\033[1m## Performing Installation\033[0m")
e := lib.Install(context.Background(), installConfig, func(event *lib.InstallProgressEvent) {
fmt.Println(p.IconGood + " " + event.Message)
})
if e != nil {
log.Fatal("Error while performing installation: " + e.Error())
}
fmt.Println("")
fmt.Println(p.IconGood + "\033[1m Installation Finished: please restart with './cells start' command\033[0m")
fmt.Println("")
},
}
func promptDB(c *install.InstallConfig) error {
connType := p.Select{
Label: "Database Connection Type",
Items: []string{"TCP", "Socket", "Manual"},
}
dbTcpHost := p.Prompt{Label: "Database Hostname", Validate: notEmpty, Default: "localhost"}
dbTcpPort := p.Prompt{Label: "Database Port", Validate: validPortNumber, Default: "3306"}
dbName := p.Prompt{Label: "Database Name", Validate: notEmpty, Default: "cells"}
dbUser := p.Prompt{Label: "Database User", Validate: notEmpty}
dbPass := p.Prompt{Label: "Database Password (leave empty if not needed)", Mask: '*'}
dbSocketFile := p.Prompt{Label: "Socket File", Validate: notEmpty}
dbDSN := p.Prompt{Label: "Manual DSN", Validate: notEmpty}
uConnIdx, _, _ := connType.Run()
var e error
if uConnIdx == 2 {
if c.DbManualDSN, e = dbDSN.Run(); e != nil {
return e
}
} else {
if uConnIdx == 0 {
c.DbConnectionType = "tcp"
if c.DbTCPHostname, e = dbTcpHost.Run(); e != nil {
return e
}
if c.DbTCPPort, e = dbTcpPort.Run(); e != nil {
return e
}
} else if uConnIdx == 1 {
c.DbConnectionType = "socket"
if c.DbSocketFile, e = dbSocketFile.Run(); e != nil {
return e
}
}
var name, user, pass string
if name, e = dbName.Run(); e != nil {
return e
}
if user, e = dbUser.Run(); e != nil {
return e
}
if pass, e = dbPass.Run(); e != nil {
return e
}
if uConnIdx == 0 {
c.DbTCPName = name
c.DbTCPUser = user
c.DbTCPPassword = pass
} else {
c.DbSocketName = name
c.DbSocketUser = user
c.DbSocketPassword = pass
}
}
if res := lib.PerformCheck(context.Background(), "DB", c); !res.Success {
return fmt.Errorf("Cannot connect to this database!")
} else {
fmt.Println(p.IconGood + " Successfully connected to the database")
}
return nil
}
func promptFPM(c *install.InstallConfig) error {
if len(c.CheckResults) > 0 && c.CheckResults[0].Name == "PHP" && c.CheckResults[0].JsonResult != "" {
type fpmResult struct {
Error string
Fpm struct {
ListenAddress string
ListenNetwork string
PhpVersion string
PhpExtensions []string
}
}
var res *fpmResult
if e := json.Unmarshal([]byte(c.CheckResults[0].JsonResult), &res); e == nil {
if !c.CheckResults[0].Success {
fmt.Println(p.IconBad + " Could not detect PHP version: " + res.Error)
} else {
fmt.Println(p.IconGood + " Php version detected: " + res.Fpm.PhpVersion)
}
fpmPrompt := p.Prompt{Label: "PHP-FPM Listen Address was detected at " + res.Fpm.ListenAddress + ". Is it correct", IsConfirm: true}
if _, err := fpmPrompt.Run(); err == nil {
return nil
}
}
}
fpmPrompt := p.Prompt{Label: "Please enter the address where Php-FPM is listening (use host:port or /path/to/socket)", Default: "localhost:9000"}
res, er := fpmPrompt.Run()
if er != nil {
return er
}
if len(res) > 0 {
c.FpmAddress = res
var network string
if strings.Contains(c.FpmAddress, ":") {
network = "tcp"
} else {
network = "unix"
}
if err := fpm.DetectByDirectConnection(&fpm.PhpFpmConfig{ListenAddress: c.FpmAddress, ListenNetwork: network}); err == nil {
fmt.Println(p.IconGood + " Php-Fpm successfully connected")
} else {
fmt.Println(p.IconBad + " Could not connect: " + err.Error())
}
}
return nil
}
func promptFrontendAdmin(c *install.InstallConfig) error {
login := p.Prompt{Label: "Admin Login (leave passwords empty if an admin is already created)", Default: "admin", Validate: notEmpty}
pwd := p.Prompt{Label: "Admin Password", Mask: '*'}
pwd2 := p.Prompt{Label: "Confirm Password", Mask: '*', Validate: func(s string) error {
if c.FrontendPassword != s {
return fmt.Errorf("Password differ!")
} else {
return nil
}
}}
var e error
if c.FrontendLogin, e = login.Run(); e != nil {
return e
}
if c.FrontendPassword, e = pwd.Run(); e != nil {
return e
}
if c.FrontendRepeatPassword, e = pwd2.Run(); e != nil {
return e
}
return nil
}
func promptAdvanced(c *install.InstallConfig) error {
confirm := p.Prompt{Label: "There are some advanced settings for ports and initial data storage. Do you want to edit them", IsConfirm: true}
if _, e := confirm.Run(); e != nil {
return nil
}
dsPath := p.Prompt{Label: "Path to the default datasource", Default: c.DsFolder, Validate: notEmpty}
oidcId := p.Prompt{Label: "OpenIdConnect ClientID (for frontend)", Default: c.ExternalDexID, Validate: notEmpty}
oidcSecret := p.Prompt{Label: "OpenIdConnect ClientID (for frontend)", Default: c.ExternalDexSecret, Validate: notEmpty}
dexPort := p.Prompt{Label: "OpenIdConnect Server Port", Default: c.ExternalDex, Validate: validPortNumber}
microPort := p.Prompt{Label: "Rest Gateway Port", Default: c.ExternalMicro, Validate: validPortNumber}
gatewayPort := p.Prompt{Label: "Data Gateway Port", Default: c.ExternalGateway, Validate: validPortNumber}
websocketPort := p.Prompt{Label: "WebSocket Port", Default: c.ExternalWebsocket, Validate: validPortNumber}
if folder, e := dsPath.Run(); e == nil {
c.DsFolder = folder
} else {
return e
}
var e error
if c.ExternalDexID, e = oidcId.Run(); e != nil {
return e
}
if c.ExternalDexSecret, e = oidcSecret.Run(); e != nil {
return e
}
if c.ExternalDex, e = dexPort.Run(); e != nil {
return e
}
if c.ExternalMicro, e = microPort.Run(); e != nil {
return e
}
if c.ExternalGateway, e = gatewayPort.Run(); e != nil {
return e
}
if c.ExternalWebsocket, e = websocketPort.Run(); e != nil {
return e
}
return nil
}
func init() {
RootCmd.AddCommand(installCliCmd)
}