forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction-etl-p8-global-meta.go
310 lines (280 loc) · 8.88 KB
/
action-etl-p8-global-meta.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
300
301
302
303
304
305
306
307
308
309
310
package actions
import (
"context"
"fmt"
"net/url"
"path"
"strings"
json "github.com/pydio/cells/x/jsonx"
"github.com/pydio/cells/common/forms"
"github.com/pydio/cells/common/etl/stores/pydio8"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
"github.com/micro/go-micro/client"
"github.com/pydio/pydio-sdk-go/config"
"go.uber.org/zap"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/etl"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/activity"
"github.com/pydio/cells/common/proto/idm"
"github.com/pydio/cells/common/proto/jobs"
"github.com/pydio/cells/common/proto/tree"
"github.com/pydio/cells/common/registry"
service "github.com/pydio/cells/common/service/proto"
"github.com/pydio/cells/common/views"
"github.com/pydio/cells/scheduler/actions"
)
type MigrateGlobalMetaAction struct {
remoteUrl *url.URL
remoteUser string
remotePassword string
remoteSkipVerify bool
mapping map[string]string
cellAdmin string
router *views.Router
slugs map[string]string
}
var (
MigrateGlobalMetaName = "actions.etl.p8-global-meta"
)
func (c *MigrateGlobalMetaAction) GetDescription(lang ...string) actions.ActionDescription {
return actions.ActionDescription{
ID: MigrateGlobalMetaName,
IsInternal: true,
Label: "Global meta from P8",
Icon: "",
Description: "Pydio 8 migration specific task (do not use manually).",
Category: actions.ActionCategoryETL,
SummaryTemplate: "",
HasForm: false,
}
}
func (c *MigrateGlobalMetaAction) GetParametersForm() *forms.Form {
return nil
}
// Unique identifier
func (c *MigrateGlobalMetaAction) GetName() string {
return MigrateGlobalMetaName
}
func (c *MigrateGlobalMetaAction) GetRouter() *views.Router {
if c.router == nil {
c.router = views.NewStandardRouter(views.RouterOptions{})
}
return c.router
}
// Pass parameters
func (c *MigrateGlobalMetaAction) Init(job *jobs.Job, cl client.Client, action *jobs.Action) error {
var ok bool
if paramUrl, ok := action.Parameters["url"]; !ok {
return fmt.Errorf("task sync user must take a url parameter")
} else {
var e error
if c.remoteUrl, e = url.Parse(paramUrl); e != nil {
return fmt.Errorf("cannot parse remote url")
}
}
if c.remoteUser, ok = action.Parameters["user"]; !ok {
return fmt.Errorf("task sync user must take a user parameter")
}
if c.remotePassword, ok = action.Parameters["password"]; !ok {
return fmt.Errorf("task sync user must take a password parameter")
}
if skipVerify, ok := action.Parameters["skipVerify"]; ok && skipVerify == "true" {
c.remoteSkipVerify = true
}
if mappingJson, ok := action.Parameters["mapping"]; !ok {
return fmt.Errorf("task sync user must take a mapping parameter")
} else {
if e := json.Unmarshal([]byte(mappingJson), &c.mapping); e != nil {
return fmt.Errorf("task sync cannot parse json parameter: " + e.Error())
}
}
if admin, ok := action.Parameters["cellAdmin"]; ok {
c.cellAdmin = admin
} else {
return fmt.Errorf("please provide a cellAdmin parameter to open all accesses")
}
c.slugs = make(map[string]string)
return nil
}
func (c *MigrateGlobalMetaAction) loadMeta(ctx context.Context, conf *config.SdkConfig, mapping map[string]string, progress chan etl.MergeOperation) error {
v1 := &pydio8.ClientV1{}
data, e := v1.ListP8GlobalMeta(conf)
if e != nil {
return e
}
adminCtx, e := ComputeContextForUser(ctx, c.cellAdmin, nil)
if e != nil {
log.TasksLogger(ctx).Error("Cannot load access list for user", zap.Any("login", c.cellAdmin), zap.Error(e))
return e
}
subClient := activity.NewActivityServiceClient(registry.GetClient(common.ServiceActivity))
metaClient := idm.NewUserMetaServiceClient(registry.GetClient(common.ServiceUserMeta))
log.TasksLogger(ctx).Info("Global Meta", zap.Any("data length", len(data)))
for wsId, users := range data {
slug := c.FindSlug(ctx, wsId)
if slug == "" {
continue
}
for user, nodes := range users {
var userCtx context.Context
if user == pydio8.P8GlobalMetaSharedUser {
userCtx = adminCtx
} else {
// Load User Context
userCtx, e = ComputeContextForUser(ctx, user, nil)
if e != nil {
log.TasksLogger(ctx).Warn("Cannot load access list for user - skipping", zap.Any("login", user), zap.Error(e))
continue
}
}
for nodePath, nodeMetas := range nodes {
// Find Node
reqNode := &tree.Node{Path: path.Join(slug, nodePath)}
r, e := c.GetRouter().ReadNode(userCtx, &tree.ReadNodeRequest{Node: reqNode})
if e != nil {
log.TasksLogger(ctx).Error("Cannot find node", reqNode.ZapPath(), zap.Error(e))
continue
}
for watchUser, watchType := range nodeMetas.Watches {
// Set Watch for user
desc := fmt.Sprintf("Should set watch %s for user %s on node %s", watchType, watchUser, r.Node.Path)
var events []string
if watchType == pydio8.P8GlobalMetaWatchBoth || watchType == pydio8.P8GlobalMetaWatchChange {
events = append(events, "change")
} else if watchType == pydio8.P8GlobalMetaWatchBoth || watchType == pydio8.P8GlobalMetaWatchRead {
events = append(events, "read")
}
_, e := subClient.Subscribe(userCtx, &activity.SubscribeRequest{
Subscription: &activity.Subscription{
UserId: user,
ObjectType: activity.OwnerType_NODE,
ObjectId: r.Node.Uuid,
Events: events,
},
})
if e != nil {
progress <- etl.MergeOperation{Description: desc, Error: e}
} else {
log.TasksLogger(ctx).Info(desc)
progress <- etl.MergeOperation{Description: desc}
}
}
for bmUser, _ := range nodeMetas.Bookmark {
desc := fmt.Sprintf("Should set bookmark for user %s on node %s", bmUser, r.Node.Path)
builder := service.NewResourcePoliciesBuilder()
builder = builder.WithUserWrite(user)
builder = builder.WithUserRead(user)
builder = builder.WithOwner(user)
_, e := metaClient.UpdateUserMeta(userCtx, &idm.UpdateUserMetaRequest{
Operation: idm.UpdateUserMetaRequest_PUT,
MetaDatas: []*idm.UserMeta{{
NodeUuid: r.Node.Uuid,
ResolvedNode: r.Node,
Namespace: "bookmark",
JsonValue: "\"true\"",
Policies: builder.Policies(),
}},
})
if e != nil {
progress <- etl.MergeOperation{Description: desc, Error: e}
} else {
log.TasksLogger(ctx).Info(desc)
progress <- etl.MergeOperation{Description: desc}
}
}
}
}
}
return nil
}
// Run the actual action code
func (c *MigrateGlobalMetaAction) Run(ctx context.Context, channels *actions.RunnableChannels, input jobs.ActionMessage) (jobs.ActionMessage, error) {
channels.StatusMsg <- "[Global Meta] Initializing Migrate Global Meta..."
progress := make(chan etl.MergeOperation)
finished := make(chan bool)
defer close(progress)
defer close(finished)
var pgErrors []error
var messages []string
go func() {
for {
select {
case op := <-progress:
channels.StatusMsg <- op.Description
log.Logger(ctx).Info("[Global Meta]", zap.Any("op", op))
if op.Total > 0 {
channels.Progress <- float32(op.Cursor) / float32(op.Total)
}
if op.Error != nil {
pgErrors = append(pgErrors, op.Error)
} else {
messages = append(messages, op.Description)
}
case <-finished:
return
}
}
}()
conf := &config.SdkConfig{
Protocol: c.remoteUrl.Scheme,
Url: c.remoteUrl.Host,
Path: c.remoteUrl.Path,
User: c.remoteUser,
Password: c.remotePassword,
SkipVerify: c.remoteSkipVerify,
}
e := c.loadMeta(ctx, conf, c.mapping, progress)
finished <- true
if e != nil {
return input.WithError(e), e
}
// Compute message output
output := input
data, _ := json.Marshal(map[string]interface{}{
"msg": messages,
"errors": pgErrors,
})
actionOutput := &jobs.ActionOutput{
Success: len(pgErrors) == 0,
StringBody: "Finished Migrating Global Meta: \n" + strings.Join(messages, ",\n"),
JsonBody: data,
}
var gE error
if len(pgErrors) > 0 {
gE = pgErrors[0]
actionOutput.ErrorString = gE.Error()
}
output.AppendOutput(actionOutput)
log.Logger(ctx).Info("Returning output: ", zap.Any("output", output))
return output, gE
}
func (c *MigrateGlobalMetaAction) FindSlug(ctx context.Context, p8WsId string) string {
if loaded, ok := c.slugs[p8WsId]; ok {
return loaded
}
mapped, ok := c.mapping[p8WsId]
if !ok {
c.slugs[p8WsId] = ""
return ""
}
wsClient := idm.NewWorkspaceServiceClient(registry.GetClient(common.ServiceWorkspace))
q, _ := ptypes.MarshalAny(&idm.WorkspaceSingleQuery{Uuid: mapped})
s, e := wsClient.SearchWorkspace(ctx, &idm.SearchWorkspaceRequest{Query: &service.Query{SubQueries: []*any.Any{q}}})
if e != nil {
c.slugs[p8WsId] = ""
return ""
}
defer s.Close()
for {
r, e := s.Recv()
if e != nil {
break
}
c.slugs[p8WsId] = r.Workspace.GetSlug()
return c.slugs[p8WsId]
}
c.slugs[p8WsId] = ""
return ""
}