Skip to content

Add profile flag to commands to allow running profiles at the same time #1368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cmd/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/dustin/go-humanize"

"github.com/elastic/elastic-package/internal/corpusgenerator"
"github.com/elastic/elastic-package/internal/install"
"github.com/elastic/elastic-package/internal/kibana"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -57,6 +58,8 @@ func setupBenchmarkCommand() *cobraext.Command {
Long: benchLongDescription,
}

cmd.PersistentFlags().StringP(cobraext.ProfileFlagName, "p", "", fmt.Sprintf(cobraext.ProfileFlagDescription, install.ProfileNameEnvVar))

pipelineCmd := getPipelineCommand()
cmd.AddCommand(pipelineCmd)

Expand Down Expand Up @@ -248,6 +251,11 @@ func systemCommandAction(cmd *cobra.Command, args []string) error {
return fmt.Errorf("locating package root failed: %w", err)
}

profile, err := cobraext.GetProfileFlag(cmd)
if err != nil {
return nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have seen this now while updating #1230, shouldn't this be return err?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, it should return the error.
Thanks for the heads up!

Opened a PR for this: #1370

}

signal.Enable()

esClient, err := elasticsearch.NewClient()
Expand All @@ -272,6 +280,7 @@ func systemCommandAction(cmd *cobra.Command, args []string) error {
system.WithPackageRootPath(packageRootPath),
system.WithESAPI(esClient.API),
system.WithKibanaClient(kc),
system.WithProfile(profile),
)
runner := system.NewSystemBenchmark(opts)

Expand Down
8 changes: 8 additions & 0 deletions cmd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/spf13/cobra"

"github.com/elastic/elastic-package/internal/cobraext"
"github.com/elastic/elastic-package/internal/install"
"github.com/elastic/elastic-package/internal/packages"
"github.com/elastic/elastic-package/internal/service"
)
Expand All @@ -35,6 +36,7 @@ func setupServiceCommand() *cobraext.Command {
Long: serviceLongDescription,
}
cmd.AddCommand(upCommand)
cmd.PersistentFlags().StringP(cobraext.ProfileFlagName, "p", "", fmt.Sprintf(cobraext.ProfileFlagDescription, install.ProfileNameEnvVar))

