Skip to content

Commit

Permalink
chore: updated docker testing
Browse files Browse the repository at this point in the history
  • Loading branch information
stephane-segning committed Aug 31, 2024
1 parent 4693b43 commit 46a5074
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 42 deletions.
50 changes: 48 additions & 2 deletions .github/workflows/test-script.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,51 @@ name: Test install script
on: [ push, pull_request ]

jobs:
test_script:
test_script_linux:
name: Test install script on Linux ${{ matrix.os }}, ${{ matrix.version }}
runs-on: ubuntu-latest
strategy:
matrix:
include:
- os: centos
version: latest
- os: centos
version: 8
- os: centos
version: 7

- os: opensuse
version: latest
- os: opensuse
version: 15

- os: ubuntu
version: latest
- os: ubuntu
version: jammy
- os: ubuntu
version: focal
- os: ubuntu
version: noble

- os: debian
version: latest
- os: debian
version: buster

- os: alpine
version: latest
- os: alpine
version: edge
- os: alpine
version: 3

- os: fedora
version: latest
- os: fedora
version: 41
- os: fedora
version: 40
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand All @@ -19,6 +62,9 @@ jobs:
with:
platforms: ${{ steps.qemu.outputs.platforms }}

- name: Pull without cache
run: docker pull ghcr.io/stephane-segning/bats-docker:${{ matrix.os }}-${{ matrix.version }}

- name: Test script
run: |
docker run --rm -v "$PWD:/app" bats/bats:latest /app/scripts/tests/test-script.bats
docker run --rm -v "$PWD:/app" ghcr.io/stephane-segning/bats-docker:${{ matrix.os }}-${{ matrix.version }} bats /app/scripts/tests/test-script.bats
95 changes: 58 additions & 37 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ log() {
local MESSAGE="$*"
local TIMESTAMP
TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
if [ "$LEVEL" = "ERROR" ] || { [ "$LEVEL" = "WARNING" ] && [ "$LOG_LEVEL" != "ERROR" ]; } || { [ "$LEVEL" = "INFO" ] && [ "$LOG_LEVEL" = "INFO" ]; }; then
echo "$TIMESTAMP [$LEVEL] $MESSAGE"
fi
echo "$TIMESTAMP [$LEVEL] $MESSAGE"
}

