/
migrate_resources.go
150 lines (131 loc) · 3.17 KB
/
migrate_resources.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
package internal
import (
"context"
"database/sql"
"encoding/base64"
"errors"
"log"
"os"
"os/exec"
"reflect"
)
type MigrateResourcesOptions struct {
DatabaseURL string
DatabaseSchema string
DryRun *bool
UpdateConfigSourceFunc func(appID string, configSourceData map[string]string, DryRun bool) error
}
// nolint: gocognit
func MigrateResources(opt *MigrateResourcesOptions) {
db := openDB(opt.DatabaseURL, opt.DatabaseSchema)
ctx := context.Background()
configSourceList, err := selectConfigSources(ctx, db, nil)
if err != nil {
log.Fatalf("failed to connect db: %s", err)
}
// dryRun default is true
dryRun := true
if opt.DryRun != nil {
dryRun = *opt.DryRun
}
var configSourcesToUpdate []*ConfigSource
for _, c := range configSourceList {
original := make(map[string]string)
for k, v := range c.Data {
original[k] = v
}
if err := opt.UpdateConfigSourceFunc(c.AppID, c.Data, dryRun); err != nil {
log.Fatalf("failed to convert resources: %s, %s", c.AppID, err)
}
updated := !reflect.DeepEqual(original, c.Data)
log.Printf("converting resources app_id: %s, updated: %t", c.AppID, updated)
if updated {
configSourcesToUpdate = append(configSourcesToUpdate, c)
}
if dryRun {
if updated {
appID := c.AppID
originalAuthgearYAMLBytes, err := base64.StdEncoding.DecodeString(original["authgear.yaml"])
if err != nil {
panic(err)
}
updatedAuthgearYAMLBytes, err := base64.StdEncoding.DecodeString(c.Data["authgear.yaml"])
if err != nil {
panic(err)
}
diff, err := Diff("authgear.yaml", originalAuthgearYAMLBytes, updatedAuthgearYAMLBytes)
if err != nil {
panic(err)
}
log.Printf("diff of authgear.yaml: %v\n", appID)
log.Printf("%v\n", diff)
}
}
}
if dryRun {
log.Printf("dry run: number of apps to update: %d", len(configSourcesToUpdate))
return
}
// update config to db
count := 0
for _, c := range configSourcesToUpdate {
err := WithTx(ctx, db, func(tx *sql.Tx) error {
err := updateConfigSource(ctx, tx, c)
return err
})
if err != nil {
log.Fatalf("failed to update resources to db: %s, %s", c.AppID, err)
} else {
log.Printf("updated resources to db: %s", c.AppID)
count++
}
}
log.Printf("updated apps count: %d", count)
}
func Diff(filename string, original []byte, updated []byte) (diff string, err error) {
fOriginal, err := os.CreateTemp("", filename)
if err != nil {
return
}
defer os.Remove(fOriginal.Name())
fUpdated, err := os.CreateTemp("", filename)
if err != nil {
return
}
defer os.Remove(fUpdated.Name())
_, err = fOriginal.Write(original)
if err != nil {
return
}
err = fOriginal.Close()
if err != nil {
return
}
_, err = fUpdated.Write(updated)
if err != nil {
return
}
err = fUpdated.Close()
if err != nil {
return
}
output, err := exec.Command( // nolint:gosec
"diff",
"-u",
fOriginal.Name(),
fUpdated.Name(),
).CombinedOutput()
if err != nil {
var exitError *exec.ExitError
if errors.As(err, &exitError) {
if exitError.ExitCode() == 0 || exitError.ExitCode() == 1 {
err = nil
}
}
if err != nil {
return
}
}
diff = string(output)
return
}