Skip to content

Commit

Permalink
chore: sync with release v0.2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
ArmandMeppa committed Feb 11, 2025
1 parent 8208ccf commit cae03f1
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 49 deletions.
6 changes: 3 additions & 3 deletions scripts/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdenti
$SERVER_NAME = if ($env:SERVER_NAME -ne $null) { $env:SERVER_NAME } else { "wazuh-agent-status" }
$CLIENT_NAME = if ($env:CLIENT_NAME -ne $null) { $env:CLIENT_NAME } else { "wazuh-agent-status-client" }
$PROFILE = if ($env:PROFILE -ne $null) { $env:PROFILE } else { "user" }
$APP_VERSION = if ($env:APP_VERSION -ne $null) { $env:APP_VERSION } else { "0.2.2" }
$APP_VERSION = if ($env:APP_VERSION -ne $null) { $env:APP_VERSION } else { "0.2.7" }

if ($PROFILE -eq "admin") {
$WAS_VERSION = $APP_VERSION
Expand Down Expand Up @@ -107,7 +107,7 @@ function Create-StartupShortcut {
}

# Download binaries
$BaseURL = "https://github.com/ADORSYS-GIS/$SERVER_NAME/releases/tag/v$WAS_VERSION"
$BaseURL = "https://github.com/ADORSYS-GIS/$SERVER_NAME/releases/download/v$WAS_VERSION"
$ServerURL = "$BaseURL/$SERVER_NAME-windows-$ARCH.exe"
$ClientURL = "$BaseURL/$CLIENT_NAME-windows-$ARCH.exe"

Expand All @@ -123,4 +123,4 @@ Create-Service -ServiceName $SERVER_NAME -ExecutablePath $SERVER_EXE -DisplayNam
PrintStep 3 "Configuring client startup..."
Create-StartupShortcut -ShortcutName $CLIENT_NAME -ExecutablePath $CLIENT_EXE

SuccessMessage "Installation completed successfully."
SuccessMessage "Installation completed successfully."
94 changes: 59 additions & 35 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,32 @@ DESKTOP_UNIT_FOLDER=${DESKTOP_UNIT_FOLDER:-"$HOME/.config/autostart"}
DESKTOP_UNIT_FILE=${DESKTOP_UNIT_FILE:-"$DESKTOP_UNIT_FOLDER/$CLIENT_NAME.desktop"}

PROFILE=${PROFILE:-"user"}
APP_VERSION=${APP_VERSION:-"0.2.5"}
APP_VERSION=${APP_VERSION:-"0.2.7"}

# Assign app version based on profile
case "$PROFILE" in
"admin") WAS_VERSION="$APP_VERSION" ;;
*) WAS_VERSION="$APP_VERSION-user" ;;
"admin") WAS_VERSION="$APP_VERSION" ;;
*) WAS_VERSION="$APP_VERSION-user" ;;
esac


# OS and Architecture Detection
case "$(uname)" in
Linux) OS="linux"; BIN_DIR="/usr/local/bin" ;;
Darwin) OS="darwin"; BIN_DIR="/usr/local/bin" ;;
*) error_exit "Unsupported operating system: $(uname)" ;;
Linux)
OS="linux"
BIN_DIR="/usr/local/bin"
;;
Darwin)
OS="darwin"
BIN_DIR="/usr/local/bin"
;;
*) error_exit "Unsupported operating system: $(uname)" ;;
esac

ARCH=$(uname -m)
case "$ARCH" in
x86_64) ARCH="amd64" ;;
arm64|aarch64) ARCH="arm64" ;;
*) error_exit "Unsupported architecture: $ARCH" ;;
x86_64) ARCH="amd64" ;;
arm64 | aarch64) ARCH="arm64" ;;
*) error_exit "Unsupported architecture: $ARCH" ;;
esac

