Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split deployAgent into build and deploy functions #4

Merged
merged 1 commit into from
Jul 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 15 additions & 17 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,28 @@

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)
}
err = saveAppEnvsFile(envs)
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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
46 changes: 14 additions & 32 deletions deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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, "")
Expand All @@ -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()
Expand All @@ -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")
}
23 changes: 21 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"os"
)

const version = "0.2.3"
const version = "0.2.4"

var printVersion bool

Expand All @@ -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)
}