Skip to content

Commit 83e37af

Browse files
committed
Merge pull request src-d#149 from camathieu/1.2-rc1
migration script from file to bolt metadata backend
2 parents 5ac5d09 + 1187594 commit 83e37af

File tree

5 files changed

+133
-13
lines changed

5 files changed

+133
-13
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ This backend has been deprecated in Plik 1.2 in favor of BoltDB backend.
175175
The authentication mechanisms ( User / Tokens ) are NOT implemented in this backend.
176176
Migration from file backend to BoltDB backend can be done using the migrate_from_file_to_bolt script.
177177

178-
go build server/migrate_from_file_to_bolt.go
178+
```
179+
server/utils/file2bolt --directory server/files --db server/plik.db
180+
```
179181

180182
This backend save upload metadata as JSON in a .config file in the upload directory.
181183
This is only suitable for a single instance deployment as locking append at the process level.

server/common/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,9 @@ func LoadConfiguration(file string) {
164164
Config.Authentication = false
165165
}
166166

167+
if Config.MetadataBackend == "file" {
168+
Config.Authentication = false
169+
}
170+
167171
Logger().Dump(logger.DEBUG, Config)
168172
}

server/metadataBackend/bolt/bolt.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,9 @@ func NewBoltMetadataBackend(config map[string]interface{}) (bmb *MetadataBackend
6969
return fmt.Errorf("Unable to create metadata bucket : %s", err)
7070
}
7171

72-
if common.Config.Authentication {
73-
_, err := tx.CreateBucketIfNotExists([]byte("users"))
74-
if err != nil {
75-
return fmt.Errorf("Unable to create user bucket : %s", err)
76-
}
72+
_, err = tx.CreateBucketIfNotExists([]byte("users"))
73+
if err != nil {
74+
return fmt.Errorf("Unable to create user bucket : %s", err)
7775
}
7876

7977
return nil
@@ -161,7 +159,7 @@ func (bmb *MetadataBackend) Create(ctx *juliet.Context, upload *common.Upload) (
161159
return
162160
}
163161

164-
log.Infof("Metadata file successfully saved")
162+
log.Infof("Upload metadata successfully saved")
165163
return
166164
}
167165

@@ -252,7 +250,7 @@ func (bmb *MetadataBackend) AddOrUpdateFile(ctx *juliet.Context, upload *common.
252250
return
253251
}
254252

255-
log.Infof("Metadata file successfully updated")
253+
log.Infof("Upload metadata successfully updated")
256254
return
257255
}
258256

@@ -311,7 +309,7 @@ func (bmb *MetadataBackend) RemoveFile(ctx *juliet.Context, upload *common.Uploa
311309
return
312310
}
313311

314-
log.Infof("Metadata successfully updated")
312+
log.Infof("Upload metadata successfully updated")
315313
return nil
316314
}
317315

@@ -379,7 +377,7 @@ func (bmb *MetadataBackend) Remove(ctx *juliet.Context, upload *common.Upload) (
379377
return
380378
}
381379

382-
log.Infof("Metadata successfully removed")
380+
log.Infof("Upload metadata successfully removed")
383381
return
384382
}
385383

@@ -452,6 +450,9 @@ func (bmb *MetadataBackend) SaveUser(ctx *juliet.Context, user *common.User) (er
452450
if err != nil {
453451
return
454452
}
453+
454+
log.Infof("User successfully saved")
455+
455456
return
456457
}
457458

@@ -534,6 +535,8 @@ func (bmb *MetadataBackend) RemoveUser(ctx *juliet.Context, user *common.User) (
534535
return
535536
}
536537

538+
log.Infof("User successfully removed")
539+
537540
return
538541
}
539542