return cobraext.NewCommand(cmd, cobraext.ContextPackage)
}
Expand All @@ -58,8 +60,14 @@ func upCommandAction(cmd *cobra.Command, args []string) error {

variantFlag, _ := cmd.Flags().GetString(cobraext.VariantFlagName)

profile, err := cobraext.GetProfileFlag(cmd)
if err != nil {
return nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here.

}

_, serviceName := filepath.Split(packageRoot)
err = service.BootUp(service.Options{
Profile: profile,
ServiceName: serviceName,
PackageRootPath: packageRoot,
DataStreamRootPath: dataStreamPath,
Expand Down
2 changes: 1 addition & 1 deletion cmd/testrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,14 @@ func testTypeCommandActionFactory(runner testrunner.TestRunner) cobraext.Command
var results []testrunner.TestResult
for _, folder := range testFolders {
r, err := testrunner.Run(testType, testrunner.TestOptions{
Profile: profile,
TestFolder: folder,
PackageRootPath: packageRootPath,
GenerateTestResult: generateTestResult,
API: esClient.API,
DeferCleanup: deferCleanup,
ServiceVariant: variantFlag,
WithCoverage: testCoverage,
Profile: profile,
})

results = append(results, r...)
Expand Down
8 changes: 8 additions & 0 deletions internal/benchrunner/runners/system/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (

"github.com/elastic/elastic-package/internal/elasticsearch"
"github.com/elastic/elastic-package/internal/kibana"
"github.com/elastic/elastic-package/internal/profile"
)

// Options contains benchmark runner options.
type Options struct {
Profile *profile.Profile
ESAPI *elasticsearch.API
KibanaClient *kibana.Client
DeferCleanup time.Duration
Expand Down Expand Up @@ -72,3 +74,9 @@ func WithDataReindexing(b bool) OptionFunc {
opts.ReindexData = b
}
}

func WithProfile(p *profile.Profile) OptionFunc {
return func(opts *Options) {
opts.Profile = p
}
}
1 change: 1 addition & 0 deletions internal/benchrunner/runners/system/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ func (r *runner) run() (report reporters.Reportable, err error) {
// Setup service.
logger.Debug("setting up service...")
serviceDeployer, err := servicedeployer.Factory(servicedeployer.FactoryOptions{
Profile: r.options.Profile,
RootPath: r.options.PackageRootPath,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import (
"github.com/elastic/elastic-package/internal/docker"
"github.com/elastic/elastic-package/internal/files"
"github.com/elastic/elastic-package/internal/logger"
"github.com/elastic/elastic-package/internal/profile"
"github.com/elastic/elastic-package/internal/stack"
)

// DockerComposeServiceDeployer knows how to deploy a service defined via
// a Docker Compose file.
type DockerComposeServiceDeployer struct {
profile *profile.Profile
ymlPaths []string
}

Expand All @@ -32,8 +34,9 @@ type dockerComposeDeployedService struct {
}

// NewDockerComposeServiceDeployer returns a new instance of a DockerComposeServiceDeployer.
func NewDockerComposeServiceDeployer(ymlPaths []string) (*DockerComposeServiceDeployer, error) {
func NewDockerComposeServiceDeployer(profile *profile.Profile, ymlPaths []string) (*DockerComposeServiceDeployer, error) {
return &DockerComposeServiceDeployer{
profile: profile,
ymlPaths: ymlPaths,
}, nil
}
Expand All @@ -53,7 +56,7 @@ func (d *DockerComposeServiceDeployer) SetUp(inCtxt ServiceContext) (DeployedSer
}

// Verify the Elastic stack network
err = stack.EnsureStackNetworkUp()
err = stack.EnsureStackNetworkUp(d.profile)
if err != nil {
return nil, fmt.Errorf("elastic stack network is not ready: %w", err)
}
Expand Down Expand Up @@ -85,7 +88,7 @@ func (d *DockerComposeServiceDeployer) SetUp(inCtxt ServiceContext) (DeployedSer
outCtxt.Hostname = p.ContainerName(serviceName)

// Connect service network with stack network (for the purpose of metrics collection)
err = docker.ConnectToNetwork(p.ContainerName(serviceName), stack.Network())
err = docker.ConnectToNetwork(p.ContainerName(serviceName), stack.Network(d.profile))
if err != nil {
return nil, fmt.Errorf("can't attach service container to the stack network: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import (
"fmt"
"os"
"path/filepath"

"github.com/elastic/elastic-package/internal/profile"
)

const devDeployDir = "_dev/benchmark/system/deploy"

// FactoryOptions defines options used to create an instance of a service deployer.
type FactoryOptions struct {
Profile *profile.Profile

RootPath string
}

Expand All @@ -37,7 +41,7 @@ func Factory(options FactoryOptions) (ServiceDeployer, error) {
case "docker":
dockerComposeYMLPath := filepath.Join(serviceDeployerPath, "docker-compose.yml")
if _, err := os.Stat(dockerComposeYMLPath); err == nil {
return NewDockerComposeServiceDeployer([]string{dockerComposeYMLPath})
return NewDockerComposeServiceDeployer(options.Profile, []string{dockerComposeYMLPath})
}
}
return nil, fmt.Errorf("unsupported service deployer (name: %s)", serviceDeployerName)
Expand Down
5 changes: 3 additions & 2 deletions internal/kind/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/elastic/elastic-package/internal/docker"
"github.com/elastic/elastic-package/internal/kubectl"
"github.com/elastic/elastic-package/internal/logger"
"github.com/elastic/elastic-package/internal/profile"
"github.com/elastic/elastic-package/internal/stack"
)

Expand All @@ -33,13 +34,13 @@ func VerifyContext() error {
}

// ConnectToElasticStackNetwork function ensures that the control plane node is connected to the Elastic stack network.
func ConnectToElasticStackNetwork() error {
func ConnectToElasticStackNetwork(profile *profile.Profile) error {
containerID, err := controlPlaneContainerID()
if err != nil {
return fmt.Errorf("can't find kind-control plane node: %w", err)
}

stackNetwork := stack.Network()
stackNetwork := stack.Network(profile)
logger.Debugf("check network connectivity between service container %s (ID: %s) and the stack network %s", ControlPlaneContainerName, containerID, stackNetwork)

networkDescriptions, err := docker.InspectNetwork(stackNetwork)
Expand Down
4 changes: 4 additions & 0 deletions internal/service/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"syscall"

"github.com/elastic/elastic-package/internal/logger"
"github.com/elastic/elastic-package/internal/profile"

"github.com/elastic/elastic-package/internal/configuration/locations"
"github.com/elastic/elastic-package/internal/testrunner/runners/system"
Expand All @@ -19,6 +20,8 @@ import (

// Options define the details of the service which should be booted up.
type Options struct {
Profile *profile.Profile

ServiceName string
PackageRootPath string
DataStreamRootPath string
Expand All @@ -30,6 +33,7 @@ type Options struct {
func BootUp(options Options) error {
logger.Debugf("Create new instance of the service deployer")
serviceDeployer, err := servicedeployer.Factory(servicedeployer.FactoryOptions{
Profile: options.Profile,
PackageRootPath: options.DataStreamRootPath,
DataStreamRootPath: options.DataStreamRootPath,
Variant: options.Variant,
Expand Down
19 changes: 14 additions & 5 deletions internal/stack/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ import (
"github.com/elastic/elastic-package/internal/builder"
"github.com/elastic/elastic-package/internal/configuration/locations"
"github.com/elastic/elastic-package/internal/files"
"github.com/elastic/elastic-package/internal/profile"
)

// DockerComposeProjectName is the name of the Docker Compose project used to boot up
// baseComposeProjectName is the base name of the Docker Compose project used to boot up
// Elastic Stack containers.
const DockerComposeProjectName = "elastic-package-stack"
const baseComposeProjectName = "elastic-package-stack"

// DockerComposeProjectName returns the docker compose project name for a given profile.
func DockerComposeProjectName(profile *profile.Profile) string {
if profile.ProfileName == "default" {
return baseComposeProjectName
}
Comment on lines +25 to +27
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this condition is added, the resulting container names would be the same as previously keeping backwards compatibility.

return baseComposeProjectName + "-" + profile.ProfileName
}

// BootUp function boots up the Elastic stack.
func BootUp(options Options) error {
Expand Down Expand Up @@ -81,7 +90,7 @@ func BootUp(options Options) error {
// to fail too.
// As a workaround, try to give another chance to docker-compose if only
// elastic-agent failed.
if onlyElasticAgentFailed() {
if onlyElasticAgentFailed(options) {
fmt.Println("Elastic Agent failed to start, trying again.")
err = dockerComposeUp(options)
}
Expand All @@ -98,8 +107,8 @@ func BootUp(options Options) error {
return nil
}

func onlyElasticAgentFailed() bool {
status, err := Status()
func onlyElasticAgentFailed(options Options) bool {
status, err := Status(options)
if err != nil {
fmt.Printf("Failed to check status of the stack after failure: %v\n", err)
return false
Expand Down
12 changes: 6 additions & 6 deletions internal/stack/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (eb *envBuilder) build() []string {
}

func dockerComposeBuild(options Options) error {
c, err := compose.NewProject(DockerComposeProjectName, options.Profile.Path(profileStackPath, SnapshotFile))
c, err := compose.NewProject(DockerComposeProjectName(options.Profile), options.Profile.Path(profileStackPath, SnapshotFile))
if err != nil {
return fmt.Errorf("could not create docker compose project: %w", err)
}
Expand All @@ -78,7 +78,7 @@ func dockerComposeBuild(options Options) error {
}

func dockerComposePull(options Options) error {
c, err := compose.NewProject(DockerComposeProjectName, options.Profile.Path(profileStackPath, SnapshotFile))
c, err := compose.NewProject(DockerComposeProjectName(options.Profile), options.Profile.Path(profileStackPath, SnapshotFile))
if err != nil {
return fmt.Errorf("could not create docker compose project: %w", err)
}
Expand All @@ -104,7 +104,7 @@ func dockerComposePull(options Options) error {
}

func dockerComposeUp(options Options) error {
c, err := compose.NewProject(DockerComposeProjectName, options.Profile.Path(profileStackPath, SnapshotFile))
c, err := compose.NewProject(DockerComposeProjectName(options.Profile), options.Profile.Path(profileStackPath, SnapshotFile))
if err != nil {
return fmt.Errorf("could not create docker compose project: %w", err)
}
Expand Down Expand Up @@ -136,7 +136,7 @@ func dockerComposeUp(options Options) error {
}

func dockerComposeDown(options Options) error {
c, err := compose.NewProject(DockerComposeProjectName, options.Profile.Path(profileStackPath, SnapshotFile))
c, err := compose.NewProject(DockerComposeProjectName(options.Profile), options.Profile.Path(profileStackPath, SnapshotFile))
if err != nil {
return fmt.Errorf("could not create docker compose project: %w", err)
}
Expand Down Expand Up @@ -182,10 +182,10 @@ func withIsReadyServices(services []string) []string {
return allServices
}

func dockerComposeStatus() ([]ServiceStatus, error) {
func dockerComposeStatus(options Options) ([]ServiceStatus, error) {
var services []ServiceStatus
// query directly to docker to avoid load environment variables (e.g. STACK_VERSION_VARIANT) and profiles
containerIDs, err := docker.ContainerIDsWithLabel(projectLabelDockerCompose, DockerComposeProjectName)
containerIDs, err := docker.ContainerIDsWithLabel(projectLabelDockerCompose, DockerComposeProjectName(options.Profile))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/stack/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func dumpStackLogs(options DumpOptions) error {
writeLogFiles(logsPath, serviceName, content)
}

err = copyDockerInternalLogs(serviceName, logsPath)
err = copyDockerInternalLogs(serviceName, logsPath, options.Profile)
if err != nil {
logger.Errorf("can't copy internal logs (service: %s): %v", serviceName, err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/stack/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func dockerComposeLogs(serviceName string, profile *profile.Profile) ([]byte, er

snapshotFile := profile.Path(profileStackPath, SnapshotFile)

p, err := compose.NewProject(DockerComposeProjectName, snapshotFile)
p, err := compose.NewProject(DockerComposeProjectName(profile), snapshotFile)
if err != nil {
return nil, fmt.Errorf("could not create docker compose project: %w", err)
}
Expand All @@ -43,14 +43,14 @@ func dockerComposeLogs(serviceName string, profile *profile.Profile) ([]byte, er
return out, nil
}

func copyDockerInternalLogs(serviceName, outputPath string) error {
func copyDockerInternalLogs(serviceName, outputPath string, profile *profile.Profile) error {
switch serviceName {
case elasticAgentService, fleetServerService:
default:
return nil // we need to pull internal logs only from Elastic-Agent and Fleets Server container
}

p, err := compose.NewProject(DockerComposeProjectName)
p, err := compose.NewProject(DockerComposeProjectName(profile))
if err != nil {
return fmt.Errorf("could not create docker compose project: %w", err)
}
Expand Down
9 changes: 5 additions & 4 deletions internal/stack/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import (
"fmt"

"github.com/elastic/elastic-package/internal/docker"
"github.com/elastic/elastic-package/internal/profile"
)

// EnsureStackNetworkUp function verifies if stack network is up and running.
func EnsureStackNetworkUp() error {
_, err := docker.InspectNetwork(Network())
func EnsureStackNetworkUp(profile *profile.Profile) error {
_, err := docker.InspectNetwork(Network(profile))
if err != nil {
return fmt.Errorf("network not available: %w", err)
}
return nil
}

// Network function returns the stack network name.
func Network() string {
return fmt.Sprintf("%s_default", DockerComposeProjectName)
func Network(profile *profile.Profile) string {
return fmt.Sprintf("%s_default", DockerComposeProjectName(profile))
}
2 changes: 1 addition & 1 deletion internal/stack/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ func (*composeProvider) Dump(options DumpOptions) (string, error) {
}

func (*composeProvider) Status(options Options) ([]ServiceStatus, error) {
return Status()
return Status(options)
}
Loading