Files
loc_az_hci/infrastructure/gitops/azure-devops-agent.sh
defiQUG c39465c2bd
Some checks failed
Test / test (push) Has been cancelled
Initial commit: loc_az_hci (smom-dbis-138 excluded via .gitignore)
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-08 09:04:46 -08:00

196 lines
4.6 KiB
Bash
Executable File

#!/bin/bash
# Azure DevOps Self-Hosted Agent Setup Script
# Installs and configures Azure DevOps agent on a Proxmox VM
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Azure DevOps configuration
AZP_URL="${AZP_URL:-}"
AZP_TOKEN="${AZP_TOKEN:-}"
AZP_AGENT_NAME="${AZP_AGENT_NAME:-$(hostname)}"
AZP_POOL="${AZP_POOL:-Default}"
AZP_WORK="${AZP_WORK:-_work}"
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
check_root() {
if [ "$EUID" -ne 0 ]; then
log_error "Please run as root"
exit 1
fi
}
validate_config() {
if [ -z "$AZP_URL" ] || [ -z "$AZP_TOKEN" ]; then
log_error "Required Azure DevOps configuration missing"
log_info "Required environment variables:"
log_info " AZP_URL - Azure DevOps organization URL (e.g., https://dev.azure.com/yourorg)"
log_info " AZP_TOKEN - Personal Access Token with Agent Pools (Read & Manage) permission"
log_info ""
log_info "Optional:"
log_info " AZP_AGENT_NAME - Agent name (default: hostname)"
log_info " AZP_POOL - Agent pool name (default: Default)"
exit 1
fi
}
detect_os() {
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
VERSION=$VERSION_ID
log_info "Detected OS: $OS $VERSION"
else
log_error "Cannot detect operating system"
exit 1
fi
}
install_dependencies() {
log_info "Installing dependencies..."
case "$OS" in
ubuntu|debian)
apt-get update
apt-get install -y curl jq git
;;
rhel|centos|fedora)
yum install -y curl jq git
;;
*)
log_warn "Unknown OS. Please install: curl, jq, git"
;;
esac
}
download_agent() {
log_info "Downloading Azure DevOps agent..."
AGENT_DIR="/opt/azp-agent"
mkdir -p "$AGENT_DIR"
cd "$AGENT_DIR"
# Download agent package
case "$(uname -m)" in
x86_64)
AGENT_URL="https://vstsagentpackage.azureedge.net/agent/2.220.0/vsts-agent-linux-x64-2.220.0.tar.gz"
;;
arm64|aarch64)
AGENT_URL="https://vstsagentpackage.azureedge.net/agent/2.220.0/vsts-agent-linux-arm64-2.220.0.tar.gz"
;;
*)
log_error "Unsupported architecture: $(uname -m)"
exit 1
;;
esac
curl -LsS "$AGENT_URL" | tar -xz
log_info "Agent downloaded to $AGENT_DIR"
}
configure_agent() {
log_info "Configuring Azure DevOps agent..."
cd "$AGENT_DIR"
./config.sh \
--unattended \
--url "$AZP_URL" \
--auth pat \
--token "$AZP_TOKEN" \
--pool "$AZP_POOL" \
--agent "$AZP_AGENT_NAME" \
--work "$AZP_WORK" \
--replace \
--acceptTeeEula
if [ $? -eq 0 ]; then
log_info "Agent configured successfully"
else
log_error "Agent configuration failed"
exit 1
fi
}
install_service() {
log_info "Installing Azure DevOps agent as a systemd service..."
cd "$AGENT_DIR"
./svc.sh install
if [ $? -eq 0 ]; then
log_info "Service installed successfully"
else
log_error "Service installation failed"
exit 1
fi
}
start_service() {
log_info "Starting Azure DevOps agent service..."
./svc.sh start
sleep 5
if systemctl is-active --quiet azp-agent; then
log_info "Agent service is running"
systemctl status azp-agent --no-pager
else
log_error "Agent service failed to start"
systemctl status azp-agent --no-pager
exit 1
fi
}
show_info() {
log_info "Azure DevOps agent setup completed!"
log_info ""
log_info "Agent details:"
log_info " Name: $AZP_AGENT_NAME"
log_info " Pool: $AZP_POOL"
log_info " Work directory: $AGENT_DIR/$AZP_WORK"
log_info ""
log_info "Useful commands:"
log_info " Status: systemctl status azp-agent"
log_info " Stop: systemctl stop azp-agent"
log_info " Start: systemctl start azp-agent"
log_info " Restart: systemctl restart azp-agent"
log_info " Logs: journalctl -u azp-agent -f"
log_info ""
log_info "View agent in Azure DevOps:"
log_info " $AZP_URL/_settings/agentpools"
}
main() {
log_info "Starting Azure DevOps agent setup..."
check_root
validate_config
detect_os
install_dependencies
download_agent
configure_agent
install_service
start_service
show_info
}
main "$@"