-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathmain.go
More file actions
132 lines (114 loc) · 3.36 KB
/
main.go
File metadata and controls
132 lines (114 loc) · 3.36 KB
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
package main
import (
"net/http"
"os"
"strings"
"time"
"github.com/pkg/errors"
cloneMgr "github.com/stackrox/rox/migrator/clone"
"github.com/stackrox/rox/migrator/log"
"github.com/stackrox/rox/pkg/config"
"github.com/stackrox/rox/pkg/grpc/routes"
"github.com/stackrox/rox/pkg/migrations"
"github.com/stackrox/rox/pkg/postgres"
"github.com/stackrox/rox/pkg/postgres/pgadmin"
"github.com/stackrox/rox/pkg/postgres/pgconfig"
"github.com/stackrox/rox/pkg/retry"
"github.com/stackrox/rox/pkg/version"
)
func main() {
startProfilingServer()
if err := run(); err != nil {
log.WriteToStderrf("Migrator failed: %+v", err)
os.Exit(1)
}
}
func startProfilingServer() {
handler := http.NewServeMux()
for path, debugHandler := range routes.DebugRoutes {
handler.Handle(path, debugHandler)
}
srv := &http.Server{Addr: ":6060", Handler: handler}
go func() {
if err := srv.ListenAndServe(); err != nil {
log.WriteToStderrf("Closing profiling server: %v", err)
}
}()
}
func run() error {
log.WriteToStderrf("Run migrator.run() with version: %s, DB sequence: %d", version.GetMainVersion(), migrations.CurrentDBVersionSeqNum())
conf := config.GetConfig()
if conf == nil {
log.WriteToStderrf("cannot get central configuration. Skipping migrator")
return nil
}
if conf.Maintenance.SafeMode {
log.WriteToStderr("configuration has safe mode set. Skipping migrator")
return nil
}
rollbackVersion := strings.TrimSpace(conf.Maintenance.ForceRollbackVersion)
if rollbackVersion != "" {
log.WriteToStderrf("conf.Maintenance.ForceRollbackVersion: %s", rollbackVersion)
}
// If using internal database, ensure the database in the connection string exists
if !pgconfig.IsExternalDatabase() {
if err := ensureDatabaseExists(); err != nil {
return err
}
}
// Create the clone manager
sourceMap, adminConfig, err := pgconfig.GetPostgresConfig()
if err != nil {
return errors.Wrap(err, "unable to get Postgres DB config")
}
dbm := cloneMgr.NewPostgres(rollbackVersion, adminConfig, sourceMap)
err = dbm.Scan()
if err != nil {
return errors.Wrap(err, "failed to scan clones")
}
// Get the clone we are migrating
pgClone, err := dbm.GetCloneToMigrate()
if err != nil {
return errors.Wrap(err, "failed to get clone to migrate")
}
log.WriteToStderrf("Clone to Migrate %q", pgClone)
err = upgrade(pgClone)
if err != nil {
return err
}
if err = dbm.Persist(pgClone); err != nil {
return err
}
return nil
}
func dbCheck(source map[string]string, adminConfig *postgres.Config) error {
// Create the central database if necessary
log.WriteToStderrf("checking if the database %q exists", pgconfig.GetActiveDB())
exists, err := pgadmin.CheckIfDBExists(adminConfig, pgconfig.GetActiveDB())
if err != nil {
log.WriteToStderrf("Could not check for central database: %v", err)
return err
}
if !exists {
err = pgadmin.CreateDB(source, adminConfig, pgadmin.EmptyDB, pgconfig.GetActiveDB())
if err != nil {
log.WriteToStderrf("Could not create central database: %v", err)
return err
}
}
return nil
}
func ensureDatabaseExists() error {
sourceMap, adminConfig, err := pgconfig.GetPostgresConfig()
if err != nil {
return err
}
if !pgconfig.IsExternalDatabase() {
return retry.WithRetry(func() error {
return dbCheck(sourceMap, adminConfig)
}, retry.Tries(60), retry.BetweenAttempts(func(_ int) {
time.Sleep(5 * time.Second)
}))
}
return nil
}