Skip to content

Commit 19e48fb

Browse files
authored
DCA-1263 agents setup (#666)
* DCA-1263 agents setup * improvements
1 parent b4c25e4 commit 19e48fb

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

app/util/bamboo/agents_setup.sh

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#!/bin/bash
2+
3+
start=$(date +%s)
4+
5+
BAMBOO_URL="http://bamboo-test-stack.com" # e.g. http://1.123.150.205:8085
6+
USERNAME="admin"
7+
PASSWORD="admin"
8+
REMOTE_AGENTS_COUNT=55
9+
10+
# shellcheck disable=SC2001
11+
# trim trailing slash from URL if any
12+
BAMBOO_URL=$(echo $BAMBOO_URL | sed 's:/*$::')
13+
14+
AGENT_JAR_URL=$BAMBOO_URL/agentServer/agentInstaller
15+
AGENT_JAR=agentInstaller.jar
16+
AGENT_HOME="bamboo-agent-home"
17+
18+
echo NUMBERS OF REMOTE AGENTS: $REMOTE_AGENTS_COUNT
19+
echo BAMBOO INSTANCE URL: $BAMBOO_URL
20+
echo BAMBOO CREDENTIALS: $USERNAME/$PASSWORD
21+
echo # move to a new line
22+
23+
echo "Step1: Check Java"
24+
if ! [[ -x "$(command -v java)" ]]; then
25+
echo "Install openjdk with apt-get"
26+
sudo apt-get update && sudo apt-get install -y openjdk-11-jre-headless
27+
if [[ $? -ne 0 ]]; then
28+
echo "Java was NOT installed. Please install Java manually."
29+
exit 1
30+
fi
31+
fi
32+
echo "Java version:"
33+
java -version
34+
echo # move to a new line
35+
36+
37+
echo "Step2: Cleanup"
38+
echo "Stop existing agents processes"
39+
pkill -f "agentServer"
40+
41+
echo "Clean up previous agents files"
42+
rm -rf $AGENT_HOME* $AGENT_JAR
43+
echo # move to a new line
44+
45+
46+
echo "Step3: Download agent installer"
47+
curl $AGENT_JAR_URL --output $AGENT_JAR
48+
echo # move to a new line
49+
50+
51+
echo "Step4: Start agents"
52+
# start agents
53+
for ((i=1;i<=REMOTE_AGENTS_COUNT;i++))
54+
do
55+
java -jar -Dbamboo.home=$AGENT_HOME"$i" -Dbamboo.fs.timestamp.precision=1000000 $AGENT_JAR $BAMBOO_URL/agentServer/ > /dev/null 2>&1 &
56+
echo Agent "$i/$REMOTE_AGENTS_COUNT" started
57+
done
58+
echo # move to a new line
59+
60+
61+
echo "Step5: Authenticate agents"
62+
# authenticate created agents
63+
for ((i=1;i<=REMOTE_AGENTS_COUNT;i++))
64+
do
65+
attempts=100
66+
sleep_time=5
67+
68+
retries=0
69+
while [ ! -f "$HOME/$AGENT_HOME$i/uuid-temp.properties" ]
70+
do
71+
((retries+=1))
72+
if [ "$retries" -eq "$attempts" ]; then
73+
echo "Error: Terminated due to timeout. Agent $i uuid-temp.properties file not found in $attempts attempts."
74+
exit 1
75+
fi
76+
echo "Waiting for agent $i/$REMOTE_AGENTS_COUNT temp properties file creation. Attempt $retries/$attempts. Sleeping $sleep_time seconds."
77+
sleep $sleep_time
78+
done
79+
80+
echo "Starting authentication of agent $i/$REMOTE_AGENTS_COUNT"
81+
attempts=100
82+
sleep_time=5
83+
retries=0
84+
85+
auth_response=-1
86+
while [ "$retries" -lt "$attempts" ] && [ "$auth_response" != "0" ]
87+
do
88+
((retries+=1))
89+
properties=$(sed -n -e 's/.*agentUuid\=//p' "$HOME/$AGENT_HOME$i/uuid-temp.properties")
90+
curl -s --fail --show-error -X PUT --user $USERNAME:$PASSWORD "$BAMBOO_URL/rest/api/latest/agent/authentication/$properties"
91+
auth_response=$?
92+
echo "Auth response for agent $i/$REMOTE_AGENTS_COUNT: $auth_response"
93+
if [ "$auth_response" != "0" ]; then
94+
echo "Waiting for agent $i/$REMOTE_AGENTS_COUNT authentication. Attempt $retries/$attempts. Sleeping $sleep_time seconds."
95+
sleep $sleep_time
96+
fi
97+
done
98+
if [ "$auth_response" != "0" ]; then
99+
echo "Error: Unable to authenticate agent $i in $attempts attempts"
100+
exit 1
101+
fi
102+
echo "Agent $i/$REMOTE_AGENTS_COUNT has been authenticated"
103+
104+
done
105+
echo # move to a new line
106+
107+
108+
echo "Step6: Wait for agents to be ready"
109+
for ((i=1;i<=REMOTE_AGENTS_COUNT;i++))
110+
do
111+
attempts=200
112+
sleep_time=5
113+
retries=0
114+
check_grep=""
115+
check_grep_fail=""
116+
117+
while [ -z "$check_grep" ]
118+
do
119+
((retries+=1))
120+
check_grep=$(grep "ready to receive builds" "$HOME/$AGENT_HOME$i/atlassian-bamboo-agent.log")
121+
check_grep_fail=$(grep "Failed to connect to" "$HOME/$AGENT_HOME$i/atlassian-bamboo-agent.log")
122+
if [ "$retries" -eq "$attempts" ]; then
123+
echo "Error: Terminated due to timeout. Agent $i is not ready in $attempts attempts."
124+
echo "See logs for more details: $HOME/$AGENT_HOME$i/atlassian-bamboo-agent.log"
125+
exit 1
126+
fi
127+
if [ -n "$check_grep_fail" ]; then
128+
echo "Error: Agent $i/$REMOTE_AGENTS_COUNT could not connect to server. Check bamboo server network setup."
129+
echo "Message: $check_grep_fail"
130+
echo "See logs for more details: $HOME/$AGENT_HOME$i/atlassian-bamboo-agent.log"
131+
exit 1
132+
fi
133+
if [ -z "$check_grep" ]; then
134+
echo "Waiting for agent $i/$REMOTE_AGENTS_COUNT to be ready. Attempt $retries/$attempts. Sleeping $sleep_time seconds."
135+
sleep $sleep_time
136+
fi
137+
done
138+
echo "Agent $i/$REMOTE_AGENTS_COUNT ready to receive builds"
139+
done
140+
echo # move to a new line
141+
142+
end=$(date +%s)
143+
echo "DCAPT util script execution is finished successfully in $((end-start)) seconds."

0 commit comments

Comments
 (0)