Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/content-service/pkg/initializer/snapshot.go
2499 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 initializer
6
7
import (
8
"context"
9
"time"
10
11
"github.com/opentracing/opentracing-go"
12
"golang.org/x/xerrors"
13
14
"github.com/gitpod-io/gitpod/common-go/log"
15
"github.com/gitpod-io/gitpod/common-go/tracing"
16
csapi "github.com/gitpod-io/gitpod/content-service/api"
17
"github.com/gitpod-io/gitpod/content-service/pkg/archive"
18
"github.com/gitpod-io/gitpod/content-service/pkg/storage"
19
)
20
21
// SnapshotInitializer downloads a snapshot from a remote storage
22
type SnapshotInitializer struct {
23
Location string
24
Snapshot string
25
Storage storage.DirectDownloader
26
FromVolumeSnapshot bool
27
}
28
29
// Run downloads a snapshot from a remote storage
30
func (s *SnapshotInitializer) Run(ctx context.Context, mappings []archive.IDMapping) (src csapi.WorkspaceInitSource, stats csapi.InitializerMetrics, err error) {
31
//nolint:ineffassign
32
span, ctx := opentracing.StartSpanFromContext(ctx, "SnapshotInitializer")
33
span.SetTag("snapshot", s.Snapshot)
34
defer tracing.FinishSpan(span, &err)
35
start := time.Now()
36
initialSize, fsErr := getFsUsage()
37
if fsErr != nil {
38
log.WithError(fsErr).Error("could not get disk usage")
39
}
40
41
src = csapi.WorkspaceInitFromBackup
42
43
if s.FromVolumeSnapshot {
44
log.Info("SnapshotInitializer detected volume snapshot, skipping")
45
return src, nil, nil
46
}
47
48
ok, err := s.Storage.DownloadSnapshot(ctx, s.Location, s.Snapshot, mappings)
49
if err != nil {
50
return src, nil, xerrors.Errorf("snapshot initializer: %w", err)
51
}
52
if !ok {
53
return src, nil, xerrors.Errorf("did not find snapshot %s", s.Snapshot)
54
}
55
56
if fsErr == nil {
57
currentSize, fsErr := getFsUsage()
58
if fsErr != nil {
59
log.WithError(fsErr).Error("could not get disk usage")
60
}
61
62
stats = csapi.InitializerMetrics{csapi.InitializerMetric{
63
Type: "snapshot",
64
Duration: time.Since(start),
65
Size: currentSize - initialSize,
66
}}
67
}
68
69
return
70
}
71
72