Skip to content

Commit

Permalink
Accept multiple locations for tsuru.yaml file
Browse files Browse the repository at this point in the history
Related to tsuru/tsuru#2133
  • Loading branch information
cezarsa committed Apr 16, 2019
1 parent 1f94879 commit 280cf49
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 27 deletions.
14 changes: 4 additions & 10 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,10 @@ func inspect(dockerClient *docker.Client, image string, filesystem Filesystem, w
if err != nil {
return fmt.Errorf("failed to load tsuru yaml: %v", err)
}
procfileDirs := []string{defaultWorkingDir, "/app/user", ""}
var procfile string
for _, d := range procfileDirs {
procfile, err = readProcfile(d, filesystem)
if err != nil {
// we can safely ignore this error since tsuru may use the image CMD/Entrypoint
fmt.Fprintf(errW, "Unable to read procfile in %v: %v", d, err)
continue
}
break
procfile, err := readProcfile(filesystem)
if err != nil {
// we can safely ignore this error since tsuru may use the image CMD/Entrypoint
fmt.Fprintf(errW, "Unable to read procfile: %v", err)
}
m := tsuru.InspectData{
TsuruYaml: tsuruYaml,
Expand Down
29 changes: 19 additions & 10 deletions tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"

Expand All @@ -24,6 +25,7 @@ import (
var (
defaultWorkingDir = "/home/application/current"
tsuruYamlFiles = []string{"tsuru.yml", "tsuru.yaml", "app.yml", "app.yaml"}
configDirs = []string{defaultWorkingDir, "/app/user", "/"}
appEnvsFile = "/tmp/app_envs"
)

Expand Down Expand Up @@ -73,10 +75,12 @@ func execScript(cmds []string, envs []bind.EnvVar, w io.Writer, fs Filesystem, e

func loadTsuruYamlRaw(fs Filesystem) []byte {
for _, yamlFile := range tsuruYamlFiles {
filePath := fmt.Sprintf("%s/%s", defaultWorkingDir, yamlFile)
tsuruYaml, err := fs.ReadFile(filePath)
if err == nil {
return tsuruYaml
for _, dir := range configDirs {
path := filepath.Join(dir, yamlFile)
tsuruYaml, err := fs.ReadFile(path)
if err == nil {
return tsuruYaml
}
}
}
return nil
Expand All @@ -103,18 +107,23 @@ func buildHooks(yamlData tsuru.TsuruYaml, envs []bind.EnvVar, fs Filesystem, exe
return execScript(cmds, envs, os.Stdout, fs, executor)
}

func readProcfile(path string, fs Filesystem) (string, error) {
procfile, err := fs.ReadFile(fmt.Sprintf("%v/Procfile", path))
if err != nil {
return "", err
func readProcfile(fs Filesystem) (string, error) {
var err error
for _, dir := range configDirs {
path := filepath.Join(dir, "Procfile")
var procfile []byte
procfile, err = fs.ReadFile(path)
if err == nil {
return string(bytes.Replace(procfile, []byte("\r\n"), []byte("\n"), -1)), nil
}
}
return string(bytes.Replace(procfile, []byte("\r\n"), []byte("\n"), -1)), nil
return "", err
}

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

func loadProcesses(t *tsuru.TsuruYaml, fs Filesystem) error {
procfile, err := readProcfile(defaultWorkingDir, fs)
procfile, err := readProcfile(fs)
if err != nil {
return err
}
Expand Down
90 changes: 83 additions & 7 deletions tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,35 +274,59 @@ func (s *S) TestDiffDeploy(c *check.C) {
}

func (s *S) TestReadProcfileNotFound(c *check.C) {
_, err := readProcfile("./fake-path", s.fs)
_, err := readProcfile(s.fs)
_, ok := err.(syscall.Errno)
c.Assert(ok, check.Equals, true)
}

func (s *S) TestReadProcfileMultiplePaths(c *check.C) {
tests := []struct {
path string
}{
{
path: "/Procfile",
},
{
path: "/app/user/Procfile",
},
{
path: "/home/application/current/Procfile",
},
}
for i, tt := range tests {
c.Log("test", i)
procfile, err := s.fs.Create(tt.path)
c.Assert(err, check.IsNil)
_, err = procfile.Write([]byte(fmt.Sprintf("web: a-%d", i)))
c.Assert(err, check.IsNil)
result, err := readProcfile(s.fs)
c.Assert(err, check.IsNil)
c.Assert(result, check.Equals, fmt.Sprintf("web: a-%d", i))
}
}

func (s *S) TestReadProcfileFound(c *check.C) {
expected := "web: ls\naxl: \"echo Guns N' Roses\""
procfilePath := "."
procfileContent := expected
procfile, err := s.fs.Create(fmt.Sprintf("%v/Procfile", procfilePath))
procfile, err := s.fs.Create("/Procfile")
c.Assert(err, check.IsNil)
_, err = procfile.Write([]byte(procfileContent))
c.Assert(err, check.IsNil)
c.Assert(procfile.Close(), check.IsNil)
result, err := readProcfile(procfilePath, s.fs)
result, err := readProcfile(s.fs)
c.Assert(err, check.IsNil)
c.Assert(result, check.Equals, expected)
}

func (s *S) TestReadProcfileNormalizeCRLFToLF(c *check.C) {
procfilePath := "."
procfileContent := "web: ls\r\nslash: \"echo Guns N' Roses\""
expected := "web: ls\nslash: \"echo Guns N' Roses\""
procfile, err := s.fs.Create(fmt.Sprintf("%v/Procfile", procfilePath))
procfile, err := s.fs.Create("/Procfile")
c.Assert(err, check.IsNil)
_, err = procfile.Write([]byte(procfileContent))
c.Assert(err, check.IsNil)
c.Assert(procfile.Close(), check.IsNil)
result, err := readProcfile(procfilePath, s.fs)
result, err := readProcfile(s.fs)
c.Assert(err, check.IsNil)
c.Assert(result, check.Equals, expected)
}
Expand All @@ -318,3 +342,55 @@ func (s *S) TestParseAllTsuruYamlEmpty(c *check.C) {
c.Assert(err, check.IsNil)
c.Assert(t, check.DeepEquals, map[string]interface{}{})
}

func (s *S) TestLoadTsuruYamlRawMultiplePaths(c *check.C) {
tests := []struct {
path string
}{
{
path: "/app.yaml",
},
{
path: "/app/user/app.yaml",
},
{
path: "/home/application/current/app.yaml",
},
{
path: "/app.yml",
},
{
path: "/app/user/app.yml",
},
{
path: "/home/application/current/app.yml",
},
{
path: "/tsuru.yaml",
},
{
path: "/app/user/tsuru.yaml",
},
{
path: "/home/application/current/tsuru.yaml",
},
{
path: "/tsuru.yml",
},
{
path: "/app/user/tsuru.yml",
},
{
path: "/home/application/current/tsuru.yml",
},
}
for i, tt := range tests {
c.Log("test", i)
config, err := s.fs.Create(tt.path)
c.Assert(err, check.IsNil)
_, err = config.Write([]byte(fmt.Sprintf("test: a-%d", i)))
c.Assert(err, check.IsNil)
result := loadTsuruYamlRaw(s.fs)
c.Assert(string(result), check.Equals, fmt.Sprintf("test: a-%d", i))
}
}

0 comments on commit 280cf49

Please sign in to comment.