func Factory()

in internal/servicedeployer/factory.go [43:121]


func Factory(options FactoryOptions) (ServiceDeployer, error) {
	devDeployPath, err := FindDevDeployPath(options)
	if err != nil {
		return nil, fmt.Errorf("can't find \"%s\" directory: %w", options.DevDeployDir, err)
	}

	serviceDeployerName, err := findServiceDeployer(devDeployPath)
	if err != nil {
		return nil, fmt.Errorf("can't find any valid service deployer: %w", err)
	}

	serviceDeployerPath := filepath.Join(devDeployPath, serviceDeployerName)

	switch serviceDeployerName {
	case "k8s":
		if _, err := os.Stat(serviceDeployerPath); err == nil {
			opts := KubernetesServiceDeployerOptions{
				Profile:                options.Profile,
				DefinitionsDir:         serviceDeployerPath,
				StackVersion:           options.StackVersion,
				PolicyName:             options.PolicyName,
				RunSetup:               options.RunSetup,
				RunTestsOnly:           options.RunTestsOnly,
				RunTearDown:            options.RunTearDown,
				DeployIndependentAgent: options.DeployIndependentAgent,
			}
			return NewKubernetesServiceDeployer(opts)
		}
	case "docker":
		dockerComposeYMLPath := filepath.Join(serviceDeployerPath, "docker-compose.yml")
		if _, err := os.Stat(dockerComposeYMLPath); err == nil {
			sv, err := useServiceVariant(devDeployPath, options.Variant)
			if err != nil {
				return nil, fmt.Errorf("can't use service variant: %w", err)
			}
			opts := DockerComposeServiceDeployerOptions{
				Profile:                options.Profile,
				YmlPaths:               []string{dockerComposeYMLPath},
				Variant:                sv,
				RunTearDown:            options.RunTearDown,
				RunTestsOnly:           options.RunTestsOnly,
				DeployIndependentAgent: options.DeployIndependentAgent,
			}
			return NewDockerComposeServiceDeployer(opts)
		}
	case "agent":
		// FIXME: This docker-compose scenario contains also the definition of the elastic-agent container
		if options.Type != TypeTest {
			return nil, fmt.Errorf("agent deployer is not supported for type %s", options.Type)
		}
		customAgentCfgYMLPath := filepath.Join(serviceDeployerPath, "custom-agent.yml")
		if _, err := os.Stat(customAgentCfgYMLPath); err != nil {
			return nil, fmt.Errorf("can't find expected file custom-agent.yml: %w", err)
		}
		policyName := getTokenPolicyName(options.StackVersion, options.PolicyName)

		opts := CustomAgentDeployerOptions{
			Profile:           options.Profile,
			DockerComposeFile: customAgentCfgYMLPath,
			StackVersion:      options.StackVersion,
			PolicyName:        policyName,

			RunTearDown:  options.RunTearDown,
			RunTestsOnly: options.RunTestsOnly,
		}
		return NewCustomAgentDeployer(opts)
	case "tf":
		if options.RunSetup || options.RunTearDown || options.RunTestsOnly {
			return nil, errors.New("terraform service deployer not supported to run by steps")
		}
		if _, err := os.Stat(serviceDeployerPath); err == nil {
			opts := TerraformServiceDeployerOptions{
				DefinitionsDir: serviceDeployerPath,
			}
			return NewTerraformServiceDeployer(opts)
		}
	}
	return nil, fmt.Errorf("unsupported service deployer (name: %s)", serviceDeployerName)
}