/
migration.go
299 lines (247 loc) · 6.16 KB
/
migration.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
package mg1
import (
"encoding/json"
"fmt"
"os"
"path"
"strings"
migrate "github.com/ipfs/fs-repo-migrations/go-migrate"
dstore "github.com/ipfs/fs-repo-migrations/ipfs-1-to-2/go-datastore"
flatfs "github.com/ipfs/fs-repo-migrations/ipfs-1-to-2/go-datastore/flatfs"
leveldb "github.com/ipfs/fs-repo-migrations/ipfs-1-to-2/go-datastore/leveldb"
dsq "github.com/ipfs/fs-repo-migrations/ipfs-1-to-2/go-datastore/query"
lock "github.com/ipfs/fs-repo-migrations/ipfs-1-to-2/repolock"
mfsr "github.com/ipfs/fs-repo-migrations/mfsr"
)
const peerKeyName = "peer.key"
type Migration struct{}
func (m Migration) Versions() string {
return "1-to-2"
}
func (m Migration) Reversible() bool {
return true
}
func (m Migration) Apply(opts migrate.Options) error {
// lock the daemon.lock file. and if we succeed, remove it at the end.
// we remove it because camlistore/lock doesn't, and we changed the filename.
// so we don't want this one around anymore.
repolk, err := lock.Lock1(opts.Path)
if err != nil {
return err
}
closedLock := false
defer func() {
if !closedLock { // unlock only if we didn't close below
repolk.Close()
}
}()
repo := mfsr.RepoPath(opts.Path)
if err := repo.CheckVersion("1"); err != nil {
return err
}
// 1) run some sanity checks to make sure we should even bother
err = sanityChecks(opts)
if err != nil {
return err
}
if opts.Verbose {
fmt.Println("performed sanity check")
}
// 2) Transfer blocks out of leveldb into flatDB
err = transferBlocksToFlatDB(opts.Path, opts.Verbose)
if err != nil {
return err
}
if opts.Verbose {
fmt.Println("moved blocks from leveldb to flatfs")
}
// 3) move ipfs path from .go-ipfs to .ipfs
newpath, err := moveIpfsDir(opts.Path)
if err != nil {
return err
}
if opts.Verbose {
fmt.Println("moved ipfs directory from .go-ipfs to .ipfs")
}
// 4) Update version number
repo = mfsr.RepoPath(newpath)
err = repo.WriteVersion("2")
if err != nil {
return err
}
if opts.Verbose {
fmt.Println("updated version file")
}
// 5) Remove daemon.lock file
if opts.Verbose {
fmt.Println("removing daemon.lock file")
}
repolk.Close()
closedLock = true
lock.Remove1(newpath) // ok if this fails.
return nil
}
func (m Migration) Revert(opts migrate.Options) error {
repolk, err := lock.Lock2(opts.Path) // lock repo.lock
if err != nil {
return err
}
defer repolk.Close()
repo := mfsr.RepoPath(opts.Path)
if err := repo.CheckVersion("2"); err != nil {
return err
}
// 1) Move directory back to .go-ipfs
npath, err := reverseIpfsDir(opts.Path)
if err != nil {
return err
}
if opts.Verbose {
fmt.Println("moved ipfs directory from .ipfs to .go-ipfs")
}
// 2) move blocks back from flatfs to leveldb
err = transferBlocksFromFlatDB(npath, opts.Verbose)
if err != nil {
return err
}
if opts.Verbose {
fmt.Println("moved blocks from flatfs to leveldb")
}
// 3) change version number back down
repo = mfsr.RepoPath(npath)
err = repo.WriteVersion("1")
if err != nil {
return err
}
if opts.Verbose {
fmt.Println("lowered version number to 1")
}
return nil
}
// sanityChecks performs a set of tests to make sure the Migration will go
// smoothly
func sanityChecks(opts migrate.Options) error {
npath := strings.Replace(opts.Path, ".go-ipfs", ".ipfs", 1)
// make sure we can move the repo from .go-ipfs to .ipfs
if npath != opts.Path {
if err := os.Mkdir(npath, 0777); err != nil {
return err
}
// we can? good, remove it now
if err := os.Remove(npath); err != nil {
// this is weird... not worth continuing
return err
}
}
return nil
}
func transferBlocksToFlatDB(repopath string, verbose bool) error {
ldbpath := path.Join(repopath, "datastore")
ldb, err := leveldb.NewDatastore(ldbpath, nil)
if err != nil {
return err
}
blockspath := path.Join(repopath, "blocks")
err = os.Mkdir(blockspath, 0777)
if err != nil && !os.IsExist(err) {
return err
}
fds, err := flatfs.New(blockspath, 4)
if err != nil {
return err
}
return transferBlocks(ldb, fds, "/b/", "", verbose)
}
func transferBlocksFromFlatDB(repopath string, verbose bool) error {
ldbpath := path.Join(repopath, "datastore")
blockspath := path.Join(repopath, "blocks")
fds, err := flatfs.New(blockspath, 4)
if err != nil {
return err
}
ldb, err := leveldb.NewDatastore(ldbpath, nil)
if err != nil {
return err
}
err = transferBlocks(fds, ldb, "", "/b/", verbose)
if err != nil {
return err
}
// Now remove the blocks directory
err = os.RemoveAll(blockspath)
if err != nil {
return err
}
return nil
}
func transferBlocks(from, to dstore.Datastore, fpref, tpref string, verbose bool) error {
q := dsq.Query{Prefix: fpref, KeysOnly: true}
res, err := from.Query(q)
if err != nil {
return err
}
showProgress := func(i int) {}
if verbose {
showProgress = func(i int) {
fmt.Printf("\rmoving objects: %d", i)
}
defer fmt.Println("")
}
i := 0
for result := range res.Next() {
i++
showProgress(i)
nkey := fmt.Sprintf("%s%s", tpref, result.Key[len(fpref):])
fkey := dstore.NewKey(result.Key)
val, err := from.Get(fkey)
if err != nil {
return err
}
err = to.Put(dstore.NewKey(nkey), val)
if err != nil {
return err
}
err = from.Delete(fkey)
if err != nil {
return err
}
}
return nil
}
func moveIpfsDir(curpath string) (string, error) {
newpath := strings.Replace(curpath, ".go-ipfs", ".ipfs", 1)
return newpath, os.Rename(curpath, newpath)
}
func reverseIpfsDir(curpath string) (string, error) {
newpath := strings.Replace(curpath, ".ipfs", ".go-ipfs", 1)
return newpath, os.Rename(curpath, newpath)
}
func loadConfigJSON(repoPath string) (map[string]interface{}, error) {
cfgPath := path.Join(repoPath, "config")
fi, err := os.Open(cfgPath)
if err != nil {
return nil, err
}
var out map[string]interface{}
err = json.NewDecoder(fi).Decode(&out)
if err != nil {
return nil, err
}
return out, nil
}
func saveConfigJSON(repoPath string, cfg map[string]interface{}) error {
cfgPath := path.Join(repoPath, "config")
fi, err := os.Create(cfgPath)
if err != nil {
return err
}
out, err := json.MarshalIndent(cfg, "", "\t")
if err != nil {
return err
}
_, err = fi.Write(out)
if err != nil {
return err
}
return nil
}