From aa4c31a32be8c8174144963d0db2d25dd52d13c5 Mon Sep 17 00:00:00 2001 From: bigeagle Date: Sun, 18 Dec 2016 23:30:41 +0800 Subject: [PATCH] feat(tunasynctl): implemented 'set-size' command to update a mirror size --- cmd/tunasynctl/tunasynctl.go | 63 ++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/cmd/tunasynctl/tunasynctl.go b/cmd/tunasynctl/tunasynctl.go index 36a18a6..ea12dcf 100644 --- a/cmd/tunasynctl/tunasynctl.go +++ b/cmd/tunasynctl/tunasynctl.go @@ -186,6 +186,57 @@ func listJobs(c *cli.Context) error { return nil } +func updateMirrorSize(c *cli.Context) error { + args := c.Args() + if len(args) != 2 { + return cli.NewExitError("Usage: tunasynctl -w ", 1) + } + workerID := c.String("worker") + mirrorID := args.Get(0) + mirrorSize := args.Get(1) + + msg := struct { + Name string `json:"name"` + Size string `json:"size"` + }{ + Name: mirrorID, + Size: mirrorSize, + } + + url := fmt.Sprintf( + "%s/workers/%s/jobs/%s/size", baseURL, workerID, mirrorID, + ) + + resp, err := tunasync.PostJSON(url, msg, client) + if err != nil { + return cli.NewExitError( + fmt.Sprintf("Failed to send request to manager: %s", + err.Error()), + 1) + } + defer resp.Body.Close() + body, _ := ioutil.ReadAll(resp.Body) + if resp.StatusCode != http.StatusOK { + return cli.NewExitError( + fmt.Sprintf("Manager failed to update mirror size: %s", body), 1, + ) + } + + var status tunasync.MirrorStatus + json.Unmarshal(body, &status) + if status.Size != mirrorSize { + return cli.NewExitError( + fmt.Sprintf( + "Mirror size error, expecting %s, manager returned %s", + mirrorSize, status.Size, + ), 1, + ) + } + + logger.Infof("Successfully updated mirror size to %s", mirrorSize) + return nil +} + func flushDisabledJobs(c *cli.Context) error { req, err := http.NewRequest("DELETE", baseURL+flushDisabledPath, nil) if err != nil { @@ -385,6 +436,18 @@ func main() { Flags: commonFlags, Action: initializeWrapper(listWorkers), }, + { + Name: "set-size", + Usage: "Set mirror size", + Flags: append( + commonFlags, + cli.StringFlag{ + Name: "worker, w", + Usage: "specify worker-id of the mirror job", + }, + ), + Action: initializeWrapper(updateMirrorSize), + }, { Name: "start", Usage: "Start a job",