Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/content-service/cmd/test.go
2498 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"context"
9
"encoding/json"
10
"fmt"
11
"os"
12
"time"
13
14
"github.com/gitpod-io/gitpod/content-service/api/config"
15
"github.com/gitpod-io/gitpod/content-service/pkg/archive"
16
"github.com/gitpod-io/gitpod/content-service/pkg/storage"
17
"github.com/spf13/cobra"
18
)
19
20
var options testOpts
21
22
type testOpts struct {
23
owner string
24
workspace string
25
instance string
26
config string
27
}
28
29
var testCmd = &cobra.Command{
30
Use: "test",
31
Short: "Test the content service",
32
}
33
34
var directCmd = &cobra.Command{
35
Use: "direct",
36
Short: "direct",
37
}
38
39
var directListCmd = &cobra.Command{
40
Use: "list <prefix>",
41
Short: "list",
42
Args: cobra.ExactArgs(1),
43
Example: "list test-owner/",
44
RunE: func(cmd *cobra.Command, args []string) error {
45
direct, err := createDirectAccess()
46
if err != nil {
47
return err
48
}
49
50
objects, err := direct.ListObjects(context.Background(), args[0])
51
if err != nil {
52
return err
53
}
54
55
for _, o := range objects {
56
fmt.Printf("object: %s\n", o)
57
}
58
59
return nil
60
},
61
}
62
63
var uploadOpts testUploadOpts
64
65
type testUploadOpts struct {
66
path string
67
name string
68
instance bool
69
}
70
71
var directUploadCmd = &cobra.Command{
72
Use: "upload",
73
Short: "upload",
74
Example: "upload --path ./backup.tar --name workspace-backup --to-instance",
75
RunE: func(cmd *cobra.Command, args []string) error {
76
direct, err := createDirectAccess()
77
if err != nil {
78
return err
79
}
80
81
var bucket string
82
var object string
83
84
if uploadOpts.instance {
85
bucket, object, err = direct.UploadInstance(context.Background(), uploadOpts.path, uploadOpts.name)
86
} else {
87
bucket, object, err = direct.Upload(context.Background(), uploadOpts.path, uploadOpts.name)
88
}
89
90
if err != nil {
91
return err
92
}
93
94
fmt.Printf("bucket: %s, object: %s\n", bucket, object)
95
96
return nil
97
},
98
}
99
100
var downloadOpts testDownloadOpts
101
102
type testDownloadOpts struct {
103
destination string
104
name string
105
}
106
107
var directDownloadCmd = &cobra.Command{
108
Use: "download",
109
Short: "download",
110
Example: "download --dest ./download --name workspace-backup",
111
RunE: func(cmd *cobra.Command, args []string) error {
112
direct, err := createDirectAccess()
113
if err != nil {
114
return err
115
}
116
117
_, err = direct.Download(context.Background(), downloadOpts.destination, downloadOpts.name, []archive.IDMapping{})
118
if err != nil {
119
return err
120
}
121
122
return nil
123
},
124
}
125
126
var presignedCmd = &cobra.Command{
127
Use: "presigned",
128
Short: "presigned",
129
}
130
131
var presignedUploadCmd = &cobra.Command{
132
Use: "upload <key>",
133
Short: "upload",
134
Args: cobra.ExactArgs(1),
135
Example: "upload gitpod-signed",
136
RunE: func(cmd *cobra.Command, args []string) error {
137
presigned, err := createPresignedAccess()
138
if err != nil {
139
return err
140
}
141
142
info, err := presigned.SignUpload(context.Background(), "", args[0], &storage.SignedURLOptions{})
143
if err != nil {
144
return err
145
}
146
147
fmt.Printf("%s\n", info.URL)
148
return nil
149
},
150
}
151
152
var presignedDownloadCmd = &cobra.Command{
153
Use: "download <key>",
154
Short: "download",
155
Args: cobra.ExactArgs(1),
156
Example: "download test-owner/workspaces/test-workspace/gitpod-upload",
157
RunE: func(cmd *cobra.Command, args []string) error {
158
presigned, err := createPresignedAccess()
159
if err != nil {
160
return err
161
}
162
163
info, err := presigned.SignDownload(context.Background(), "", args[0], &storage.SignedURLOptions{})
164
if err != nil {
165
return err
166
}
167
168
fmt.Printf("%s\n", info.URL)
169
return nil
170
},
171
}
172
173
var presignedUsageCmd = &cobra.Command{
174
Use: "usage <prefix>",
175
Short: "usage",
176
Args: cobra.ExactArgs(1),
177
Example: "usage test-owner/",
178
RunE: func(cmd *cobra.Command, args []string) error {
179
presigned, err := createPresignedAccess()
180
if err != nil {
181
return err
182
}
183
184
size, err := presigned.DiskUsage(context.Background(), "", args[0])
185
if err != nil {
186
return err
187
}
188
189
fmt.Printf("%v\n", size)
190
191
return nil
192
},
193
}
194
195
var presignedExistsCmd = &cobra.Command{
196
Use: "exists <key>",
197
Short: "exists",
198
Args: cobra.ExactArgs(1),
199
Example: "exists test-owner/workspaces/test-workspace/gitpod-upload",
200
RunE: func(cmd *cobra.Command, args []string) error {
201
presigned, err := createPresignedAccess()
202
if err != nil {
203
return err
204
}
205
206
exists, err := presigned.ObjectExists(context.Background(), "", args[0])
207
if err != nil {
208
return err
209
}
210
fmt.Printf("%v\n", exists)
211
212
return nil
213
},
214
}
215
216
var presignedDeleteCmd = &cobra.Command{
217
Use: "delete",
218
Short: "delete",
219
}
220
221
var presignedDeleteBucketUserId string
222
var presignedDeleteBucketCmd = &cobra.Command{
223
Use: "bucket <bucket>",
224
Short: "bucket",
225
Args: cobra.ExactArgs(1),
226
Example: "delete bucket gitpod-s3",
227
RunE: func(cmd *cobra.Command, args []string) error {
228
presigned, err := createPresignedAccess()
229
if err != nil {
230
return err
231
}
232
233
if err = presigned.DeleteBucket(context.Background(), presignedDeleteBucketUserId, args[0]); err != nil {
234
return err
235
}
236
237
return nil
238
},
239
}
240
241
var presignedDeleteObjectCmd = &cobra.Command{
242
Use: "object <key>",
243
Short: "object",
244
Args: cobra.ExactArgs(1),
245
Example: "delete object test-owner/backup.tar",
246
RunE: func(cmd *cobra.Command, args []string) error {
247
presigned, err := createPresignedAccess()
248
if err != nil {
249
return err
250
}
251
252
if err := presigned.DeleteObject(context.Background(), "", &storage.DeleteObjectQuery{Prefix: args[0]}); err != nil {
253
return err
254
}
255
256
return nil
257
},
258
}
259
260
func createDirectAccess() (direct storage.DirectAccess, err error) {
261
var cfg *config.StorageConfig
262
if options.config != "" {
263
cfg, err = getTestConfig(options.config)
264
if err != nil {
265
return nil, err
266
}
267
268
} else {
269
cfg = &config.StorageConfig{
270
Kind: config.S3Storage,
271
S3Config: &config.S3Config{
272
Bucket: "gitpod-s3",
273
Region: "eu-central-1",
274
CredentialsFile: "./credentials",
275
},
276
}
277
}
278
279
direct, err = storage.NewDirectAccess(cfg)
280
if err != nil {
281
return nil, err
282
}
283
284
if err = direct.Init(context.TODO(), options.owner, options.workspace, options.instance); err != nil {
285
return nil, err
286
}
287
288
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
289
defer cancel()
290
291
if err = direct.EnsureExists(ctx); err != nil {
292
return nil, err
293
}
294
295
return direct, nil
296
}
297
298
func createPresignedAccess() (presigned storage.PresignedAccess, err error) {
299
var cfg *config.StorageConfig
300
if options.config != "" {
301
cfg, err = getTestConfig(options.config)
302
if err != nil {
303
return nil, err
304
}
305
306
} else {
307
cfg = &config.StorageConfig{
308
Kind: config.S3Storage,
309
S3Config: &config.S3Config{
310
Bucket: "gitpod-s3",
311
Region: "eu-central-1",
312
CredentialsFile: "./credentials",
313
},
314
}
315
}
316
317
presigned, err = storage.NewPresignedAccess(cfg)
318
if err != nil {
319
return nil, err
320
}
321
322
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
323
defer cancel()
324
325
if err = presigned.EnsureExists(ctx, cfg.S3Config.Bucket); err != nil {
326
return nil, err
327
}
328
329
return presigned, nil
330
}
331
332
func getTestConfig(path string) (*config.StorageConfig, error) {
333
ctnt, err := os.ReadFile(path)
334
if err != nil {
335
return nil, err
336
}
337
338
var cfg config.StorageConfig
339
err = json.Unmarshal(ctnt, &cfg)
340
if err != nil {
341
return nil, err
342
}
343
344
return &cfg, nil
345
}
346
347
func init() {
348
// direct
349
directCmd.PersistentFlags().StringVar(&options.owner, "owner", "test-owner", "owner")
350
directCmd.PersistentFlags().StringVar(&options.workspace, "workspace", "test-workspace", "workspace")
351
directCmd.PersistentFlags().StringVar(&options.instance, "instance", "test-instance", "instance")
352
353
directUploadCmd.PersistentFlags().StringVar(&uploadOpts.path, "path", "", "path of the file to be uploaded")
354
directUploadCmd.PersistentFlags().StringVar(&uploadOpts.name, "name", "", "name that will be used for the blob in object storage")
355
directUploadCmd.PersistentFlags().BoolVar(&uploadOpts.instance, "to-instance", false, "save to workspace or instance folder")
356
357
directDownloadCmd.PersistentFlags().StringVar(&downloadOpts.destination, "dest", "", "destination of downloaded file")
358
directDownloadCmd.PersistentFlags().StringVar(&downloadOpts.name, "name", "", "name of the blob in S3")
359
360
directCmd.AddCommand(directListCmd)
361
directCmd.AddCommand(directUploadCmd)
362
directCmd.AddCommand(directDownloadCmd)
363
364
// presigned
365
presignedDeleteBucketCmd.PersistentFlags().StringVar(&presignedDeleteBucketUserId, "userId", "test-owner", "userId")
366
presignedDeleteCmd.AddCommand(presignedDeleteBucketCmd)
367
presignedDeleteCmd.AddCommand(presignedDeleteObjectCmd)
368
369
presignedCmd.AddCommand(presignedUploadCmd)
370
presignedCmd.AddCommand(presignedDownloadCmd)
371
presignedCmd.AddCommand(presignedUsageCmd)
372
presignedCmd.AddCommand(presignedExistsCmd)
373
presignedCmd.AddCommand(presignedDeleteCmd)
374
375
// test
376
testCmd.PersistentFlags().StringVar(&options.config, "config", "", "config path")
377
378
testCmd.AddCommand(directCmd)
379
testCmd.AddCommand(presignedCmd)
380
381
rootCmd.AddCommand(testCmd)
382
}
383
384