/
migrate.go
369 lines (286 loc) · 11.4 KB
/
migrate.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
package migrator
import (
"context"
"errors"
"log"
"os"
"strings"
"time"
"github.com/RocketChat/filestore-migrator/config"
"github.com/RocketChat/filestore-migrator/store"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/gridfs"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
// Migrate needs to be initialized to begin any migration
type Migrate struct {
storeName string
skipErrors bool
sourceStore store.Provider
destinationStore store.Provider
databaseName string
connectionString string
fileCollectionName string
fileOffset time.Time
session mongo.Session
uniqueID string
tempFileLocation string
fileDelay time.Duration
debug bool
}
// New takes the config and returns an initialized Migrate ready to begin migrations
func New(config *config.Config, skipErrors bool) (*Migrate, error) {
if config.Database.ConnectionString == "" {
return nil, errors.New("Missing connectionString for Rocket.Chat's Mongo")
}
if config.Database.Database == "" {
return nil, errors.New("Missing db for Rocket.Chat's DB")
}
if config.TempFileLocation == "" {
config.TempFileLocation = "files"
}
config.TempFileLocation = strings.TrimSuffix(config.TempFileLocation, "/")
fileDelay := time.Millisecond * 10
if config.FileDelay != "" {
delay, err := time.ParseDuration(config.FileDelay)
if err != nil {
log.Println("invalid fileDelay value")
return nil, err
}
fileDelay = delay
}
migrate := &Migrate{
skipErrors: skipErrors,
databaseName: config.Database.Database,
connectionString: config.Database.ConnectionString,
tempFileLocation: config.TempFileLocation,
fileDelay: fileDelay,
debug: config.DebugMode,
}
if _, err := os.Stat(config.TempFileLocation + "/uploads"); os.IsNotExist(err) {
if err := os.MkdirAll(config.TempFileLocation+"/uploads", 0777); err != nil {
migrate.debugLog(err)
return nil, errors.New("Temp Directory doesn't exist and unable to create it")
}
}
if _, err := os.Stat(config.TempFileLocation + "/avatars"); os.IsNotExist(err) {
if err := os.MkdirAll(config.TempFileLocation+"/avatars", 0777); err != nil {
migrate.debugLog(err)
return nil, errors.New("Temp Directory doesn't exist and unable to create it")
}
}
if config.Source.Type != "" {
switch config.Source.Type {
case "GridFS":
session, err := connectDB(config.Database.ConnectionString)
if err != nil {
return nil, err
}
sourceStore := &store.GridFSProvider{
Database: config.Database.Database,
Session: session,
TempFileLocation: config.TempFileLocation,
Buckets: make(map[string]*gridfs.Bucket),
}
migrate.sourceStore = sourceStore
case "GoogleStorage":
if (config.Source.GoogleStorage.Bucket == "" || config.Source.GoogleStorage.JSONKey == "") && !config.Source.ReferenceOnly {
return nil, errors.New("Make sure you include all of the required options for GoogleStorage")
}
sourceStore := &store.GoogleStorageProvider{
JSONKey: config.Source.GoogleStorage.JSONKey,
Bucket: config.Source.GoogleStorage.Bucket,
TempFileLocation: config.TempFileLocation,
}
migrate.sourceStore = sourceStore
case "AmazonS3":
if (config.Source.AmazonS3.AccessID == "" || config.Source.AmazonS3.AccessKey == "" || config.Source.AmazonS3.Bucket == "") && !config.Source.ReferenceOnly {
return nil, errors.New("Make sure you include all of the required options for AmazonS3")
}
sourceStore := &store.S3Provider{
Endpoint: config.Source.AmazonS3.Endpoint,
AccessID: config.Source.AmazonS3.AccessID,
AccessKey: config.Source.AmazonS3.AccessKey,
Region: config.Source.AmazonS3.Region,
Bucket: config.Source.AmazonS3.Bucket,
UseSSL: config.Source.AmazonS3.UseSSL,
TempFileLocation: config.TempFileLocation,
}
migrate.sourceStore = sourceStore
case "FileSystem":
if config.Source.FileSystem.Location == "" && !config.Source.ReferenceOnly {
return nil, errors.New("Make sure you include all of the required options for FileSystem")
}
config.Source.FileSystem.Location = strings.TrimSuffix(config.Source.FileSystem.Location, "/")
if !config.Source.ReferenceOnly {
if _, err := os.Stat(config.Source.FileSystem.Location); os.IsNotExist(err) {
return nil, errors.New("Filesystem source location does not exist or is unaccessible")
}
}
sourceStore := &store.FileSystemStorageProvider{
Location: config.Source.FileSystem.Location,
TempFileLocation: config.TempFileLocation,
}
migrate.sourceStore = sourceStore
default:
return nil, errors.New("Invalid Source Type")
}
migrate.debugLog("Source store type set to: ", config.Source.Type)
}
if config.Destination.Type != "" {
switch config.Destination.Type {
case "AmazonS3":
if config.Destination.AmazonS3.AccessID == "" || config.Destination.AmazonS3.AccessKey == "" || config.Destination.AmazonS3.Bucket == "" {
return nil, errors.New("Make sure you include all of the required options for AmazonS3")
}
destinationStore := &store.S3Provider{
Endpoint: config.Destination.AmazonS3.Endpoint,
AccessID: config.Destination.AmazonS3.AccessID,
AccessKey: config.Destination.AmazonS3.AccessKey,
Region: config.Destination.AmazonS3.Region,
Bucket: config.Destination.AmazonS3.Bucket,
UseSSL: config.Destination.AmazonS3.UseSSL,
}
migrate.destinationStore = destinationStore
case "GoogleStorage":
if config.Destination.GoogleStorage.Bucket == "" || config.Destination.GoogleStorage.JSONKey == "" {
return nil, errors.New("Make sure you include all of the required options for AmazonS3")
}
destinationStore := &store.GoogleStorageProvider{
JSONKey: config.Destination.GoogleStorage.JSONKey,
Bucket: config.Destination.GoogleStorage.Bucket,
}
migrate.destinationStore = destinationStore
case "FileSystem":
if config.Destination.FileSystem.Location == "" {
return nil, errors.New("Make sure you include all of the required options for FileSystem")
}
if _, err := os.Stat(config.Destination.FileSystem.Location); os.IsNotExist(err) {
if err := os.MkdirAll(config.Destination.FileSystem.Location, 0777); err != nil {
migrate.debugLog(err)
return nil, errors.New("filesystem directory doesn't exist and unable to create it")
}
}
destinationStore := &store.FileSystemStorageProvider{
Location: config.Destination.FileSystem.Location,
}
migrate.destinationStore = destinationStore
default:
return nil, errors.New("Invalid Destination Type")
}
migrate.debugLog("Destination store type set to: ", config.Destination.Type)
}
if migrate.sourceStore == nil && migrate.destinationStore == nil {
return nil, errors.New("At least a source or destination store must be provided")
}
return migrate, nil
}
var ErrNoJsonKey = errors.New("no-json-key")
// GetRocketChatStore uses database to build source Store from settings
func GetRocketChatStore(dbConfig config.DatabaseConfig) (*config.MigrateTarget, error) {
session, err := connectDB(dbConfig.ConnectionString)
if err != nil {
return nil, err
}
defer session.EndSession(context.TODO())
client := session.Client()
db := client.Database(dbConfig.Database)
settingsCollection := db.Collection("rocketchat_settings")
var fileUploadStorageType rocketChatSetting
if err = settingsCollection.FindOne(context.TODO(), bson.M{"_id": "FileUpload_Storage_Type"}).Decode(&fileUploadStorageType); err != nil {
return nil, err
}
sourceStore := &config.MigrateTarget{}
switch fileUploadStorageType.Value {
case "GridFS":
sourceStore.Type = "GridFS"
return sourceStore, nil
case "AmazonS3":
sourceStore.Type = "AmazonS3"
var awsAccessID rocketChatSetting
if err := settingsCollection.FindOne(context.TODO(), bson.M{"_id": "FileUpload_S3_AWSAccessKeyId"}).Decode(&awsAccessID); err != nil {
return nil, err
}
var awsSecret rocketChatSetting
if err := settingsCollection.FindOne(context.TODO(), bson.M{"_id": "FileUpload_S3_AWSSecretAccessKey"}).Decode(&awsSecret); err != nil {
return nil, err
}
var bucket rocketChatSetting
if err := settingsCollection.FindOne(context.TODO(), bson.M{"_id": "FileUpload_S3_Bucket"}).Decode(&bucket); err != nil {
return nil, err
}
var region rocketChatSetting
if err := settingsCollection.FindOne(context.TODO(), bson.M{"_id": "FileUpload_S3_Region"}).Decode(®ion); err != nil {
return nil, err
}
var s3url rocketChatSetting
if err := settingsCollection.FindOne(context.TODO(), bson.M{"_id": "FileUpload_S3_BucketURL"}).Decode(&s3url); err != nil {
return nil, err
}
if s3url.Value == "" {
s3url.Value = "s3.amazonaws.com"
}
sourceStore.AmazonS3.Endpoint = s3url.Value
sourceStore.AmazonS3.Bucket = bucket.Value
sourceStore.AmazonS3.AccessID = awsAccessID.Value
sourceStore.AmazonS3.AccessKey = awsSecret.Value
sourceStore.AmazonS3.Region = region.Value
sourceStore.AmazonS3.UseSSL = true
return sourceStore, nil
case "GoogleCloudStorage":
sourceStore.Type = "GoogleStorage"
var settingValue rocketChatSetting
if err := settingsCollection.FindOne(context.TODO(), bson.M{"_id": "FileUpload_GoogleStorage_Bucket"}).Decode(&settingValue); err != nil {
return nil, err
}
sourceStore.GoogleStorage.Bucket = settingValue.Value
//this is a weird one
// currently a workaround, that asks for a json key file to be downloaded first
// returns an error but appropriate consumer should catch and fix
return sourceStore, ErrNoJsonKey
case "FileSystem":
sourceStore.Type = "FileSystem"
var filesystemLocation rocketChatSetting
if err := settingsCollection.FindOne(context.TODO(), bson.M{"_id": "FileUpload_FileSystemPath"}).Decode(&filesystemLocation); err != nil {
return nil, err
}
sourceStore.FileSystem.Location = filesystemLocation.Value
return sourceStore, nil
default:
return nil, errors.New("unable to detect supported fileupload storage type. (Unable to detect google storage currently)")
}
}
func connectDB(connectionstring string) (mongo.Session, error) {
secondaryPreferred := false
if strings.Contains(connectionstring, "ssl=true") {
connectionstring = strings.Replace(connectionstring, "&ssl=true", "", -1)
connectionstring = strings.Replace(connectionstring, "?ssl=true&", "?", -1)
}
if strings.Contains(connectionstring, "readPreference=secondaryPreferred") {
connectionstring = strings.Replace(connectionstring, "&readPreference=secondaryPreferred", "", -1)
connectionstring = strings.Replace(connectionstring, "?readPreference=secondaryPreferred", "", -1)
secondaryPreferred = true
}
if strings.Contains(connectionstring, "readPreference=secondary") {
connectionstring = strings.Replace(connectionstring, "&readPreference=secondary", "", -1)
connectionstring = strings.Replace(connectionstring, "?readPreference=secondary", "", -1)
secondaryPreferred = true
}
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(connectionstring))
if err != nil {
panic(err)
}
var sessionOpts *options.SessionOptions = nil
if secondaryPreferred {
sessionOpts = &options.SessionOptions{
DefaultReadPreference: readpref.SecondaryPreferred(),
}
}
sess, err := client.StartSession(sessionOpts)
if err != nil {
return nil, err
}
return sess, nil
}