func (e *Engine) executeAllChildStages(childStages *list.List, mediator stages.Mediator) { for childStage := childStages.Front(); childStage != nil; childStage = childStage.Next() { log.Debugf("Child name %+v\n", childStage.Value.(stages.Stage).GetStageName()) childInputCh := *childStage.Value.(stages.Stage).GetInputCh() go func(stage stages.Stage) { e.ExecuteStage(stage) }(childStage.Value.(stages.Stage)) log.Debugf("Input child: %+v", mediator) childInputCh <- mediator log.Debugf("Closing input: %+v", childStage.Value.(stages.Stage).GetStageName()) close(childInputCh) } }
func (e *Engine) waitAllChildStages(childStages *list.List, stage *stages.Stage) { for childStage := childStages.Front(); childStage != nil; childStage = childStage.Next() { s := childStage.Value.(stages.Stage) for { log.Debugf("Receiving child: %v", s.GetStageName()) childReceived, ok := <-*s.GetOutputCh() if !ok { log.Debug("Closing child output") break } log.Debugf("Sending child: %v", childReceived) *(*stage).GetOutputCh() <- childReceived log.Debugf("Send child: %v", childReceived) } log.Debugf("Finished executing child: %v", s.GetStageName()) } }
//Execute executes a stage using the supplied mediator func (e *Engine) Execute(stage stages.Stage, mediator stages.Mediator) stages.Mediator { monitorCh := e.MonitorCh mediator.Type = "start" name := stage.GetStageName() log.Debugf("----- Execute %v start ------\n", name) go func(mediator stages.Mediator) { *stage.GetInputCh() <- mediator close(*stage.GetInputCh()) }(mediator) go e.ExecuteStage(stage) for { receive, ok := <-*stage.GetOutputCh() if !ok { log.Debugf("outputCh closed") break } log.Debugf("outputCh received %+v\n", receive) } var receives = make([]stages.Mediator, 0) for { receive := <-*monitorCh receives = append(receives, receive) if receive.Type == "end" { log.Debugf("monitorCh closed") log.Debugf("monitorCh last received: %+v\n", receive) log.Debugf("----- Execute %v done ------\n\n", name) return e.bindReceives(&receives) } log.Debugf("monitorCh received %+v\n", receive) } }
func (e *Engine) finalizeMonitorChAfterExecute(mediators []stages.Mediator) { if mediators[0].Type == "start" { log.Debug("Finalize monitor channel..") mediatorEnd := stages.Mediator{States: make(map[string]string), Type: "end"} *e.MonitorCh <- mediatorEnd } else { log.Debugf("Skipped finalizing") } }
func (commandStage *CommandStage) runCommand() bool { cmd := exec.Command("sh", "-c", commandStage.Command) log.Infof("[command] exec: %s", commandStage.BaseStage.StageName) log.Debugf("[command] exec command literal: %s", commandStage.Command) cmd.Dir = commandStage.Directory result, outResult, errResult := execCommand(cmd, "exec", commandStage.BaseStage.StageName) commandStage.SetOutResult(*outResult) commandStage.SetErrResult(*errResult) return result }
// Run exectues specified shell script. func (shellScriptStage *ShellScriptStage) Run() bool { log.Infof("[shell] exec: %s", shellScriptStage.BaseStage.StageName) log.Debugf("[shell] specified file: %s\n", shellScriptStage.File) if shellScriptStage.preCheck() == false { log.Infof("failed preCheck before running script...") return false } shellScriptStage.AddCommand("sh " + shellScriptStage.File) return shellScriptStage.CommandStage.Run() }
func (e *Engine) executePipeline(pipeline *pipelines.Pipeline, name string) stages.Mediator { log.Infof("Preparing to run %s process...", name) var mediator stages.Mediator for stageItem := pipeline.Stages.Front(); stageItem != nil; stageItem = stageItem.Next() { log.Debugf("Executing planned stage: %s\n", stageItem.Value) mediator = e.Execute(stageItem.Value.(stages.Stage), mediator) } log.Infof("Finished running %s process...", name) return mediator }
func (commandStage *CommandStage) runOnlyIf() bool { if commandStage.OnlyIf == "" { return true } log.Infof("[command] only_if: found \"only_if\" attribute in stage \"%s\"", commandStage.BaseStage.StageName) cmd := exec.Command("sh", "-c", commandStage.OnlyIf) log.Infof("[command] only_if: %s", commandStage.BaseStage.StageName) log.Debugf("[command] only_if literal: %s", commandStage.OnlyIf) cmd.Dir = commandStage.Directory result, _, _ := execCommand(cmd, "only_if", commandStage.BaseStage.StageName) return result }
func (e *Engine) receiveInputs(inputCh *chan stages.Mediator) []stages.Mediator { var mediatorsReceived = make([]stages.Mediator, 0) for { m, ok := <-*inputCh if !ok { break } log.Debugf("received input: %+v", m) mediatorsReceived = append(mediatorsReceived, m) } return mediatorsReceived }
func (self *ResourceValidator) Validate() bool { // check if files exists for file := self.files.Front(); file != nil; file = file.Next() { filePath := file.Value.(string) log.Debugf("checking file: %v", filePath) if _, err := os.Stat(filePath); err == nil { log.Debugf("file exists") } else { log.Errorf("file: %v does not exists", filePath) return false } } // check if command exists if len(self.command) == 0 { // return true when no command is registrated return true } cmd := exec.Command("which", self.command) err := cmd.Run() if err != nil { log.Errorf("command: %v does not exists", self.command) return false } return true }
// ExecuteStage executes the supplied stage func (e *Engine) ExecuteStage(stage stages.Stage) { log.Debug("Receiving input") mediatorsReceived := e.receiveInputs(stage.GetInputCh()) log.Debugf("Received input size: %v", len(mediatorsReceived)) log.Debugf("Mediator received: %+v", mediatorsReceived) log.Debugf("Execute as parent: %+v", stage) log.Debugf("Execute as parent name %+v", stage.GetStageName()) var result string if !e.isUpstreamAnyFailure(mediatorsReceived) || e.Opts.StopOnAnyFailure { result = strconv.FormatBool(stage.(stages.Runner).Run()) } else { log.Warnf("Execution is skipped: %v", stage.GetStageName()) result = "skipped" } log.Debugf("Stage execution results: %+v, %+v", stage.GetStageName(), result) e.Resources.ReportStageResult(stage, result) mediator := stages.Mediator{States: make(map[string]string)} mediator.States[stage.GetStageName()] = result if childStages := stage.GetChildStages(); childStages.Len() > 0 { log.Debugf("Execute childstage: %v", childStages) e.executeAllChildStages(&childStages, mediator) e.waitAllChildStages(&childStages, &stage) } log.Debugf("Sending output of stage: %+v %v", stage.GetStageName(), mediator) *stage.GetOutputCh() <- mediator log.Debugf("Closing output of stage: %+v", stage.GetStageName()) close(*stage.GetOutputCh()) for _, m := range mediatorsReceived { *e.MonitorCh <- m } *e.MonitorCh <- mediator e.finalizeMonitorChAfterExecute(mediatorsReceived) }
// AddChildStage appends one child stage. func (b *BaseStage) AddChildStage(stage Stage) { log.Debugf("added childstage: %v", stage) b.ChildStages.PushBack(stage) }