From 49bd8c1484eae07301fb55d2c314bf0a884d163c Mon Sep 17 00:00:00 2001 From: Guilherme Rezende Date: Thu, 13 Jul 2017 17:24:51 -0300 Subject: [PATCH] split deployAgent into build and deploy functions --- client.go | 31 +++++++++++++++++++++++++++++++ deploy.go | 32 +++++++++++++++----------------- deploy_test.go | 46 ++++++++++++++-------------------------------- main.go | 23 +++++++++++++++++++++-- 4 files changed, 81 insertions(+), 51 deletions(-) diff --git a/client.go b/client.go index 92f5f67..0964b1e 100644 --- a/client.go +++ b/client.go @@ -35,6 +35,37 @@ var httpClient = &http.Client{ Timeout: time.Minute, } +func (c Client) getAppEnvs(appName string) ([]bind.EnvVar, error) { + hostname, err := os.Hostname() + if err != nil { + return nil, err + } + v := url.Values{} + v.Set("hostname", hostname) + u := c.url(fmt.Sprintf("/apps/%s/env", appName)) + req, err := http.NewRequest("GET", u, strings.NewReader(v.Encode())) + if err != nil { + return nil, err + } + req.Header.Set("Authorization", fmt.Sprintf("bearer %s", c.Token)) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + resp, err := httpClient.Do(req) + if err != nil { + return nil, err + } + var envs []bind.EnvVar + defer resp.Body.Close() + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + err = json.Unmarshal(data, &envs) + if err != nil { + return nil, fmt.Errorf("invalid response from tsuru API: %s", data) + } + return envs, nil +} + func (c Client) registerUnit(appName string, customData TsuruYaml) ([]bind.EnvVar, error) { var err error var yamlData []byte diff --git a/deploy.go b/deploy.go index 429c310..3d82fed 100644 --- a/deploy.go +++ b/deploy.go @@ -4,22 +4,11 @@ package main -import ( - "log" -) +import "log" -func deployAgent(args []string) { +func build(c Client, appName string, cmd []string) { log.SetFlags(0) - // backward compatibility with tsuru 0.12.x - if args[len(args)-1] == "deploy" { - args = args[:len(args)-1] - } - c := Client{ - URL: args[0], - Token: args[1], - } - var yamlData TsuruYaml - envs, err := c.registerUnit(args[2], yamlData) + envs, err := c.getAppEnvs(appName) if err != nil { log.Fatal(err) } @@ -27,7 +16,16 @@ func deployAgent(args []string) { if err != nil { log.Fatal(err) } - err = execScript(args[3:], envs, nil) + err = execScript(cmd, envs, nil) + if err != nil { + log.Fatal(err) + } +} + +func deploy(c Client, appName string) { + log.SetFlags(0) + var yamlData TsuruYaml + envs, err := c.registerUnit(appName, yamlData) if err != nil { log.Fatal(err) } @@ -36,7 +34,7 @@ func deployAgent(args []string) { log.Fatal(err) } if !firstDeploy { - err = c.sendDiffDeploy(diff, args[2]) + err = c.sendDiffDeploy(diff, appName) if err != nil { log.Fatal(err) } @@ -53,7 +51,7 @@ func deployAgent(args []string) { if err != nil { log.Fatal(err) } - _, err = c.registerUnit(args[2], yamlData) + _, err = c.registerUnit(appName, yamlData) if err != nil { log.Fatal(err) } diff --git a/deploy_test.go b/deploy_test.go index c63b9ba..8d029a0 100644 --- a/deploy_test.go +++ b/deploy_test.go @@ -14,13 +14,9 @@ import ( "gopkg.in/check.v1" ) -func (s *S) TestDeploy(c *check.C) { +func (s *S) TestBuild(c *check.C) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/apps/app1/diff" { - fmt.Fprint(w, "") - return - } - c.Assert(r.URL.Path, check.Equals, "/apps/app1/units/register") + c.Assert(r.URL.Path, check.Equals, "/apps/app1/env") envs := []bind.EnvVar{{ Name: "foo", Value: "bar", @@ -29,31 +25,14 @@ func (s *S) TestDeploy(c *check.C) { e, _ := json.Marshal(envs) w.Write(e) })) - tsuruYmlData := `hooks: - build: - - ls - - ls` - f, err := s.fs.Create(fmt.Sprintf("%s/%s", defaultWorkingDir, "tsuru.yml")) - c.Assert(err, check.IsNil) - defer f.Close() - diff, err := s.fs.Create(fmt.Sprintf("%s/%s", defaultWorkingDir, "diff")) - c.Assert(err, check.IsNil) - defer diff.Close() - _, err = f.WriteString(tsuruYmlData) - c.Assert(err, check.IsNil) - _, err = diff.WriteString(`diff`) - c.Assert(err, check.IsNil) - procfileData := `web: run-app` - p, err := s.fs.Create(fmt.Sprintf("%s/%s", defaultWorkingDir, "Procfile")) - defer p.Close() - c.Assert(err, check.IsNil) - _, err = p.WriteString(procfileData) - c.Assert(err, check.IsNil) - args := []string{server.URL, "fake-token", "app1", "ls"} - deployAgent(args) + client := Client{ + URL: server.URL, + Token: "fake-token", + } + build(client, "app1", []string{"ls"}) } -func (s *S) TestDeployBackwardCompatibility(c *check.C) { +func (s *S) TestDeploy(c *check.C) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/apps/app1/diff" { fmt.Fprint(w, "") @@ -73,8 +52,8 @@ func (s *S) TestDeployBackwardCompatibility(c *check.C) { - ls - ls` f, err := s.fs.Create(fmt.Sprintf("%s/%s", defaultWorkingDir, "tsuru.yml")) - defer f.Close() c.Assert(err, check.IsNil) + defer f.Close() diff, err := s.fs.Create(fmt.Sprintf("%s/%s", defaultWorkingDir, "diff")) c.Assert(err, check.IsNil) defer diff.Close() @@ -88,6 +67,9 @@ func (s *S) TestDeployBackwardCompatibility(c *check.C) { c.Assert(err, check.IsNil) _, err = p.WriteString(procfileData) c.Assert(err, check.IsNil) - args := []string{server.URL, "fake-token", "app1", "ls", "deploy"} - deployAgent(args) + client := Client{ + URL: server.URL, + Token: "fake-token", + } + deploy(client, "app1") } diff --git a/main.go b/main.go index 5695c63..6432403 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ import ( "os" ) -const version = "0.2.3" +const version = "0.2.4" var printVersion bool @@ -24,5 +24,24 @@ func main() { fmt.Printf("deploy-agent version %s\n", version) return } - deployAgent(os.Args[1:]) + c := Client{ + URL: os.Args[1], + Token: os.Args[2], + } + appName := os.Args[3] + command := os.Args[4:] + if command[len(command)-1] == "build" { + build(c, appName, command[:len(command)-1]) + return + } + if command[len(command)-1] == "deploy-only" { + deploy(c, appName) + return + } + // backward compatibility with tsuru < 1.4.0 + if command[len(command)-1] == "deploy" { + command = command[:len(command)-1] + } + build(c, appName, command) + deploy(c, appName) }