Skip to content

Commit

Permalink
mirror: retry publish if manifest too old
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroProfundis committed Feb 24, 2021
1 parent 4789ce3 commit 2ce40ed
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 46 deletions.
110 changes: 67 additions & 43 deletions cmd/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,57 +222,29 @@ func newMirrorModifyCmd() *cobra.Command {
return cmd.Help()
}

component := args[0]

env := environment.GlobalEnv()

comp, ver := environment.ParseCompVersion(component)
m, err := env.V1Repository().FetchComponentManifest(comp, true)
if err != nil {
return err
}

v1manifest.RenewManifest(m, time.Now())
if desc != "" {
m.Description = desc
}

flagSet := set.NewStringSet()
cmd.Flags().Visit(func(f *pflag.Flag) {
flagSet.Insert(f.Name)
})

publishInfo := &model.PublishInfo{}
if ver == "" {
if flagSet.Exist("standalone") {
publishInfo.Stand = &standalone
}
if flagSet.Exist("hide") {
publishInfo.Hide = &hidden
var reqErr error
pubErr := utils.Retry(func() error {
err := doPublish(args[0], desc, privPath, standalone, hidden, yanked, flagSet)
if err != nil && err == repository.ErrManifestTooOld {
// retry if the error is manifest too old
return err // return err to trigger next retry
}
if flagSet.Exist("yank") {
publishInfo.Yank = &yanked
}
} else if flagSet.Exist("yank") {
if ver.IsNightly() {
return errors.New("nightly version can't be yanked")
}
for p := range m.Platforms {
vi, ok := m.Platforms[p][ver.String()]
if !ok {
continue
}
vi.Yanked = yanked
m.Platforms[p][ver.String()] = vi
}
}
reqErr = err // keep the error info
return nil // return nil to end the retry loop
}, utils.RetryOption{
Attempts: 5,
Timeout: time.Minute * 3,
})

manifest, err := sign(privPath, m)
if err != nil {
return err
if reqErr != nil {
return reqErr
}

return env.V1Repository().Mirror().Publish(manifest, publishInfo)
return pubErr
},
}

Expand All @@ -284,6 +256,58 @@ func newMirrorModifyCmd() *cobra.Command {
return cmd
}

// request mirror to publish
func doPublish(
component, desc, privPath string,
standalone, hidden, yanked bool,
flagSet set.StringSet,
) error {
env := environment.GlobalEnv()

comp, ver := environment.ParseCompVersion(component)
m, err := env.V1Repository().FetchComponentManifest(comp, true)
if err != nil {
return err
}

v1manifest.RenewManifest(m, time.Now())
if desc != "" {
m.Description = desc
}

publishInfo := &model.PublishInfo{}
if ver == "" {
if flagSet.Exist("standalone") {
publishInfo.Stand = &standalone
}
if flagSet.Exist("hide") {
publishInfo.Hide = &hidden
}
if flagSet.Exist("yank") {
publishInfo.Yank = &yanked
}
} else if flagSet.Exist("yank") {
if ver.IsNightly() {
return errors.New("nightly version can't be yanked")
}
for p := range m.Platforms {
vi, ok := m.Platforms[p][ver.String()]
if !ok {
continue
}
vi.Yanked = yanked
m.Platforms[p][ver.String()] = vi
}
}

manifest, err := sign(privPath, m)
if err != nil {
return err
}

return env.V1Repository().Mirror().Publish(manifest, publishInfo)
}

// the `mirror rotate` sub command
func newMirrorRotateCmd() *cobra.Command {
addr := "0.0.0.0:8080"
Expand Down
9 changes: 6 additions & 3 deletions pkg/repository/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ const (
OptionHidden = "hidden"
)

// ErrNotFound represents the resource not exists.
var ErrNotFound = stderrors.New("not found")
// predefined errors
var (
ErrNotFound = stderrors.New("not found") // resource does not exists.
ErrManifestTooOld = stderrors.New("component manifest is too old, update it before publish")
)

type (
// DownloadProgress represents the download progress notifier
Expand Down Expand Up @@ -443,7 +446,7 @@ func (l *httpMirror) Publish(manifest *v1manifest.Manifest, info model.Component
}
switch resp.StatusCode {
case http.StatusConflict:
return errors.Errorf("Local component manifest is not new enough, update it first")
return ErrManifestTooOld
case http.StatusForbidden:
return errors.Errorf("The server refused, make sure you have access to this component")
default:
Expand Down

0 comments on commit 2ce40ed

Please sign in to comment.