Skip to content

Commit

Permalink
removes global executor
Browse files Browse the repository at this point in the history
  • Loading branch information
andrestc committed Mar 29, 2018
1 parent b34ab9e commit 437137b
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 31 deletions.
9 changes: 5 additions & 4 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@ package main
import (
"log"

"github.com/tsuru/tsuru/exec"
"github.com/tsuru/tsuru/fs"
)

func build(c Client, appName string, cmd []string, filesystem fs.Fs) {
func build(c Client, appName string, cmd []string, filesystem fs.Fs, executor exec.Executor) {
log.SetFlags(0)
envs, err := c.getAppEnvs(appName)
if err != nil {
log.Fatal(err)
}
err = execScript(cmd, envs, nil, filesystem)
err = execScript(cmd, envs, nil, filesystem, executor)
if err != nil {
log.Fatal(err)
}
}

func deploy(c Client, appName string, filesystem fs.Fs) {
func deploy(c Client, appName string, filesystem fs.Fs, executor exec.Executor) {
log.SetFlags(0)
var yamlData TsuruYaml
envs, err := c.registerUnit(appName, yamlData)
Expand All @@ -43,7 +44,7 @@ func deploy(c Client, appName string, filesystem fs.Fs) {
if err != nil {
log.Fatal(err)
}
err = buildHooks(yamlData, envs, filesystem)
err = buildHooks(yamlData, envs, filesystem, executor)
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (s *S) TestBuild(c *check.C) {
URL: server.URL,
Token: "fake-token",
}
build(client, "app1", []string{"ls"}, s.fs)
build(client, "app1", []string{"ls"}, s.fs, s.exec)
}

func (s *S) TestDeploy(c *check.C) {
Expand Down Expand Up @@ -71,5 +71,5 @@ func (s *S) TestDeploy(c *check.C) {
URL: server.URL,
Token: "fake-token",
}
deploy(client, "app1", s.fs)
deploy(client, "app1", s.fs, s.exec)
}
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ func main() {

switch command[len(command)-1] {
case "build":
build(c, appName, command[:len(command)-1], &fs.OsFs{})
build(c, appName, command[:len(command)-1], &fs.OsFs{}, &exec.OsExecutor{})
case "deploy-only":
deploy(c, appName, &fs.OsFs{})
deploy(c, appName, &fs.OsFs{}, &exec.OsExecutor{})
case "deploy":
// backward compatibility with tsuru < 1.4.0
command = command[:len(command)-1]
fallthrough
default:
build(c, appName, command, &fs.OsFs{})
deploy(c, appName, &fs.OsFs{})
build(c, appName, command, &fs.OsFs{}, &exec.OsExecutor{})
deploy(c, appName, &fs.OsFs{}, &exec.OsExecutor{})
}
}
2 changes: 1 addition & 1 deletion sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type dockerExecutor struct {

func (d *dockerExecutor) Execute(opts exec.ExecuteOptions) error {
if d.executor == nil {
d.executor = executor()
d.executor = &exec.OsExecutor{}
}
opts.Args = append([]string{"exec", "-t", d.containerID, opts.Cmd}, opts.Args...)
opts.Cmd = dockerBinary
Expand Down
2 changes: 1 addition & 1 deletion sidecar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func (s *S) TestDockerExecutor(c *check.C) {
e := dockerExecutor{containerID: "dd5e0fbf6d3c"}
e := dockerExecutor{containerID: "dd5e0fbf6d3c", executor: s.exec}
e.Execute(exec.ExecuteOptions{
Cmd: "/bin/ps",
})
Expand Down
1 change: 0 additions & 1 deletion suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ var _ = check.Suite(&S{})
func (s *S) SetUpTest(c *check.C) {
s.fs = &fstest.RecordingFs{}
s.exec = &exectest.FakeExecutor{}
osExecutor = s.exec
err := s.fs.Mkdir(defaultWorkingDir, 0777)
c.Assert(err, check.IsNil)
}
17 changes: 4 additions & 13 deletions tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,11 @@ var (
appEnvsFile = "/tmp/app_envs"
)

var osExecutor exec.Executor

func executor() exec.Executor {
if osExecutor == nil {
return &exec.OsExecutor{}
}
return osExecutor
}

func execScript(cmds []string, envs []bind.EnvVar, w io.Writer, filesystem fs.Fs) error {
func execScript(cmds []string, envs []bind.EnvVar, w io.Writer, filesystem fs.Fs, executor exec.Executor) error {
if w == nil {
w = ioutil.Discard
}
currentExecutor, err := user.ChangeUser(executor(), envs)
currentExecutor, err := user.ChangeUser(executor, envs)
if err != nil {
return err
}
Expand Down Expand Up @@ -111,10 +102,10 @@ func loadTsuruYaml(filesystem fs.Fs) (TsuruYaml, error) {
return tsuruYamlData, nil
}

func buildHooks(yamlData TsuruYaml, envs []bind.EnvVar, filesystem fs.Fs) error {
func buildHooks(yamlData TsuruYaml, envs []bind.EnvVar, filesystem fs.Fs, executor exec.Executor) error {
cmds := append([]string{}, yamlData.Hooks.BuildHooks...)
fmt.Fprintln(os.Stdout, "---- Running build hooks ----")
return execScript(cmds, envs, os.Stdout, filesystem)
return execScript(cmds, envs, os.Stdout, filesystem, executor)
}

func readProcfile(path string, filesystem fs.Fs) (string, error) {
Expand Down
9 changes: 4 additions & 5 deletions tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (s *S) TestExecScript(c *check.C) {
Public: true,
}}
buf := bytes.NewBufferString("")
err := execScript(cmds, envs, buf, s.fs)
err := execScript(cmds, envs, buf, s.fs, s.exec)
c.Assert(err, check.IsNil)
executedCmds := s.exec.GetCommands("/bin/bash")
c.Assert(len(executedCmds), check.Equals, 2)
Expand All @@ -52,8 +52,7 @@ func (s *S) TestExecScript(c *check.C) {

func (s *S) TestExecScriptWithError(c *check.C) {
cmds := []string{"not-exists"}
osExecutor = &exectest.ErrorExecutor{}
err := execScript(cmds, nil, nil, s.fs)
err := execScript(cmds, nil, nil, s.fs, &exectest.ErrorExecutor{})
c.Assert(err, check.NotNil)
}

Expand All @@ -68,7 +67,7 @@ func (s *S) TestExecScriptWorkingDirNotExist(c *check.C) {
Value: "bar",
Public: true,
}}
err = execScript(cmds, envs, nil, s.fs)
err = execScript(cmds, envs, nil, s.fs, s.exec)
c.Assert(err, check.IsNil)
executedCmds := s.exec.GetCommands("/bin/bash")
c.Assert(len(executedCmds), check.Equals, 1)
Expand Down Expand Up @@ -124,7 +123,7 @@ func (s *S) TestHooks(c *check.C) {
Value: "bar",
Public: true,
}}
err := buildHooks(tsuruYaml, envs, s.fs)
err := buildHooks(tsuruYaml, envs, s.fs, s.exec)
c.Assert(err, check.IsNil)
executedCmds := s.exec.GetCommands("/bin/bash")
c.Assert(len(executedCmds), check.Equals, 2)
Expand Down

0 comments on commit 437137b

Please sign in to comment.