Skip to content

Commit b3b6e06

Browse files
committed
edit: AZURE login and health check-in + temp file write
1 parent 8979077 commit b3b6e06

File tree

1 file changed

+81
-107
lines changed

1 file changed

+81
-107
lines changed

.github/workflows/main.yaml

Lines changed: 81 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -90,123 +90,96 @@ jobs:
9090
az login --service-principal -u $AZURE_USERNAME -p $AZURE_PASSWORD --tenant $AZURE_TENANT > /dev/null 2>&1
9191
9292
echo "Checking Azure health status..."
93-
script_content=$(cat scripts/check_docker_instance.sh)
94-
deploy_wrapper="
95-
mkdir -p /home/$AZURE_VMUSER/scripts
96-
cat > /home/$AZURE_VMUSER/scripts/check_docker_instance.sh << 'SCRIPT_END'
97-
$script_content
98-
SCRIPT_END
99-
chmod +x /home/$AZURE_VMUSER/scripts/check_docker_instance.sh
100-
/home/$AZURE_VMUSER/scripts/check_docker_instance.sh
101-
"
102-
103-
OUTPUT=$(az vm run-command invoke \
93+
vm_state=$(az vm get-instance-view \
10494
-g $AZURE_RESOURCE_GROUP \
10595
-n $AZURE_VM_INSTANCE \
106-
--command-id RunShellScript \
107-
--scripts "$deploy_wrapper" \
108-
--output json 2>&1)
96+
--query "instanceView.statuses[?starts_with(code, 'PowerState/')].code" \
97+
-o tsv 2>/dev/null || echo "PowerState/unavailable")
98+
99+
if [[ "$vm_state" == "PowerState/running" ]]; then
100+
echo "VM is running (state: $vm_state)"
101+
102+
echo "Creating script to check docker instance"
103+
script_content=$(cat scripts/check_docker_instance.sh)
104+
deploy_wrapper="
105+
mkdir -p /home/$AZURE_VMUSER/scripts
106+
cat > /home/$AZURE_VMUSER/scripts/check_docker_instance.sh << 'SCRIPT_END'
107+
$script_content
108+
SCRIPT_END
109+
chmod +x /home/$AZURE_VMUSER/scripts/check_docker_instance.sh
110+
/home/$AZURE_VMUSER/scripts/check_docker_instance.sh
111+
"
112+
113+
echo "Running deploy DOCKER checker"
114+
OUTPUT=$(az vm run-command invoke \
115+
-g $AZURE_RESOURCE_GROUP \
116+
-n $AZURE_VM_INSTANCE \
117+
--command-id RunShellScript \
118+
--scripts "$deploy_wrapper" \
119+
--output json 2>&1)
109120
110-
# Check if the command executed successfully
111-
echo "Checking if command passed/failed..."
112-
if [ $? -ne 0 ]; then
121+
# Check if the command executed successfully
122+
echo "Checking if command passed/failed..."
123+
if [ $? -ne 0 ]; then
113124
echo "✗ Failed to execute command on Azure VM"
114125
echo "Error: $OUTPUT"
115-
echo "azure_status=unavailable" >> $GITHUB_ENV
116-
else
117-
# Parse the output
118-
echo "Parsing output..."
119-
stdout_content=$(echo "$OUTPUT" | jq -r '.value[0].message' 2>/dev/null | grep -oP '\[stdout\]\K.*' | sed 's/\\n/\n/g')
120-
121-
# Determine the status based on output
122-
echo "Determing Azure VM Instance status..."
123-
if echo "$stdout_content" | grep -q "NO_CONTAINERS"; then
124-
echo "✓ No Docker containers running on Azure VM - VM is available"
125-
echo "azure_status=available" >> $GITHUB_ENV
126-
elif echo "$stdout_content" | grep -q "NO_APP_CONTAINERS"; then
127-
echo "✓ No application containers running on Azure VM - VM is available"
128-
echo "azure_status=available" >> $GITHUB_ENV
129-
elif echo "$stdout_content" | grep -q "VERIGEN_RUNNING"; then
130-
echo "✗ VeriGenLLM-v2 is already running on Azure VM"
131-
# Extract and display the container details
132-
container_info=$(echo "$stdout_content" | grep -A 10 "VERIGEN_RUNNING" | tail -n +2 | head -n -1)
133-
echo "Running containers:"
134-
echo "$container_info"
135-
echo "azure_status=busy" >> $GITHUB_ENV
136-
elif echo "$stdout_content" | grep -q "OTHER_APPS_RUNNING"; then
137-
echo "⚠ Other applications are running on Azure VM"
138-
# Extract and display the container details
139-
container_info=$(echo "$stdout_content" | grep -A 10 "OTHER_APPS_RUNNING" | tail -n +2 | head -n -1)
140-
echo "Running containers:"
141-
echo "$container_info"
142-
echo "azure_status=busy" >> $GITHUB_ENV
126+
echo "azure_status=unknown" >> $GITHUB_ENV
143127
else
144-
echo "✗ Unable to determine Azure VM status"
145-
echo "azure_status=unknown" >> $GITHUB_ENV
128+
# Parse the output
129+
echo "Parsing output..."
130+
stdout_content=$(echo "$OUTPUT" | jq -r '.value[0].message' 2>/dev/null | grep -oP '\[stdout\]\K.*' | sed 's/\\n/\n/g')
131+
132+
# Determine the status based on output
133+
echo "Determing Azure VM Instance status..."
134+
if echo "$stdout_content" | grep -q "NO_CONTAINERS"; then
135+
echo "✓ No Docker containers running on Azure VM - VM is available"
136+
echo "azure_status=available" >> $GITHUB_ENV
137+
elif echo "$stdout_content" | grep -q "NO_APP_CONTAINERS"; then
138+
echo "✓ No application containers running on Azure VM - VM is available"
139+
echo "azure_status=available" >> $GITHUB_ENV
140+
elif echo "$stdout_content" | grep -q "VERIGEN_RUNNING"; then
141+
echo "✗ VeriGenLLM-v2 is already running on Azure VM"
142+
# Extract and display the container details
143+
container_info=$(echo "$stdout_content" | grep -A 10 "VERIGEN_RUNNING" | tail -n +2 | head -n -1)
144+
echo "Running containers:"
145+
echo "$container_info"
146+
echo "azure_status=busy" >> $GITHUB_ENV
147+
elif echo "$stdout_content" | grep -q "OTHER_APPS_RUNNING"; then
148+
echo "⚠ Other applications are running on Azure VM"
149+
# Extract and display the container details
150+
container_info=$(echo "$stdout_content" | grep -A 10 "OTHER_APPS_RUNNING" | tail -n +2 | head -n -1)
151+
echo "Running containers:"
152+
echo "$container_info"
153+
echo "azure_status=busy" >> $GITHUB_ENV
154+
else
155+
echo "✗ Unable to determine Azure VM status"
156+
echo "azure_status=unknown" >> $GITHUB_ENV
157+
fi
146158
fi
147-
fi
148-
149-
# Deploy to Azure if VM is available
150-
# Condition 1: Azure VM could be OFF -> Start and deploy + RLFT
151-
- name: Deploy to Azure VM by starting it
152-
env:
153-
AZURE_USERNAME: ${{ secrets.AZURE_USERNAME }}
154-
AZURE_PASSWORD: ${{ secrets.AZURE_PASSWORD }}
155-
AZURE_TENANT: ${{ secrets.AZURE_TENANT }}
156-
AZURE_RESOURCE_GROUP: ${{ secrets.AZURE_RESOURCE_GROUP }}
157-
AZURE_VM_INSTANCE: ${{ secrets.AZURE_VM_INSTANCE }}
158-
AZURE_VMUSER: ${{ secrets.AZURE_VMUSER }}
159-
APIKEYS_FILE: ${{ secrets.APIKEYS_FILE }}
160-
GCP_SECRETS_FILE: ${{ secrets.GCP_SECRETS_FILE }}
161-
GITHUB_REPO_URL: ${{ github.server_url }}/${{ github.repository }}.git
162-
GITHUB_BRANCH: "enhance-v1"
163-
if: env.azure_status == 'unavailable'
164-
run: |
165-
echo "Deploying to Azure VM..."
166159

