Path: blob/main/components/content-service/pkg/executor/executor.go
2501 views
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package executor56import (7"context"8"encoding/json"9"io"1011"google.golang.org/protobuf/encoding/protojson"1213csapi "github.com/gitpod-io/gitpod/content-service/api"14"github.com/gitpod-io/gitpod/content-service/pkg/initializer"15"github.com/gitpod-io/gitpod/content-service/pkg/storage"16)1718type config struct {19URLs map[string]string `json:"urls,omitempty"`20Req json.RawMessage `json:"req,omitempty"`21FromBackup string `json:"fromBackupURL,omitempty"`22}2324// PrepareFromBackup produces executor config to restore a backup25func PrepareFromBackup(url string) ([]byte, error) {26return json.Marshal(config{27FromBackup: url,28})29}3031// Prepare writes the config required by Execute to a stream32func Prepare(req *csapi.WorkspaceInitializer, urls map[string]string) ([]byte, error) {33ilr, err := protojson.Marshal(req)34if err != nil {35return nil, err36}3738return json.Marshal(config{39URLs: urls,40Req: json.RawMessage(string(ilr)),41})42}4344// Execute runs an initializer to place content in destination based on the configuration read45// from the cfgin stream.46func Execute(ctx context.Context, destination string, cfgin io.Reader, forceGitUser bool, opts ...initializer.InitializeOpt) (src csapi.WorkspaceInitSource, err error) {47var cfg config48err = json.NewDecoder(cfgin).Decode(&cfg)49if err != nil {50return "", err51}5253var (54rs storage.DirectDownloader55ilr initializer.Initializer56)57if cfg.FromBackup == "" {58var req csapi.WorkspaceInitializer59err = protojson.Unmarshal(cfg.Req, &req)60if err != nil {61return "", err62}6364rs = &storage.NamedURLDownloader{URLs: cfg.URLs}65ilr, err = initializer.NewFromRequest(ctx, destination, rs, &req, initializer.NewFromRequestOpts{66ForceGitpodUserForGit: forceGitUser,67})68if err != nil {69return "", err70}71} else {72rs = &storage.NamedURLDownloader{73URLs: map[string]string{74storage.DefaultBackup: cfg.FromBackup,75},76}77ilr = &initializer.EmptyInitializer{}78}7980src, stats, err := initializer.InitializeWorkspace(ctx, destination, rs, append(opts, initializer.WithInitializer(ilr))...)81if err != nil {82return "", err83}8485err = initializer.PlaceWorkspaceReadyFile(ctx, destination, src, stats, initializer.GitpodUID, initializer.GitpodGID)86if err != nil {87return src, err88}8990return src, nil91}929394