Skip to content

Commit

Permalink
updated scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
oakrizan committed Oct 10, 2024
1 parent b9cc4b5 commit 32053ac
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 54 deletions.
5 changes: 5 additions & 0 deletions .buildkite/hooks/pre-command
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ if [[ "$BUILDKITE_PIPELINE_SLUG" == "auditbeat" || \
export BUILDKITE_ANALYTICS_TOKEN
fi

if [[ "$BUILDKITE_PIPELINE_SLUG" == "beats-xpack-agentbeat" ]]; then
AGENTBEAT_PATH=$(.buildkite/scripts/agentbeat/setup_agentbeat.py)
export AGENTBEAT_PATH
fi

CPU_ARCH=$(uname -m)
PLATFORM_TYPE=$(uname)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import subprocess
import sys
import tarfile
import os
import re

PATH = 'x-pack/agentbeat/build/distributions'
Expand Down Expand Up @@ -46,7 +45,6 @@ def get_artifact_pattern() -> str:


def download_agentbeat(pattern, path) -> str:
log('--- Downloading agentbeat')
try:
subprocess.run(
['buildkite-agent', 'artifact', 'download', pattern, '.',
Expand All @@ -70,20 +68,18 @@ def get_filename(path) -> str:


def extract_agentbeat(filename):
log('~~~ Extracting agentbeat')
filepath = PATH + '/' + filename

if filepath.endswith('.zip'):
unzip_agentbeat(filepath)
else:
untar_agentbeat(filepath)
log('Successfully extracted agentbeat')


def unzip_agentbeat(filepath):
try:
subprocess.run(
['unzip', filepath],
['unzip', '-qq', filepath],
check=True, stdout=sys.stdout, stderr=sys.stderr, text=True)
except subprocess.CalledProcessError as e:
log_err(e)
Expand All @@ -93,40 +89,24 @@ def unzip_agentbeat(filepath):
def untar_agentbeat(filepath):
try:
with tarfile.open(filepath, 'r:gz') as tar:
tar.list()
tar.extractall()
except Exception as e:
log_err(e)
exit(1)


def add_to_path(filepath):
def get_path_to_executable(filepath) -> str:
pattern = r'(.*)(?=\.zip|.tar\.gz)'
match = re.match(pattern, filepath)
if match:
path = f'../build/distributions/{match.group(1)}/agentbeat'
log("--- PATH: " + str(path))
os.environ['AGENTBEAT_PATH'] = str(path)
path = f'../../{match.group(1)}/agentbeat'
return path
else:
log_err("No agentbeat executable found")
exit(1)


def install_synthetics():
log('--- Installing @elastic/synthetics')

try:
subprocess.run(
['npm', 'install', '-g', '@elastic/synthetics'],
check=True
)
except subprocess.CalledProcessError:
log_err('Failed to install @elastic/synthetics')
exit(1)


artifact_pattern = get_artifact_pattern()
archive = download_agentbeat(artifact_pattern, PATH)
extract_agentbeat(archive)
add_to_path(archive)
install_synthetics()
log(get_path_to_executable(archive))
10 changes: 5 additions & 5 deletions .buildkite/x-pack/pipeline.xpack.agentbeat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,8 @@ steps:
# depends_on:
# - agentbeat-package-linux
command: |
set -euo pipefail
./.buildkite/scripts/agentbeat/prepare_env.py
cd x-pack/agentbeat
ls -la ../../agentbeat-9.0.0-SNAPSHOT-linux-x86_64
mage -v testWithSpec
agents:
provider: "gcp"
Expand All @@ -123,10 +122,11 @@ steps:

- label: ":linux: Agentbeat/Integration tests Windows"
key: "agentbeat-it-windows"
env:
PLATFORM: "windows/amd64"
# depends_on:
# - agentbeat-package-linux
command: |
./.buildkite/scripts/agentbeat/prepare_env.py
cd x-pack/agentbeat
mage -v testWithSpec
agents:
Expand All @@ -142,7 +142,7 @@ steps:
# - agentbeat-package-linux
# command: |
# set -euo pipefail
# ./.buildkite/scripts/agentbeat/prepare_env.py
# ./.buildkite/scripts/agentbeat/setup_agentbeat.py
# agents:
# provider: "orka"
# imagePrefix: "${IMAGE_MACOS_X86_64}"
Expand All @@ -153,7 +153,7 @@ steps:
# - agentbeat-package-linux
# command: |
# set -euo pipefail
# ./.buildkite/scripts/agentbeat/prepare_env.py
# ./.buildkite/scripts/agentbeat/setup_agentbeat.py
# agents:
# provider: "orka"
# imagePrefix: "${IMAGE_MACOS_ARM}"
12 changes: 1 addition & 11 deletions dev-tools/mage/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,7 @@ type command struct {
}

// SpecCommands parses agent.beat.spec.yml and collects commands for tests
func SpecCommands() []string {
specPath := os.Getenv("AGENTBEAT_SPEC")
if specPath == "" {
log.Fatal("AGENTBEAT_SPEC is not defined")
}

platform := os.Getenv("PLATFORM")
if platform == "" {
log.Fatal("PLATFORM is not defined")
}

func SpecCommands(specPath string, platform string) []string {
spec, _ := parseToObj(specPath)

filteredInputs := filter(spec.Inputs, func(input input) bool {
Expand Down
35 changes: 22 additions & 13 deletions x-pack/agentbeat/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package main
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -217,10 +218,19 @@ func PythonIntegTest(ctx context.Context) error {

// TestWithSpec executes unique commands from agentbeat.spec.yml and validates that app haven't exited with non-zero
func TestWithSpec(ctx context.Context) {
var commands = devtools.SpecCommands()
specPath := os.Getenv("AGENTBEAT_SPEC")
if specPath == "" {
log.Fatal("AGENTBEAT_SPEC is not defined\n")
}

platform := os.Getenv("PLATFORM")
if platform == "" {
log.Fatal("PLATFORM is not defined\n")
}

var commands = devtools.SpecCommands(specPath, platform)

agentbeatPath := os.Getenv("AGENTBEAT_PATH")
fmt.Printf("--- AGENTBEAT_PATH: %s", agentbeatPath)

cmdResults := make(map[string]bool)

Expand All @@ -230,33 +240,32 @@ func TestWithSpec(ctx context.Context) {

hasFailures := false
for cmd, res := range cmdResults {
if res {
fmt.Printf("Command [%s] succeeded", cmd)
} else {
fmt.Printf("Command [%s] failed", cmd)
if !res {
fmt.Printf("~~~ Failed: [%s]\n", cmd)
fmt.Print(res)
hasFailures = true
}
}

if hasFailures {
fmt.Printf("Some inputs failed. Exiting with error")
fmt.Printf("Some inputs failed. Exiting with error\n")
os.Exit(1)
}
}

func agentbeatCmd(agentbeatPath string, command string) bool {
cmd := exec.Command(agentbeatPath, command)
fmt.Printf("Running command: %v", cmd)
fmt.Printf("Running command: %v\n", cmd)

if err := cmd.Start(); err != nil {
_ = fmt.Errorf("failed to start command: %v", err)
fmt.Printf("failed to start command: %v\n", err)
}

defer func() {
if err := cmd.Process.Kill(); err != nil {
_ = fmt.Errorf("failed to kill process: %v", err)
fmt.Printf("failed to kill process: %v\n", err)
} else {
_ = fmt.Errorf("command process killed")
fmt.Print("command process killed\n")
}
}()

Expand All @@ -269,11 +278,11 @@ func agentbeatCmd(agentbeatPath string, command string) bool {

select {
case err := <-done:
_ = fmt.Errorf("command exited before %s: %v", timeout.String(), err)
fmt.Printf("command exited before %s: %v\n", timeout.String(), err)
return false

case <-deadline:
_ = fmt.Errorf("%s", cmd.Stdout)
fmt.Printf("%s\n", cmd.Stdout)
return true
}
}

0 comments on commit 32053ac

Please sign in to comment.