Repository navigation
Expand file tree
/
Copy pathplugin.go
More file actions
78 lines (64 loc) · 2.21 KB
/
Copy pathplugin.go
File metadata and controls
78 lines (64 loc) · 2.21 KB
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
package plugin
import (
"context"
"io"
"github.com/go-gorp/gorp"
"github.com/rockbears/log"
"github.com/ovh/cds/engine/api/objectstore"
"github.com/ovh/cds/sdk"
)
// AddBinary add binary to the plugin, uploading it to objectsore and updates databases
func AddBinary(ctx context.Context, db gorp.SqlExecutor, storage objectstore.Driver, p *sdk.GRPCPlugin, b *sdk.GRPCPluginBinary, r io.ReadCloser) error {
objectPath, err := storage.Store(b, r)
if err != nil {
return err
}
b.ObjectPath = objectPath
p.Binaries = append(p.Binaries, *b)
return Update(db, p)
}
// UpdateBinary updates binary for the plugin, uploading it to objectsore and updates databases
func UpdateBinary(ctx context.Context, db gorp.SqlExecutor, storageDriver objectstore.Driver, p *sdk.GRPCPlugin, b *sdk.GRPCPluginBinary, r io.ReadCloser) error {
var oldBinary *sdk.GRPCPluginBinary
var index int
for i := range p.Binaries {
if p.Binaries[i].OS == b.OS && p.Binaries[i].Arch == b.Arch {
oldBinary = &p.Binaries[i]
index = i
break
}
}
if oldBinary == nil {
return sdk.WithStack(sdk.ErrUnsupportedOSArchPlugin)
}
if err := storageDriver.Delete(ctx, oldBinary); err != nil {
log.Error(ctx, "UpdateBinary> unable to delete %+v", oldBinary)
}
objectPath, err := storageDriver.Store(b, r)
if err != nil {
return err
}
b.ObjectPath = objectPath
p.Binaries[index] = *b
return Update(db, p)
}
// DeleteBinary remove a binary for the plugin from objectsore and updates databases.
func DeleteBinary(ctx context.Context, db gorp.SqlExecutor, storageDriver objectstore.Driver, p *sdk.GRPCPlugin, os, arch string) error {
var oldBinary *sdk.GRPCPluginBinary
filteredBinaries := make(sdk.GRPCPluginBinaries, 0, len(p.Binaries))
for i := range p.Binaries {
if p.Binaries[i].OS == os && p.Binaries[i].Arch == arch {
oldBinary = &p.Binaries[i]
} else {
filteredBinaries = append(filteredBinaries, p.Binaries[i])
}
}
if oldBinary == nil {
return sdk.WithStack(sdk.ErrUnsupportedOSArchPlugin)
}
if err := storageDriver.Delete(ctx, oldBinary); err != nil {
log.ErrorWithStackTrace(ctx, sdk.WrapError(err, "unable to delete plugin %s binary %s/%s", p.Name, os, arch))
}
p.Binaries = filteredBinaries
return Update(db, p)
}