Skip to content

Commit

Permalink
fix: Update install script to use a temporary directory and improve e…
Browse files Browse the repository at this point in the history
…rror handling
  • Loading branch information
bengo237 committed Sep 19, 2024
1 parent 92f0ee3 commit 3a5c3d5
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ error_message() {
log ERROR "$*"
}

# WAZUH_MANAGER is required
## WAZUH_MANAGER is required
if [ -z "$WAZUH_MANAGER" ]; then
error_message "WAZUH_MANAGER is required"
exit 1
Expand Down Expand Up @@ -69,13 +69,14 @@ fi

import_keys() {
info_message "Importing GPG key and setting up the repository for $OS"
# Import GPG key and set up the repository for Linux
GPG_KEY_URL="https://packages.wazuh.com/key/GPG-KEY-WAZUH"
if [ "$OS" = "Linux" ]; then
if [ "$PACKAGE_MANAGER" = "yum" ]; then
if ! rpm -q gpg-pubkey --qf '%{SUMMARY}\n' | grep -q "Wazuh"; then
curl -s $GPG_KEY_URL | $GPG_IMPORT_CMD
info_message "GPG key imported successfully."
fi
if ! rpm -q gpg-pubkey --qf '%{SUMMARY}\n' | grep -q "Wazuh"; then
curl -s $GPG_KEY_URL | $GPG_IMPORT_CMD
info_message "GPG key imported successfully."
fi
fi

if [ "$PACKAGE_MANAGER" = "apt" ]; then
Expand Down Expand Up @@ -107,34 +108,36 @@ EOF

installation() {
info_message "Installing Wazuh agent for $OS"
# Update and install Wazuh agent for Linux or download and install for macOS
if [ "$OS" = "Linux" ]; then
$PACKAGE_MANAGER update
WAZUH_MANAGER="$WAZUH_MANAGER" $PACKAGE_MANAGER install wazuh-agent
elif [ "$OS" = "macOS" ]; then
ARCH=$(uname -m)
BASE_URL="https://packages.wazuh.com/4.x/macos"

if [ "$ARCH" = "arm64" ]; then
# For Apple Silicon (M1/M2)
if [ "$ARCH" = "x86_64" ]; then
PKG_NAME="wazuh-agent-4.9.0-1.intel64.pkg"
elif [ "$ARCH" = "arm64" ]; then
PKG_NAME="wazuh-agent-4.9.0-1.arm64.pkg"
else
# For Intel-based Macs
PKG_NAME="wazuh-agent-4.9.0-1.x86_64.pkg"
error_message "Unsupported macOS architecture"
exit 1
fi

PKG_URL="$BASE_URL/$PKG_NAME"
TMP_DIR="$(mktemp -d)"
TMP_DIR="$(mktemp -d -t wazuh.XXXXXX)"
info_message "Using temporary directory: $TMP_DIR"
curl -o "$TMP_DIR/$PKG_NAME" "$PKG_URL"
info_message "Wazuh agent downloaded successfully."
echo "WAZUH_MANAGER='$WAZUH_MANAGER'" > /tmp/wazuh_envs
installer -pkg "$TMP_DIR/$PKG_NAME" -target /
sudo installer -pkg "$TMP_DIR/$PKG_NAME" -target /
rm -rf "$TMP_DIR" # Clean up the temporary directory after installation
fi
info_message "Wazuh agent installed successfully."
}


disable_repo() {
# Disable Wazuh repository after installation for Linux
if [ "$OS" = "Linux" ]; then
if [ "$PACKAGE_MANAGER" = "apt" ]; then
sed -i "s/^deb/#deb/" $REPO_FILE
Expand All @@ -146,13 +149,15 @@ disable_repo() {
}

config() {
# Replace MANAGER_IP placeholder with the actual manager IP in ossec.conf for Linux
if [ "$OS" = "Linux" ] && [ -n "$WAZUH_MANAGER" ]; then
sed -i "s/MANAGER_IP/$WAZUH_MANAGER/" "$OSSEC_CONF_PATH"
info_message "Wazuh manager IP configured successfully."
fi
}

start_agent() {
# Reload systemd daemon and enable/start services based on init system for Linux
if [ "$OS" = "Linux" ]; then
SYSTEMD_RUNNING=$(ps -C systemd > /dev/null 2>&1 && echo "yes" || echo "no")
if [ "$SYSTEMD_RUNNING" = "yes" ]; then
Expand Down

0 comments on commit 3a5c3d5

Please sign in to comment.