# Text Formatting
Expand Down Expand Up @@ -98,7 +103,7 @@ remove_file() {
create_service_file() {
info_message "Removing old service file if it exists..."
remove_file "$SERVICE_FILE"

info_message "Creating a new systemd service file..."
create_file "$SERVICE_FILE" "
[Unit]
Expand All @@ -119,21 +124,21 @@ WantedBy=multi-user.target
reload_and_enable_service() {
info_message "Reloading systemd daemon..."
maybe_sudo systemctl daemon-reload

info_message "Enabling service to start at boot..."
maybe_sudo systemctl enable "$SERVER_NAME"

info_message "Starting the service..."
maybe_sudo systemctl start "$SERVER_NAME"

info_message "Systemd service enabled and started."
}

# Desktop Unit File Creation
create_desktop_unit_file() {
info_message "Creating desktop unit directory if it doesn't exist..."
mkdir -p "$DESKTOP_UNIT_FOLDER"

info_message "Creating desktop unit file for autostart..."
create_file "$DESKTOP_UNIT_FILE" "
[Desktop Entry]
Expand All @@ -152,7 +157,7 @@ X-GNOME-Autostart-enabled=true
create_launchd_plist_file() {
local name="$1"
local filepath="$2"

info_message "Creating plist file for $name..."
create_file "$filepath" "
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
Expand All @@ -172,27 +177,41 @@ create_launchd_plist_file() {
</dict>
</plist>
"
info_message "Unloading previous plist file (if any)..."
maybe_sudo launchctl unload "$filepath" 2>/dev/null || true

info_message "Loading new plist file..."
maybe_sudo launchctl load -w "$filepath"

if [ "$name" = "$SERVER_NAME" ]; then
info_message "Loading new daemon plist file..."
maybe_sudo launchctl bootstrap "system $filepath" 2>/dev/null || warn_message "loading previous plist file failed: $filepath"
else
info_message "Loading new agent plist file..."
launchctl bootstrap "gui/$(id) $filepath" 2>/dev/null || warn_message "loading previous plist file failed: $filepath"
fi
info_message "macOS Launchd plist file created and loaded: $filepath"
}

unload_plist_file() {
local filepath="$1"

if [ -f "$filepath" ]; then
info_message "Unloading previous plist file (if any)..."
maybe_sudo launchctl bootout "gui/$(id) $filepath" 2>/dev/null || warn_message "Unloading previous plist file failed: $filepath"
info_message "Previous plist file unloaded: $filepath"
else
warn_message "Plist file: $filepath does not exist. Skipping."
fi
}

# Startup Configurations
make_server_launch_at_startup() {
case "$OS" in
linux) create_service_file && reload_and_enable_service ;;
darwin) create_launchd_plist_file "$SERVER_NAME" "$SERVER_LAUNCH_AGENT_FILE" ;;
linux) create_service_file && reload_and_enable_service ;;
darwin) create_launchd_plist_file "$SERVER_NAME" "$SERVER_LAUNCH_AGENT_FILE" ;;
esac
}

make_client_launch_at_startup() {
case "$OS" in
linux) create_desktop_unit_file ;;
darwin) create_launchd_plist_file "$CLIENT_NAME" "$CLIENT_LAUNCH_AGENT_FILE" ;;
linux) create_desktop_unit_file ;;
darwin) create_launchd_plist_file "$CLIENT_NAME" "$CLIENT_LAUNCH_AGENT_FILE" ;;
esac
}

