func (t *tutumBackend) Install(eng *engine.Engine) error { eng.Register("tutum", func(job *engine.Job) engine.Status { if len(job.Args) == 1 { tutumConnector = job.Args[0] } if !tutum.IsAuthenticated() { return job.Errorf("You need to provide your Tutum credentials in ~/.tutum or environment variables TUTUM_USER and TUTUM_APIKEY") } job.Eng.Register("containers", func(job *engine.Job) engine.Status { log.Printf("Received '%s' operation....", job.Name) path := fmt.Sprintf( "/containers/json?all=%s&limit=%s", url.QueryEscape(job.Getenv("all")), url.QueryEscape(job.Getenv("limit")), ) resp, err := tutumConnectorCall("GET", path, "") if err != nil { return job.Errorf("%s: get: %v", path, err) } c := engine.NewTable("Created", 0) body, err := ioutil.ReadAll(resp.Body) if err != nil { return job.Errorf("%s: read body: %v", path, err) } if _, err := c.ReadListFrom(body); err != nil { return job.Errorf("%s: readlist: %v", path, err) } c.WriteListTo(job.Stdout) return engine.StatusOK }) job.Eng.Register("create", func(job *engine.Job) engine.Status { log.Printf("Received '%s' operation....", job.Name) path := fmt.Sprintf( "/containers/create", ) config := runconfig.ContainerConfigFromJob(job) data, err := json.Marshal(config) if err != nil { return job.Errorf("%s: json marshal: %v", path, err) } resp, err := tutumConnectorCall("POST", path, string(data)) if err != nil { return job.Errorf("%s: post: %v", path, err) } body, err := ioutil.ReadAll(resp.Body) if err != nil { return job.Errorf("%s: read body: %#v", path, err) } var containerOut struct { Id string Warnings []string } err = json.Unmarshal([]byte(body), &containerOut) _, err = job.Printf("%s\n", containerOut.Id) if err != nil { return job.Errorf("%s: write body: %#v", path, err) } log.Printf("%s", string(body)) return engine.StatusOK }) job.Eng.Register("container_delete", func(job *engine.Job) engine.Status { log.Printf("Received '%s' operation....", job.Name) path := fmt.Sprintf( "/containers/%s?force=%s", job.Args[0], url.QueryEscape(job.Getenv("forceRemove")), ) _, err := tutumConnectorCall("DELETE", path, "") if err != nil { return job.Errorf("%s: delete: %v", path, err) } return engine.StatusOK }) job.Eng.Register("start", func(job *engine.Job) engine.Status { log.Printf("Received '%s' operation....", job.Name) path := fmt.Sprintf("/containers/%s/start", job.Args[0]) config := runconfig.ContainerConfigFromJob(job) data, err := json.Marshal(config) if err != nil { return job.Errorf("%s: json marshal: %v", path, err) } _, err = tutumConnectorCall("POST", path, string(data)) if err != nil { return job.Errorf("%s: post: %v", path, err) } return engine.StatusOK }) job.Eng.Register("stop", func(job *engine.Job) engine.Status { log.Printf("Received '%s' operation....", job.Name) path := fmt.Sprintf( "/containers/%s/stop?t=%s", job.Args[0], url.QueryEscape(job.Getenv("t")), ) _, err := tutumConnectorCall("POST", path, "") if err != nil { return job.Errorf("%s: post: %v", path, err) } return engine.StatusOK }) job.Eng.Register("kill", func(job *engine.Job) engine.Status { log.Printf("Received '%s' operation....", job.Name) path := fmt.Sprintf( "/containers/%s/kill?signal=%s", job.Args[0], job.Args[1], ) _, err := tutumConnectorCall("POST", path, "") if err != nil { return job.Errorf("%s: post: %v", path, err) } return engine.StatusOK }) job.Eng.Register("restart", func(job *engine.Job) engine.Status { log.Printf("Received '%s' operation....", job.Name) path := fmt.Sprintf( "/containers/%s/restart?t=%s", job.Args[0], url.QueryEscape(job.Getenv("t")), ) _, err := tutumConnectorCall("POST", path, "") if err != nil { return job.Errorf("%s: post: %v", path, err) } return engine.StatusOK }) job.Eng.Register("inspect", func(job *engine.Job) engine.Status { log.Printf("Received '%s' operation....", job.Name) path := fmt.Sprintf( "/containers/%s/json", job.Args[0], ) resp, err := tutumConnectorCall("GET", path, "") if err != nil { return job.Errorf("%s: get: %v", path, err) } _, err = io.Copy(job.Stdout, resp.Body) if err != nil { return job.Errorf("%s: copy stream: %v", path, err) } return engine.StatusOK }) job.Eng.Register("logs", func(job *engine.Job) engine.Status { log.Printf("Received '%s' operation....", job.Name) path := fmt.Sprintf( "/containers/%s/logs?stdout=%s&stderr=%s", job.Args[0], url.QueryEscape(job.Getenv("stdout")), url.QueryEscape(job.Getenv("stderr")), ) resp, err := tutumConnectorCall("GET", path, "") if err != nil { return job.Errorf("%s: get: %v", path, err) } _, err = io.Copy(job.Stdout, resp.Body) if err != nil { return job.Errorf("%s: copy stream: %v", path, err) } return engine.StatusOK }) job.Eng.Register("version", func(job *engine.Job) engine.Status { log.Printf("Received '%s' operation....", job.Name) path := "/version" resp, err := tutumConnectorCall("GET", path, "") if err != nil { return job.Errorf("%s: get: %v", path, err) } _, err = io.Copy(job.Stdout, resp.Body) if err != nil { return job.Errorf("%s: copy stream: %v", path, err) } return engine.StatusOK }) job.Eng.RegisterCatchall(func(job *engine.Job) engine.Status { return job.Errorf("Operation not yet supported: %s", job.Name) }) return engine.StatusOK }) return nil }
func (rax *RaxCloud) run(ctx *cli.Context, eng *engine.Engine) (err error) { if err = rax.configure(ctx); err == nil { if _, name, err := rax.findTargetHost(); err == nil { if name == "" { rax.createHost(DASS_TARGET_PREFIX + RandomString()[:12]) } } else { return err } eng.Register("create", func(job *engine.Job) (status engine.Status) { if ctx, err := rax.getHostContext(); err == nil { defer ctx.Close() return ctx.exec(job, func(client HttpClient) (status engine.Status) { path := "/containers/create" config := runconfig.ContainerConfigFromJob(job) if data, err := json.Marshal(config); err != nil { return job.Errorf("marshaling failure : %v", err) } else if resp, err := client.Post(path, string(data)); err != nil { return job.Error(err) } else { var container struct { Id string Warnings []string } if body, err := ioutil.ReadAll(resp.Body); err != nil { return job.Errorf("Failed to copy response body: %v", err) } else if err := json.Unmarshal([]byte(body), &container); err != nil { return job.Errorf("Failed to read container info from body: %v", err) } else { job.Printf("%s\n", container.Id) } } return engine.StatusOK }) } else { return job.Errorf("Failed to create host context: %v", err) } }) eng.Register("container_delete", func(job *engine.Job) (status engine.Status) { if ctx, err := rax.getHostContext(); err == nil { defer ctx.Close() return ctx.exec(job, func(client HttpClient) (status engine.Status) { path := fmt.Sprintf("/containers/%s?force=%s", job.Args[0], url.QueryEscape(job.Getenv("forceRemove"))) if _, err := client.Delete(path, ""); err != nil { return job.Error(err) } return engine.StatusOK }) } else { return job.Errorf("Failed to create host context: %v", err) } }) eng.Register("containers", func(job *engine.Job) (status engine.Status) { if ctx, err := rax.getHostContext(); err == nil { defer ctx.Close() return ctx.exec(job, func(client HttpClient) (status engine.Status) { path := fmt.Sprintf("/containers/json?all=%s&limit=%s", url.QueryEscape(job.Getenv("all")), url.QueryEscape(job.Getenv("limit"))) if resp, err := client.Get(path, ""); err != nil { return job.Error(err) } else { created := engine.NewTable("Created", 0) if body, err := ioutil.ReadAll(resp.Body); err != nil { return job.Errorf("Failed to copy response body: %v", err) } else if created.ReadListFrom(body); err != nil { return job.Errorf("Failed to read list from body: %v", err) } else { created.WriteListTo(job.Stdout) } } return engine.StatusOK }) } else { return job.Errorf("Failed to create host context: %v", err) } }) eng.Register("version", func(job *engine.Job) (status engine.Status) { if ctx, err := rax.getHostContext(); err == nil { defer ctx.Close() return ctx.exec(job, func(client HttpClient) (status engine.Status) { path := "/version" if resp, err := client.Get(path, ""); err != nil { return job.Errorf("Failed call %s(path:%s): %v", job.Name, path, err) } else if _, err := io.Copy(job.Stdout, resp.Body); err != nil { return job.Errorf("Failed to copy response body: %v", err) } return engine.StatusOK }) } else { return job.Errorf("Failed to create host context: %v", err) } }) eng.Register("start", func(job *engine.Job) (status engine.Status) { if ctx, err := rax.getHostContext(); err == nil { defer ctx.Close() return ctx.exec(job, func(client HttpClient) (status engine.Status) { path := fmt.Sprintf("/containers/%s/start", job.Args[0]) config := runconfig.ContainerConfigFromJob(job) if data, err := json.Marshal(config); err != nil { return job.Errorf("marshaling failure : %v", err) } else if _, err := client.Post(path, string(data)); err != nil { return job.Errorf("Failed call %s(path:%s): %v", job.Name, path, err) } return engine.StatusOK }) } else { return job.Errorf("Failed to create host context: %v", err) } }) eng.Register("stop", func(job *engine.Job) (status engine.Status) { if ctx, err := rax.getHostContext(); err == nil { defer ctx.Close() return ctx.exec(job, func(client HttpClient) (status engine.Status) { path := fmt.Sprintf("/containers/%s/stop?t=%s", job.Args[0], url.QueryEscape(job.Getenv("t"))) if _, err := client.Post(path, ""); err != nil { return job.Errorf("Failed call %s(path:%s): %v", job.Name, path, err) } return engine.StatusOK }) } else { return job.Errorf("Failed to create host context: %v", err) } }) eng.Register("kill", func(job *engine.Job) (status engine.Status) { if ctx, err := rax.getHostContext(); err == nil { defer ctx.Close() return ctx.exec(job, func(client HttpClient) (status engine.Status) { path := fmt.Sprintf("/containers/%s/kill?signal=%s", job.Args[0], job.Args[1]) if _, err := client.Post(path, ""); err != nil { return job.Errorf("Failed call %s(path:%s): %v", job.Name, path, err) } return engine.StatusOK }) } else { return job.Errorf("Failed to create host context: %v", err) } }) eng.Register("restart", func(job *engine.Job) (status engine.Status) { if ctx, err := rax.getHostContext(); err == nil { defer ctx.Close() return ctx.exec(job, func(client HttpClient) (status engine.Status) { path := fmt.Sprintf("/containers/%s/restart?t=%s", url.QueryEscape(job.Getenv("t"))) if _, err := client.Post(path, ""); err != nil { return job.Errorf("Failed call %s(path:%s): %v", job.Name, path, err) } return engine.StatusOK }) } else { return job.Errorf("Failed to create host context: %v", err) } }) eng.Register("inspect", func(job *engine.Job) (status engine.Status) { if ctx, err := rax.getHostContext(); err == nil { defer ctx.Close() return ctx.exec(job, func(client HttpClient) (status engine.Status) { path := fmt.Sprintf("/containers/%s/json", job.Args[0]) if resp, err := client.Post(path, ""); err != nil { return job.Errorf("Failed call %s(path:%s): %v", job.Name, path, err) } else if _, err := io.Copy(job.Stdout, resp.Body); err != nil { return job.Errorf("Failed to copy response body: %v", err) } return engine.StatusOK }) } else { return job.Errorf("Failed to create host context: %v", err) } }) eng.Register("attach", func(job *engine.Job) (status engine.Status) { if ctx, err := rax.getHostContext(); err == nil { defer ctx.Close() return ctx.exec(job, func(client HttpClient) (status engine.Status) { path := fmt.Sprintf("/containers/%s/attach?stream=%s&stdout=%s&stderr=%s", job.Args[0], url.QueryEscape(job.Getenv("stream")), url.QueryEscape(job.Getenv("stdout")), url.QueryEscape(job.Getenv("stderr"))) if resp, err := client.Post(path, ""); err != nil { return job.Errorf("Failed call %s(path:%s): %v", job.Name, path, err) } else if _, err := io.Copy(job.Stdout, resp.Body); err != nil { return job.Errorf("Failed to copy response body: %v", err) } return engine.StatusOK }) } else { return job.Errorf("Failed to create host context: %v", err) } }) eng.Register("pull", func(job *engine.Job) (status engine.Status) { if ctx, err := rax.getHostContext(); err == nil { defer ctx.Close() return ctx.exec(job, func(client HttpClient) (status engine.Status) { path := fmt.Sprintf("/images/create?fromImage=%s&tag=%s", job.Args[0], url.QueryEscape(job.Getenv("tag"))) if resp, err := client.Post(path, ""); err != nil { return job.Errorf("Failed call %s(path:%s): %v", job.Name, path, err) } else if _, err := io.Copy(job.Stdout, resp.Body); err != nil { return job.Errorf("Failed to copy response body: %v", err) } return engine.StatusOK }) } else { return job.Errorf("Failed to create host context: %v", err) } }) eng.Register("logs", func(job *engine.Job) (status engine.Status) { if ctx, err := rax.getHostContext(); err == nil { defer ctx.Close() return ctx.exec(job, func(client HttpClient) (status engine.Status) { path := fmt.Sprintf("/containers/%s/logs?stdout=%s&stderr=%s", job.Args[0], url.QueryEscape(job.Getenv("stdout")), url.QueryEscape(job.Getenv("stderr"))) if resp, err := client.Get(path, ""); err != nil { return job.Errorf("Failed call %s(path:%s): %v", job.Name, path, err) } else if _, err := io.Copy(job.Stdout, resp.Body); err != nil { return job.Errorf("Failed to copy response body: %v", err) } return engine.StatusOK }) } else { return job.Errorf("Failed to create host context: %v", err) } }) eng.RegisterCatchall(func(job *engine.Job) engine.Status { log.Printf("[UNIMPLEMENTED] %s %#v %#v %#v", job.Name, *job, job.Env(), job.Args) return engine.StatusOK }) } return err }