Skip to content

Commit 87c7c76

Browse files
committed
[GitLab CI] Add VM deployment attempts
Add a new environment variable to the GitLab CI executor to allow for additional VM deployment attempts. GitLab automatically retries the prepare step 3 times. This env var allows additional retry attempts. This would allow more flexibility for the user to configure the number of attempts.
1 parent 43f1a85 commit 87c7c76

File tree

2 files changed

+90
-49
lines changed

2 files changed

+90
-49
lines changed

GitLab/custom-executor.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ The provided scripts expect the following environment variables to be set:
6565
- `CUSTOM_ENV_ORKA_VM_NAME_PREFIX` - The prefix of the generated VM name. Defaults to `gl-runner`.
6666
- `CUSTOM_ENV_ORKA_VM_USER` - User used to SSH to the VM.
6767
- `CUSTOM_ENV_ORKA_SSH_KEY_FILE` - The private SSH key contents to use when connecting to the VM. This key was created earlier during the Orka base image setup.
68+
- `CUSTOM_ENV_VM_DEPLOYMENT_ATTEMPTS` - The number of attempts the executor tries to deploy a VM before it fails. Defaults to `1`. Note - GitLab automatically retries 3 times. This env var allows additional retry attempts.
6869

6970
For more information about GitLab CI/CD environment variables, see [here][env-variables].
7071

GitLab/scripts/prepare.sh

Lines changed: 89 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,83 @@ currentDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
66

77
source "${currentDir}/base.sh"
88

9+
VM_DEPLOYMENT_ATTEMPTS=${CUSTOM_ENV_VM_DEPLOYMENT_ATTEMPTS:-1}
10+
911
function cleanup_on_failure {
1012
if [ -f "$BUILD_ID" ]; then
1113
vm_name=$(<"$BUILD_ID")
12-
echo "Failure detected. Cleaning up VM '$vm_name'..."
14+
echo "Failure detected. Cleaning up VM '$vm_name'..." >&2
1315

14-
orka3 vm delete "$vm_name" || echo "Failed to delete VM '$vm_name'"
16+
orka3 vm delete "$vm_name" || echo "Failed to delete VM '$vm_name'" >&2
1517

16-
echo "VM cleanup completed."
18+
echo "VM cleanup completed." >&2
1719
fi
1820

1921
system_failure
2022
}
2123

24+
function delete_vm_and_cleanup {
25+
if [ -f "$BUILD_ID" ]; then
26+
vm_name=$(<"$BUILD_ID")
27+
echo "Deleting VM '$vm_name'..." >&2
28+
orka3 vm delete "$vm_name" || echo "Failed to delete VM '$vm_name'" >&2
29+
rm -f "$BUILD_ID"
30+
rm -f "$CONNECTION_INFO_ID"
31+
fi
32+
}
33+
34+
function attempt_deployment {
35+
local vm_name=$1
36+
37+
echo "Deploying VM with name '$vm_name'..."
38+
39+
if ! vm_info=$(orka3 vm deploy "$vm_name" --config "$ORKA_CONFIG_NAME" -o json); then
40+
echo "VM deployment failed" >&2
41+
return 1
42+
fi
43+
44+
echo "VM deployed successfully."
45+
46+
vm_ip=$(echo "$vm_info" | jq -r '.[0]|.ip')
47+
vm_ip=$(map_ip "$vm_ip")
48+
vm_ssh_port=$(echo "$vm_info" | jq -r '.[0]|.ssh')
49+
50+
if ! valid_ip "$vm_ip"; then
51+
echo "Invalid ip: $vm_ip" >&2
52+
return 1
53+
fi
54+
55+
if [ -z "$vm_ssh_port" ]; then
56+
echo "Invalid port: $vm_ssh_port" >&2
57+
return 1
58+
fi
59+
60+
echo "$vm_ip;$vm_ssh_port" > "$CONNECTION_INFO_ID"
61+
62+
echo "Connecting to '$vm_name' ($vm_ip:$vm_ssh_port)"
63+
64+
echo "Waiting for SSH access to be available..."
65+
local ssh_ready=false
66+
for i in $(seq 1 30); do
67+
if ssh -i "$ORKA_SSH_KEY_FILE" -o ConnectTimeout=60 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$ORKA_VM_USER@$vm_ip" -p "$vm_ssh_port" "echo ok" >/dev/null 2>&1; then
68+
ssh_ready=true
69+
break
70+
fi
71+
sleep 1s
72+
done
73+
74+
if [ "$ssh_ready" = false ]; then
75+
echo 'Waited 30 seconds for sshd to start, exiting...' >&2
76+
return 1
77+
fi
78+
79+
touch ~/.ssh/known_hosts
80+
ssh-keygen -R "[$vm_ip]:$vm_ssh_port"
81+
ssh-keyscan -t rsa -p "$vm_ssh_port" "$vm_ip" >> ~/.ssh/known_hosts
82+
83+
return 0
84+
}
85+
2286
trap cleanup_on_failure ERR
2387