Expand All @@ -218,16 +237,16 @@ validate_installation() {
error_exit "Systemd service file is missing: $SERVICE_FILE."
fi

systemctl is-enabled "$SERVER_NAME" >/dev/null 2>&1 && \
success_message "Systemd service is enabled: $SERVER_NAME." || \
systemctl is-enabled "$SERVER_NAME" >/dev/null 2>&1 &&
success_message "Systemd service is enabled: $SERVER_NAME." ||
error_exit "Systemd service is not enabled: $SERVER_NAME."

if [ -f "$DESKTOP_UNIT_FILE" ]; then
success_message "Desktop autostart file exists: $DESKTOP_UNIT_FILE."
else
error_exit "Desktop autostart file is missing: $DESKTOP_UNIT_FILE."
fi

elif [ "$OS" = "darwin" ]; then
if [ -f "$SERVER_LAUNCH_AGENT_FILE" ]; then
success_message "macOS Launchd server plist exists: $SERVER_LAUNCH_AGENT_FILE."
Expand All @@ -243,8 +262,8 @@ validate_installation() {
fi

case "$OS" in
linux) success_message "Installation complete! Restart your system to apply changes for the wazuh agent status." ;;
darwin) success_message "Installation complete!" ;;
linux) success_message "Installation complete! Restart your system to apply changes for the wazuh agent status." ;;
darwin) success_message "Installation complete!" ;;
esac
}

Expand All @@ -254,7 +273,7 @@ trap 'rm -rf "$TEMP_DIR"' EXIT

SERVER_BIN_NAME="$SERVER_NAME-$OS-$ARCH"
CLIENT_BIN_NAME="$CLIENT_NAME-$OS-$ARCH"
BASE_URL="https://github.com/ADORSYS-GIS/$SERVER_NAME/releases/tag/v$WAS_VERSION"
BASE_URL="https://github.com/ADORSYS-GIS/$SERVER_NAME/releases/download/v$WAS_VERSION"
SERVER_URL="$BASE_URL/$SERVER_BIN_NAME"
CLIENT_URL="$BASE_URL/$CLIENT_BIN_NAME"

Expand All @@ -263,12 +282,14 @@ echo "$BASE_URL"

print_step_header 1 "Binaries Download"
info_message "Downloading server binary from $SERVER_URL..."
curl -SL -o "$TEMP_DIR/$SERVER_BIN_NAME" "$SERVER_URL" || error_exit "Failed to download $SERVER_BIN_NAME"
curl -SL --progress-bar -o "$TEMP_DIR/$SERVER_BIN_NAME" "$SERVER_URL" || error_exit "Failed to download $SERVER_BIN_NAME"
info_message "Downloading client binary $CLIENT_URL..."
curl -SL -o "$TEMP_DIR/$CLIENT_BIN_NAME" "$CLIENT_URL" || error_exit "Failed to download $CLIENT_BIN_NAME"
curl -SL --progress-bar -o "$TEMP_DIR/$CLIENT_BIN_NAME" "$CLIENT_URL" || error_exit "Failed to download $CLIENT_BIN_NAME"
success_message "Binaries downloaded successfully."

print_step_header 2 "Binaries Installation"
info_message "Create Binary directory $BIN_DIR if it doesn't exist"
maybe_sudo mkdir -p "$BIN_DIR" || error_exit "Failed to create directory $BIN_DIR"
info_message "Installing server binary to $BIN_DIR..."
maybe_sudo mv "$TEMP_DIR/$SERVER_BIN_NAME" "$BIN_DIR/$SERVER_NAME"
maybe_sudo chmod +x "$BIN_DIR/$SERVER_NAME"
Expand All @@ -281,7 +302,10 @@ print_step_header 3 "Server Service Configuration"
make_server_launch_at_startup

print_step_header 4 "Client Service Configuration"
if [ "$OS" = "darwin" ]; then
unload_plist_file "$CLIENT_LAUNCH_AGENT_FILE"
fi
make_client_launch_at_startup

print_step_header 5 "Validating installation and configuration..."
validate_installation
validate_installation
156 changes: 156 additions & 0 deletions scripts/uninstall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# Define variables used in the uninstallation script
$SERVER_NAME = "wazuh-agent-status"
$CLIENT_NAME = "wazuh-agent-status-client"
$BIN_DIR = "C:\Program Files\$SERVER_NAME"
$SERVER_EXE = "$BIN_DIR\$SERVER_NAME.exe"
$CLIENT_EXE = "$BIN_DIR\$CLIENT_NAME.exe"



