harbor/dao/replication_job.go

421 lines
9.9 KiB
Go
Raw Normal View History

2016-06-16 08:57:45 +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.
2016-06-16 08:57:45 +02:00
*/
2016-05-10 13:38:50 +02:00
package dao
import (
"fmt"
"time"
2016-05-16 07:57:30 +02:00
2016-05-27 09:04:20 +02:00
"strings"
2016-05-10 13:38:50 +02:00
"github.com/astaxie/beego/orm"
"github.com/vmware/harbor/models"
)
2016-05-27 12:46:07 +02:00
// AddRepTarget ...
2016-05-10 13:38:50 +02:00
func AddRepTarget(target models.RepTarget) (int64, error) {
2016-06-07 07:41:02 +02:00
o := GetOrmer()
2016-05-10 13:38:50 +02:00
return o.Insert(&target)
}
2016-05-27 12:46:07 +02:00
// GetRepTarget ...
2016-05-10 13:38:50 +02:00
func GetRepTarget(id int64) (*models.RepTarget, error) {
2016-06-07 07:41:02 +02:00
o := GetOrmer()
2016-05-10 13:38:50 +02:00
t := models.RepTarget{ID: id}
err := o.Read(&t)
if err == orm.ErrNoRows {
return nil, nil
}
return &t, err
}
2016-05-27 12:46:07 +02:00
// GetRepTargetByName ...
func GetRepTargetByName(name string) (*models.RepTarget, error) {
2016-06-07 07:41:02 +02:00
o := GetOrmer()
t := models.RepTarget{Name: name}
err := o.Read(&t, "Name")
if err == orm.ErrNoRows {
return nil, nil
}
return &t, err
}
// GetRepTargetByConnInfo ...
func GetRepTargetByConnInfo(endpoint, username string) (*models.RepTarget, error) {
o := GetOrmer()
t := models.RepTarget{
URL: endpoint,
Username: username,
}
err := o.Read(&t, "URL", "Username")
if err == orm.ErrNoRows {
return nil, nil
}
return &t, err
}
2016-05-27 12:46:07 +02:00
// DeleteRepTarget ...
2016-05-10 13:38:50 +02:00
func DeleteRepTarget(id int64) error {
2016-06-07 07:41:02 +02:00
o := GetOrmer()
2016-05-10 13:38:50 +02:00
_, err := o.Delete(&models.RepTarget{ID: id})
return err
}
2016-05-27 12:46:07 +02:00
// UpdateRepTarget ...
func UpdateRepTarget(target models.RepTarget) error {
2016-06-07 07:41:02 +02:00
o := GetOrmer()
_, err := o.Update(&target, "URL", "Name", "Username", "Password")
return err
}
2016-06-13 10:49:46 +02:00
// FilterRepTargets filters targets by name
func FilterRepTargets(name string) ([]*models.RepTarget, error) {
2016-06-07 07:41:02 +02:00
o := GetOrmer()
2016-06-13 02:31:32 +02:00
2016-06-13 10:49:46 +02:00
var args []interface{}
2016-05-23 10:48:55 +02:00
2016-06-13 10:49:46 +02:00
sql := `select * from replication_target `
if len(name) != 0 {
sql += `where name like ? `
args = append(args, "%"+name+"%")
2016-06-13 02:31:32 +02:00
}
2016-06-13 10:49:46 +02:00
sql += `order by creation_time`
2016-06-13 02:31:32 +02:00
var targets []*models.RepTarget
2016-06-13 02:54:42 +02:00
2016-06-13 10:49:46 +02:00
if _, err := o.Raw(sql, args).QueryRows(&targets); err != nil {
2016-06-13 02:31:32 +02:00
return nil, err
}
return targets, nil
}
2016-05-27 12:46:07 +02:00
// AddRepPolicy ...
2016-05-10 13:38:50 +02:00
func AddRepPolicy(policy models.RepPolicy) (int64, error) {
2016-06-07 07:41:02 +02:00
o := GetOrmer()
2016-05-10 13:38:50 +02:00
sqlTpl := `insert into replication_policy (name, project_id, target_id, enabled, description, cron_str, start_time, creation_time, update_time ) values (?, ?, ?, ?, ?, ?, %s, NOW(), NOW())`
var sql string
if policy.Enabled == 1 {
sql = fmt.Sprintf(sqlTpl, "NOW()")
} else {
sql = fmt.Sprintf(sqlTpl, "NULL")
}
p, err := o.Raw(sql).Prepare()
if err != nil {
return 0, err
}
r, err := p.Exec(policy.Name, policy.ProjectID, policy.TargetID, policy.Enabled, policy.Description, policy.CronStr)
if err != nil {
return 0, err
}
id, err := r.LastInsertId()
return id, err
}
2016-05-27 12:46:07 +02:00
// GetRepPolicy ...
2016-05-10 13:38:50 +02:00
func GetRepPolicy(id int64) (*models.RepPolicy, error) {
2016-06-07 07:41:02 +02:00
o := GetOrmer()
2016-06-13 10:49:46 +02:00
sql := `select * from replication_policy where id = ?`
var policy models.RepPolicy
if err := o.Raw(sql, id).QueryRow(&policy); err != nil {
if err == orm.ErrNoRows {
return nil, nil
}
2016-06-13 10:49:46 +02:00
return nil, err
2016-05-10 13:38:50 +02:00
}
2016-06-13 10:49:46 +02:00
return &policy, nil
}
// FilterRepPolicies filters policies by name and project ID
func FilterRepPolicies(name string, projectID int64) ([]*models.RepPolicy, error) {
o := GetOrmer()
var args []interface{}
sql := `select rp.id, rp.project_id, p.name as project_name, rp.target_id,
rt.name as target_name, rp.name, rp.enabled, rp.description,
rp.cron_str, rp.start_time, rp.creation_time, rp.update_time
from replication_policy rp
join project p on rp.project_id=p.project_id
join replication_target rt on rp.target_id=rt.id `
if len(name) != 0 && projectID != 0 {
sql += `where rp.name like ? and rp.project_id = ? `
args = append(args, "%"+name+"%")
args = append(args, projectID)
} else if len(name) != 0 {
sql += `where rp.name like ? `
args = append(args, "%"+name+"%")
} else if projectID != 0 {
sql += `where rp.project_id = ? `
args = append(args, projectID)
}
sql += `order by rp.creation_time`
var policies []*models.RepPolicy
if _, err := o.Raw(sql, args).QueryRows(&policies); err != nil {
return nil, err
}
return policies, nil
2016-05-10 13:38:50 +02:00
}
2016-05-27 12:46:07 +02:00
// GetRepPolicyByName ...
func GetRepPolicyByName(name string) (*models.RepPolicy, error) {
2016-06-07 07:41:02 +02:00
o := GetOrmer()
2016-06-13 10:49:46 +02:00
sql := `select * from replication_policy where name = ?`
var policy models.RepPolicy
if err := o.Raw(sql, name).QueryRow(&policy); err != nil {
if err == orm.ErrNoRows {
return nil, nil
}
2016-06-13 10:49:46 +02:00
return nil, err
}
2016-06-13 10:49:46 +02:00
return &policy, nil
}
2016-05-27 12:46:07 +02:00
// GetRepPolicyByProject ...
func GetRepPolicyByProject(projectID int64) ([]*models.RepPolicy, error) {
2016-06-07 07:41:02 +02:00
o := GetOrmer()
2016-06-13 10:49:46 +02:00
sql := `select * from replication_policy where project_id = ?`
var policies []*models.RepPolicy
if _, err := o.Raw(sql, projectID).QueryRows(&policies); err != nil {
return nil, err
}
return policies, nil
}
2016-05-27 12:46:07 +02:00
// GetRepPolicyByTarget ...
func GetRepPolicyByTarget(targetID int64) ([]*models.RepPolicy, error) {
o := GetOrmer()
sql := `select * from replication_policy where target_id = ?`
var policies []*models.RepPolicy
if _, err := o.Raw(sql, targetID).QueryRows(&policies); err != nil {
return nil, err
}
return policies, nil
}
// GetRepPolicyByProjectAndTarget ...
func GetRepPolicyByProjectAndTarget(projectID, targetID int64) ([]*models.RepPolicy, error) {
o := GetOrmer()
sql := `select * from replication_policy where project_id = ? and target_id = ?`
var policies []*models.RepPolicy
if _, err := o.Raw(sql, projectID, targetID).QueryRows(&policies); err != nil {
return nil, err
}
return policies, nil
}
2016-06-13 11:32:22 +02:00
// UpdateRepPolicy ...
func UpdateRepPolicy(policy *models.RepPolicy) error {
o := GetOrmer()
_, err := o.Update(policy, "TargetID", "Name", "Enabled", "Description", "CronStr")
2016-06-13 11:32:22 +02:00
return err
}
2016-05-27 12:46:07 +02:00
// DeleteRepPolicy ...
2016-05-10 13:38:50 +02:00
func DeleteRepPolicy(id int64) error {
2016-06-07 07:41:02 +02:00
o := GetOrmer()
2016-05-10 13:38:50 +02:00
_, err := o.Delete(&models.RepPolicy{ID: id})
return err
}
2016-05-27 12:46:07 +02:00
// UpdateRepPolicyEnablement ...
2016-05-27 09:04:20 +02:00
func UpdateRepPolicyEnablement(id int64, enabled int) error {
2016-06-07 07:41:02 +02:00
o := GetOrmer()
2016-05-10 13:38:50 +02:00
p := models.RepPolicy{
ID: id,
Enabled: enabled,
}
var err error
if enabled == 1 {
p.StartTime = time.Now()
_, err = o.Update(&p, "Enabled", "StartTime")
} else {
_, err = o.Update(&p, "Enabled")
}
2016-05-27 09:04:20 +02:00
2016-05-10 13:38:50 +02:00
return err
}
2016-05-27 12:46:07 +02:00
// EnableRepPolicy ...
2016-05-10 13:38:50 +02:00
func EnableRepPolicy(id int64) error {
2016-05-27 09:04:20 +02:00
return UpdateRepPolicyEnablement(id, 1)
2016-05-10 13:38:50 +02:00
}
2016-05-27 12:46:07 +02:00
// DisableRepPolicy ...
2016-05-10 13:38:50 +02:00
func DisableRepPolicy(id int64) error {
2016-05-27 09:04:20 +02:00
return UpdateRepPolicyEnablement(id, 0)
2016-05-10 13:38:50 +02:00
}
2016-05-27 12:46:07 +02:00
// AddRepJob ...
2016-05-10 13:38:50 +02:00
func AddRepJob(job models.RepJob) (int64, error) {
2016-06-07 07:41:02 +02:00
o := GetOrmer()
2016-05-10 13:38:50 +02:00
if len(job.Status) == 0 {
job.Status = models.JobPending
}
if len(job.TagList) > 0 {
job.Tags = strings.Join(job.TagList, ",")
}
2016-05-10 13:38:50 +02:00
return o.Insert(&job)
}
2016-05-27 12:46:07 +02:00
// GetRepJob ...
2016-05-10 13:38:50 +02:00
func GetRepJob(id int64) (*models.RepJob, error) {
2016-06-07 07:41:02 +02:00
o := GetOrmer()
2016-05-10 13:38:50 +02:00
j := models.RepJob{ID: id}
err := o.Read(&j)
if err == orm.ErrNoRows {
return nil, nil
}
genTagListForJob(&j)
return &j, nil
2016-05-10 13:38:50 +02:00
}
2016-05-27 12:46:07 +02:00
// GetRepJobByPolicy ...
func GetRepJobByPolicy(policyID int64) ([]*models.RepJob, error) {
var res []*models.RepJob
_, err := repJobPolicyIDQs(policyID).All(&res)
genTagListForJob(res...)
return res, err
}
// FilterRepJobs filters jobs by repo and policy ID
2016-06-29 10:58:16 +02:00
func FilterRepJobs(policyID int64, repository, status string, startTime,
endTime *time.Time, limit int) ([]*models.RepJob, error) {
o := GetOrmer()
2016-06-27 10:24:04 +02:00
qs := o.QueryTable(new(models.RepJob))
if policyID != 0 {
qs = qs.Filter("PolicyID", policyID)
}
2016-06-27 10:24:04 +02:00
if len(repository) != 0 {
qs = qs.Filter("Repository__icontains", repository)
}
if len(status) != 0 {
qs = qs.Filter("Status__icontains", status)
}
2016-06-29 10:58:16 +02:00
if startTime != nil {
fmt.Printf("%v\n", startTime)
qs = qs.Filter("CreationTime__gte", startTime)
}
if endTime != nil {
fmt.Printf("%v\n", endTime)
qs = qs.Filter("CreationTime__lte", endTime)
}
if limit != 0 {
qs = qs.Limit(limit)
}
qs = qs.OrderBy("-CreationTime")
var jobs []*models.RepJob
2016-06-27 10:24:04 +02:00
_, err := qs.All(&jobs)
if err != nil {
return nil, err
}
genTagListForJob(jobs...)
return jobs, nil
}
// GetRepJobToStop get jobs that are possibly being handled by workers of a certain policy.
func GetRepJobToStop(policyID int64) ([]*models.RepJob, error) {
var res []*models.RepJob
_, err := repJobPolicyIDQs(policyID).Filter("status__in", models.JobPending, models.JobRunning).All(&res)
genTagListForJob(res...)
return res, err
}
func repJobQs() orm.QuerySeter {
2016-06-07 07:41:02 +02:00
o := GetOrmer()
return o.QueryTable("replication_job")
}
func repJobPolicyIDQs(policyID int64) orm.QuerySeter {
return repJobQs().Filter("policy_id", policyID)
}
2016-05-27 12:46:07 +02:00
// DeleteRepJob ...
2016-05-10 13:38:50 +02:00
func DeleteRepJob(id int64) error {
2016-06-07 07:41:02 +02:00
o := GetOrmer()
2016-05-10 13:38:50 +02:00
_, err := o.Delete(&models.RepJob{ID: id})
return err
}
2016-05-27 12:46:07 +02:00
// UpdateRepJobStatus ...
2016-05-10 13:38:50 +02:00
func UpdateRepJobStatus(id int64, status string) error {
2016-06-07 07:41:02 +02:00
o := GetOrmer()
2016-05-10 13:38:50 +02:00
j := models.RepJob{
ID: id,
Status: status,
}
num, err := o.Update(&j, "Status")
if num == 0 {
2016-05-16 07:57:30 +02:00
err = fmt.Errorf("Failed to update replication job with id: %d %s", id, err.Error())
2016-05-10 13:38:50 +02:00
}
return err
}
// ResetRunningJobs update all running jobs status to pending
func ResetRunningJobs() error {
o := GetOrmer()
sql := fmt.Sprintf("update replication_job set status = '%s' where status = '%s'", models.JobPending, models.JobRunning)
_, err := o.Raw(sql).Exec()
return err
}
2016-06-20 07:04:27 +02:00
// GetRepJobByStatus get jobs of certain statuses
func GetRepJobByStatus(status ...string) ([]*models.RepJob, error) {
var res []*models.RepJob
2016-06-20 07:04:27 +02:00
var t []interface{}
for _, s := range status {
t = append(t, interface{}(s))
}
_, err := repJobQs().Filter("status__in", t...).All(&res)
genTagListForJob(res...)
return res, err
}
func genTagListForJob(jobs ...*models.RepJob) {
for _, j := range jobs {
if len(j.Tags) > 0 {
j.TagList = strings.Split(j.Tags, ",")
}
}
}