Skip to content

Commit

Permalink
tiup: implement renew subcommand to extend expiration date of compone… (
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroProfundis authored Jul 14, 2021
1 parent 09c49be commit de5c898
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
51 changes: 51 additions & 0 deletions cmd/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ of components or the repository itself.`,
newMirrorShowCmd(),
newMirrorSetCmd(),
newMirrorModifyCmd(),
newMirrorRenewCmd(),
newMirrorGrantCmd(),
newMirrorRotateCmd(),
)
Expand Down Expand Up @@ -337,6 +338,56 @@ func newMirrorModifyCmd() *cobra.Command {
return cmd
}

// the `mirror renew` sub command
func newMirrorRenewCmd() *cobra.Command {
var privPath string
var days int

cmd := &cobra.Command{
Use: "renew <component> [flags]",
Short: "Renew the manifest of a published component.",
Long: "Renew the manifest of a published component, bump version and extend its expire time.",
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
if len(args) != 1 {
return cmd.Help()
}

component := args[0]

env := environment.GlobalEnv()

comp, _ := environment.ParseCompVersion(component)
m, err := env.V1Repository().FetchComponentManifest(comp, true)
if err != nil {
// ignore manifest expiration error
if v1manifest.IsExpirationError(perrs.Cause(err)) {
fmt.Println(err)
} else {
return err
}
}

if days > 0 {
v1manifest.RenewManifest(m, time.Now(), time.Hour*24*time.Duration(days))
} else {
v1manifest.RenewManifest(m, time.Now())
}

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

return env.V1Repository().Mirror().Publish(manifest, &model.PublishInfo{})
},
}

cmd.Flags().StringVarP(&privPath, "key", "k", "", "private key path")
cmd.Flags().IntVar(&days, "days", 0, "after how many days the manifest expires, 0 means builtin default values of manifests")
return cmd
}

// the `mirror rotate` sub command
func newMirrorRotateCmd() *cobra.Command {
addr := "0.0.0.0:8080"
Expand Down
15 changes: 10 additions & 5 deletions pkg/repository/v1_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,14 @@ func (r *V1Repository) updateComponentManifest(id string, withYanked bool) (*v1m
}

var component v1manifest.Component
manifest, err := r.fetchComponentManifest(&item, url, &component, fileVersion.Length)
if err != nil {
return nil, err
manifest, fetchErr := r.fetchComponentManifest(&item, url, &component, fileVersion.Length)
if fetchErr != nil {
// ignore manifest expiration error here and continue building component object,
// the manifest expiration error should be handled by caller, so try to return it
// with a valid component object.
if !v1manifest.IsExpirationError(errors.Cause(fetchErr)) {
return nil, fetchErr
}
}

if oldVersion != 0 && component.Version < oldVersion {
Expand All @@ -472,7 +477,7 @@ func (r *V1Repository) updateComponentManifest(id string, withYanked bool) (*v1m
return nil, err
}

return &component, nil
return &component, fetchErr
}

// DownloadComponent downloads the component specified by item into local file,
Expand Down Expand Up @@ -601,7 +606,7 @@ func (r *V1Repository) fetchBase(url string, maxSize uint, f func(reader io.Read

m, err := f(reader)
if err != nil {
return nil, errors.Annotatef(err, "read manifest from mirror(%s) failed", r.mirror.Source())
return m, errors.Annotatef(err, "read manifest from mirror(%s) failed", r.mirror.Source())
}
return m, nil
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/repository/v1manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,14 +415,18 @@ func ReadManifest(input io.Reader, role ValidManifest, keys *KeyStore) (*Manifes
}

// RenewManifest resets and extends the expire time of manifest
func RenewManifest(m ValidManifest, startTime time.Time) {
func RenewManifest(m ValidManifest, startTime time.Time, extend ...time.Duration) {
// manifest with 0 version means it's unversioned
if m.Base().Version > 0 {
m.Base().Version++
}

// only update expire field when it's old than target expire time
targetExpire := startTime.Add(ManifestsConfig[m.Base().Ty].Expire)
// only update expire field when it's older than target expire time
duration := ManifestsConfig[m.Base().Ty].Expire
if len(extend) > 0 {
duration = extend[0]
}
targetExpire := startTime.Add(duration)
currentExpire, err := time.Parse(time.RFC3339, m.Base().Expires)
if err != nil {
m.Base().Expires = targetExpire.Format(time.RFC3339)
Expand Down

0 comments on commit de5c898

Please sign in to comment.