Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 116 additions & 7 deletions pipeline/device-agent.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
#!/bin/bash
set -e
export PATH="$PATH:/usr/local/go/bin"

# ----------------------------
# Load environment file
# ----------------------------

load_device_agent_env() {
local device="${1:-}"
local env_file=""

SCRIPT_DIR="${SCRIPT_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" >/dev/null 2>&1 && pwd)}"
if [[ -z "$device" ]]; then
device="${DEVICE_TYPE:-k3s}"
fi
device="$(tr '[:upper:]' '[:lower:]' <<< "$device")"
if [[ "$device" != "docker" && "$device" != "k3s" ]]; then
echo "[ERROR] Invalid device type: '$device' (expected 'docker' or 'k3s')"
return 1
fi
env_file="$SCRIPT_DIR/device-agent_${device}.env"
if [[ ! -f "$env_file" && -f "$PWD/device-agent_${device}.env" ]]; then
env_file="$PWD/device-agent_${device}.env"
fi
if [[ ! -f "$env_file" ]]; then
echo "[WARN] Env file not found for device type '$device': $env_file"
return 1
fi
#echo "[INFO] Loading device agent environment: $env_file"
echo "[INFO] Device type selected: $device"
# shellcheck disable=SC1090
source "$env_file"
export DEVICE_TYPE="$device"
return 0
}
load_device_agent_env "$1" || true

# ----------------------------
# Environment & Validation Functions
# ----------------------------
Expand Down Expand Up @@ -85,9 +120,6 @@ install_basic_utilities() {
fi
}




install_docker_and_compose() {
cd $HOME

Expand Down Expand Up @@ -422,6 +454,42 @@ build_device_agent_docker() {
# Device Workload Fleet Management Client Service Functions
# ----------------------------

create_device_agent_systemd_service() {
echo "🔧 Creating systemd service for device-agent auto-start..."

# Get the actual docker-compose directory path
local compose_dir="$HOME/sandbox/docker-compose"

# Create systemd service file
sudo tee /etc/systemd/system/device-agent.service > /dev/null <<EOF
[Unit]
Description=Margo Device Agent
Requires=docker.service
After=docker.service network-online.target
Wants=network-online.target

[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=${compose_dir}
ExecStartPre=/bin/sleep 10
ExecStart=/usr/bin/docker compose up -d
ExecStop=/usr/bin/docker compose down
TimeoutStartSec=0

[Install]
WantedBy=multi-user.target
EOF

# Reload systemd and enable the service
sudo systemctl daemon-reload
sudo systemctl enable device-agent.service

echo "✅ Device-agent systemd service created and enabled"
echo "📋 Service will start device-agent automatically on boot"
echo "📁 Working directory: ${compose_dir}"
}

start_device_agent_docker_service() {
echo 'Starting workload-fleet-management-client...'
cd "$HOME/sandbox/docker-compose"
Expand All @@ -446,8 +514,10 @@ start_device_agent_docker_service() {
mkdir -p data
enable_docker_runtime
docker compose up -d
}

#systemd service for auto-start if vm reboots
create_device_agent_systemd_service
}

stop_device_agent_service_docker() {
echo "Stopping workload-fleet-management-client..."
Expand Down Expand Up @@ -1339,8 +1409,15 @@ create_device_ecdsa_certs() {



}
pause() {
echo
read -rp "Press Enter to continue..." _
}

# ----------------------------
# Menu Functions
# ----------------------------

show_menu() {
echo "Choose an option:"
Expand All @@ -1356,7 +1433,8 @@ show_menu() {
echo "10) cleanup-residual"
echo "11) create_device_rsa_certs"
echo "12) create_device_ecdsa_certs"
read -rp "Enter choice [1-12]: " choice
echo "13) Exit"
read -rp "Enter choice [1-13]: " choice
case $choice in
1) install_prerequisites;;
2) uninstall_prerequisites;;
Expand All @@ -1370,13 +1448,44 @@ show_menu() {
10) cleanup_residual;;
11) create_device_rsa_certs ;;
12) create_device_ecdsa_certs ;;
13) echo "👋 Goodbye!"; exit 0 ;;
*) echo "Invalid choice" ;;
esac

pause
}

# ----------------------------
# Main Script Execution
# ----------------------------
if [ -z "$1" ]; then
show_menu
main_loop() {
while true; do
show_menu
done
}

if [[ -z "$1" || "$1" == "docker" || "$1" == "k3s" ]]; then
main_loop
else
case "$1" in
install) install_prerequisites ;;
uninstall) uninstall_prerequisites ;;
start-docker) start_device_agent_docker ;;
stop-docker) stop_device_agent_docker ;;
start-k3s) start_device_agent_kubernetes ;;
stop-k3s) stop_device_agent_kubernetes ;;
status) show_status ;;
otel-install) install_otel_collector_promtail_wrapper ;;
otel-uninstall) uninstall_otel_collector_promtail_wrapper ;;
cleanup) cleanup_residual ;;
create-rsa-certs) create_device_rsa_certs ;;
create-ecdsa-certs) create_device_ecdsa_certs ;;

*)
echo "Usage:"
echo " $0 [docker|k3s]"
echo " DEVICE_TYPE=docker $0"
exit 1
;;
esac
fi
2 changes: 1 addition & 1 deletion pipeline/wfm-cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ delete_instance() {
# ----------------------------
show_menu() {
clear
echo "🎛️ WFM CLI Interactive Interface"
echo "🎛️ WFM CLI Interactive Interface(easy-CLI)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WFM CLI Interactive Interface(EasyCLI)"

echo "================================="
echo "Choose an option:"
echo "1) 📦 List Application Package"
Expand Down
Loading