Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
chore: refactor build artifacts methods (#689)
Browse files Browse the repository at this point in the history
* feat: use local beats directory for elastic-agent installers

* docs: document BEATS_LOCAL_PATH variable

* chore: check that the local file exists

* chore: add unit tests for the BEATS_LOCAL_PATH variable

* chore: simplify logic after test coverage

* chore: enrich test name

* chore: rename test

* chore: move default values to the end of the descriptions

* chore: add a helper method to build artifact names

* chore: build artifact name only once

* chore: pass the entire filename to calculate the GCP bucket coordinates

* chore: simplify method signature, as the file name is not changed within the method

* chore: use buildArtifactName method

* chore: pass filename to helper methods to avoid unnecessary calculations

* chore: rename variable for consistency

* chore: add license headers

* chore: use lowercase comparison for artifact name

* chore: build artifact changes from Beats CI to Elastic's artifactory

* fix: support building artifact names from local

Local builds uses the same as the CI

* WIP

* fix: rename docker-ubi8 installer key

* chore: pass a fallback version when building artifact name

In the case we are using a PR , where the version is "pr-12345", then we
need to use the base version of the product

* chore: remove log

* fix: do not override artifact name in Docker installer

* feat: support consuming the docker images from snapshots or local in standalone mode

* chore: add unit tests for fetching docker images from local Beats repo

* chore: apply version to Docker installer

* chore: support loading metricbeat image from local repository

* chore: exrtact loadImage method to docker helper

* feat: support consuming CI artifacts in metricbeat

* fix: remove non-existent field from logrus log

* chore: extract download logic to helper methods

We are discarding the stale parameter, because the stale version is set
when the installer is required, so we simply download what is requested

* chore: fix typo

* chore: check agent version before setting it

* chore: use the docker client to load the image instead of the docker binary

* chore: add unit tests for check method

* chore: pass fallback version to check method

* fix: properly calculate version for PRs

* chore: move PR check logic to a method

* chore: make test independent of maintenance branch
# Conflicts:
#	e2e/_suites/fleet/fleet.go
#	e2e/_suites/fleet/ingest-manager_test.go
#	e2e/_suites/fleet/installers.go
#	e2e/_suites/fleet/services.go
#	e2e/_suites/fleet/services_test.go
  • Loading branch information
mdelapenya committed Feb 8, 2021
1 parent a960848 commit 156d301
Show file tree
Hide file tree
Showing 12 changed files with 797 additions and 405 deletions.
34 changes: 34 additions & 0 deletions cli/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ package docker

import (
"bytes"
"compress/gzip"
"context"
"os"
"path/filepath"
"strings"

"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -172,6 +175,37 @@ func RemoveContainer(containerName string) error {
return nil
}

// LoadImage loads a TAR file in the local docker engine
func LoadImage(imagePath string) error {
fileNamePath, err := filepath.Abs(imagePath)
if err != nil {
return err
}

_, err = os.Stat(fileNamePath)
if err != nil || os.IsNotExist(err) {
return err
}

dockerClient := getDockerClient()
file, err := os.Open(imagePath)

input, err := gzip.NewReader(file)
imageLoadResponse, err := dockerClient.ImageLoad(context.Background(), input, false)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"image": fileNamePath,
}).Error("Could not load the Docker image.")
return err
}

log.WithFields(log.Fields{
"response": imageLoadResponse,
}).Debug("Docker image loaded successfully")
return nil
}

