-
Notifications
You must be signed in to change notification settings - Fork 34
/
ensure_plugins.go
368 lines (319 loc) · 9.33 KB
/
ensure_plugins.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package runtime
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/google/go-github/github"
"github.com/pkg/errors"
"github.com/Southclaws/sampctl/download"
"github.com/Southclaws/sampctl/pawnpackage"
"github.com/Southclaws/sampctl/print"
"github.com/Southclaws/sampctl/resource"
"github.com/Southclaws/sampctl/run"
"github.com/Southclaws/sampctl/util"
"github.com/Southclaws/sampctl/versioning"
)
// EnsurePlugins validates and downloads plugin binary files
func EnsurePlugins(
ctx context.Context,
gh *github.Client,
cfg *run.Runtime,
cacheDir string,
noCache bool,
) (err error) {
fileExt := pluginExtForFile(cfg.Platform)
var (
newPlugins = []run.Plugin{}
files []run.Plugin
)
for _, plugin := range cfg.PluginDeps {
files, err = EnsureVersionedPlugin(ctx, gh, plugin, cfg.WorkingDir, cfg.Platform, cfg.Version, cacheDir, true, false, noCache)
if err != nil {
print.Warn("failed to ensure plugin", plugin, err)
err = nil
continue
}
print.Verb("adding dependency", plugin, "files", files, "to plugins list")
newPlugins = append(newPlugins, files...)
}
added := make(map[run.Plugin]struct{})
// trim extensions for plugins list, they are added later by GenerateServerCFG if needed
for _, plugin := range newPlugins {
pluginName := run.Plugin(strings.TrimSuffix(string(plugin), fileExt))
if _, ok := added[pluginName]; ok {
continue
}
print.Verb("adding plugin by local filename", pluginName)
cfg.Plugins = append(cfg.Plugins, pluginName)
added[pluginName] = struct{}{}
}
if len(added) != 0 {
pluginsDir := util.FullPath(filepath.Join(cfg.WorkingDir, "plugins"))
err = os.MkdirAll(pluginsDir, 0700)
if err != nil {
return errors.Wrap(err, "failed to create runtime plugins directory")
}
}
return err
}
// EnsureVersionedPlugin automatically downloads a plugin binary from its github releases page
func EnsureVersionedPlugin(
ctx context.Context,
gh *github.Client,
meta versioning.DependencyMeta,
dir string,
platform string,
version string,
cacheDir string,
plugins bool,
includes bool,
noCache bool,
) (files []run.Plugin, err error) {
filename, resource, err := EnsureVersionedPluginCached(ctx, meta, platform, version, cacheDir, noCache, gh)
if err != nil {
return
}
print.Verb(meta, "retrieved package to file:", filename)
if resource.Archive {
print.Verb(meta, "plugin resource is an archive")
var (
ext = filepath.Ext(filename)
method download.ExtractFunc
)
if ext == ".zip" {
method = download.Unzip
} else if ext == ".gz" {
method = download.Untar
} else {
err = errors.Errorf("unsupported archive format: %s", filename)
return
}
paths := make(map[string]string)
// get plugins
if plugins {
for _, plugin := range resource.Plugins {
print.Verb(meta, "marking plugin path", plugin, "for extraction to ./plugins/")
paths[plugin] = "plugins/"
}
}
// get include directories
if includes {
for _, include := range resource.Includes {
print.Verb(meta, "marking include path", include, "for extraction")
paths[include] = ""
}
}
// get additional files
for src, dest := range resource.Files {
print.Verb(meta, "marking misc file path", src, "for extraction to", dest)
paths[src] = dest
}
var extractedFiles map[string]string
extractedFiles, err = method(filename, dir, paths)
if err != nil {
err = errors.Wrapf(err, "failed to extract plugin %s to %s", meta, dir)
return
}
if len(extractedFiles) == 0 {
//nolint:lll
err = errors.Errorf("no files extracted from plugin %s: check the package definition of this dependency against the release assets", meta)
return
}
print.Verb(meta, "extracted", len(extractedFiles), "plugin files to", dir)
for source, target := range extractedFiles {
for _, plugin := range resource.Plugins {
print.Verb(meta, "checking resource source", source, "against plugin", plugin)
if source == plugin {
files = append(files, run.Plugin(filepath.Base(target)))
}
}
}
} else {
print.Verb(meta, "plugin resource is a single file")
base := filepath.Base(filename)
finalDir := filepath.Join(dir, "plugins")
destination := filepath.Join(finalDir, base)
err = os.MkdirAll(finalDir, 0700)
if err != nil {
err = errors.Wrapf(err, "failed to create path for plugin resource %s to %s", filename, destination)
return
}
err = util.CopyFile(filename, destination)
if err != nil {
err = errors.Wrapf(err, "failed to copy non-archive file %s to %s", filename, destination)
return
}
files = []run.Plugin{run.Plugin(base)}
}
return files, err
}
// EnsureVersionedPluginCached ensures that a plugin exists in the cache
func EnsureVersionedPluginCached(
ctx context.Context,
meta versioning.DependencyMeta,
platform,
version,
cacheDir string,
noCache bool,
gh *github.Client,
) (
filename string,
resource *resource.Resource,
err error,
) {
hit := false
// only pull from cache if there is a version tag specified
if !noCache && meta.Tag != "" {
hit, filename, resource, err = PluginFromCache(meta, platform, version, cacheDir)
if err != nil {
err = errors.Wrapf(err, "failed to get plugin %s from cache", meta)
return
}
}
if !hit {
if meta.Tag == "" {
//nolint:lll
print.Info("Downloading newest plugin because no version is specified. Consider specifying a version for this dependency.")
}
filename, resource, err = PluginFromNet(ctx, gh, meta, platform, version, cacheDir)
if err != nil {
err = errors.Wrapf(err, "failed to get plugin %s from net", meta)
return
}
}
return filename, resource, nil
}
// PluginFromCache tries to grab a plugin asset from the cache, `hit` indicates if it was successful
func PluginFromCache(
meta versioning.DependencyMeta,
platform string,
version string,
cacheDir string,
) (hit bool, filename string, resource *resource.Resource, err error) {
resourcePath := filepath.Join(cacheDir, GetResourcePath(meta))
print.Verb("getting plugin resource from cache", meta, resourcePath)
pkg, err := pawnpackage.GetCachedPackage(meta, cacheDir)
if err != nil {
print.Verb("cache hit failed while trying to get cached package:", err)
err = nil
hit = false
return
}
if pkg.Format == "" {
return
}
files, err := ioutil.ReadDir(resourcePath)
if err != nil {
print.Verb("cache hit failed while trying to read cached plugin folder:", err)
err = nil
hit = false
return
}
resource, err = GetResource(pkg.Resources, platform, version)
if err != nil {
return
}
matcher, err := regexp.Compile(resource.Name)
if err != nil {
err = errors.Wrap(err, "resource name is not a valid regular expression")
return
}
found := false
name := ""
for _, file := range files {
name = file.Name()
if matcher.MatchString(name) {
found = true
break
}
}
if !found {
return
}
hit = true
filename = filepath.Join(resourcePath, name)
return hit, filename, resource, nil
}
// PluginFromNet downloads a plugin from the given metadata to the cache directory
func PluginFromNet(
ctx context.Context,
gh *github.Client,
meta versioning.DependencyMeta,
platform string,
version string,
cacheDir string,
) (filename string, resource *resource.Resource, err error) {
print.Info(meta, "downloading plugin resource for", platform)
resourcePathOnly := GetResourcePath(meta)
resourcePath := filepath.Join(cacheDir, resourcePathOnly)
err = os.MkdirAll(resourcePath, 0700)
if err != nil {
err = errors.Wrap(err, "failed to create cache directory for package resources")
return
}
pkg, err := pawnpackage.GetRemotePackage(ctx, gh, meta)
if err != nil {
err = errors.Wrap(err, "failed to get remote package definition file")
return
}
resource, err = GetResource(pkg.Resources, platform, version)
if err != nil {
return
}
matcher, err := regexp.Compile(resource.Name)
if err != nil {
err = errors.Wrap(err, "resource name is not a valid regular expression")
return
}
filename, _, err = download.ReleaseAssetByPattern(ctx, gh, meta, matcher, resourcePathOnly, "", cacheDir)
if err != nil {
return
}
print.Verb(meta, "downloaded", filename, "to cache")
return filename, resource, nil
}
// GetResource searches a list of resources for one that matches the given platform
func GetResource(resources []resource.Resource, platform string, version string) (*resource.Resource, error) {
if version == "" {
version = "0.3.7"
}
found := false
var tmp *resource.Resource
for _, res := range resources {
if res.Platform == platform {
if res.Version == version {
tmp = &res
found = true
break
}
}
}
if !found {
for _, res := range resources {
if res.Platform == platform && res.Version == "" {
print.Verb("no resource matching version: ", version, ", falling back to the first resource matching platform: ", platform)
tmp = &res
found = true
break;
}
}
}
if !found {
return nil, errors.Errorf("plugin does not provide binaries for target platform %s and/or version %s", platform, version)
}
if err := tmp.Validate(); err != nil {
return nil, errors.Wrap(err, "matching resource found but is invalid")
}
return tmp, nil
}
// GetResourcePath returns a path where a resource should be stored given the metadata
func GetResourcePath(meta versioning.DependencyMeta) (path string) {
tag := meta.Tag
if tag == "" {
tag = "latest"
}
return filepath.Join("plugins", meta.Repo, tag)
}