forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-ni.go
341 lines (284 loc) · 9.58 KB
/
install-ni.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
/*
* 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 (
"bytes"
"context"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/proto/install"
"github.com/pydio/cells/discovery/install/lib"
json "github.com/pydio/cells/x/jsonx"
)
type NiInstallConfig struct {
install.InstallConfig `yaml:",inline"`
ProxyConfigs []*install.ProxyConfig `json:"proxyConfigs" yaml:"proxyconfigs"`
CustomConfigs map[string]interface{} `json:"customConfigs" yaml:"customconfigs"`
}
func nonInteractiveInstall(cmd *cobra.Command, args []string) (*install.InstallConfig, error) {
if niYamlFile != "" || niJsonFile != "" {
return installFromConf()
}
pconf, err := proxyConfigFromArgs()
if err != nil {
return nil, err
}
err = applyProxySites([]*install.ProxyConfig{pconf})
if err != nil {
return nil, err
}
return &install.InstallConfig{ProxyConfig: pconf}, nil
}
func proxyConfigFromArgs() (*install.ProxyConfig, error) {
proxyConfig := &install.ProxyConfig{}
if niBindUrl == "" {
niBindUrl = "default"
}
if niBindUrl == "default" {
def := *config.DefaultBindingSite
proxyConfig = &def
} else if p := strings.Split(niBindUrl, ":"); len(p) != 2 {
return nil, fmt.Errorf("Bind URL %s is not valid. Please correct to use an [IP|DOMAIN]:[PORT] string", niBindUrl)
} else {
if p[0] == "" {
// Only port is set - use DefaultBindSite host
pp := strings.Split(config.DefaultBindingSite.Binds[0], ":")
niBindUrl = pp[0] + ":" + p[1]
}
proxyConfig.Binds = []string{niBindUrl}
}
if niNoTls {
proxyConfig.TLSConfig = nil
} else if niCertFile != "" && niKeyFile != "" {
tlsConf := &install.ProxyConfig_Certificate{
Certificate: &install.TLSCertificate{
CertFile: niCertFile,
KeyFile: niKeyFile,
}}
proxyConfig.TLSConfig = tlsConf
} else if niLeEmailContact != "" {
if !niLeAcceptEula {
return nil, fmt.Errorf("you must accept Let's Encrypt EULA by setting the corresponding flag in order to use this mode")
}
tlsConf := &install.ProxyConfig_LetsEncrypt{
LetsEncrypt: &install.TLSLetsEncrypt{
Email: niLeEmailContact,
AcceptEULA: niLeAcceptEula,
StagingCA: niLeUseStagingCA,
},
}
proxyConfig.TLSConfig = tlsConf
} else {
tlsConf := &install.ProxyConfig_SelfSigned{
SelfSigned: &install.TLSSelfSigned{}, // Leave hostnames empty
}
proxyConfig.TLSConfig = tlsConf
}
if niExtUrl != "" {
extURL, err := guessSchemeAndParseBaseURL(niExtUrl, true)
if err != nil {
return nil, fmt.Errorf("could not parse provided URL %s: %s", niExtUrl, err.Error())
}
proxyConfig.ReverseProxyURL = extURL.String()
}
return proxyConfig, nil
}
func installFromConf() (*install.InstallConfig, error) {
fmt.Printf("\033[1m## Performing Installation\033[0m \n")
installConf, err := unmarshallConf()
if err != nil {
return nil, err
}
updateMultiple := false
if installConf.ProxyConfig == nil {
fmt.Println(".... No proxy config")
if envProxy, e := proxyConfigFromArgs(); e == nil {
fmt.Println(".... No error while retrieving proxy from args")
fmt.Printf(".... Env Proxy: %v\n", envProxy)
installConf.ProxyConfig = envProxy
updateMultiple = true
}
}
if installConf.ProxyConfig == nil {
installConf.ProxyConfig = config.DefaultBindingSite
updateMultiple = true
}
// Preconfiguring Sites
if updateMultiple {
installConf.ProxyConfigs = append(installConf.ProxyConfigs, installConf.ProxyConfig)
}
err = applyProxySites(installConf.ProxyConfigs)
if err != nil {
return nil, fmt.Errorf("could not preconfigure proxy: %s", err.Error())
}
// Preconfiguring any custom value passed in Json/Yaml
if installConf.CustomConfigs != nil {
for k, v := range installConf.CustomConfigs {
fmt.Println(".... Setting custom configuration key " + k)
cPath := strings.Split(k, "/")
if e := config.Set(v, cPath...); e != nil {
return nil, fmt.Errorf("could not set value for config key " + k)
}
}
if e := config.Save(common.PydioSystemUsername, "Setting custom configs from installation file"); e != nil {
return nil, e
}
}
iConf := &installConf.InstallConfig
if installConf.FrontendLogin == "" {
// only proxy conf => return and launch browser install server
fmt.Println("FrontendLogin not specified in conf, starting browser-based installation")
// Make a copy (including defaults => including FrontendLogin) and store it as Partial
i := *iConf
err = lib.MergeWithDefaultConfig(&i)
if err != nil {
return nil, err
}
lib.PartialDefaultConfig = &i
return iConf, nil
}
// Merge with GetDefaults()
err = lib.MergeWithDefaultConfig(iConf)
if err != nil {
log.Fatal("Could not merge conf with defaults", err)
}
// Check if pre-configured DB is up and running
nbRetry := 20
for i := 0; i < nbRetry; i++ {
if res := lib.PerformCheck(context.Background(), "DB", iConf); res.Success {
break
}
if i == nbRetry-1 {
fmt.Println("[Error] Cannot connect to database, you should double check your server and your connection configuration.")
return nil, fmt.Errorf("No DB. Aborting...")
}
fmt.Println("... Cannot connect to database, wait before retry")
<-time.After(10 * time.Second)
}
err = lib.Install(context.Background(), iConf, lib.INSTALL_ALL, func(event *lib.InstallProgressEvent) {
fmt.Println(event.Message)
})
if err != nil {
return nil, fmt.Errorf("error while performing installation: %s", err.Error())
}
return iConf, nil
}
func unmarshallConf() (*NiInstallConfig, error) {
confFromFile := &NiInstallConfig{}
var path string
if niYamlFile != "" {
path = niYamlFile
file, err := ioutil.ReadFile(niYamlFile)
if err != nil {
return nil, fmt.Errorf("could not read YAML file at %s: %s", niYamlFile, err.Error())
}
// Replace environment variables before unmarshalling
resolvedFile, err := replaceEnvVars(file)
if err != nil {
return nil, fmt.Errorf("could not replace environment variable in YAML file at %s: %s", niYamlFile, err.Error())
}
err = yaml.Unmarshal(resolvedFile, &confFromFile)
if err != nil {
return nil, fmt.Errorf("error parsing YAML file at %s: %s", niYamlFile, err.Error())
}
}
if niJsonFile != "" {
path = niJsonFile
file, err := ioutil.ReadFile(niJsonFile)
if err != nil {
return nil, fmt.Errorf("could not read JSON file at %s: %s", niJsonFile, err.Error())
}
err = json.Unmarshal(file, &confFromFile)
if err != nil {
return nil, fmt.Errorf("error parsing JSON file at %s: %s", niJsonFile, err.Error())
}
}
if confFromFile.ProxyConfig != nil && len(confFromFile.ProxyConfigs) > 0 {
return nil, fmt.Errorf("Use one of proxyConfig or proxyConfigs keys, but not both")
}
if confFromFile.ProxyConfig != nil {
confFromFile.ProxyConfigs = append(confFromFile.ProxyConfigs, confFromFile.ProxyConfig)
} else if len(confFromFile.ProxyConfigs) > 0 {
confFromFile.ProxyConfig = confFromFile.ProxyConfigs[0]
}
if confFromFile.CustomConfigs != nil {
if title, o := confFromFile.CustomConfigs["frontend/plugin/core.pydio/APPLICATION_TITLE"]; o {
confFromFile.FrontendApplicationTitle = title.(string)
}
if lang, o := confFromFile.CustomConfigs["frontend/plugin/core.pydio/DEFAULT_LANGUAGE"]; o {
confFromFile.FrontendDefaultLanguage = lang.(string)
}
}
fmt.Printf("... Install config loaded from %s \n", path)
return confFromFile, nil
}
func applyProxySites(sites []*install.ProxyConfig) error {
// Save configs
config.Set(sites, "defaults", "sites")
err := config.Save("cli", "Saving sites configs")
if err != nil {
return err
}
// Clean TLS context after the update
// config.ResetTlsConfigs()
return nil
}
// replaceEnvVars replaces all occurrences of environment variables.
// Thanks to mholt and Light Code Labs, LLC. See: https://github.com/caddyserver/caddy
func replaceEnvVars(input []byte) ([]byte, error) {
var offset int
for {
begin := bytes.Index(input[offset:], spanOpen)
if begin < 0 {
break
}
begin += offset // make beginning relative to input, not offset
end := bytes.Index(input[begin+len(spanOpen):], spanClose)
if end < 0 {
break
}
end += begin + len(spanOpen) // make end relative to input, not begin
// get the name; if there is no name, skip it
envVarName := input[begin+len(spanOpen) : end]
if len(envVarName) == 0 {
offset = end + len(spanClose)
continue
}
// get the value of the environment variable
envVarValue := []byte(os.ExpandEnv(os.Getenv(string(envVarName))))
// splice in the value
input = append(input[:begin],
append(envVarValue, input[end+len(spanClose):]...)...)
// continue at the end of the replacement
offset = begin + len(envVarValue)
}
return input, nil
}
// spanOpen and spanClose are used to bound spans that
// contain the name of an environment variable.
var spanOpen, spanClose = []byte{'{', '$'}, []byte{'}'}