-
Notifications
You must be signed in to change notification settings - Fork 43
/
mirror.go
183 lines (154 loc) · 4.81 KB
/
mirror.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
package sync
import (
"context"
"fmt"
"github.com/FleekHQ/space-daemon/config"
"github.com/FleekHQ/space-daemon/core/space/domain"
"github.com/FleekHQ/space-daemon/core/textile/model"
"github.com/FleekHQ/space-daemon/core/textile/utils"
"github.com/FleekHQ/space-daemon/log"
"github.com/textileio/go-threads/core/thread"
"github.com/textileio/go-threads/db"
bucketsClient "github.com/textileio/textile/v2/api/bucketsd/client"
api_buckets_pb "github.com/textileio/textile/v2/api/bucketsd/pb"
"github.com/textileio/textile/v2/buckets"
)
const mirrorThreadKeyName = "mirrorV1"
func (s *synchronizer) setMirrorFileBackup(ctx context.Context, path, bucketSlug string, isInProgress bool) error {
mf, err := s.model.FindMirrorFileByPathAndBucketSlug(ctx, path, bucketSlug)
if err != nil {
return err
}
if mf != nil {
// update
mf.Backup = !isInProgress
mf.BackupInProgress = isInProgress
_, err = s.model.UpdateMirrorFile(ctx, mf)
if err != nil {
return err
}
} else {
// create
mf := &domain.MirrorFile{
Path: path,
BucketSlug: bucketSlug,
Backup: !isInProgress,
BackupInProgress: isInProgress,
Shared: false,
}
_, err := s.model.CreateMirrorFile(ctx, mf)
if err != nil {
return err
}
}
return nil
}
// unset mirror file as backup
func (s *synchronizer) unsetMirrorFileBackup(ctx context.Context, path, bucketSlug string) error {
mf, err := s.model.FindMirrorFileByPathAndBucketSlug(ctx, path, bucketSlug)
if err != nil {
return err
}
if mf == nil {
log.Warn(fmt.Sprintf("mirror file (path=%+v bucketSlug=%+v) does not exist", path, bucketSlug))
return nil
}
// do not delete the instance because it might be shared
mf.Backup = false
mf.BackupInProgress = false
if _, err = s.model.UpdateMirrorFile(ctx, mf); err != nil {
return err
}
return nil
}
func (s *synchronizer) addCurrentUserAsFileOwner(ctx context.Context, bucket, path string) error {
bucketModel, err := s.model.FindBucket(ctx, bucket)
if err != nil {
return err
}
roles := make(map[string]buckets.Role)
pk, err := s.kc.GetStoredPublicKey()
if err != nil {
return err
}
tpk := thread.NewLibp2pPubKey(pk)
roles[tpk.String()] = buckets.Admin
mirror, err := s.getMirrorBucket(ctx, bucket)
if err != nil {
return err
}
bucketsClient := mirror.GetClient()
bucketCtx, _, err := s.getBucketCtx(ctx, bucketModel.RemoteDbID, bucketModel.RemoteBucketSlug, true, bucketModel.EncryptionKey)
if err != nil {
return err
}
return bucketsClient.PushPathAccessRoles(bucketCtx, mirror.GetData().Key, path, roles)
}
// Creates a mirror bucket.
func (s *synchronizer) createMirrorBucket(ctx context.Context, slug string, enckey []byte) (*model.MirrorBucketSchema, error) {
newSlug := slug + "_mirror"
log.Debug("Creating a new mirror bucket with slug " + newSlug)
dbID, err := s.createMirrorThread(ctx, newSlug)
if err != nil {
return nil, err
}
hubCtx, _, err := s.getBucketCtx(ctx, utils.CastDbIDToString(*dbID), newSlug, true, enckey)
if err != nil {
return nil, err
}
existingBuckets, err := s.hubBuckets.List(hubCtx)
if err != nil {
return nil, err
}
var root *api_buckets_pb.Root
for _, b := range existingBuckets.Roots {
if b.Name == newSlug {
log.Debug("Mirror bucket with slug " + newSlug + " already exists")
root = b
break
}
}
if root == nil {
createResp, err := s.hubBuckets.Create(hubCtx, bucketsClient.WithName(newSlug), bucketsClient.WithPrivate(true))
if err != nil {
return nil, err
}
root = createResp.Root
}
return &model.MirrorBucketSchema{
RemoteDbID: utils.CastDbIDToString(*dbID),
RemoteBucketKey: root.Key,
RemoteBucketSlug: newSlug,
HubAddr: s.cfg.GetString(config.TextileHubTarget, ""),
}, nil
}
// Creates a remote hub thread for the mirror bucket
func (s *synchronizer) createMirrorThread(ctx context.Context, slug string) (*thread.ID, error) {
log.Debug("createMirrorThread: Generating a new threadID ...")
var err error
ctx, err = s.hubAuth.GetHubContext(ctx)
if err != nil {
return nil, err
}
dbID, err := utils.NewDeterministicThreadID(s.kc, utils.MirrorBucketVariantGen(slug))
if err != nil {
return nil, err
}
managedKey, err := s.kc.GetManagedThreadKey(mirrorThreadKeyName + "_" + slug)
if err != nil {
log.Error("error getting managed thread key", err)
return nil, err
}
// If dbID is not found, GetDBInfo returns "thread not found" error
info, err := s.hubThreads.GetDBInfo(ctx, dbID)
if err == nil {
log.Debug("createMirrorThread: Db already exists with name " + info.Name)
return &dbID, nil
}
log.Debug("createMirrorThread: Creating Thread DB for bucket at db " + dbID.String())
if err := s.hubThreads.NewDB(ctx, dbID, db.WithNewManagedThreadKey(managedKey)); err != nil {
return nil, err
}
log.Debug("createMirrorThread: Thread DB Created")
return &dbID, nil
}