-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
492 lines (428 loc) · 15.1 KB
/
main.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"strings"
"sync"
"time"
"github.com/pixlise/core/v4/api/dbCollections"
"github.com/pixlise/core/v4/api/filepaths"
"github.com/pixlise/core/v4/core/awsutil"
"github.com/pixlise/core/v4/core/fileaccess"
"github.com/pixlise/core/v4/core/logger"
"github.com/pixlise/core/v4/core/mongoDBConnection"
protos "github.com/pixlise/core/v4/generated-protos"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
var maxItemsToRead int
var quantLogLimitCount int
var t0 = time.Now().UnixMilli()
func main() {
defer reportFailedTasks()
//rand.Seed(time.Now().UnixNano())
fmt.Printf("Started: %v\n", time.Now().String())
var sourceMongoSecret string
var destMongoSecret string
var dataBucket string
var destDataBucket string
var configBucket string
var destConfigBucket string
var userContentBucket string
var destUserContentBucket string
var srcEnvName string
var destEnvName string
var auth0Domain, auth0ClientId, auth0Secret string
var limitToDatasetIDs string
var migrateDatasetsEnabled bool
var migrateROIsEnabled bool
var migrateDiffractionPeaksEnabled bool
var migrateRGBMixesEnabled bool
var migrateTagsEnabled bool
var migrateQuantsEnabled bool
var migrateElementSetsEnabled bool
var migrateZStacksEnabled bool
var migrateExpressionsEnabled bool
var fixROISharingMode bool
var fixROIIndexesMode bool
var jsonImportDir string
var migrateOrphanedSharedROIsFile string
flag.StringVar(&sourceMongoSecret, "sourceMongoSecret", "", "Source mongo DB secret")
flag.StringVar(&destMongoSecret, "destMongoSecret", "", "Destination mongo DB secret")
flag.StringVar(&dataBucket, "dataBucket", "", "Data bucket")
flag.StringVar(&destDataBucket, "destDataBucket", "", "Destination data bucket")
flag.StringVar(&userContentBucket, "userContentBucket", "", "User content bucket")
flag.StringVar(&destUserContentBucket, "destUserContentBucket", "", "Destination user content bucket")
flag.StringVar(&configBucket, "configBucket", "", "Config bucket")
flag.StringVar(&destConfigBucket, "destConfigBucket", "", "Destination config bucket (some files are copied)")
flag.StringVar(&srcEnvName, "srcEnvName", "", "Source Environment Name")
flag.StringVar(&destEnvName, "destEnvName", "", "Destination Environment Name")
flag.IntVar(&maxItemsToRead, "maxItems", 0, "Max number of items to read into any table, 0=unlimited")
flag.StringVar(&auth0Domain, "auth0Domain", "", "Auth0 domain for management API")
flag.StringVar(&auth0ClientId, "auth0ClientId", "", "Auth0 client id for management API")
flag.StringVar(&auth0Secret, "auth0Secret", "", "Auth0 secret for management API")
flag.StringVar(&limitToDatasetIDs, "limitToDatasetIDs", "", "Comma-separated dataset IDs to limit import to (for speed/testing)")
flag.IntVar(&quantLogLimitCount, "quantLogLimitCount", 0, "Limits how many log files are copied (for speed/testing)")
flag.BoolVar(&migrateDatasetsEnabled, "migrateDatasetsEnabled", true, "Should we migrate datasets?")
flag.BoolVar(&migrateROIsEnabled, "migrateROIsEnabled", true, "Should we migrate ROIs?")
flag.BoolVar(&migrateDiffractionPeaksEnabled, "migrateDiffractionPeaksEnabled", true, "Should we migrate Diffraction Peaks?")
flag.BoolVar(&migrateRGBMixesEnabled, "migrateRGBMixesEnabled", true, "Should we migrate RGB Mixes?")
flag.BoolVar(&migrateTagsEnabled, "migrateTagsEnabled", true, "Should we migrate Tags?")
flag.BoolVar(&migrateQuantsEnabled, "migrateQuantsEnabled", true, "Should we migrate Quants?")
flag.BoolVar(&migrateElementSetsEnabled, "migrateElementSetsEnabled", true, "Should we migrate Element Sets?")
flag.BoolVar(&migrateZStacksEnabled, "migrateZStacksEnabled", true, "Should we migrate Z-Stacks?")
flag.BoolVar(&migrateExpressionsEnabled, "migrateExpressionsEnabled", true, "Should we migrate expressions?")
flag.BoolVar(&fixROISharingMode, "fixROISharingMode", false, "Fixing ROI sharing states (this is a whole separate mode, won't migrate other things)")
flag.BoolVar(&fixROIIndexesMode, "fixROIIndexesMode", false, "Fixing ROI indexes (this is a whole separate mode, won't migrate other things)")
flag.StringVar(&migrateOrphanedSharedROIsFile, "migrateOrphanedSharedROIsFile", "", "Migrates shared ROIs whose id exists in the file (this is a whole separate mode, won't migrate other things)")
flag.StringVar(&jsonImportDir, "jsonImportDir", "", "If not empty, this is expected to be a directory to read JSON data into DB from. File names must be collection name.")
flag.Parse()
// Check they're not empty
checkNotEmpty := []string{
dataBucket,
destDataBucket,
configBucket,
destConfigBucket,
userContentBucket,
destUserContentBucket,
srcEnvName,
destEnvName,
auth0Domain,
auth0ClientId,
auth0Secret,
}
checkNotEmptyName := []string{
"dataBucket",
"destDataBucket",
"configBucket",
"destConfigBucket",
"userContentBucket",
"destUserContentBucket",
"srcEnvName",
"destEnvName",
"auth0Domain",
"auth0ClientId",
"auth0Secret",
}
for c, s := range checkNotEmpty {
if len(s) <= 0 {
log.Fatalf("Parameter: %v was empty", checkNotEmptyName[c])
}
}
limitToDatasetIDsList := strings.Split(limitToDatasetIDs, ",")
// Get a session for the bucket region
sess, err := awsutil.GetSession()
if err != nil {
log.Fatalf("Failed to create AWS session. Error: %v", err)
}
s3svc, err := awsutil.GetS3(sess)
if err != nil {
log.Fatalf("Failed to create AWS S3 service. Error: %v", err)
}
fs := fileaccess.MakeS3Access(s3svc)
// Init logger - this used to be local=stdout, cloud env=cloudwatch, but we now write all logs to stdout
iLog := &logger.StdOutLogger{}
iLog.SetLogLevel(logger.LogInfo)
// Connect to mongo
sourceMongoClient, err := mongoDBConnection.Connect(sess, sourceMongoSecret, iLog)
if err != nil {
fatalError(err)
}
destMongoClient, err := mongoDBConnection.Connect(sess, destMongoSecret, iLog)
if err != nil {
fatalError(err)
}
// Destination DB is the new pixlise one
srcExprDB := sourceMongoClient.Database(mongoDBConnection.GetDatabaseName("expressions", srcEnvName))
srcUserDB := sourceMongoClient.Database(mongoDBConnection.GetDatabaseName("userdatabase", srcEnvName))
// Destination DB is the new pixlise one
destDB := destMongoClient.Database(mongoDBConnection.GetDatabaseName("pixlise", destEnvName))
if fixROISharingMode {
fixROISharing(auth0Domain, auth0ClientId, auth0Secret, fs, userContentBucket, limitToDatasetIDsList, destDB)
return
}
if fixROIIndexesMode {
fixROIIndexes(dataBucket, fs, destDB)
return
}
if len(migrateOrphanedSharedROIsFile) > 0 {
runMigrateOrphanedSharedROIs(migrateOrphanedSharedROIsFile, fs, userContentBucket, limitToDatasetIDsList, destDB)
return
}
// Clear out ownership table first, but only for the bits we're about to import
if migrateDatasetsEnabled {
clearOwnership(destDB, protos.ObjectType_OT_SCAN)
}
if migrateROIsEnabled {
clearOwnership(destDB, protos.ObjectType_OT_ROI)
}
if migrateRGBMixesEnabled {
clearOwnership(destDB, protos.ObjectType_OT_EXPRESSION_GROUP)
}
if migrateQuantsEnabled {
clearOwnership(destDB, protos.ObjectType_OT_QUANTIFICATION)
}
if migrateElementSetsEnabled {
clearOwnership(destDB, protos.ObjectType_OT_ELEMENT_SET)
}
if migrateExpressionsEnabled {
clearOwnership(destDB, protos.ObjectType_OT_EXPRESSION)
clearOwnership(destDB, protos.ObjectType_OT_DATA_MODULE)
}
var listingWG sync.WaitGroup
listingWG.Add(2)
userGroups := map[string]string{}
go func() {
defer listingWG.Done()
fmt.Println("==========================================")
fmt.Println("Migrating data from old users DB...")
fmt.Println("==========================================")
err := migrateUsersDB(srcUserDB, destDB)
if err != nil {
fatalError(err)
}
if len(auth0Domain) > 0 && len(auth0ClientId) > 0 && len(auth0Secret) > 0 {
fmt.Println("==========================================")
fmt.Println("Migrating user groups from Auth0...")
fmt.Println("==========================================")
userGroups, err = migrateAuth0UserGroups(auth0Domain, auth0ClientId, auth0Secret, destDB)
if err != nil {
fatalError(err)
}
}
if migrateExpressionsEnabled {
fmt.Println("==========================================")
fmt.Println("Migrating data from old expressions DB...")
fmt.Println("==========================================")
err = migrateExpressionsDB(srcExprDB, destDB, userGroups)
if err != nil {
fatalError(err)
}
} else {
fmt.Println("Skipping migration of expressions...")
}
if migrateDatasetsEnabled {
fmt.Println("==========================================")
fmt.Println("Migrating data from datasets bucket...")
fmt.Println("==========================================")
fmt.Println("Datasets...")
err = migrateDatasets(configBucket, dataBucket, destDataBucket, fs, destDB, limitToDatasetIDsList, userGroups)
if err != nil {
fatalError(err)
}
} else {
fmt.Println("Skipping migration of datasets...")
}
fmt.Println("==========================================")
fmt.Printf("Importing JSON files from %v\n", jsonImportDir)
fmt.Println("==========================================")
err = importJSONFiles(jsonImportDir, destDB)
if err != nil {
fatalError(err)
}
fmt.Println("==========================================")
fmt.Println("Migrating data from config bucket...")
fmt.Println("==========================================")
fmt.Println("Detector Configs...")
err = migrateDetectorConfigs(configBucket, destConfigBucket, fs, destDB)
if err != nil {
fatalError(err)
}
fmt.Println("PIXLISE/PIQUANT Config files...")
migrateConfigs(configBucket, destConfigBucket, fs) // fatalError called if any fail
fmt.Println("Piquant Version...")
err = migratePiquantVersion(configBucket, fs, destDB)
if err != nil {
fatalError(err)
}
}()
/* Decided to leave PIQUANT configs in S3 because that way PIQUANT docker container has authenticated direct access
fmt.Println("Piquant Configs...")
err = migratePiquantConfigs(configBucket, fs, destDB)
if err != nil {
fatalError(err)
}*/
userContentPaths := []string{}
go func() {
defer listingWG.Done()
// List all of S3 user contents
fmt.Println("Listing user contents from S3...")
var err error
userContentPaths, err = fs.ListObjects(userContentBucket, filepaths.RootUserContent)
if err != nil {
fatalError(err)
}
fmt.Printf(" Listed %v files\n", len(userContentPaths))
}()
listingWG.Wait()
fmt.Println("==========================================")
fmt.Println("Migrating data from user content bucket...")
fmt.Println("==========================================")
var wg sync.WaitGroup
if migrateZStacksEnabled {
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println("Quant Z-stacks...")
err = migrateMultiQuants(userContentBucket, userContentPaths, limitToDatasetIDsList, fs, destDB)
if err != nil {
fatalError(err)
}
}()
} else {
fmt.Println("Skipping migration of z-stacks...")
}
if migrateQuantsEnabled {
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println("Quants...")
err = migrateQuants(userContentBucket, userContentPaths, limitToDatasetIDsList, fs, destDB, destUserContentBucket, userGroups)
if err != nil {
fatalError(err)
}
}()
} else {
fmt.Println("Skipping migration of quants...")
}
if migrateElementSetsEnabled {
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println("Element Sets...")
err = migrateElementSets(userContentBucket, userContentPaths, fs, destDB, userGroups)
if err != nil {
fatalError(err)
}
}()
} else {
fmt.Println("Skipping migration of element sets...")
}
if migrateROIsEnabled {
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println("ROIs...")
err = migrateROIs(userContentBucket, userContentPaths, limitToDatasetIDsList, fs, destDB, userGroups)
if err != nil {
fatalError(err)
}
}()
} else {
fmt.Println("Skipping migration of ROIs...")
}
if migrateRGBMixesEnabled {
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println("RGB Mixes...")
err = migrateRGBMixes(userContentBucket, userContentPaths, fs, destDB, userGroups)
if err != nil {
fatalError(err)
}
}()
} else {
fmt.Println("Skipping migration of RGB mixes...")
}
if migrateTagsEnabled {
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println("Tags...")
err = migrateTags(userContentBucket, userContentPaths, fs, destDB)
if err != nil {
fatalError(err)
}
}()
} else {
fmt.Println("Skipping migration of tags...")
}
if migrateDiffractionPeaksEnabled {
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println("Diffraction Peak...")
err = migrateDiffraction(userContentBucket, userContentPaths, fs, destDB)
if err != nil {
fatalError(err)
}
}()
} else {
fmt.Println("Skipping migration of diffraction peaks...")
}
// Wait for all
wg.Wait()
printFinishStats()
}
func fatalError(err error) {
printFinishStats()
log.Fatal(err)
}
func printFinishStats() {
t1 := time.Now().UnixMilli()
sec := (t1 - t0) / 1000
fmt.Printf("Runtime %v seconds\n", sec)
}
func clearOwnership(destDB *mongo.Database, objType protos.ObjectType) {
coll := destDB.Collection(dbCollections.OwnershipName)
result, err := coll.DeleteMany(context.TODO(), bson.M{"objecttype": objType})
if err != nil {
fatalError(err)
}
if result.DeletedCount <= 0 {
log.Printf("Warning: Deleted 0 items from ownership collection for type: %v", objType)
}
}
func fixROISharing(auth0Domain, auth0ClientId, auth0Secret string, fs fileaccess.S3Access, userContentBucket string, limitToDatasetIDsList []string, destDB *mongo.Database) {
userGroups, err := migrateAuth0UserGroups(auth0Domain, auth0ClientId, auth0Secret, destDB)
if err != nil {
fatalError(err)
}
/*
var listingWG sync.WaitGroup
listingWG.Add(1)
userContentPaths := []string{}
go func() {
defer listingWG.Done()
// List all of S3 user contents
fmt.Println("Listing user contents from S3...")
var err error
userContentPaths, err = fs.ListObjects(userContentBucket, filepaths.RootUserContent)
if err != nil {
fatalError(err)
}
fmt.Printf(" Listed %v files\n", len(userContentPaths))
}()
listingWG.Wait()
paths := strings.Join(userContentPaths, "\n")
err = os.WriteFile("roi-all-s3paths.txt", []byte(paths), 0777)
if err != nil {
fatalError(err)
}
*/
paths, err := os.ReadFile("roi-all-s3paths.txt")
if err != nil {
fatalError(err)
}
userContentPaths := strings.Split(string(paths), "\n")
err = migrateROIShares(userContentBucket, userContentPaths, limitToDatasetIDsList, fs, destDB, userGroups)
if err != nil {
fatalError(err)
}
}
func runMigrateOrphanedSharedROIs(sharedIdsToMigrateFile string, fs fileaccess.S3Access, userContentBucket string, limitToDatasetIDsList []string, destDB *mongo.Database) {
paths, err := os.ReadFile("roi-all-s3paths.txt")
if err != nil {
fatalError(err)
}
userContentPaths := strings.Split(string(paths), "\n")
sharedIdsToMigrateFileContents, err := os.ReadFile(sharedIdsToMigrateFile)
if err != nil {
fatalError(err)
}
sharedIdsToMigrate := strings.Split(string(sharedIdsToMigrateFileContents), "\n")
migrateOrphanedSharedROIs(sharedIdsToMigrate, userContentBucket, userContentPaths, limitToDatasetIDsList, fs, destDB, "m3l7kzikydo35znm")
}