33
44# Function to be called on shutdown
55cleanup () {
6- echo " Container stopped. Running cleanup..."
7- cd " $INSTALL_FOLDER /$AGENT_NUMBER "
8- ./config.sh remove --unattended --auth pat --token " $AZP_TOKEN "
9- echo " Agent successfully unregistered."
10- exit 0
6+ echo " Container stopped. Running cleanup..."
7+ cd " $INSTALL_FOLDER /$AGENT_NUMBER "
8+ ./config.sh remove --unattended --auth pat --token " $AZP_TOKEN "
9+ echo " Agent successfully unregistered."
10+ exit 0
1111}
1212
1313# Trap SIGTERM and call cleanup
1414trap cleanup SIGTERM
1515
1616# Add azureagent user to Docker group if /var/run/docker.sock exists
1717if [ -S /var/run/docker.sock ]; then
18- DOCKER_SOCK_GID=$( stat -c ' %g' /var/run/docker.sock)
19- echo " Adding azureagent user to GID $DOCKER_SOCK_GID for Docker access..."
20- sudo groupadd -g " $DOCKER_SOCK_GID " -f dockerhost 2> /dev/null || true
21- sudo usermod -aG " $DOCKER_SOCK_GID " azureagent 2> /dev/null || true
22- # sudo groupadd -g "$DOCKER_SOCK_GID" docker|| true
18+ DOCKER_SOCK_GID=$( stat -c ' %g' /var/run/docker.sock)
19+ echo " Adding azureagent user to GID $DOCKER_SOCK_GID for Docker access..."
20+ sudo groupadd -g " $DOCKER_SOCK_GID " -f dockerhost 2> /dev/null || true
21+ sudo usermod -aG " $DOCKER_SOCK_GID " azureagent 2> /dev/null || true
22+ # sudo groupadd -g "$DOCKER_SOCK_GID" docker|| true
2323fi
2424
2525# Verify required environment variables
2626if [ -z " $AZP_URL " ]; then
27- echo " Error: AZP_URL must be defined"
28- exit 1
27+ echo " Error: AZP_URL must be defined"
28+ exit 1
2929fi
3030
3131# Retrieve Azure DevOps token from AWS Secrets Manager if not provided
3232if [ -z " $AZP_TOKEN " ]; then
33- if [ -n " $AZURE_DEVOPS_TOKEN_SECRET_ARN " ] && [ -n " $AWS_REGION " ]; then
34- echo " Retrieving Azure DevOps token from AWS Secrets Manager..."
35-
36- # Use official aws-ssm (hypolas/aws-ssm-light ) first
37- if command -v aws-ssm > /dev/null 2>&1 ; then
38- echo " Using aws-ssm (hypolas/aws-ssm-light )..."
39- # Syntax: aws-ssm <secret-id> [region]
40- SECRET_TOKEN=$( aws-ssm " $AZURE_DEVOPS_TOKEN_SECRET_ARN " " $AWS_REGION " 2> /dev/null)
41- elif command -v light_ssm > /dev/null 2>&1 ; then
42- echo " Using light_ssm (fallback)..."
43- SECRET_TOKEN=$( light_ssm " $AZURE_DEVOPS_TOKEN_SECRET_ARN " " $AWS_REGION " 2> /dev/null)
44- else
45- echo " ❌ No AWS Secrets Manager client available (aws-ssm or light_ssm)"
46- echo " Install aws-ssm from hypolas/aws-ssm-light or enable INSTALL_AWS_SSM=true"
47- exit 1
48- fi
49-
50- if [ -n " $ SECRET_TOKEN" ] && [ " $ SECRET_TOKEN" != " null" ]; then
51- AZP_TOKEN=" $SECRET_TOKEN "
52- echo " ✅ Token retrieved from AWS Secrets Manager"
53- else
54- echo " ❌ Failed to retrieve token from Secrets Manager"
55- exit 1
56- fi
57- else
58- echo " ❌ AZP_TOKEN not provided and AWS Secrets Manager not configured"
59- echo " Provide either AZP_TOKEN, or AWS_REGION + AZURE_DEVOPS_TOKEN_SECRET_ARN"
60- exit 1
61- fi
33+ if [ -n " $AZURE_DEVOPS_TOKEN_SECRET_ARN " ] && [ -n " $AWS_REGION " ]; then
34+ echo " Retrieving Azure DevOps token from AWS Secrets Manager..."
35+
36+ # Use official aws-ssm (hypolas/aws-ssm-lite ) first
37+ if command -v aws-ssm > /dev/null 2>&1 ; then
38+ echo " Using aws-ssm (hypolas/aws-ssm-lite )..."
39+ # Syntax: aws-ssm <secret-id> [region]
40+ SECRET_TOKEN=$( aws-ssm " ${ AZURE_DEVOPS_TOKEN_SECRET_ARN} " " ${ AWS_REGION} " 2> /dev/null)
41+ elif command -v light_ssm > /dev/null 2>&1 ; then
42+ echo " Using light_ssm (fallback)..."
43+ SECRET_TOKEN=$( light_ssm " ${ AZURE_DEVOPS_TOKEN_SECRET_ARN} " " ${ AWS_REGION} " 2> /dev/null)
44+ else
45+ echo " ❌ No AWS Secrets Manager client available (aws-ssm or light_ssm)"
46+ echo " Install aws-ssm from hypolas/aws-ssm-lite or enable INSTALL_AWS_SSM=true"
47+ exit 1
48+ fi
49+
50+ if [[ -n ${ SECRET_TOKEN} ]] && [[ ${ SECRET_TOKEN} != " null" ] ]; then
51+ AZP_TOKEN=" ${ SECRET_TOKEN} "
52+ echo " ✅ Token retrieved from AWS Secrets Manager"
53+ else
54+ echo " ❌ Failed to retrieve token from Secrets Manager"
55+ exit 1
56+ fi
57+ else
58+ echo " ❌ AZP_TOKEN not provided and AWS Secrets Manager not configured"
59+ echo " Provide either AZP_TOKEN, or AWS_REGION + AZURE_DEVOPS_TOKEN_SECRET_ARN"
60+ exit 1
61+ fi
6262else
63- echo " ✅ Azure DevOps token provided directly"
63+ echo " ✅ Azure DevOps token provided directly"
6464fi
6565
66- if [ -z " $ AZP_POOL" ]; then
67- echo " Error: AZP_POOL must be defined"
68- exit 1
66+ if [[ -z ${ AZP_POOL} ] ]; then
67+ echo " Error: AZP_POOL must be defined"
68+ exit 1
6969fi
7070
71- if [ -z " $ AGENT_NUMBER" ]; then
72- echo " Error: AGENT_NUMBER must be defined"
73- exit 1
71+ if [[ -z ${ AGENT_NUMBER} ] ]; then
72+ echo " Error: AGENT_NUMBER must be defined"
73+ exit 1
7474fi
7575
7676# Set default values if necessary
@@ -80,29 +80,29 @@ DEFAULT_VOLUMES=${DEFAULT_VOLUMES:-"/var/run/docker.sock:/var/run/docker.sock,/c
8080
8181# Retrieve INSTANCE_ID from AWS metadata if not provided
8282if [ -z " $INSTANCE_ID " ]; then
83- echo " Retrieving INSTANCE_ID from AWS using IMDSv2..."
84-
85- # Retrieve IMDSv2 token to secure metadata access
86- IMDS_TOKEN=$( curl -X PUT " http://169.254.169.254/latest/api/token" \
87- -H " X-aws-ec2-metadata-token-ttl-seconds: 21600" \
88- -s 2> /dev/null) || true
89-
90- if [ -n " $IMDS_TOKEN " ]; then
91- # Use token to retrieve instance ID
92- INSTANCE_ID=$( curl -H " X-aws-ec2-metadata-token: $IMDS_TOKEN " \
93- -s " http://169.254.169.254/latest/meta-data/instance-id" 2> /dev/null) || true
94- fi
95-
96- echo " INSTANCE_ID from IMDSv2: $INSTANCE_ID "
97-
98- if [ -z " $INSTANCE_ID " ] || [ " $INSTANCE_ID " = " " ]; then
99- echo " Warning: Unable to retrieve AWS INSTANCE_ID, using hostname"
100- INSTANCE_ID=$( hostname)
101- else
102- echo " INSTANCE_ID retrieved from AWS: $INSTANCE_ID "
103- fi
83+ echo " Retrieving INSTANCE_ID from AWS using IMDSv2..."
84+
85+ # Retrieve IMDSv2 token to secure metadata access
86+ IMDS_TOKEN=$( curl -X PUT " http://169.254.169.254/latest/api/token" \
87+ -H " X-aws-ec2-metadata-token-ttl-seconds: 21600" \
88+ -s 2> /dev/null) || true
89+
90+ if [ -n " $IMDS_TOKEN " ]; then
91+ # Use token to retrieve instance ID
92+ INSTANCE_ID=$( curl -H " X-aws-ec2-metadata-token: $IMDS_TOKEN " \
93+ -s " http://169.254.169.254/latest/meta-data/instance-id" 2> /dev/null) || true
94+ fi
95+
96+ echo " INSTANCE_ID from IMDSv2: $INSTANCE_ID "
97+
98+ if [ -z " $INSTANCE_ID " ] || [ " $INSTANCE_ID " = " " ]; then
99+ echo " Warning: Unable to retrieve AWS INSTANCE_ID, using hostname"
100+ INSTANCE_ID=$( hostname)
101+ else
102+ echo " INSTANCE_ID retrieved from AWS: $INSTANCE_ID "
103+ fi
104104else
105- echo " INSTANCE_ID provided: $INSTANCE_ID "
105+ echo " INSTANCE_ID provided: $INSTANCE_ID "
106106fi
107107
108108echo " =========================================="
@@ -121,21 +121,21 @@ echo "=========================================="
121121# Configure the agent
122122echo " Configuring Azure DevOps agent..."
123123/opt/setup-scripts/configure-agent.sh \
124- " $INSTALL_FOLDER " \
125- " $AZP_URL " \
126- " $AZP_TOKEN " \
127- " $AZP_POOL " \
128- " $AZP_AGENT_NAME " \
129- " $AGENT_NUMBER " \
130- " $INSTANCE_ID "
124+ " $INSTALL_FOLDER " \
125+ " $AZP_URL " \
126+ " $AZP_TOKEN " \
127+ " $AZP_POOL " \
128+ " $AZP_AGENT_NAME " \
129+ " $AGENT_NUMBER " \
130+ " $INSTANCE_ID "
131131
132132# Add capabilities
133133echo " Adding capabilities..."
134134/opt/setup-scripts/add-capabilities.sh \
135- " $DEFAULT_CONTAINER_IMAGE " \
136- " $DEFAULT_VOLUMES " \
137- " $AGENT_NUMBER " \
138- " $INSTALL_FOLDER "
135+ " $DEFAULT_CONTAINER_IMAGE " \
136+ " $DEFAULT_VOLUMES " \
137+ " $AGENT_NUMBER " \
138+ " $INSTALL_FOLDER "
139139
140140echo " Configuration complete. Starting agent..."
141141
0 commit comments