2016-05-10 13:38:50 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2016-05-16 13:34:24 +02:00
|
|
|
"github.com/vmware/harbor/api"
|
2016-05-10 13:38:50 +02:00
|
|
|
"github.com/vmware/harbor/dao"
|
|
|
|
"github.com/vmware/harbor/job"
|
2016-05-25 09:24:44 +02:00
|
|
|
"github.com/vmware/harbor/job/config"
|
2016-05-23 13:39:13 +02:00
|
|
|
"github.com/vmware/harbor/job/utils"
|
2016-05-10 13:38:50 +02:00
|
|
|
"github.com/vmware/harbor/models"
|
|
|
|
"github.com/vmware/harbor/utils/log"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ReplicationJob struct {
|
2016-05-16 13:34:24 +02:00
|
|
|
api.BaseAPI
|
2016-05-10 13:38:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ReplicationReq struct {
|
2016-05-25 10:33:45 +02:00
|
|
|
PolicyID int64 `json:"policy_id"`
|
|
|
|
Repo string `json:"repository"`
|
|
|
|
Operation string `json:"operation"`
|
|
|
|
TagList []string `json:"tags"`
|
2016-05-10 13:38:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rj *ReplicationJob) Post() {
|
|
|
|
var data ReplicationReq
|
|
|
|
rj.DecodeJSONReq(&data)
|
|
|
|
log.Debugf("data: %+v", data)
|
|
|
|
p, err := dao.GetRepPolicy(data.PolicyID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to get policy, error: %v", err)
|
|
|
|
rj.RenderError(http.StatusInternalServerError, fmt.Sprintf("Failed to get policy, id: %d", data.PolicyID))
|
|
|
|
return
|
|
|
|
}
|
2016-05-13 15:43:17 +02:00
|
|
|
if p == nil {
|
|
|
|
log.Errorf("Policy not found, id: %d", data.PolicyID)
|
|
|
|
rj.RenderError(http.StatusNotFound, fmt.Sprintf("Policy not found, id: %d", data.PolicyID))
|
|
|
|
return
|
|
|
|
}
|
2016-05-24 07:15:27 +02:00
|
|
|
if len(data.Repo) == 0 { // sync all repositories
|
|
|
|
repoList, err := getRepoList(p.ProjectID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to get repository list, project id: %d, error: %v", p.ProjectID, err)
|
|
|
|
rj.RenderError(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debugf("repo list: %v", repoList)
|
|
|
|
for _, repo := range repoList {
|
|
|
|
err := rj.addJob(repo, data.PolicyID, models.RepOpTransfer)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to insert job record, error: %v", err)
|
|
|
|
rj.RenderError(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2016-05-10 13:38:50 +02:00
|
|
|
}
|
2016-05-24 07:15:27 +02:00
|
|
|
} else { // sync a single repository
|
|
|
|
var op string
|
|
|
|
if len(data.Operation) > 0 {
|
|
|
|
op = data.Operation
|
|
|
|
} else {
|
|
|
|
op = models.RepOpTransfer
|
|
|
|
}
|
2016-05-25 10:33:45 +02:00
|
|
|
err := rj.addJob(data.Repo, data.PolicyID, op, data.TagList...)
|
2016-05-10 13:38:50 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to insert job record, error: %v", err)
|
|
|
|
rj.RenderError(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 10:33:45 +02:00
|
|
|
func (rj *ReplicationJob) addJob(repo string, policyID int64, operation string, tags ...string) error {
|
2016-05-24 07:15:27 +02:00
|
|
|
j := models.RepJob{
|
|
|
|
Repository: repo,
|
|
|
|
PolicyID: policyID,
|
|
|
|
Operation: operation,
|
2016-05-25 10:33:45 +02:00
|
|
|
TagList: tags,
|
2016-05-24 07:15:27 +02:00
|
|
|
}
|
|
|
|
log.Debugf("Creating job for repo: %s, policy: %d", repo, policyID)
|
|
|
|
id, err := dao.AddRepJob(j)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debugf("Send job to scheduler, job id: %d", id)
|
|
|
|
job.Schedule(id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:09:44 +02:00
|
|
|
type RepActionReq struct {
|
|
|
|
PolicyID int64 `json:"policy_id"`
|
|
|
|
Action string `json:"action"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rj *ReplicationJob) HandleAction() {
|
|
|
|
var data RepActionReq
|
|
|
|
rj.DecodeJSONReq(&data)
|
|
|
|
//Currently only support stop action
|
|
|
|
if data.Action != "stop" {
|
|
|
|
log.Errorf("Unrecognized action: %s", data.Action)
|
|
|
|
rj.RenderError(http.StatusBadRequest, fmt.Sprintf("Unrecongized action: %s", data.Action))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
jobs, err := dao.GetRepJobToStop(data.PolicyID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to get jobs to stop, error: %v", err)
|
|
|
|
rj.RenderError(http.StatusInternalServerError, "Faild to get jobs to stop")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var jobIDList []int64
|
|
|
|
for _, j := range jobs {
|
|
|
|
jobIDList = append(jobIDList, j.ID)
|
|
|
|
}
|
|
|
|
job.WorkerPool.StopJobs(jobIDList)
|
|
|
|
}
|
|
|
|
|
2016-05-23 13:39:13 +02:00
|
|
|
func (rj *ReplicationJob) GetLog() {
|
|
|
|
idStr := rj.Ctx.Input.Param(":id")
|
|
|
|
jid, err := strconv.ParseInt(idStr, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error parsing job id: %s, error: %v", idStr, err)
|
|
|
|
rj.RenderError(http.StatusBadRequest, "Invalid job id")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logFile := utils.GetJobLogPath(jid)
|
|
|
|
rj.Ctx.Output.Download(logFile)
|
|
|
|
}
|
|
|
|
|
2016-05-10 13:38:50 +02:00
|
|
|
// calls the api from UI to get repo list
|
|
|
|
func getRepoList(projectID int64) ([]string, error) {
|
2016-05-25 09:24:44 +02:00
|
|
|
/*
|
|
|
|
uiUser := os.Getenv("UI_USR")
|
|
|
|
if len(uiUser) == 0 {
|
|
|
|
uiUser = "admin"
|
|
|
|
}
|
|
|
|
uiPwd := os.Getenv("UI_PWD")
|
|
|
|
if len(uiPwd) == 0 {
|
|
|
|
uiPwd = "Harbor12345"
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
uiURL := config.LocalHarborURL()
|
2016-05-10 13:38:50 +02:00
|
|
|
client := &http.Client{}
|
2016-05-27 10:21:32 +02:00
|
|
|
req, err := http.NewRequest("GET", uiURL+"/api/repositories?project_id="+strconv.Itoa(int(projectID)), nil)
|
2016-05-10 13:38:50 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error when creating request: %v")
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-25 09:24:44 +02:00
|
|
|
//req.SetBasicAuth(uiUser, uiPwd)
|
|
|
|
req.AddCookie(&http.Cookie{Name: models.UISecretCookie, Value: config.UISecret()})
|
|
|
|
//dump, err := httputil.DumpRequest(req, true)
|
|
|
|
//log.Debugf("req: %q", dump)
|
2016-05-10 13:38:50 +02:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error when calling UI api to get repositories, error: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
log.Errorf("Unexpected status code: %d", resp.StatusCode)
|
|
|
|
dump, _ := httputil.DumpResponse(resp, true)
|
|
|
|
log.Debugf("response: %q", dump)
|
|
|
|
return nil, fmt.Errorf("Unexpected status code when getting repository list: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2016-05-17 12:49:02 +02:00
|
|
|
log.Errorf("Failed to read the response body, error: %v", err)
|
2016-05-10 13:38:50 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var repoList []string
|
|
|
|
err = json.Unmarshal(body, &repoList)
|
|
|
|
return repoList, err
|
|
|
|
}
|