-
Notifications
You must be signed in to change notification settings - Fork 28
/
snapshot_helper.go
167 lines (148 loc) · 5.01 KB
/
snapshot_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package tasks
import (
"context"
"fmt"
"path/filepath"
"github.com/content-services/content-sources-backend/pkg/api"
"github.com/content-services/content-sources-backend/pkg/dao"
"github.com/content-services/content-sources-backend/pkg/models"
"github.com/content-services/content-sources-backend/pkg/pulp_client"
"github.com/content-services/content-sources-backend/pkg/tasks/helpers"
"github.com/google/uuid"
"github.com/rs/zerolog"
)
// ResumableSnapshotInterface used to store various references needed
//
// for snapshotHelper to be resumable. Typically implemented by the task using the helper
type ResumableSnapshotInterface interface {
SavePublicationTaskHref(href string) error
GetPublicationTaskHref() *string
SaveDistributionTaskHref(href string) error
GetDistributionTaskHref() *string
SaveSnapshotIdent(id string) error
GetSnapshotIdent() *string
}
// SnapshotHelper is meant to be used by another task, and be able to turn a repository Version into a
//
// snapshot, with everything needed in pulp (publication, distributions)
type SnapshotHelper struct {
pulpClient pulp_client.PulpClient
ctx context.Context
payload ResumableSnapshotInterface
logger *zerolog.Logger
orgId string
repo api.RepositoryResponse
daoReg *dao.DaoRegistry
domainName string
}
func (sh *SnapshotHelper) Run(versionHref string) error {
publicationHref, err := sh.findOrCreatePublication(versionHref)
if err != nil {
return err
}
if sh.payload.GetSnapshotIdent() == nil {
err = sh.payload.SaveSnapshotIdent(uuid.NewString())
if err != nil {
return fmt.Errorf("unable to save snapshot ident: %w", err)
}
}
distPath := fmt.Sprintf("%v/%v", sh.repo.UUID, *sh.payload.GetSnapshotIdent())
helper := helpers.NewPulpDistributionHelper(sh.ctx, sh.pulpClient)
distHref, addedContentGuard, err := helper.CreateOrUpdateDistribution(sh.orgId, publicationHref, *sh.payload.GetSnapshotIdent(), distPath)
if err != nil {
return err
}
latestPathIdent := helpers.GetLatestRepoDistPath(sh.repo.UUID)
_, _, err = helper.CreateOrUpdateDistribution(sh.orgId, publicationHref, sh.repo.UUID, latestPathIdent)
if err != nil {
return err
}
version, err := sh.pulpClient.GetRpmRepositoryVersion(sh.ctx, versionHref)
if err != nil {
return err
}
if version.ContentSummary == nil {
sh.logger.Error().Msgf("Found nil content Summary for version %v", versionHref)
}
current, added, removed := ContentSummaryToContentCounts(version.ContentSummary)
snap := models.Snapshot{
VersionHref: versionHref,
PublicationHref: publicationHref,
DistributionPath: distPath,
RepositoryPath: filepath.Join(sh.domainName, distPath),
DistributionHref: distHref,
RepositoryConfigurationUUID: sh.repo.UUID,
ContentCounts: current,
AddedCounts: added,
RemovedCounts: removed,
ContentGuardAdded: addedContentGuard,
}
sh.logger.Debug().Msgf("Snapshot created at: %v", distPath)
err = sh.daoReg.Snapshot.Create(sh.ctx, &snap)
if err != nil {
return err
}
return nil
}
func (sh *SnapshotHelper) Cleanup() error {
if sh.payload.GetDistributionTaskHref() != nil {
task, err := sh.pulpClient.CancelTask(sh.ctx, *sh.payload.GetDistributionTaskHref())
if err != nil {
return err
}
task, err = sh.pulpClient.GetTask(sh.ctx, *sh.payload.GetDistributionTaskHref())
if err != nil {
return err
}
versionHref := pulp_client.SelectRpmDistributionHref(&task)
if versionHref != nil {
_, err = sh.pulpClient.DeleteRpmDistribution(sh.ctx, *versionHref)
if err != nil {
return err
}
}
}
if sh.payload.GetSnapshotIdent() != nil {
err := sh.daoReg.Snapshot.Delete(sh.ctx, *sh.payload.GetSnapshotIdent())
if err != nil {
return err
}
}
return nil
}
func (sh *SnapshotHelper) findOrCreatePublication(versionHref string) (string, error) {
var publicationHref *string
// Publication
publication, err := sh.pulpClient.FindRpmPublicationByVersion(sh.ctx, versionHref)
if err != nil {
return "", err
}
if publication == nil || publication.PulpHref == nil {
if sh.payload.GetPublicationTaskHref() == nil {
publicationTaskHref, err := sh.pulpClient.CreateRpmPublication(sh.ctx, versionHref)
if err != nil {
return "", err
}
if publicationTaskHref == nil {
return "", fmt.Errorf("publicationTaskHref cannot be nil")
}
err = sh.payload.SavePublicationTaskHref(*publicationTaskHref)
if err != nil {
return "", err
}
} else {
sh.logger.Debug().Str("pulp_task_id", *sh.payload.GetPublicationTaskHref()).Msg("Resuming Publication task")
}
publicationTask, err := sh.pulpClient.PollTask(sh.ctx, *sh.payload.GetPublicationTaskHref())
if err != nil {
return "", err
}
publicationHref = pulp_client.SelectPublicationHref(publicationTask)
if publicationHref == nil {
return "", fmt.Errorf("Could not find a publication href in task: %v", publicationTask.PulpHref)
}
} else {
publicationHref = publication.PulpHref
}
return *publicationHref, nil
}