2488
echo "Authenticating with Orka..."
@@ -28,52 +92,28 @@ orka3 user set-token "$ORKA_TOKEN"
2892

2993
echo "Authenticated."
3094

31-
echo "Generating VM name..."
32-
33-
vm_name=$(generate_vm_name)
34-
35-
echo "Generated VM name: '$vm_name'"
36-
37-
echo "$vm_name" > "$BUILD_ID"
38-
39-
echo "Deploying VM with name '$vm_name'..."
40-
41-
vm_info=$(orka3 vm deploy "$vm_name" --config "$ORKA_CONFIG_NAME" -o json)
42-
43-
echo "VM deployed successfully."
44-
45-
vm_ip=$(echo "$vm_info" | jq -r '.[0]|.ip')
46-
vm_ip=$(map_ip "$vm_ip")
47-
vm_ssh_port=$(echo "$vm_info" | jq -r '.[0]|.ssh')
48-
49-
if ! valid_ip "$vm_ip"; then
50-
echo "Invalid ip: $vm_ip" 1>&2
51-
exit "$SYSTEM_FAILURE_EXIT_CODE"
52-
fi
53-
54-
if [ -z "$vm_ssh_port" ]; then
55-
echo "Invalid port: $vm_ssh_port" 1>&2
56-
exit "$SYSTEM_FAILURE_EXIT_CODE"
57-
fi
58-
59-
echo "$vm_ip;$vm_ssh_port" > "$CONNECTION_INFO_ID"
60-
61-
echo "Connecting to '$vm_name' ($vm_ip:$vm_ssh_port)"
62-
63-
echo "Waiting for SSH access to be available..."
64-
for i in $(seq 1 30); do
65-
if ssh -i "$ORKA_SSH_KEY_FILE" -o ConnectTimeout=60 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$ORKA_VM_USER@$vm_ip" -p "$vm_ssh_port" "echo ok" >/dev/null 2>&1; then
95+
for attempt in $(seq 1 "$VM_DEPLOYMENT_ATTEMPTS"); do
96+
echo "Deployment attempt $attempt of $VM_DEPLOYMENT_ATTEMPTS"
97+
98+
echo "Generating VM name..."
99+
vm_name=$(generate_vm_name)
100+
echo "Generated VM name: '$vm_name'"
101+
echo "$vm_name" > "$BUILD_ID"
102+
103+
if attempt_deployment "$vm_name"; then
104+
echo "Deployment successful on attempt $attempt"
66105
break
106+
else
107+
echo "Deployment attempt $attempt failed"
108+
109+
delete_vm_and_cleanup
110+
111+
if [ "$attempt" -eq "$VM_DEPLOYMENT_ATTEMPTS" ]; then
112+
echo "All deployment attempts failed. Exiting..." >&2
113+
exit "$SYSTEM_FAILURE_EXIT_CODE"
114+
else
115+
echo "Retrying deployment..."
116+
sleep 2
117+
fi
67118
fi
68-
69-
if [ "$i" == "30" ]; then
70-
echo 'Waited 30 seconds for sshd to start, exiting...' 1>&2
71-
exit "$SYSTEM_FAILURE_EXIT_CODE"
72-
fi
73-
74-
sleep 1s
75119
done
76-
77-
touch ~/.ssh/known_hosts
78-
ssh-keygen -R "[$vm_ip]:$vm_ssh_port"
79-
ssh-keyscan -t rsa -p "$vm_ssh_port" "$vm_ip" >> ~/.ssh/known_hosts

0 commit comments

Comments
 (0)