Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/content-service/pkg/executor/executor.go
2501 views
1
// Copyright (c) 2020 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 executor
6
7
import (
8
"context"
9
"encoding/json"
10
"io"
11
12
"google.golang.org/protobuf/encoding/protojson"
13
14
csapi "github.com/gitpod-io/gitpod/content-service/api"
15
"github.com/gitpod-io/gitpod/content-service/pkg/initializer"
16
"github.com/gitpod-io/gitpod/content-service/pkg/storage"
17
)
18
19
type config struct {
20
URLs map[string]string `json:"urls,omitempty"`
21
Req json.RawMessage `json:"req,omitempty"`
22
FromBackup string `json:"fromBackupURL,omitempty"`
23
}
24
25
// PrepareFromBackup produces executor config to restore a backup
26
func PrepareFromBackup(url string) ([]byte, error) {
27
return json.Marshal(config{
28
FromBackup: url,
29
})
30
}
31
32
// Prepare writes the config required by Execute to a stream
33
func Prepare(req *csapi.WorkspaceInitializer, urls map[string]string) ([]byte, error) {
34
ilr, err := protojson.Marshal(req)
35
if err != nil {
36
return nil, err
37
}
38
39
return json.Marshal(config{
40
URLs: urls,
41
Req: json.RawMessage(string(ilr)),
42
})
43
}
44
45
// Execute runs an initializer to place content in destination based on the configuration read
46
// from the cfgin stream.
47
func Execute(ctx context.Context, destination string, cfgin io.Reader, forceGitUser bool, opts ...initializer.InitializeOpt) (src csapi.WorkspaceInitSource, err error) {
48
var cfg config
49
err = json.NewDecoder(cfgin).Decode(&cfg)
50
if err != nil {
51
return "", err
52
}
53
54
var (
55
rs storage.DirectDownloader
56
ilr initializer.Initializer
57
)
58
if cfg.FromBackup == "" {
59
var req csapi.WorkspaceInitializer
60
err = protojson.Unmarshal(cfg.Req, &req)
61
if err != nil {
62
return "", err
63
}
64
65
rs = &storage.NamedURLDownloader{URLs: cfg.URLs}
66
ilr, err = initializer.NewFromRequest(ctx, destination, rs, &req, initializer.NewFromRequestOpts{
67
ForceGitpodUserForGit: forceGitUser,
68
})
69
if err != nil {
70
return "", err
71
}
72
} else {
73
rs = &storage.NamedURLDownloader{
74
URLs: map[string]string{
75
storage.DefaultBackup: cfg.FromBackup,
76
},
77
}
78
ilr = &initializer.EmptyInitializer{}
79
}
80
81
src, stats, err := initializer.InitializeWorkspace(ctx, destination, rs, append(opts, initializer.WithInitializer(ilr))...)
82
if err != nil {
83
return "", err
84
}
85
86
err = initializer.PlaceWorkspaceReadyFile(ctx, destination, src, stats, initializer.GitpodUID, initializer.GitpodGID)
87
if err != nil {
88
return src, err
89
}
90
91
return src, nil
92
}
93
94