Skip to content

Commit

Permalink
all: post full YAML content to register unit endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ggarnier committed Nov 30, 2018
1 parent 3f44cdb commit ed371f6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
14 changes: 9 additions & 5 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ func build(c tsuru.Client, appName string, cmd []string, fs Filesystem, executor
}

func deploy(c tsuru.Client, appName string, fs Filesystem, executor exec.Executor) error {
var yamlData tsuru.TsuruYaml
envs, err := c.RegisterUnit(appName, yamlData)
envs, err := c.RegisterUnit(appName, nil)
if err != nil {
return err
}
Expand All @@ -38,7 +37,7 @@ func deploy(c tsuru.Client, appName string, fs Filesystem, executor exec.Executo
return err
}
}
yamlData, err = loadTsuruYaml(fs)
yamlData, err := loadTsuruYaml(fs)
if err != nil {
return err
}
Expand All @@ -50,7 +49,12 @@ func deploy(c tsuru.Client, appName string, fs Filesystem, executor exec.Executo
if err != nil {
return err
}
_, err = c.RegisterUnit(appName, yamlData)
fullYamlData, err := loadAllTsuruYaml(fs)
if err != nil {
return err
}
fullYamlData["processes"] = yamlData.Processes
_, err = c.RegisterUnit(appName, fullYamlData)
return err
}

Expand Down Expand Up @@ -111,7 +115,7 @@ func inspect(dockerClient *docker.Client, image string, filesystem Filesystem, w
if err != nil {
return fmt.Errorf("failed to inspect image %q: %v", image, err)
}
tsuruYaml, err := loadTsuruYaml(filesystem)
tsuruYaml, err := loadAllTsuruYaml(filesystem)
if err != nil {
return fmt.Errorf("failed to load tsuru yaml: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/tsuru/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,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 map[string]interface{}) ([]bind.EnvVar, error) {
var err error
var yamlData []byte
if !customData.IsEmpty() {
if len(customData) != 0 {
yamlData, err = json.Marshal(customData)
if err != nil {
return nil, err
Expand Down
11 changes: 8 additions & 3 deletions internal/tsuru/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *S) TestClient(c *check.C) {
c.Assert(val.Get("customdata"), check.Equals, "")
} else {
customdata := val.Get("customdata")
expected := "{\"hooks\":{\"build\":[\"ls\",\"ls\"],\"restart\":null},\"processes\":{\"web\":\"test\"}}"
expected := "{\"hooks\":{\"build\":[\"ls\",\"ls\"]},\"processes\":{\"web\":\"test\"}}"
c.Assert(customdata, check.Equals, expected)
}
envs := []bind.EnvVar{{
Expand All @@ -56,9 +56,14 @@ func (s *S) TestClient(c *check.C) {
Token: "test-token",
Version: "0.2.1",
}
_, err := cli.RegisterUnit("test", TsuruYaml{})
_, err := cli.RegisterUnit("test", nil)
c.Assert(err, check.IsNil)
t := TsuruYaml{Hooks: Hook{BuildHooks: []string{"ls", "ls"}}, Processes: map[string]string{"web": "test"}}
t := map[string]interface{}{
"hooks": map[string]interface{}{
"build": []string{"ls", "ls"},
},
"processes": map[string]string{"web": "test"},
}
_, err = cli.RegisterUnit("test", t)
c.Assert(err, check.IsNil)
}
Expand Down
17 changes: 17 additions & 0 deletions tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ func loadTsuruYaml(fs Filesystem) (tsuru.TsuruYaml, error) {
return tsuruYamlData, nil
}

func loadAllTsuruYaml(fs Filesystem) (map[string]interface{}, error) {
var tsuruYamlData map[string]interface{}
for _, yamlFile := range tsuruYamlFiles {
filePath := fmt.Sprintf("%s/%s", defaultWorkingDir, yamlFile)
tsuruYaml, err := fs.ReadFile(filePath)
if err != nil {
continue
}
err = yaml.Unmarshal(tsuruYaml, &tsuruYamlData)
if err != nil {
return map[string]interface{}{}, err
}
break
}
return tsuruYamlData, nil
}

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 ----")
Expand Down
4 changes: 2 additions & 2 deletions tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ healthcheck:
Healthcheck: map[string]interface{}{
"path": "/test",
"method": "GET",
"status": 200,
"status": float64(200),
"match": ".*OK",
"allowed_failures": 0,
"allowed_failures": float64(0),
},
}
t, err := loadTsuruYaml(s.fs)
Expand Down

0 comments on commit ed371f6

Please sign in to comment.