Exemple #1
0
func (parser *Parser) mapStage(stageMap map[interface{}]interface{},
	requiredStages map[string]map[interface{}]interface{}) (stages.Stage, error) {
	mergedStageMap, err := parser.extractStage(stageMap, requiredStages)
	if err != nil {
		return nil, err
	}
	stage, err := parser.initStage(mergedStageMap)
	if err != nil {
		return nil, err
	}

	if stageName := mergedStageMap["name"]; stageName != nil {
		stage.SetStageName(mergedStageMap["name"].(string))
	} else if stageName := mergedStageMap["stage_name"]; stageName != nil {
		log.Warn("found property \"stage_name\"")
		log.Warn("property \"stage_name\" is deprecated. please use \"stage\" instead.")
		stage.SetStageName(mergedStageMap["stage_name"].(string))
	}

	stageOpts := stages.NewStageOpts()
	if reportingFullOutput := mergedStageMap["report_full_output"]; reportingFullOutput != nil {
		stageOpts.ReportingFullOutput = true
	}
	stage.SetStageOpts(*stageOpts)

	newStageValue := reflect.ValueOf(stage).Elem()
	newStageType := reflect.TypeOf(stage).Elem()
	for i := 0; i < newStageType.NumField(); i++ {
		tagName := newStageType.Field(i).Tag.Get("config")
		isReplace := newStageType.Field(i).Tag.Get("is_replace")
		for stageOptKey, stageOptVal := range mergedStageMap {
			if tagName != stageOptKey {
				continue
			} else if stageOptVal == nil {
				log.Warnf("stage option \"%s\" is not specified", stageOptKey)
				continue
			}
			parser.setFieldVal(newStageValue.Field(i), stageOptVal, isReplace)
		}
	}

	parallelStages := mergedStageMap["parallel"]
	if parallelStages == nil {
		if parallelStages = mergedStageMap["run_after"]; parallelStages != nil {
			log.Warn("`run_after' will be obsoleted in near future. Use `parallel' instead.")
		}
	}

	if parallelStages != nil {
		for _, parallelStage := range parallelStages.([]interface{}) {
			childStage, err := parser.mapStage(parallelStage.(map[interface{}]interface{}), requiredStages)
			if err != nil {
				return nil, err
			}
			stage.AddChildStage(childStage)
		}
	}
	return stage, nil
}
Exemple #2
0
func (envVariables *EnvVariables) regexReplace(input string) string {
	matched := (*envVariables.envPattern).FindStringSubmatch(input)
	if len(matched) == 2 {
		if replaced := (*envVariables.variables)[matched[1]]; replaced != "" {
			return replaced
		}
		log.Warnf("NO environment variable: %s", matched[0])
		return ""

	}
	return input
}
Exemple #3
0
//LoadLastUpdate loads the last update
func LoadLastUpdate(fname string) (Update, error) {
	file, err := ioutil.ReadFile(fname)
	log.Infof("opening file: \"%s\"...", fname)
	if err != nil {
		log.Warnf("error occured opening file: \"%s\" ...", fname)
		log.Warnf(err.Error())
		log.Warnf("continue the process with the new settings")
		update := Update{
			Time:      time.Date(1970, time.November, 10, 15, 0, 0, 0, time.Local),
			Succeeded: true,
			Status:    "inprogress"}
		return update, nil
	}

	log.Infof("loading last update form \"%s\"\n", string(file))
	var update Update
	if err := json.Unmarshal(file, &update); err != nil {
		log.Warnf("failed to load \"%s\" ...", fname)
		log.Warnf(err.Error())
		log.Warnf("continue the process with the new settings")
		update := Update{
			Time:      time.Now(),
			Succeeded: true,
			Status:    "inprogress"}
		return update, nil
	}

	if update.Status == "inprogress" {
		return Update{}, errors.New("update is currently run in another process")
	}

	log.Info("setting update status into \"inprogress\"...")
	return update, nil
}
Exemple #4
0
func execCommand(cmd *exec.Cmd, prefix string, name string) (bool, *string, *string) {
	out, err := cmd.StdoutPipe()
	outE, errE := cmd.StderrPipe()

	if err != nil {
		log.Warnf("[command] %s err: %s", prefix, out)
		log.Warnf("[command] %s err: %s", prefix, err)
		return false, nil, nil
	}

	if errE != nil {
		log.Warnf("[command] %s err: %s", prefix, outE)
		log.Warnf("[command] %s err: %s", prefix, errE)
		return false, nil, nil
	}

	err = cmd.Start()
	if err != nil {
		log.Warnf("[command] %s err: %s", prefix, err)
		return false, nil, nil
	}
	outResult := copyStream(out, prefix, name)
	errResult := copyStream(outE, prefix, name)

	err = cmd.Wait()
	if err != nil {
		log.Warnf("[command] %s err: %s", prefix, err)
		return false, &outResult, &errResult
	}
	return true, &outResult, &errResult
}
Exemple #5
0
// Run registered commands.
func (commandStage *CommandStage) Run() bool {
	// Check OnlyIf
	if commandStage.runOnlyIf() == false {
		log.Warnf("[command] exec: skipped this stage \"%s\", since only_if condition failed", commandStage.BaseStage.StageName)
		return true
	}

	// Run command
	result := commandStage.runCommand()
	if result == false {
		log.Errorf("[command] exec: failed stage \"%s\"", commandStage.BaseStage.StageName)
	}
	return result
}
Exemple #6
0
func (e *Engine) executeStage(stage stages.Stage, received []stages.Mediator) string {
	var result string
	if !e.isUpstreamAnyFailure(received) || e.Opts.StopOnAnyFailure {
		result = strconv.FormatBool(stage.(stages.Runner).Run())
		e.EnvVariables.ExportSpecialVariable("__OUT[\""+stage.GetStageName()+"\"]", stage.GetOutResult())
		e.EnvVariables.ExportSpecialVariable("__ERR[\""+stage.GetStageName()+"\"]", stage.GetErrResult())
		e.EnvVariables.ExportSpecialVariable("__RESULT[\""+stage.GetStageName()+"\"]", result)
	} else {
		log.Warnf("Execution is skipped: %v", stage.GetStageName())
		result = "skipped"
	}
	e.Resources.ReportStageResult(stage, result)
	log.Debugf("Stage execution results: %+v, %+v", stage.GetStageName(), result)
	return result
}
Exemple #7
0
func (e *Engine) executeStage(stage stages.Stage, received []stages.Mediator, mediator stages.Mediator) string {
	var result string
	if !e.isUpstreamAnyFailure(received) || e.Opts.StopOnAnyFailure {
		result = strconv.FormatBool(stage.(stages.Runner).Run())
		e.EnvVariables.ExportSpecialVariable("__OUT[\""+stage.GetStageName()+"\"]", stage.GetOutResult())
		e.EnvVariables.ExportSpecialVariable("__ERR[\""+stage.GetStageName()+"\"]", stage.GetErrResult())
		e.EnvVariables.ExportSpecialVariable("__COMBINED[\""+stage.GetStageName()+"\"]", stage.GetCombinedResult())
		e.EnvVariables.ExportSpecialVariable("__RESULT[\""+stage.GetStageName()+"\"]", result)
		e.executeChildStages(&stage, &mediator)
	} else {
		log.Warnf("Execution is skipped: %v", stage.GetStageName())
		if childStages := stage.GetChildStages(); childStages.Len() > 0 {
			for childStage := childStages.Front(); childStage != nil; childStage = childStage.Next() {
				log.Warnf("Execution of child stage is skipped: %v", childStage.Value.(stages.Stage).GetStageName())
			}
		}
		result = "skipped"
	}
	if !stage.GetSuppressAll() {
		e.Resources.ReportStageResult(stage, result)
	}
	log.Debugf("Stage execution results: %+v, %+v", stage.GetStageName(), result)
	return result
}
Exemple #8
0
func execCommand(cmd *exec.Cmd, prefix string, name string) (bool, *string, *string, *string) {
	outPipe, err := cmd.StdoutPipe()

	if err != nil {
		log.Warnf("[command] %s err: %s", prefix, outPipe)
		log.Warnf("[command] %s err: %s", prefix, err)
		return false, nil, nil, nil
	}

	errPipe, err := cmd.StderrPipe()

	if err != nil {
		log.Warnf("[command] %s err: %s", prefix, errPipe)
		log.Warnf("[command] %s err: %s", prefix, err)
		return false, nil, nil, nil
	}

	var bufout, buferr bytes.Buffer

	outReader := io.TeeReader(outPipe, &bufout)
	errReader := io.TeeReader(errPipe, &buferr)

	if err = cmd.Start(); err != nil {
		log.Warnf("[command] %s err: %s", prefix, err)
		return false, nil, nil, nil
	}

	var combinedResult string

	out := make(chan string)
	done := make(chan bool)

	go printOutput(outReader, prefix, name, out, done)
	go printOutput(errReader, prefix, name, out, done)

	doneCount := 0

loop:
	for {
		select {
		case s := <-out:
			combinedResult += s
		case <-done:
			doneCount++
			if doneCount > 1 {
				break loop
			}
		}
	}

	err = cmd.Wait()

	outResult := bufout.String()
	errResult := buferr.String()

	if err != nil {
		log.Warnf("[command] %s err: %s", prefix, err)
		return false, &outResult, &errResult, &combinedResult
	}
	return true, &outResult, &errResult, &combinedResult
}