167-
echo "Azure Cloud login..."
168-
az login --service-principal -u $AZURE_USERNAME -p $AZURE_PASSWORD --tenant $AZURE_TENANT > /dev/null 2>&1
169-
170-
echo "Starting Azure VM..."
171-
az vm start --resource-group $AZURE_RESOURCE_GROUP --name $AZURE_VM_INSTANCE
172-
173-
if [ $? -ne 0 ]; then
160+
elif [[ "$vm_state" == "PowerState/deallocated" ]]; then
161+
echo "Azure VM instance deallocated"
162+
echo "Starting Azure VM..."
163+
OUTPUT=$(az vm start --resource-group $AZURE_RESOURCE_GROUP \
164+
--name $AZURE_VM_INSTANCE)
165+
if [ $? -ne 0 ]; then
174166
echo "✗ Failed to start Azure VM"
175167
echo "azure_status=failure" >> $GITHUB_ENV
176168
exit 0
177-
fi
178-
179-
# Wait for VM to be ready
180-
sleep 60
181-
182-
# Create and run deployment script
183-
script_content=$(cat scripts/starter.sh)
184-
deploy_wrapper="
185-
mkdir -p /home/$AZURE_VMUSER/scripts
186-
cat > /home/$AZURE_VMUSER/scripts/starter.sh << 'SCRIPT_END'
187-
$script_content
188-
SCRIPT_END
189-
chmod +x /home/$AZURE_VMUSER/scripts/starter.sh
190-
/home/$AZURE_VMUSER/scripts/starter.sh '$GITHUB_REPO_URL' '$GITHUB_BRANCH' '$GCP_SECRETS_FILE' '$APIKEYS_FILE'
191-
"
169+
fi
170+
171+
# Wait for VM to be ready
172+
sleep 60
173+
echo "✓ Azure VM has started"
174+
echo "azure_status=available" >> $GITHUB_ENV
192175

