Skip to content

Commit

Permalink
Use arrays for service commands (#51)
Browse files Browse the repository at this point in the history
This allows multiple commands for each phase of a service
  • Loading branch information
cpg1111 authored Jul 22, 2016
1 parent 4ef5a0a commit 6187c27
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 44 deletions.
10 changes: 5 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ type Service struct {
TagType string
Path string
BuildLogFilePath string
BuildCMD string
TestCMD string
CheckCMD string
CreateCMD string
UpdateCMD string
BuildCMD []string
TestCMD []string
CheckCMD []string
CreateCMD []string
UpdateCMD []string
HealthCheck HealthCheck `toml:"[Services.HealthCheck]"`
DependsOn []string
}
Expand Down
10 changes: 5 additions & 5 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ func TestLoad(t *testing.T) {
TagType: "",
Path: ".",
BuildLogFilePath: "./test.log",
BuildCMD: "bash -c 'docker build -f Dockerfile_build -t maestro_build . && docker run -v $(pwd)/dist:/opt/bin/ && docker build -t cpg1111/maestro .'",
TestCMD: "go test -v ./...",
CheckCMD: "docker ps -a",
CreateCMD: "docker push cpg1111/maestro",
UpdateCMD: "docker rm -f test && docker run -n test -d test",
BuildCMD: []string{"bash -c 'docker build -f Dockerfile_build -t maestro_build . && docker run -v $(pwd)/dist:/opt/bin/ && docker build -t cpg1111/maestro .'"},
TestCMD: []string{"go test -v ./..."},
CheckCMD: []string{"docker ps -a"},
CreateCMD: []string{"docker push cpg1111/maestro"},
UpdateCMD: []string{"docker rm -f test && docker run -n test -d test"},
DependsOn: []string{},
HealthCheck: HealthCheck{
Type: "",
Expand Down
80 changes: 46 additions & 34 deletions pipeline/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,65 +184,77 @@ func (s *Service) execSrvCmd(cmdStr, path string) (*exec.Cmd, error) {
}

func (s *Service) execCheck() (bool, error) {
if s.conf.CheckCMD == "" {
if len(s.conf.CheckCMD) == 0 {
return true, nil
}
cmdStr, tmplErr := util.TemplateCommits(s.conf.CheckCMD, s.lastCommit, s.currCommit)
if tmplErr != nil {
return false, tmplErr
}
cmd, cmdErr := util.FmtCommand(cmdStr, s.conf.Path)
if cmdErr != nil {
return false, cmdErr
}
checkErr := cmd.Run()
if checkErr != nil {
return false, checkErr
for i := range s.conf.CheckCMD {
cmdStr, tmplErr := util.TemplateCommits(s.conf.CheckCMD[i], s.lastCommit, s.currCommit)
if tmplErr != nil {
return false, tmplErr
}
cmd, cmdErr := util.FmtCommand(cmdStr, s.conf.Path)
if cmdErr != nil {
return false, cmdErr
}
checkErr := cmd.Run()
if checkErr != nil {
return false, checkErr
}
}
return true, nil
}

func (s *Service) execBuild() error {
_, err := s.execSrvCmd(s.conf.BuildCMD, s.conf.Path)
for i := range s.conf.BuildCMD {
_, err := s.execSrvCmd(s.conf.BuildCMD[i], s.conf.Path)
return err
}
log.Println("Built")
return err
return nil
}

func (s *Service) execTests() error {
log.Println("Testing")
_, err := s.execSrvCmd(s.conf.TestCMD, s.conf.Path)
for i := range s.conf.TestCMD {
_, err := s.execSrvCmd(s.conf.TestCMD[i], s.conf.Path)
return err
}
log.Println("Tested")
return err
return nil
}

func (s *Service) execCreate() error {
if s.conf.CreateCMD == "" {
if len(s.conf.CreateCMD) == 0 {
return nil
}
cmd, err := s.execSrvCmd(s.conf.CreateCMD, s.conf.Path)
if err != nil {
return err
}
if s.conf.HealthCheck.Type == "PTRACE_ATTACH" {
passPid := HealthCheck(&s.conf).(func(pid int) error)
return passPid(cmd.Process.Pid).(error)
for i := range s.conf.CreateCMD {
cmd, err := s.execSrvCmd(s.conf.CreateCMD[i], s.conf.Path)
if err != nil {
return err
}
if s.conf.HealthCheck.Type == "PTRACE_ATTACH" {
passPid := HealthCheck(&s.conf).(func(pid int) error)
return passPid(cmd.Process.Pid).(error)
}
}
return HealthCheck(&s.conf).(error)
}

func (s *Service) execUpdate() error {
if s.conf.UpdateCMD == "" {
if len(s.conf.UpdateCMD) == 0 {
return nil
}
cmd, err := s.execSrvCmd(s.conf.UpdateCMD, s.conf.Path)
if err != nil {
return err
}
if s.conf.HealthCheck.Type == "PTRACE_ATTACH" {
passPid := HealthCheck(&s.conf).(func(pid int) error)
passed := passPid(cmd.Process.Pid)
if passed != nil {
return passed.(error)
for i := range s.conf.UpdateCMD {
cmd, err := s.execSrvCmd(s.conf.UpdateCMD[i], s.conf.Path)
if err != nil {
return err
}
if s.conf.HealthCheck.Type == "PTRACE_ATTACH" {
passPid := HealthCheck(&s.conf).(func(pid int) error)
passed := passPid(cmd.Process.Pid)
if passed != nil {
return passed.(error)
}
}
}
checkRes := HealthCheck(&s.conf)
Expand Down

0 comments on commit 6187c27

Please sign in to comment.