# Logging helpers
Expand All @@ -35,12 +33,12 @@ success_message() {
# Variables
LOG_LEVEL=${LOG_LEVEL:-INFO}
WAZUH_MANAGER=${WAZUH_MANAGER:-'master.wazuh.adorsys.team'}
WAZUH_AGENT_VERSION=${WAZUH_AGENT_VERSION:-'4.8.0-1'}
WAZUH_AGENT_VERSION=${WAZUH_AGENT_VERSION:-'4.8.2-1'}
WAZUH_AGENT_NAME=${WAZUH_AGENT_NAME:-}

# Check if WAZUH_AGENT_NAME is set; if not, exit with an error
if [ -z "${WAZUH_AGENT_NAME:-}" ]; then
echo "Error: WAZUH_AGENT_NAME is not set. Please set this variable before running the script."
if [ -z "${WAZUH_AGENT_NAME}" ]; then
error_message "WAZUH_AGENT_NAME is not set. Please set this variable before running the script."
exit 1
fi

Expand All @@ -49,26 +47,36 @@ OS_NAME=$(uname -s)
ARCH=$(uname -m)

# Package details based on OS and architecture
if [ "$OS_NAME" = "Linux" ]; then
if command -v apt &> /dev/null; then
PACKAGE_MANAGER="apt"
PACKAGE_FILE="wazuh-agent_${WAZUH_AGENT_VERSION}_$( [ "$ARCH" = "x86_64" ] && echo "amd64" || echo "arm64" ).deb"
elif command -v yum &> /dev/null; then
PACKAGE_MANAGER="yum"
PACKAGE_FILE="wazuh-agent-${WAZUH_AGENT_VERSION}.$( [ "$ARCH" = "x86_64" ] && echo "x86_64" || echo "aarch64" ).rpm"
else
error_message "Unsupported package manager"
case "$OS_NAME" in
Linux)
if command -v apt &> /dev/null; then
PACKAGE_MANAGER="apt/pool/main/w/wazuh-agent"
PACKAGE_FILE="wazuh-agent_${WAZUH_AGENT_VERSION}_$( [ "$ARCH" = "x86_64" ] && echo "amd64" || echo "arm64" ).deb"
elif command -v yum &> /dev/null; then
PACKAGE_MANAGER="yum"
PACKAGE_FILE="wazuh-agent-${WAZUH_AGENT_VERSION}.$( [ "$ARCH" = "x86_64" ] && echo "x86_64" || echo "aarch64" ).rpm"
elif command -v dnf &> /dev/null; then
PACKAGE_MANAGER="dnf"
PACKAGE_FILE="wazuh-agent-${WAZUH_AGENT_VERSION}.$( [ "$ARCH" = "x86_64" ] && echo "x86_64" || echo "aarch64" ).rpm"
elif command -v zypper &> /dev/null; then
PACKAGE_MANAGER="zypper"
PACKAGE_FILE="wazuh-agent-${WAZUH_AGENT_VERSION}.$( [ "$ARCH" = "x86_64" ] && echo "x86_64" || echo "aarch64" ).rpm"
else
error_message "Unsupported package manager"
exit 1
fi
PACKAGE_URL="https://packages.wazuh.com/4.x/${PACKAGE_MANAGER}/${PACKAGE_FILE}"
;;
Darwin)
PACKAGE_MANAGER="installer"
PACKAGE_FILE="wazuh-agent-${WAZUH_AGENT_VERSION}.$( [ "$ARCH" = "x86_64" ] && echo "intel64" || echo "arm64" ).pkg"
PACKAGE_URL="https://packages.wazuh.com/4.x/macos/${PACKAGE_FILE}"
;;
*)
error_message "Unsupported operating system: $OS_NAME"
exit 1
fi
PACKAGE_URL="https://packages.wazuh.com/4.x/${PACKAGE_MANAGER}/${PACKAGE_FILE}"
elif [ "$OS_NAME" = "Darwin" ]; then
PACKAGE_MANAGER="installer"
PACKAGE_FILE="wazuh-agent-${WAZUH_AGENT_VERSION}.$( [ "$ARCH" = "x86_64" ] && echo "intel64" || echo "arm64" ).pkg"
PACKAGE_URL="https://packages.wazuh.com/4.x/macos/${PACKAGE_FILE}"
else
error_message "Unsupported operating system: $OS_NAME"
exit 1
fi
;;
esac