193-
OUTPUT=$(az vm run-command invoke \
194-
-g $AZURE_RESOURCE_GROUP \
195-
-n $AZURE_VM_INSTANCE \
196-
--command-id RunShellScript \
197-
--scripts "$deploy_wrapper" \
198-
--output json 2>&1)
199-
200-
if [ $? -eq 0 ]; then
201-
echo "✓ Successfully deployed to Azure VM"
202-
echo "azure_status=success" >> $GITHUB_ENV
203176
else
204-
echo "✗ Failed to deploy on Azure VM"
205-
echo "azure_status=failure" >> $GITHUB_ENV
177+
echo "✗ Azure instance is in unexpected state: $vm_state"
178+
echo "azure_status=unknown" >> $GITHUB_ENV
179+
exit 0
206180
fi
207181

208182
# Deploy to Azure if VM is available
209-
# Condition 2: Azure VM is ON -> Deploy + RLFT
210183
- name: Deploy to Azure VM
211184
env:
212185
AZURE_USERNAME: ${{ secrets.AZURE_USERNAME }}
@@ -224,8 +197,8 @@ jobs:
224197
echo "Azure Cloud login..."
225198
az login --service-principal -u $AZURE_USERNAME -p $AZURE_PASSWORD --tenant $AZURE_TENANT > /dev/null 2>&1
226199
227-
echo "Copying starter script to Azure VM"
228200
# Create and run deployment script
201+
echo "Copying starter script to Azure VM"
229202
script_content=$(cat scripts/starter.sh)
230203
deploy_wrapper="
231204
mkdir -p /home/$AZURE_VMUSER/scripts
@@ -235,7 +208,8 @@ jobs:
235208
chmod +x /home/$AZURE_VMUSER/scripts/starter.sh
236209
/home/$AZURE_VMUSER/scripts/starter.sh '$GITHUB_REPO_URL' '$GITHUB_BRANCH' '$GCP_SECRETS_FILE' '$APIKEYS_FILE'
237210
"
238-
211+
212+
echo "Running starter script in Azure VM"
239213
OUTPUT=$(az vm run-command invoke \
240214
-g $AZURE_RESOURCE_GROUP \
241215
-n $AZURE_VM_INSTANCE \
@@ -269,7 +243,7 @@ jobs:
269243
GCP_VMUSER: ${{ secrets.GCP_VMUSER }}
270244
GCP_INSTANCE_NAME: ${{ secrets.GCP_INSTANCE_NAME }}
271245
GCP_INSTANCE_ZONE: ${{ secrets.GCP_INSTANCE_ZONE }}
272-
if: env.azure_status != 'available' && env.azure_status != 'success'
246+
if: env.azure_status != 'available' || env.azure_status != 'success'
273247
run: |
274248
echo "Deploying to GCP because Azure return VM instance: busy/failure/unknown"
275249
@@ -290,7 +264,7 @@ jobs:
290264
"auth_provider_x509_cert_url": "$GCP_CERT",
291265
"client_x509_cert_url": "$GCP_CERT_URI",
292266
"universe_domain": "$GCP_DOMAIN"
293-
}
267+
}
294268
EOF
295269
296270
echo "GCP Cloud login..."
@@ -394,7 +368,7 @@ jobs:
394368
GCP_SECRETS_FILE: ${{ secrets.GCP_SECRETS_FILE }}
395369
GITHUB_REPO_URL: ${{ github.server_url }}/${{ github.repository }}.git
396370
GITHUB_BRANCH: "enhance-v1"
397-
if: env.azure_status != 'available' && env.azure_status != 'success' && env.gcp_status == 'available'
371+
if: env.azure_status != 'available' || env.azure_status != 'success' && env.gcp_status == 'available'
398372
run: |
399373
echo "Deploying App to GCP VM"
400374
@@ -415,7 +389,7 @@ jobs:
415389
"auth_provider_x509_cert_url": "$GCP_CERT",
416390
"client_x509_cert_url": "$GCP_CERT_URI",
417391
"universe_domain": "$GCP_DOMAIN"
418-
}
392+
}
419393
EOF
420394
421395
echo "GCP Cloud login..."

0 commit comments

Comments
 (0)