diff --git a/api/replication_policy.go b/api/replication_policy.go index 10cb67313..10906ba39 100644 --- a/api/replication_policy.go +++ b/api/replication_policy.go @@ -122,5 +122,13 @@ func (pa *RepPolicyAPI) UpdateEnablement() { log.Infof("replication of %d triggered", pa.policyID) } }() + } else { + go func() { + if err := postReplicationAction(pa.policyID, "stop"); err != nil { + log.Errorf("failed to stop replication of %d: %v", pa.policyID, err) + } else { + log.Infof("try to stop replication of %d", pa.policyID) + } + }() } } diff --git a/api/utils.go b/api/utils.go index ef4919cf5..fead92469 100644 --- a/api/utils.go +++ b/api/utils.go @@ -167,26 +167,64 @@ func TriggerReplicationByRepository(repository string, tags []string, operation } } +func postReplicationAction(policyID int64, acton string) error { + data := struct { + PolicyID int64 `json:"policy_id"` + Action string `json:"action"` + }{ + PolicyID: policyID, + Action: acton, + } + + b, err := json.Marshal(&data) + if err != nil { + return err + } + + url := buildReplicationActionURL() + + resp, err := http.DefaultClient.Post(url, "application/json", bytes.NewBuffer(b)) + if err != nil { + return err + } + + if resp.StatusCode == http.StatusOK { + return nil + } + + defer resp.Body.Close() + + b, err = ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + + return fmt.Errorf("%d %s", resp.StatusCode, string(b)) +} + func buildReplicationURL() string { url := getJobServiceURL() - url = strings.TrimSpace(url) - url = strings.TrimRight(url, "/") - return fmt.Sprintf("%s/api/jobs/replication", url) } func buildJobLogURL(jobID string) string { url := getJobServiceURL() - url = strings.TrimSpace(url) - url = strings.TrimRight(url, "/") - return fmt.Sprintf("%s/api/jobs/replication/%s/log", url, jobID) } +func buildReplicationActionURL() string { + url := getJobServiceURL() + return fmt.Sprintf("%s/api/jobs/replication/actions", url) +} + func getJobServiceURL() string { url := os.Getenv("JOB_SERVICE_URL") + url = strings.TrimSpace(url) + url = strings.TrimRight(url, "/") + if len(url) == 0 { url = "http://jobservice" } + return url } diff --git a/models/replication_job.go b/models/replication_job.go index a10d26761..97fc8443a 100644 --- a/models/replication_job.go +++ b/models/replication_job.go @@ -60,7 +60,7 @@ type RepJob struct { // RepTarget is the model for a replication targe, i.e. destination, which wraps the endpoint URL and username/password of a remote registry. type RepTarget struct { ID int64 `orm:"column(id)" json:"id"` - URL string `orm:"column(url)" json:"url"` + URL string `orm:"column(url)" json:"endpoint"` Name string `orm:"column(name)" json:"name"` Username string `orm:"column(username)" json:"username"` Password string `orm:"column(password)" json:"password"`