forked from toolshed/abra
		
	feat(deploy): set timeout via label (!290)
Solves coop-cloud/organising#437 A timeout can be specified globally for a recipe using this label: `coop-cloud.${STACK_NAME}.timeout=${TIMEOUT:-120}`. This sets the default timeout to 120s. An app specific timeout can be set using the env `TIMEOUT`. Co-authored-by: Moritz <moritz.m@local-it.org> Reviewed-on: coop-cloud/abra#290
This commit is contained in:
		@ -201,6 +201,12 @@ recipes.
 | 
			
		||||
			logrus.Fatal(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		stack.WaitTimeout, err = config.GetTimeoutFromLabel(compose, stackName)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			logrus.Fatal(err)
 | 
			
		||||
		}
 | 
			
		||||
		logrus.Debugf("set waiting timeout to %d s", stack.WaitTimeout)
 | 
			
		||||
 | 
			
		||||
		if err := stack.RunDeploy(cl, deployOpts, compose, stackName, internal.DontWaitConverge); err != nil {
 | 
			
		||||
			logrus.Fatal(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@ -161,6 +161,12 @@ func DeployAction(c *cli.Context) error {
 | 
			
		||||
		logrus.Warn("skipping domain checks as requested")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	stack.WaitTimeout, err = config.GetTimeoutFromLabel(compose, stackName)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		logrus.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	logrus.Debugf("set waiting timeout to %d s", stack.WaitTimeout)
 | 
			
		||||
 | 
			
		||||
	if err := stack.RunDeploy(cl, deployOpts, compose, app.Name, DontWaitConverge); err != nil {
 | 
			
		||||
		logrus.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -149,7 +149,7 @@ update chaos deployments use the "--chaos" flag. Use it with care.
 | 
			
		||||
	},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// getLabel reads docker labels in the format of "coop-cloud.${STACK_NAME}.${LABEL}".
 | 
			
		||||
// getLabel reads docker labels from running services in the format of "coop-cloud.${STACK_NAME}.${LABEL}".
 | 
			
		||||
func getLabel(cl *dockerclient.Client, stackName string, label string) (string, error) {
 | 
			
		||||
	filter := filters.NewArgs()
 | 
			
		||||
	filter.Add("label", fmt.Sprintf("%s=%s", convert.LabelNamespace, stackName))
 | 
			
		||||
@ -171,7 +171,7 @@ func getLabel(cl *dockerclient.Client, stackName string, label string) (string,
 | 
			
		||||
	return "", nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// getBoolLabel reads a boolean docker label.
 | 
			
		||||
// getBoolLabel reads a boolean docker label from running services
 | 
			
		||||
func getBoolLabel(cl *dockerclient.Client, stackName string, label string) (bool, error) {
 | 
			
		||||
	lableValue, err := getLabel(cl, stackName, label)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@ -409,7 +409,7 @@ func tryUpgrade(cl *dockerclient.Client, stackName, recipeName string, conf *run
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if !updatesEnabled {
 | 
			
		||||
		logrus.Debugf("Don't update %s due to disabling auto updates or missing ENABLE_AUTO_UPDATE env.", stackName)
 | 
			
		||||
		logrus.Debugf("Don't update %s due to disabled auto updates or missing ENABLE_AUTO_UPDATE env.", stackName)
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -544,3 +544,29 @@ func SetUpdateLabel(compose *composetypes.Config, stackName string, appEnv AppEn
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetLabel reads docker labels in the format of "coop-cloud.${STACK_NAME}.${LABEL}" from the local compose files
 | 
			
		||||
func GetLabel(compose *composetypes.Config, stackName string, label string) string {
 | 
			
		||||
	for _, service := range compose.Services {
 | 
			
		||||
		if service.Name == "app" {
 | 
			
		||||
			labelKey := fmt.Sprintf("coop-cloud.%s.%s", stackName, label)
 | 
			
		||||
			logrus.Debugf("get label '%s'", labelKey)
 | 
			
		||||
			if labelValue, ok := service.Deploy.Labels[labelKey]; ok {
 | 
			
		||||
				return labelValue
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	logrus.Debugf("no %s label found for %s", label, stackName)
 | 
			
		||||
	return ""
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetTimeoutFromLabel reads the timeout value from docker label "coop-cloud.${STACK_NAME}.TIMEOUT" and returns 50 as default value
 | 
			
		||||
func GetTimeoutFromLabel(compose *composetypes.Config, stackName string) (int, error) {
 | 
			
		||||
	var timeout = 50 // Default Timeout
 | 
			
		||||
	var err error = nil
 | 
			
		||||
	if timeoutLabel := GetLabel(compose, stackName, "timeout"); timeoutLabel != "" {
 | 
			
		||||
		logrus.Debugf("timeout label: %s", timeoutLabel)
 | 
			
		||||
		timeout, err = strconv.Atoi(timeoutLabel)
 | 
			
		||||
	}
 | 
			
		||||
	return timeout, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -31,6 +31,9 @@ const (
 | 
			
		||||
	ResolveImageNever    = "never"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Timeout to wait until docker services converge, default is 50s (random choice)
 | 
			
		||||
var WaitTimeout int = 50
 | 
			
		||||
 | 
			
		||||
type StackStatus struct {
 | 
			
		||||
	Services []swarm.Service
 | 
			
		||||
	Err      error
 | 
			
		||||
@ -457,7 +460,7 @@ func WaitOnService(ctx context.Context, cl *dockerClient.Client, serviceID, appN
 | 
			
		||||
 | 
			
		||||
	go io.Copy(ioutil.Discard, pipeReader)
 | 
			
		||||
 | 
			
		||||
	timeout := 50 * time.Second
 | 
			
		||||
	timeout := time.Duration(WaitTimeout) * time.Second
 | 
			
		||||
 | 
			
		||||
	select {
 | 
			
		||||
	case err := <-errChan:
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user