/
installer.go
230 lines (208 loc) · 5.94 KB
/
installer.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
/*
* 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 lib is in charge of installing cells. Used by both the Rest service and the CLI-based install.
package lib
import (
"context"
"net/url"
json "github.com/pydio/cells/x/jsonx"
"github.com/pborman/uuid"
"github.com/pydio/minio-go"
"go.uber.org/zap"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/install"
)
const (
InstallAll = 1 << iota
InstallDb
InstallDs
InstallConfig
InstallFrontend
)
type InstallProgressEvent struct {
Progress int
Message string
}
func Install(ctx context.Context, c *install.InstallConfig, flags byte, publisher func(event *InstallProgressEvent)) error {
//log.Logger(ctx).Info("Starting installation now")
publisher(&InstallProgressEvent{Message: "Starting installation now", Progress: 0})
if (flags&InstallAll) != 0 || (flags&InstallDb) != 0 {
if err := actionDatabaseAdd(c); err != nil {
log.Logger(ctx).Error("Error while adding database", zap.Error(err))
return err
}
publisher(&InstallProgressEvent{Message: "Created main database", Progress: 30})
}
if (flags&InstallAll) != 0 || (flags&InstallDs) != 0 {
if err := actionDatasourceAdd(c); err != nil {
log.Logger(ctx).Error("Error while adding datasource", zap.Error(err))
return err
}
publisher(&InstallProgressEvent{Message: "Created default datasources", Progress: 60})
}
if (flags&InstallAll) != 0 || (flags&InstallConfig) != 0 {
if err := actionConfigsSet(c); err != nil {
log.Logger(ctx).Error("Error while getting ports", zap.Error(err))
return err
}
publisher(&InstallProgressEvent{Message: "Configuration of gateway services", Progress: 80})
}
if (flags&InstallAll) != 0 || (flags&InstallFrontend) != 0 {
if err := actionFrontendsAdd(c); err != nil {
log.Logger(ctx).Error("Error while creating logs directory", zap.Error(err))
return err
}
publisher(&InstallProgressEvent{Message: "Creation of logs directory", Progress: 99})
}
return nil
}
func PerformCheck(ctx context.Context, name string, c *install.InstallConfig) *install.CheckResult {
result := &install.CheckResult{}
wrapError := func(e error) {
result.Success = false
data, _ := json.Marshal(map[string]string{"error": e.Error()})
result.JsonResult = string(data)
}
switch name {
case "DB":
// Create DSN
dsn, e := dsnFromInstallConfig(c)
if e != nil {
wrapError(e)
break
}
if e := checkConnection(dsn); e != nil {
wrapError(e)
break
}
jData := map[string]interface{}{"message": "successfully connected to database"}
if installExists, adminExists, err := checkCellsInstallExists(dsn); err == nil {
if installExists {
jData["tablesFound"] = true
}
if adminExists {
jData["adminFound"] = true
}
}
result.Success = true
data, _ := json.Marshal(jData)
result.JsonResult = string(data)
case "S3_KEYS":
endpoint := "s3.amazonaws.com"
secure := true
if c.GetDsS3Custom() != "" {
if u, e := url.Parse(c.GetDsS3Custom()); e != nil {
wrapError(e)
break
} else {
endpoint = u.Host
if u.Scheme == "http" {
secure = false
}
}
}
mc, e := minio.NewCore(endpoint, c.GetDsS3ApiKey(), c.GetDsS3ApiSecret(), secure)
if e != nil {
wrapError(e)
break
}
// Check if buckets can be created
data := make(map[string]interface{})
var buckets []string
if bb, er := mc.ListBuckets(); er != nil {
wrapError(er)
break
} else {
for _, b := range bb {
buckets = append(buckets, b.Name)
}
}
data["buckets"] = buckets
testBC := uuid.New()
if er := mc.MakeBucket(testBC, c.GetDsS3CustomRegion()); er == nil {
mc.RemoveBucket(testBC)
data["canCreate"] = true
} else {
data["canCreate"] = false
}
result.Success = true
dd, _ := json.Marshal(data)
result.JsonResult = string(dd)
case "S3_BUCKETS":
endpoint := "s3.amazonaws.com"
secure := true
if c.GetDsS3Custom() != "" {
if u, e := url.Parse(c.GetDsS3Custom()); e != nil {
wrapError(e)
break
} else {
endpoint = u.Host
if u.Scheme == "http" {
secure = false
}
}
}
mc, e := minio.NewCore(endpoint, c.GetDsS3ApiKey(), c.GetDsS3ApiSecret(), secure)
if e != nil {
wrapError(e)
break
}
// Check if buckets can be created
data := make(map[string]interface{})
buckets := make(map[string]struct{})
if bb, er := mc.ListBuckets(); er != nil {
wrapError(er)
break
} else {
for _, b := range bb {
buckets[b.Name] = struct{}{}
}
}
toCheck := []string{
c.GetDsS3BucketDefault(),
c.GetDsS3BucketPersonal(),
c.GetDsS3BucketCells(),
c.GetDsS3BucketBinaries(),
c.GetDsS3BucketThumbs(),
c.GetDsS3BucketVersions(),
}
var created []string
for _, check := range toCheck {
if _, ok := buckets[check]; ok { // already exists
continue
}
if e := mc.MakeBucket(check, c.GetDsS3CustomRegion()); e != nil {
wrapError(e)
break
} else {
created = append(created, check)
}
}
result.Success = true
data["bucketsCreated"] = created
dd, _ := json.Marshal(data)
result.JsonResult = string(dd)
default:
result.Success = false
data, _ := json.Marshal(map[string]string{"error": "unsupported check type " + name})
result.JsonResult = string(data)
}
return result
}