2016-05-25 10:51:40 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package replication
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/vmware/harbor/models"
|
|
|
|
"github.com/vmware/harbor/utils/log"
|
2016-06-23 06:14:26 +02:00
|
|
|
"github.com/vmware/harbor/utils/registry"
|
|
|
|
"github.com/vmware/harbor/utils/registry/auth"
|
2016-05-25 10:51:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// StateDelete ...
|
|
|
|
StateDelete = "delete"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Deleter deletes repository or tags
|
|
|
|
type Deleter struct {
|
|
|
|
repository string // prject_name/repo_name
|
|
|
|
tags []string
|
|
|
|
|
|
|
|
dstURL string // url of target registry
|
|
|
|
dstUsr string // username ...
|
|
|
|
dstPwd string // username ...
|
|
|
|
|
2016-06-21 10:39:03 +02:00
|
|
|
insecure bool
|
|
|
|
|
2016-06-23 06:14:26 +02:00
|
|
|
dstClient *registry.Repository
|
|
|
|
|
2016-05-25 10:51:40 +02:00
|
|
|
logger *log.Logger
|
|
|
|
}
|
|
|
|
|
2016-05-26 05:27:16 +02:00
|
|
|
// NewDeleter returns a Deleter
|
2016-06-23 06:14:26 +02:00
|
|
|
func NewDeleter(repository string, tags []string, dstURL, dstUsr, dstPwd string, insecure bool, logger *log.Logger) (*Deleter, error) {
|
|
|
|
dstCred := auth.NewBasicAuthCredential(dstUsr, dstPwd)
|
|
|
|
dstClient, err := newRepositoryClient(dstURL, insecure, dstCred,
|
|
|
|
repository, "repository", repository, "pull", "push", "*")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-27 09:04:20 +02:00
|
|
|
deleter := &Deleter{
|
2016-05-26 05:27:16 +02:00
|
|
|
repository: repository,
|
|
|
|
tags: tags,
|
|
|
|
dstURL: dstURL,
|
|
|
|
dstUsr: dstUsr,
|
|
|
|
dstPwd: dstPwd,
|
2016-06-21 10:39:03 +02:00
|
|
|
insecure: insecure,
|
2016-06-23 06:14:26 +02:00
|
|
|
dstClient: dstClient,
|
2016-05-26 05:27:16 +02:00
|
|
|
logger: logger,
|
|
|
|
}
|
2016-06-24 07:00:56 +02:00
|
|
|
deleter.logger.Infof("initialization completed: repository: %s, tags: %v, destination URL: %s, insecure: %v, destination user: %s",
|
|
|
|
deleter.repository, deleter.tags, deleter.dstURL, deleter.insecure, deleter.dstUsr)
|
2016-06-23 06:14:26 +02:00
|
|
|
return deleter, nil
|
2016-05-26 05:27:16 +02:00
|
|
|
}
|
|
|
|
|
2016-05-25 10:51:40 +02:00
|
|
|
// Exit ...
|
|
|
|
func (d *Deleter) Exit() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enter deletes repository or tags
|
|
|
|
func (d *Deleter) Enter() (string, error) {
|
|
|
|
|
|
|
|
if len(d.tags) == 0 {
|
2016-06-23 06:14:26 +02:00
|
|
|
tags, err := d.dstClient.ListTag()
|
|
|
|
if err != nil {
|
|
|
|
d.logger.Errorf("an error occurred while listing tags of repository %s on %s with user %s: %v", d.repository, d.dstURL, d.dstUsr, err)
|
2016-05-25 10:51:40 +02:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-06-23 06:14:26 +02:00
|
|
|
d.tags = append(d.tags, tags...)
|
2016-05-25 10:51:40 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 06:14:26 +02:00
|
|
|
d.logger.Infof("tags %v will be deleted", d.tags)
|
|
|
|
|
2016-05-25 10:51:40 +02:00
|
|
|
for _, tag := range d.tags {
|
2016-06-23 06:14:26 +02:00
|
|
|
|
|
|
|
if err := d.dstClient.DeleteTag(tag); err != nil {
|
2016-05-25 10:51:40 +02:00
|
|
|
d.logger.Errorf("an error occurred while deleting repository %s:%s on %s with user %s: %v", d.repository, tag, d.dstURL, d.dstUsr, err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.logger.Infof("repository %s:%s on %s has been deleted", d.repository, tag, d.dstURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
return models.JobFinished, nil
|
|
|
|
}
|