server/metadataBackend/file/config.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ import (
3535

3636
// MetadataBackendConfig object
3737
type MetadataBackendConfig struct {
38-
Directory string
39-
TokenDirectory string
38+
Directory string
4039
}
4140

4241
// NewFileMetadataBackendConfig configures the backend
@@ -48,7 +47,6 @@ func NewFileMetadataBackendConfig(config map[string]interface{}) (mbc *MetadataB
4847
// data backend so by default files and
4948
// metadata are colocated
5049
mbc.Directory = "files"
51-
mbc.TokenDirectory = "tokens"
5250
utils.Assign(mbc, config)
5351
return
5452
}

server/utils/file2bolt.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
3+
Plik upload server
4+
5+
The MIT License (MIT)
6+
7+
Copyright (c) <2015>
8+
- Mathieu Bodjikian <[email protected]>
9+
- Charles-Antoine Mathieu <[email protected]>
10+
11+
Permission is hereby granted, free of charge, to any person obtaining a copy
12+
of this software and associated documentation files (the "Software"), to deal
13+
in the Software without restriction, including without limitation the rights
14+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15+
copies of the Software, and to permit persons to whom the Software is
16+
furnished to do so, subject to the following conditions:
17+
18+
The above copyright notice and this permission notice shall be included in
19+
all copies or substantial portions of the Software.
20+
21+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27+
THE SOFTWARE.
28+
**/
29+
30+
package main
31+
32+
import (
33+
"flag"
34+
"fmt"
35+
"io/ioutil"
36+
"os"
37+
38+
"github.com/root-gg/plik/server/Godeps/_workspace/src/github.com/root-gg/juliet"
39+
"github.com/root-gg/plik/server/metadataBackend/bolt"
40+
"github.com/root-gg/plik/server/metadataBackend/file"
41+
)
42+
43+
// This script migrate upload metadata from file backend to Bolt backend
44+
//
45+
// go run file2bolt.go --directory ../files --db ../plik.db
46+
// [02/01/2016 22:00:48][INFO][bolt.go:164 Create] Upload metadata successfully saved
47+
// [02/01/2016 22:00:48][INFO][bolt.go:164 Create] Upload metadata successfully saved
48+
// 2 upload imported
49+
//
50+
// Some .config "no such file or directory" errors are normal if you already switched to Bolt metadata backend
51+
// while using the file data backend as it will create upload directories but not .config files.
52+
53+
func main() {
54+
// Parse command line arguments
55+
var directoryPath = flag.String("directory", "../files", "File metadatabackend base path")
56+
var dbPath = flag.String("db", "../plik.db", "Bold db path")
57+
flag.Parse()
58+
59+
if *directoryPath == "" || *dbPath == "" {
60+
fmt.Println("usage : file2bolt --directory path --db path")
61+
os.Exit(1)
62+
}
63+
64+
// Initialize File metadata backend
65+
fileConfig := map[string]interface{}{"Directory": *directoryPath}
66+
fmb := file.NewFileMetadataBackend(fileConfig)
67+
68+
// Initialize Bolt metadata backend
69+
boltConfig := map[string]interface{}{"Path": *dbPath}
70+
bmb := bolt.NewBoltMetadataBackend(boltConfig)
71+
72+
counter := 0
73+
74+
// upload ids are the name of the second level of directories of the file metadata backend
75+
dirs1, err := ioutil.ReadDir(*directoryPath)
76+
if err != nil {
77+
fmt.Printf("Unable to open directory %s : %s\n", *directoryPath, err)
78+
os.Exit(1)
79+
}
80+
for _, dir1 := range dirs1 {
81+
if dir1.IsDir() {
82+
path := *directoryPath + "/" + dir1.Name()
83+
dirs2, err := ioutil.ReadDir(path)
84+
if err != nil {
85+
fmt.Printf("Unable to open directory %s : %s\n", path, err)
86+
os.Exit(1)
87+
}
88+
for _, dir2 := range dirs2 {
89+
if dir2.IsDir() {
90+
uploadID := dir2.Name()
91+
92+
// Load upload from file metadata backend
93+
upload, err := fmb.Get(juliet.NewContext(), uploadID)
94+
if err != nil {
95+
fmt.Printf("Unable to load upload %s : %s\n", uploadID, err)
96+
continue
97+
}
98+
99+
// Save upload to bolt metadata backend
100+
err = bmb.Create(juliet.NewContext(), upload)
101+
if err != nil {
102+
fmt.Printf("Unable to save upload %s : %s\n", uploadID, err)
103+
continue
104+
}
105+
106+
counter++
107+
}
108+
}
109+
}
110+
}
111+
112+
fmt.Printf("%d upload imported\n", counter)
113+
}

0 commit comments

Comments
 (0)