Skip to content

Commit

Permalink
all: move tsuru client logic to internal pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
andrestc committed Mar 29, 2018
1 parent 40323f8 commit 6772dfd
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 63 deletions.
15 changes: 8 additions & 7 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ package main
import (
"log"

"github.com/tsuru/deploy-agent/internal/tsuru"
"github.com/tsuru/tsuru/exec"
)

func build(c Client, appName string, cmd []string, fs Filesystem, executor exec.Executor) {
func build(c tsuru.Client, appName string, cmd []string, fs Filesystem, executor exec.Executor) {
log.SetFlags(0)
envs, err := c.getAppEnvs(appName)
envs, err := c.GetAppEnvs(appName)
if err != nil {
log.Fatal(err)
}
Expand All @@ -22,10 +23,10 @@ func build(c Client, appName string, cmd []string, fs Filesystem, executor exec.
}
}

func deploy(c Client, appName string, fs Filesystem, executor exec.Executor) {
func deploy(c tsuru.Client, appName string, fs Filesystem, executor exec.Executor) {
log.SetFlags(0)
var yamlData TsuruYaml
envs, err := c.registerUnit(appName, yamlData)
var yamlData tsuru.TsuruYaml
envs, err := c.RegisterUnit(appName, yamlData)
if err != nil {
log.Fatal(err)
}
Expand All @@ -34,7 +35,7 @@ func deploy(c Client, appName string, fs Filesystem, executor exec.Executor) {
log.Fatal(err)
}
if !firstDeploy {
err = c.sendDiffDeploy(diff, appName)
err = c.SendDiffDeploy(diff, appName)
if err != nil {
log.Fatal(err)
}
Expand All @@ -51,7 +52,7 @@ func deploy(c Client, appName string, fs Filesystem, executor exec.Executor) {
if err != nil {
log.Fatal(err)
}
_, err = c.registerUnit(appName, yamlData)
_, err = c.RegisterUnit(appName, yamlData)
if err != nil {
log.Fatal(err)
}
Expand Down
5 changes: 3 additions & 2 deletions deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"net/http/httptest"

"github.com/tsuru/deploy-agent/internal/tsuru"
"github.com/tsuru/tsuru/app/bind"
"gopkg.in/check.v1"
)
Expand All @@ -25,7 +26,7 @@ func (s *S) TestBuild(c *check.C) {
e, _ := json.Marshal(envs)
w.Write(e)
}))
client := Client{
client := tsuru.Client{
URL: server.URL,
Token: "fake-token",
}
Expand Down Expand Up @@ -67,7 +68,7 @@ func (s *S) TestDeploy(c *check.C) {
c.Assert(err, check.IsNil)
_, err = p.WriteString(procfileData)
c.Assert(err, check.IsNil)
client := Client{
client := tsuru.Client{
URL: server.URL,
Token: "fake-token",
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 24 additions & 8 deletions client.go → internal/tsuru/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main
package tsuru

import (
"encoding/json"
Expand All @@ -18,9 +18,25 @@ import (
"github.com/tsuru/tsuru/app/bind"
)

type TsuruYaml struct {
Hooks Hook `json:"hooks,omitempty"`
Processes map[string]string `json:"processes,omitempty"`
Healthcheck map[string]interface{} `yaml:"healthcheck" json:"healthcheck,omitempty"`
}

type Hook struct {
BuildHooks []string `yaml:"build,omitempty" json:"build"`
Restart map[string]interface{} `yaml:"restart" json:"restart"`
}

func (t *TsuruYaml) IsEmpty() bool {
return len(t.Hooks.BuildHooks) == 0 && t.Processes == nil
}

type Client struct {
URL string
Token string
URL string
Token string
Version string
}

var httpClient = &http.Client{
Expand All @@ -34,7 +50,7 @@ var httpClient = &http.Client{
Timeout: time.Minute,
}

func (c Client) getAppEnvs(appName string) ([]bind.EnvVar, error) {
func (c Client) GetAppEnvs(appName string) ([]bind.EnvVar, error) {
hostname, err := os.Hostname()
if err != nil {
return nil, err
Expand Down Expand Up @@ -64,10 +80,10 @@ func (c Client) getAppEnvs(appName string) ([]bind.EnvVar, error) {
return envs, nil
}

func (c Client) registerUnit(appName string, customData TsuruYaml) ([]bind.EnvVar, error) {
func (c Client) RegisterUnit(appName string, customData TsuruYaml) ([]bind.EnvVar, error) {
var err error
var yamlData []byte
if !customData.isEmpty() {
if !customData.IsEmpty() {
yamlData, err = json.Marshal(customData)
if err != nil {
return nil, err
Expand Down Expand Up @@ -103,7 +119,7 @@ func (c Client) registerUnit(appName string, customData TsuruYaml) ([]bind.EnvVa
return envs, nil
}

func (c Client) sendDiffDeploy(diff, appName string) error {
func (c Client) SendDiffDeploy(diff, appName string) error {
var err error
v := url.Values{}
v.Set("customdata", diff)
Expand Down Expand Up @@ -135,5 +151,5 @@ func (c Client) url(path string) string {
func (c Client) setHeaders(req *http.Request) {
req.Header.Set("Authorization", fmt.Sprintf("bearer %s", c.Token))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("X-Agent-Version", version)
req.Header.Set("X-Agent-Version", c.Version)
}
31 changes: 20 additions & 11 deletions client_test.go → internal/tsuru/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,33 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main
package tsuru

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/tsuru/tsuru/app/bind"
"gopkg.in/check.v1"
)

func Test(t *testing.T) { check.TestingT(t) }

type S struct{}

var _ = check.Suite(&S{})

func (s *S) TestClient(c *check.C) {
call := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
call++
c.Assert(r.Header.Get("Authorization"), check.Not(check.Equals), "")
c.Assert(r.Header.Get("Content-Type"), check.Equals, "application/x-www-form-urlencoded")
c.Assert(r.Header.Get("X-Agent-Version"), check.Equals, version)
c.Assert(r.Header.Get("X-Agent-Version"), check.Equals, "0.2.1")
c.Assert(r.URL.Path, check.Equals, "/apps/test/units/register")
b, err := ioutil.ReadAll(r.Body)
c.Assert(err, check.IsNil)
Expand All @@ -45,13 +52,14 @@ func (s *S) TestClient(c *check.C) {
w.Write(e)
}))
cli := Client{
URL: server.URL,
Token: "test-token",
URL: server.URL,
Token: "test-token",
Version: "0.2.1",
}
_, err := cli.registerUnit("test", TsuruYaml{})
_, err := cli.RegisterUnit("test", TsuruYaml{})
c.Assert(err, check.IsNil)
t := TsuruYaml{Hooks: Hook{BuildHooks: []string{"ls", "ls"}}, Processes: map[string]string{"web": "test"}}
_, err = cli.registerUnit("test", t)
_, err = cli.RegisterUnit("test", t)
c.Assert(err, check.IsNil)
}

Expand All @@ -77,7 +85,7 @@ func (s *S) TestClientSendDiff(c *check.C) {
call++
c.Assert(r.Header.Get("Authorization"), check.Not(check.Equals), "")
c.Assert(r.Header.Get("Content-Type"), check.Equals, "application/x-www-form-urlencoded")
c.Assert(r.Header.Get("X-Agent-Version"), check.Equals, version)
c.Assert(r.Header.Get("X-Agent-Version"), check.Equals, "0.2.1")
c.Assert(r.URL.Path, check.Equals, "/apps/test/diff")
b, err := ioutil.ReadAll(r.Body)
c.Assert(err, check.IsNil)
Expand All @@ -91,12 +99,13 @@ func (s *S) TestClientSendDiff(c *check.C) {
}
}))
cli := Client{
URL: server.URL,
Token: "test-token",
URL: server.URL,
Token: "test-token",
Version: "0.2.1",
}
err := cli.sendDiffDeploy("", "test")
err := cli.SendDiffDeploy("", "test")
c.Assert(err, check.IsNil)
err = cli.sendDiffDeploy(diff, "test")
err = cli.SendDiffDeploy(diff, "test")
c.Assert(err, check.IsNil)
}

Expand Down
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"os"

"github.com/tsuru/deploy-agent/internal/tsuru"
"github.com/tsuru/tsuru/exec"
"github.com/tsuru/tsuru/fs"
)
Expand All @@ -27,9 +28,10 @@ func main() {
return
}

c := Client{
URL: os.Args[1],
Token: os.Args[2],
c := tsuru.Client{
URL: os.Args[1],
Token: os.Args[2],
Version: version,
}
appName := os.Args[3]
command := os.Args[4:]
Expand Down
26 changes: 6 additions & 20 deletions tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"regexp"
"strings"

"github.com/tsuru/deploy-agent/internal/tsuru"
"github.com/tsuru/deploy-agent/internal/user"
"github.com/tsuru/tsuru/app/bind"
"github.com/tsuru/tsuru/exec"
Expand Down Expand Up @@ -65,23 +66,8 @@ func execScript(cmds []string, envs []bind.EnvVar, w io.Writer, fs Filesystem, e
return nil
}

type TsuruYaml struct {
Hooks Hook `json:"hooks,omitempty"`
Processes map[string]string `json:"processes,omitempty"`
Healthcheck map[string]interface{} `yaml:"healthcheck" json:"healthcheck,omitempty"`
}

type Hook struct {
BuildHooks []string `yaml:"build,omitempty" json:"build"`
Restart map[string]interface{} `yaml:"restart" json:"restart"`
}

func (t *TsuruYaml) isEmpty() bool {
return len(t.Hooks.BuildHooks) == 0 && t.Processes == nil
}

func loadTsuruYaml(fs Filesystem) (TsuruYaml, error) {
var tsuruYamlData TsuruYaml
func loadTsuruYaml(fs Filesystem) (tsuru.TsuruYaml, error) {
var tsuruYamlData tsuru.TsuruYaml
for _, yamlFile := range tsuruYamlFiles {
filePath := fmt.Sprintf("%s/%s", defaultWorkingDir, yamlFile)
tsuruYaml, err := fs.ReadFile(filePath)
Expand All @@ -90,14 +76,14 @@ func loadTsuruYaml(fs Filesystem) (TsuruYaml, error) {
}
err = yaml.Unmarshal(tsuruYaml, &tsuruYamlData)
if err != nil {
return TsuruYaml{}, err
return tsuru.TsuruYaml{}, err
}
break
}
return tsuruYamlData, nil
}

func buildHooks(yamlData TsuruYaml, envs []bind.EnvVar, fs Filesystem, executor exec.Executor) error {
func buildHooks(yamlData tsuru.TsuruYaml, envs []bind.EnvVar, fs Filesystem, executor exec.Executor) error {
cmds := append([]string{}, yamlData.Hooks.BuildHooks...)
fmt.Fprintln(os.Stdout, "---- Running build hooks ----")
return execScript(cmds, envs, os.Stdout, fs, executor)
Expand All @@ -113,7 +99,7 @@ func readProcfile(path string, fs Filesystem) (string, error) {

var procfileRegex = regexp.MustCompile(`^([\w-]+):\s*(\S.+)$`)

func loadProcesses(t *TsuruYaml, fs Filesystem) error {
func loadProcesses(t *tsuru.TsuruYaml, fs Filesystem) error {
procfile, err := readProcfile(defaultWorkingDir, fs)
if err != nil {
return err
Expand Down
25 changes: 13 additions & 12 deletions tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import (
"os"
"syscall"

"github.com/tsuru/deploy-agent/internal/tsuru"
"github.com/tsuru/tsuru/app/bind"
"github.com/tsuru/tsuru/exec/exectest"
"gopkg.in/check.v1"
)

func (s *S) TestIsEmpty(c *check.C) {
t := TsuruYaml{}
c.Assert(t.isEmpty(), check.Equals, true)
t := tsuru.TsuruYaml{}
c.Assert(t.IsEmpty(), check.Equals, true)
t.Processes = map[string]string{"web": "python something.py"}
c.Assert(t.isEmpty(), check.Equals, false)
c.Assert(t.IsEmpty(), check.Equals, false)
}

func (s *S) TestExecScript(c *check.C) {
Expand Down Expand Up @@ -94,8 +95,8 @@ healthcheck:
_, err := s.fs.Create(tsuruYmlPath)
c.Assert(err, check.IsNil)
c.Assert(s.testFS().HasAction(fmt.Sprintf("create %s", tsuruYmlPath)), check.Equals, true)
expected := TsuruYaml{
Hooks: Hook{
expected := tsuru.TsuruYaml{
Hooks: tsuru.Hook{
BuildHooks: []string{"test", "another_test"},
Restart: map[string]interface{}{
"before": []interface{}{"static"},
Expand All @@ -115,8 +116,8 @@ healthcheck:
}

func (s *S) TestHooks(c *check.C) {
tsuruYaml := TsuruYaml{
Hooks: Hook{BuildHooks: []string{"ls", "cd"}},
tsuruYaml := tsuru.TsuruYaml{
Hooks: tsuru.Hook{BuildHooks: []string{"ls", "cd"}},
}
envs := []bind.EnvVar{{
Name: "foo",
Expand All @@ -142,12 +143,12 @@ func (s *S) TestLoadProcesses(c *check.C) {
_, err := s.fs.Create(procfilePath)
c.Assert(err, check.IsNil)
c.Assert(s.testFS().HasAction(fmt.Sprintf("create %s", procfilePath)), check.Equals, true)
expected := TsuruYaml{
expected := tsuru.TsuruYaml{
Processes: map[string]string{
"web": "python app.py",
},
}
t := TsuruYaml{}
t := tsuru.TsuruYaml{}
err = loadProcesses(&t, s.fs)
c.Assert(err, check.IsNil)
c.Assert(t, check.DeepEquals, expected)
Expand All @@ -164,13 +165,13 @@ another-worker: run-task
_, err := s.fs.Create(procfilePath)
c.Assert(err, check.IsNil)
c.Assert(s.testFS().HasAction(fmt.Sprintf("create %s", procfilePath)), check.Equals, true)
expected := TsuruYaml{
expected := tsuru.TsuruYaml{
Processes: map[string]string{
"web": "python app.py",
"another-worker": "run-task",
},
}
t := TsuruYaml{}
t := tsuru.TsuruYaml{}
err = loadProcesses(&t, s.fs)
c.Assert(err, check.IsNil)
c.Assert(t, check.DeepEquals, expected)
Expand All @@ -184,7 +185,7 @@ func (s *S) TestDontLoadWrongProcfile(c *check.C) {
_, err := s.fs.Create(procfilePath)
c.Assert(err, check.IsNil)
c.Assert(s.testFS().HasAction(fmt.Sprintf("create %s", procfilePath)), check.Equals, true)
t := TsuruYaml{}
t := tsuru.TsuruYaml{}
err = loadProcesses(&t, s.fs)
c.Assert(err, check.NotNil)
c.Assert(err.Error(), check.Equals, `invalid Procfile, no processes found in "web:\n\t@python test.py"`)
Expand Down

0 comments on commit 6772dfd

Please sign in to comment.