Exemplo n.º 1
0
func (store *GardenStore) Create(logger lager.Logger, container executor.Container) (executor.Container, error) {
	if container.State != executor.StateInitializing {
		return executor.Container{}, executor.ErrInvalidTransition
	}
	container.State = executor.StateCreated

	logStreamer := log_streamer.New(
		container.LogConfig.Guid,
		container.LogConfig.SourceName,
		container.LogConfig.Index,
	)

	fmt.Fprintf(logStreamer.Stdout(), "Creating container\n")

	container, err := store.exchanger.CreateInGarden(logger, store.gardenClient, container)
	if err != nil {
		fmt.Fprintf(logStreamer.Stderr(), "Failed to create container\n")
		return executor.Container{}, err
	}

	fmt.Fprintf(logStreamer.Stdout(), "Successfully created container\n")

	return container, nil
}
Exemplo n.º 2
0
func (store *GardenStore) Run(logger lager.Logger, container executor.Container) error {
	logger = logger.Session("run", lager.Data{
		"guid": container.Guid,
	})
	logger.Info("started")
	defer logger.Info("finished")

	gardenContainer, err := store.lookup(logger, container.Guid)
	if err != nil {
		return err
	}
	logger.Debug("found-garden-container")

	if container.State != executor.StateCreated {
		logger.Debug("container-invalid-state-transition", lager.Data{
			"current_state":  container.State,
			"expected_state": executor.StateCreated,
		})

		transitionErr := executor.ErrInvalidTransition
		result := executor.ContainerRunResult{
			Failed:        true,
			FailureReason: transitionErr.Error(),
		}

		err := store.transitionToComplete(logger, gardenContainer, result)
		if err != nil {
			logger.Error("failed-transition-to-complete", err)
		}

		return transitionErr
	}

	logStreamer := log_streamer.New(
		container.LogConfig.Guid,
		container.LogConfig.SourceName,
		container.LogConfig.Index,
	)

	var setupStep, actionStep, monitorStep steps.Step

	if container.Setup != nil {
		setupStep = store.transformer.StepFor(
			logStreamer,
			container.Setup,
			gardenContainer,
			container.ExternalIP,
			container.Ports,
			logger.Session("setup"),
		)
	}

	actionStep = store.transformer.StepFor(
		logStreamer,
		container.Action,
		gardenContainer,
		container.ExternalIP,
		container.Ports,
		logger.Session("action"),
	)

	hasStartedRunning := make(chan struct{}, 1)

	if container.Monitor != nil {
		monitorStep = steps.NewMonitor(
			func() steps.Step {
				return store.transformer.StepFor(
					logStreamer,
					container.Monitor,
					gardenContainer,
					container.ExternalIP,
					container.Ports,
					logger.Session("monitor-run"),
				)
			},
			hasStartedRunning,
			logger.Session("monitor"),
			store.clock,
			logStreamer,
			time.Duration(container.StartTimeout)*time.Second,
			store.healthyMonitoringInterval,
			store.unhealthyMonitoringInterval,
			store.workPool,
		)
	}

	var longLivedAction steps.Step
	if monitorStep != nil {
		longLivedAction = steps.NewCodependent([]steps.Step{actionStep, monitorStep}, false)
	} else {
		longLivedAction = actionStep

		// this container isn't monitored, so we mark it running right away
		hasStartedRunning <- struct{}{}
	}

	var step steps.Step
	if setupStep != nil {
		step = steps.NewSerial([]steps.Step{setupStep, longLivedAction})
	} else {
		step = longLivedAction
	}

	store.runStepProcess(logger, step, hasStartedRunning, gardenContainer, container.Guid)

	return nil
}