Path: blob/main/components/content-service/pkg/initializer/snapshot.go
2499 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 initializer56import (7"context"8"time"910"github.com/opentracing/opentracing-go"11"golang.org/x/xerrors"1213"github.com/gitpod-io/gitpod/common-go/log"14"github.com/gitpod-io/gitpod/common-go/tracing"15csapi "github.com/gitpod-io/gitpod/content-service/api"16"github.com/gitpod-io/gitpod/content-service/pkg/archive"17"github.com/gitpod-io/gitpod/content-service/pkg/storage"18)1920// SnapshotInitializer downloads a snapshot from a remote storage21type SnapshotInitializer struct {22Location string23Snapshot string24Storage storage.DirectDownloader25FromVolumeSnapshot bool26}2728// Run downloads a snapshot from a remote storage29func (s *SnapshotInitializer) Run(ctx context.Context, mappings []archive.IDMapping) (src csapi.WorkspaceInitSource, stats csapi.InitializerMetrics, err error) {30//nolint:ineffassign31span, ctx := opentracing.StartSpanFromContext(ctx, "SnapshotInitializer")32span.SetTag("snapshot", s.Snapshot)33defer tracing.FinishSpan(span, &err)34start := time.Now()35initialSize, fsErr := getFsUsage()36if fsErr != nil {37log.WithError(fsErr).Error("could not get disk usage")38}3940src = csapi.WorkspaceInitFromBackup4142if s.FromVolumeSnapshot {43log.Info("SnapshotInitializer detected volume snapshot, skipping")44return src, nil, nil45}4647ok, err := s.Storage.DownloadSnapshot(ctx, s.Location, s.Snapshot, mappings)48if err != nil {49return src, nil, xerrors.Errorf("snapshot initializer: %w", err)50}51if !ok {52return src, nil, xerrors.Errorf("did not find snapshot %s", s.Snapshot)53}5455if fsErr == nil {56currentSize, fsErr := getFsUsage()57if fsErr != nil {58log.WithError(fsErr).Error("could not get disk usage")59}6061stats = csapi.InitializerMetrics{csapi.InitializerMetric{62Type: "snapshot",63Duration: time.Since(start),64Size: currentSize - initialSize,65}}66}6768return69}707172