// RemoveDevNetwork removes the developer network
func RemoveDevNetwork() error {
dockerClient := getDockerClient()
Expand Down
16 changes: 7 additions & 9 deletions e2e/_suites/fleet/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,9 @@ func (fts *FleetTestSuite) anStaleAgentIsDeployedToFleetWithInstaller(image, ver
agentVersion = version

// prepare installer for stale version
if agentVersion != agentVersionBackup {
i := GetElasticAgentInstaller(image, installerType)
installerType = fmt.Sprintf("%s-%s", installerType, version)
fts.Installers[fmt.Sprintf("%s-%s", image, installerType)] = i
if fts.Version != agentVersionBackup {
i := GetElasticAgentInstaller(image, installerType, fts.Version)
fts.Installers[fmt.Sprintf("%s-%s-%s", image, installerType, version)] = i
}

return fts.anAgentIsDeployedToFleetWithInstaller(image, installerType)
Expand Down Expand Up @@ -371,11 +370,10 @@ func (fts *FleetTestSuite) theAgentIsListedInFleetWithStatus(desiredStatus strin
// the agent is not listed in Fleet
if desiredStatus == "offline" || desiredStatus == "inactive" {
log.WithFields(log.Fields{
"isAgentInStatus": isAgentInStatus,
"elapsedTime": exp.GetElapsedTime(),
"hostname": fts.Hostname,
"retries": retryCount,
"status": desiredStatus,
"elapsedTime": exp.GetElapsedTime(),
"hostname": fts.Hostname,
"retries": retryCount,
"status": desiredStatus,
}).Info("The Agent is not present in Fleet, as expected")
return nil
} else if desiredStatus == "online" {
Expand Down
26 changes: 6 additions & 20 deletions e2e/_suites/fleet/ingest-manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"os"
"path"
"strings"
"time"

"github.com/cucumber/godog"
Expand Down Expand Up @@ -91,10 +90,12 @@ func setUpSuite() {
imts = IngestManagerTestSuite{
Fleet: &FleetTestSuite{
Installers: map[string]ElasticAgentInstaller{
"centos-systemd": GetElasticAgentInstaller("centos", "systemd"),
"centos-tar": GetElasticAgentInstaller("centos", "tar"),
"debian-systemd": GetElasticAgentInstaller("debian", "systemd"),
"debian-tar": GetElasticAgentInstaller("debian", "tar"),
"centos-systemd-" + agentVersion: GetElasticAgentInstaller("centos", "systemd", agentVersion),
"centos-tar-" + agentVersion: GetElasticAgentInstaller("centos", "tar", agentVersion),
"debian-systemd-" + agentVersion: GetElasticAgentInstaller("debian", "systemd", agentVersion),
"debian-tar-" + agentVersion: GetElasticAgentInstaller("debian", "tar", agentVersion),
"docker-default-" + agentVersion: GetElasticAgentInstaller("docker", "default", agentVersion),
"docker-ubi8-" + agentVersion: GetElasticAgentInstaller("docker", "ubi8", agentVersion),
},
},
StandAlone: &StandAloneTestSuite{},
Expand Down Expand Up @@ -226,21 +227,6 @@ func (imts *IngestManagerTestSuite) processStateOnTheHost(process string, state
return checkProcessStateOnTheHost(containerName, process, state)
}

// checkElasticAgentVersion returns a fallback version (agentVersionBase) if the version set by the environment is empty
func checkElasticAgentVersion(version string) string {
environmentVersion := os.Getenv("ELASTIC_AGENT_VERSION")

if environmentVersion == "" {
return agentVersionBase
}

if strings.HasPrefix(strings.ToLower(environmentVersion), "pr-") {
return agentVersionBase
}

return version
}

// name of the container for the service:
// we are using the Docker client instead of docker-compose
// because it does not support returning the output of a
Expand Down
114 changes: 101 additions & 13 deletions e2e/_suites/fleet/installers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package main

import (
"fmt"

"github.com/elastic/e2e-testing/cli/docker"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -98,6 +103,85 @@ func (i *DEBPackage) Uninstall() error {
return nil
}

// DockerPackage implements operations for a DEB installer
type DockerPackage struct {
BasePackage
installerPath string
ubi8 bool
// optional fields
arch string
artifact string
OS string
version string
}

// NewDockerPackage creates an instance for the Docker installer
func NewDockerPackage(binaryName string, profile string, image string, service string, installerPath string, ubi8 bool) *DockerPackage {
return &DockerPackage{
BasePackage: BasePackage{
binaryName: binaryName,
image: image,
profile: profile,
service: service,
},
installerPath: installerPath,
ubi8: ubi8,
}
}

// Install installs a Docker package
func (i *DockerPackage) Install(containerName string, token string) error {
log.Trace("No install commands for Docker packages")
return nil
}

// InstallCerts installs the certificates for a Docker package
func (i *DockerPackage) InstallCerts() error {
log.Trace("No install certs commands for Docker packages")
return nil
}

// Preinstall executes operations before installing a Docker package
func (i *DockerPackage) Preinstall() error {
return docker.LoadImage(i.installerPath)
}

// Postinstall executes operations after installing a Docker package
func (i *DockerPackage) Postinstall() error {
log.Trace("No postinstall commands for Docker packages")
return nil
}

// Uninstall uninstalls a Docker package
func (i *DockerPackage) Uninstall() error {
log.Trace("No uninstall commands for Docker packages")
return nil
}

// WithArch sets the architecture
func (i *DockerPackage) WithArch(arch string) *DockerPackage {
i.arch = arch
return i
}

// WithArtifact sets the artifact
func (i *DockerPackage) WithArtifact(artifact string) *DockerPackage {
i.artifact = artifact
return i
}

// WithOS sets the OS
func (i *DockerPackage) WithOS(OS string) *DockerPackage {
i.OS = OS
return i
}

// WithVersion sets the version
func (i *DockerPackage) WithVersion(version string) *DockerPackage {
i.version = version
return i
}

// RPMPackage implements operations for a RPM installer
type RPMPackage struct {
BasePackage
Expand Down Expand Up @@ -214,20 +298,24 @@ func (i *TARPackage) Preinstall() error {
return err
}

version := checkElasticAgentVersion(i.version)

// simplify layout
cmds := []string{"mv", fmt.Sprintf("/%s-%s-%s-%s", i.artifact, version, i.OS, i.arch), "/elastic-agent"}
err = execCommandInService(i.profile, i.image, i.service, cmds, false)
if err != nil {
log.WithFields(log.Fields{
"command": cmds,
"error": err,
"image": i.image,
"service": i.service,
}).Error("Could not extract agent package in the box")

return err
cmds := [][]string{
[]string{"rm", "-fr", "/elastic-agent"},
[]string{"mv", fmt.Sprintf("/%s-%s-%s-%s", i.artifact, i.version, i.OS, i.arch), "/elastic-agent"},
}
for _, cmd := range cmds {
err = execCommandInService(i.profile, i.image, i.service, cmd, false)
if err != nil {
log.WithFields(log.Fields{
"command": cmd,
"error": err,
"image": i.image,
"service": i.service,
"version": i.version,
}).Error("Could not extract agent package in the box")

return err
}
}

return nil
Expand Down
Loading

0 comments on commit 156d301

Please sign in to comment.