# Ensure root privileges, either directly or through sudo
maybe_sudo() {
Expand All @@ -87,7 +95,7 @@ maybe_sudo() {
init_wazuh_agent() {
info_message "Initializing Wazuh agent..."

if [ "$(uname)" = "Linux" ]; then
if [ "$OS_NAME" = "Linux" ]; then
if command -v systemctl >/dev/null 2>&1; then
maybe_sudo systemctl daemon-reload
maybe_sudo systemctl enable wazuh-agent
Expand All @@ -96,10 +104,10 @@ init_wazuh_agent() {
maybe_sudo update-rc.d wazuh-agent defaults 95 10
maybe_sudo service wazuh-agent start
else
maybe_sudo /var/ossec/bin/wazuh-control start;
maybe_sudo /var/ossec/bin/wazuh-control start
fi
elif [ "$(uname)" = "Darwin" ]; then
maybe_sudo /Library/Ossec/bin/wazuh-control start;
elif [ "$OS_NAME" = "Darwin" ]; then
maybe_sudo /Library/Ossec/bin/wazuh-control start
else
error_message "Unsupported operating system."
fi
Expand All @@ -112,22 +120,32 @@ TEMP_DIR=$(mktemp -d) || { error_message "Failed to create a temporary directory
trap 'rm -rf "$TEMP_DIR"' EXIT

# Download the package to the temporary directory using curl with a progress bar
info_message "Downloading Wazuh agent from $PACKAGE_URL..."
curl -L --progress-bar -o "$TEMP_DIR/$PACKAGE_FILE" "$PACKAGE_URL" || { error_message "Failed to download Wazuh agent package. Ensure you have a strong internet connection. Aborting..."; exit 1; }
success_message "Wazuh agent downloaded successfully"
download_package() {
info_message "Downloading Wazuh agent from $PACKAGE_URL..."
HTTP_STATUS=$(curl -w "%{http_code}" -L --progress-bar -o "$TEMP_DIR/$PACKAGE_FILE" "$PACKAGE_URL")
if [ "$HTTP_STATUS" -ne 200 ]; then
log ERROR "Failed to download Wazuh agent package. HTTP Status: $HTTP_STATUS. Aborting..."
exit 1
fi
success_message "Wazuh agent downloaded successfully"
}

download_package

# Install the package
info_message "Installing Wazuh agent..."
PACKAGE_PATH="$TEMP_DIR/$PACKAGE_FILE"
case "$PACKAGE_MANAGER" in
"apt")
maybe_sudo WAZUH_MANAGER="$WAZUH_MANAGER" WAZUH_AGENT_NAME="$WAZUH_AGENT_NAME" dpkg -i "$PACKAGE_PATH"
maybe_sudo dpkg -i "$PACKAGE_PATH"
maybe_sudo apt-get install -f -y
;;
"yum")
maybe_sudo WAZUH_MANAGER="$WAZUH_MANAGER" WAZUH_AGENT_NAME="$WAZUH_AGENT_NAME" yum install -y "$PACKAGE_PATH"
"yum" | "dnf" | "zypper")
maybe_sudo $PACKAGE_MANAGER install -y "$PACKAGE_PATH"
;;
"installer")
echo "WAZUH_MANAGER=$WAZUH_MANAGER && WAZUH_AGENT_NAME=$WAZUH_AGENT_NAME" > /tmp/wazuh_envs
echo "WAZUH_MANAGER=$WAZUH_MANAGER" > /tmp/wazuh_envs
echo "WAZUH_AGENT_NAME=$WAZUH_AGENT_NAME" >> /tmp/wazuh_envs
maybe_sudo installer -pkg "$PACKAGE_PATH" -target /
;;
esac
Expand All @@ -139,4 +157,7 @@ if [ $? -ne 0 ]; then
fi
success_message "Wazuh agent installed successfully"

init_wazuh_agent
init_wazuh_agent || true # TODO: Fix this

# The cleanup will be automatically done due to the trap
info_message "Temporary files cleaned up."
2 changes: 1 addition & 1 deletion scripts/setup-agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ USER=${USER:-"root"}
GROUP=${GROUP:-"wazuh"}

WAZUH_MANAGER=${WAZUH_MANAGER:-'master.wazuh.adorsys.team'}
WAZUH_AGENT_VERSION=${WAZUH_AGENT_VERSION:-'4.8.0-1'}
WAZUH_AGENT_VERSION=${WAZUH_AGENT_VERSION:-'4.8.2-1'}
WAZUH_AGENT_NAME=${WAZUH_AGENT_NAME:-}

# Step 0: Ensure Curl and JQ are installed
Expand Down
5 changes: 3 additions & 2 deletions scripts/tests/test-script.bats
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#!/usr/bin/env bats

WAZUH_MANAGER="10.0.0.2"
bash /app/scripts/deps.sh

chmod +x /app/scripts/install.sh

# Test if the script runs without errors
@test "script runs without errors" {
export WAZUH_AGENT_NAME="test-agent-123"
/app/scripts/install.sh
export WAZUH_MANAGER="10.0.0.2"
run /app/scripts/install.sh
[ "$status" -eq 0 ]
}

0 comments on commit 46a5074

Please sign in to comment.