From 6187c271c8a85616399862bdd1949eb4c3066424 Mon Sep 17 00:00:00 2001 From: Christian Grabowski Date: Fri, 22 Jul 2016 14:11:56 -0400 Subject: [PATCH] Use arrays for service commands (#51) This allows multiple commands for each phase of a service --- config/config.go | 10 +++--- config/config_test.go | 10 +++--- pipeline/service.go | 80 +++++++++++++++++++++++++------------------ 3 files changed, 56 insertions(+), 44 deletions(-) diff --git a/config/config.go b/config/config.go index b07c2cc..9d888e8 100644 --- a/config/config.go +++ b/config/config.go @@ -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 } diff --git a/config/config_test.go b/config/config_test.go index 6a2209a..54801f1 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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: "", diff --git a/pipeline/service.go b/pipeline/service.go index db0409c..e78dca4 100644 --- a/pipeline/service.go +++ b/pipeline/service.go @@ -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)