function Log {
param (
[string]$Level,
[string]$Message,
[string]$Color = "White" # Default color
)
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Write-Host "$Timestamp $Level $Message" -ForegroundColor $Color
}

# Logging helpers with colors
function InfoMessage {
param ([string]$Message)
Log "[INFO]" $Message "White"
}

function WarnMessage {
param ([string]$Message)
Log "[WARNING]" $Message "Yellow"
}

function ErrorMessage {
param ([string]$Message)
Log "[ERROR]" $Message "Red"
}

function SuccessMessage {
param ([string]$Message)
Log "[SUCCESS]" $Message "Green"
}

function PrintStep {
param (
[int]$StepNumber,
[string]$Message
)
Log "[STEP]" "Step ${StepNumber}: $Message" "White"
}

# Exit script with an error message
function ErrorExit {
param ([string]$Message)
ErrorMessage $Message
exit 1
}


function Remove-File {
param (
[Parameter(Mandatory = $true)]
[string]$FilePath
)
InfoMessage "Removing '$FilePath'"
try {
if (Test-Path -Path $FilePath) {
Remove-Item -Path $FilePath -Force -ErrorAction Stop
InfoMessage "File '$FilePath' has been successfully removed."
} else {
WarnMessage "File '$FilePath' does not exist."
}
} catch {
ErrorMessage "An error occurred while trying to remove the file: $_"
}
}


function Remove-Service {
param (
[Parameter(Mandatory=$true)]
[string]$ServiceName
)

# Check if the service exists
$service = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue

if ($service) {
# Stop the service if it's running
if ($service.Status -eq 'Running') {

Stop-Service -Name $ServiceName -Force
}

# Remove the service using sc.exe
sc.exe delete $ServiceName | Out-Null

InfoMessage "Service '$ServiceName' has been removed successfully."
} else {
WarnMessage "Service '$ServiceName' not found."
}
}


function Remove-StartupShortcut {
param (
[Parameter(Mandatory = $true)]
[string]$ShortcutName
)


# Check if the process is running
$process = Get-Process -Name $ShortcutName -ErrorAction SilentlyContinue

if ($process) {
InfoMessage "Process '$ShortcutName' is running. Stopping it..."
Stop-Process -Name $ShortcutName -Force
InfoMessage "Process '$ShortcutName' has been stopped."
}
else {
WarnMessage "Process '$ShortcutName' is not running. Skipping..."
}
# Define full path of the shortcut

InfoMessage "Removing Shortcut '$ShortcutName' from Startup..."
$ShortcutPath = [System.IO.Path]::Combine($env:APPDATA, "Microsoft\Windows\Start Menu\Programs\Startup", "$ShortcutName.lnk")

# Check if the shortcut exists and remove it
if (Test-Path $ShortcutPath) {
Remove-Item -Path $ShortcutPath -Force
InfoMessage "Shortcut '$ShortcutName' removed from Startup."
} else {
WarnMessage "Shortcut '$ShortcutName' not found in Startup."
}
}


function Remove-Binaries {
Remove-File $SERVER_EXE
Remove-File $CLIENT_EXE
Remove-File $BIN_DIR
}

# Function to uninstall application and clean up
function Uninstall-WazuhAgentStatus {
try {

Remove-StartupShortcut -ShortcutName $CLIENT_NAME
Remove-Service -ServiceName $SERVER_NAME

Remove-Binaries
SuccessMessage "Wazuh Agent Status uninstalled successfully"
}
catch {
ErrorMessage "Wazuh Agent Status Uninstall Failed: $($_.Exception.Message)"
}
}

Uninstall-WazuhAgentStatus
Loading

0 comments on commit cae03f1

Please sign in to comment.