-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
252 lines (231 loc) · 7.12 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
package main
import (
"context"
"fmt"
"io"
"os"
"sync"
"time"
"github.com/coralproject/coral-importer/common/coral"
"github.com/coralproject/coral-importer/internal/warnings"
"github.com/coralproject/coral-importer/strategies/csv"
"github.com/coralproject/coral-importer/strategies/legacy"
"github.com/coralproject/coral-importer/strategies/legacy/mapper"
"github.com/fatih/color"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
const (
// CurrentMigrationVersion is the version representing the most recent migration
// that this strategy is designed to handle. This should be updated as revisions
// are applied to this strategy for future versions.
CurrentMigrationVersion int64 = 1582929716101
)
func main() {
start := time.Now()
// Configure the writer for the logger. We'll set this in the before hook of
// the CLI.
ctx, cancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
var logFile io.Closer
defer func() {
if logFile == nil {
return
}
cancel()
wg.Wait()
logFile.Close()
}()
app := cli.NewApp()
app.Name = "github.com/coralproject/coral-importer"
app.Usage = "imports comment exports from other providers into Coral"
app.Version = fmt.Sprintf("%v, commit %v, built at %v against migration %d", version, commit, date, CurrentMigrationVersion)
app.Flags = []cli.Flag{
&cli.Int64Flag{
Name: "migrationID",
EnvVars: []string{"CORAL_MIGRATION_ID"},
Usage: "ID of the most recent migration associated with your installation",
},
&cli.StringFlag{
Name: "log",
EnvVars: []string{"CORAL_LOG"},
Required: true,
Usage: "output directory for where the logs will be written to",
},
&cli.BoolFlag{
Name: "forceSkipMigrationCheck",
Usage: "used to skip the migration version check",
},
&cli.BoolFlag{
Name: "disableMonotonicCursorTimes",
Usage: "used to disable monotonic cursor times which adds a offset to the same times to ensure all emitted times are unique",
},
&cli.DurationFlag{
Name: "memoryStatFrequency",
Usage: "specify the frequency of measurements of memory usage, default is never",
},
}
app.Before = func(c *cli.Context) error {
// Configure the logger.
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logrus.JSONFormatter{})
f, err := os.OpenFile(c.String("log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
return cli.Exit(err.Error(), 1)
}
logFile = f
logrus.SetOutput(f)
memoryStatFrequency := c.Duration("memoryStatFrequency")
if memoryStatFrequency > 0 {
wg.Add(1)
go func() {
defer wg.Done()
StartLoggingMemoryStats(ctx, memoryStatFrequency)
}()
}
// Check that the imported needs updating.
if c.Bool("forceSkipMigrationCheck") {
logrus.Warn("skipping migration check")
} else if c.Int64("migrationID") != CurrentMigrationVersion {
logrus.WithFields(logrus.Fields{
"migrationID": c.Int("migrationID"),
"currentMigrationVersion": CurrentMigrationVersion,
}).Fatal("migration version mismatch, update importer to support new migrations or skip with --forceSkipMigrationCheck")
}
// Add support for the monotonic cursor times if not disabled.
if c.Bool("disableMonotonicCursorTimes") {
logrus.Warn("monotonic cursor times are disabled, some entries may have duplicate cursor times")
} else {
logrus.Info("monotonic cursor times are enabled, cursor times will be offset automatically")
coral.EnableMonotonicCursorTime()
}
color.New(color.Bold).Printf("coral-importer (%s)\n", c.App.Version)
return nil
}
app.Commands = []*cli.Command{
{
Name: "csv",
Usage: "a migrator designed to migrate data from the standardized CSV format",
Action: csv.CLI,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "tenantID",
Usage: "ID of the Tenant to import for",
Required: true,
},
&cli.StringFlag{
Name: "siteID",
Usage: "ID of the Site to import for",
Required: true,
},
&cli.StringFlag{
Name: "auth",
Usage: "type of profile to emit (One of \"sso\" or \"local\")",
Value: "sso",
},
&cli.StringFlag{
Name: "input",
Usage: "folder where the CSV input files are located",
Required: true,
},
&cli.StringFlag{
Name: "output",
Usage: "folder where the outputted mongo files should be placed",
Required: true,
},
&cli.BoolFlag{
Name: "dryRun",
Usage: "processes data to validate inputs without actually writing files",
},
},
},
{
Name: "legacy",
Usage: "a migrator designed to import data from previous versions of Coral",
Action: legacy.CLI,
Subcommands: []*cli.Command{
{
Name: "map",
Usage: "perform mapping of legacy fields into importable files",
Action: mapper.CLI,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "post",
EnvVars: []string{"CORAL_MAPPER_POST_DIRECTORY"},
Usage: "directory to write files that have been processed by the mapper",
Required: true,
},
},
},
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "tenantID",
EnvVars: []string{"CORAL_TENANT_ID"},
Usage: "ID of the Tenant to import for",
Required: true,
},
&cli.StringFlag{
Name: "siteID",
EnvVars: []string{"CORAL_SITE_ID"},
Usage: "ID of the Site to import for",
Required: true,
},
&cli.StringFlag{
Name: "preferredPerspectiveModel",
Usage: "the preferred model to use for copying over toxicity scores",
Value: "SEVERE_TOXICITY",
},
&cli.StringFlag{
Name: "input",
EnvVars: []string{"CORAL_INPUT_DIRECTORY"},
Usage: "folder where the output from mongoexport is located, separated into collection named JSON files",
Required: true,
},
&cli.StringFlag{
Name: "output",
EnvVars: []string{"CORAL_OUTPUT_DIRECTORY"},
Usage: "folder where the outputted mongo files should be placed",
Required: true,
},
&cli.BoolFlag{
Name: "dryRun",
Usage: "processes data to validate inputs without actually writing files",
},
},
},
}
if err := app.Run(os.Args); err != nil {
color.New(color.Bold, color.FgRed).Println(err.Error())
logrus.WithError(err).Fatal()
}
warnings.Every(func(warning *warnings.Warning) {
occurrences := warning.Occurrences()
if occurrences == 0 {
return
}
logrus.WithFields(logrus.Fields{
"warning": warning.String(),
"occurrences": occurrences,
"keys": warning.Keys(),
}).Warn("warning occurred")
})
profiles := warnings.UnsupportedUserProfileProvider.Keys()
if len(profiles) > 1 {
logrus.WithFields(logrus.Fields{
"profiles": profiles,
}).Warn("multiple forign user profiles found, multiple passes of mapper required")
} else if len(profiles) == 1 {
logrus.WithFields(logrus.Fields{
"profiles": profiles,
}).Warn("forign user profile found, mapper required")
}
took := time.Since(start)
color.New(color.Bold, color.FgGreen).Printf("\nCompleted, took %s\n", took)
logrus.WithField("took", took).Info("completed")
}