forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-ni.go
198 lines (160 loc) · 5.25 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
/*
* 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"
"io/ioutil"
"net/url"
"strings"
"time"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"github.com/pydio/cells/common/proto/install"
"github.com/pydio/cells/discovery/install/lib"
)
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 = applyProxyConfig(pconf)
if err != nil {
return nil, err
}
return &install.InstallConfig{ProxyConfig: pconf}, nil
}
func proxyConfigFromArgs() (*install.ProxyConfig, error) {
proxyConfig := &install.ProxyConfig{}
bindURL, err := guessSchemeAndParseBaseURL(niBindUrl, true)
if err != nil {
return nil, fmt.Errorf("could not parse provided bind URL %s: %s", niBindUrl, err.Error())
}
parts := strings.Split(bindURL.Host, ":")
if len(parts) != 2 {
return nil, fmt.Errorf("Bind URL %s is not valid. Please correct to use an [IP|DOMAIN]:[PORT] string", niBindUrl)
}
scheme := "https"
if niNoTls { // NO TLS
scheme = "http"
} 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{
Hostnames: []string{bindURL.Hostname()},
},
}
proxyConfig.TLSConfig = tlsConf
}
bindURL.Scheme = scheme
extURL, err := url.Parse(niExtUrl)
if !strings.HasPrefix(niExtUrl, "http") {
extURL, err = guessSchemeAndParseBaseURL(niExtUrl, proxyConfig.GetTLSConfig() != nil)
}
if err != nil {
return nil, fmt.Errorf("could not parse provided external URL %s: %s", niExtUrl, err.Error())
}
proxyConfig.BindURL = bindURL.String()
proxyConfig.ExternalURL = extURL.String()
return proxyConfig, nil
}
func installFromConf() (*install.InstallConfig, error) {
fmt.Printf("\033[1m## Performing Installation\033[0m \n")
installConf, err := unmarshallConf()
// Preconfiguring proxy:
err = applyProxyConfig(installConf.GetProxyConfig())
if err != nil {
return nil, fmt.Errorf("could not preconfigure proxy: %s", err.Error())
}
if installConf.InternalUrl == "" {
// only proxy conf => return and launch browser install server
return installConf, nil
}
// Check if pre-configured DB is up and running
nbRetry := 10
for i := 0; i < nbRetry; i++ {
if res := lib.PerformCheck(context.Background(), "DB", installConf); res.Success {
break
}
if i == nbRetry-1 {
fmt.Println("[Error] Cannot connect to database, you should double check your server and your connection config")
return nil, fmt.Errorf("no DB. Aborting...")
}
fmt.Println("... Cannot connect to database, wait before retry")
<-time.After(3 * time.Second)
}
err = lib.Install(context.Background(), installConf, 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 installConf, nil
}
func unmarshallConf() (*install.InstallConfig, error) {
var confFromFile *install.InstallConfig
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())
}
err = yaml.Unmarshal(file, &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())
}
}
fmt.Printf("... Install config loaded from %s \n", path)
return confFromFile, nil
}