1+ #! /bin/sh
2+ # Report VM is ready to Azure API in the absence of the Azure VM Agent
3+ # Adapted from https://learn.microsoft.com/en-us/azure/virtual-machines/linux/no-agent#bash-script
4+
5+ set -e
6+
7+ attempts=1
8+ retrieved_goal_state=false
9+ until [ " $attempts " -gt 5 ]
10+ do
11+ echo " obtaining goal state - attempt $attempts "
12+ goalstate=$( curl --fail -v -X ' GET' -H " x-ms-agent-name: azure-vm-register" \
13+ -H " Content-Type: text/xml;charset=utf-8" \
14+ -H " x-ms-version: 2012-11-30" \
15+ " http://168.63.129.16/machine/?comp=goalstate" )
16+ if [ $? -eq 0 ]
17+ then
18+ echo " successfully retrieved goal state"
19+ retrieved_goal_state=true
20+ break
21+ fi
22+ sleep 5
23+ attempts=$(( attempts+ 1 ))
24+ done
25+
26+ if [ " $retrieved_goal_state " != " true" ]
27+ then
28+ echo " failed to obtain goal state - cannot register this VM"
29+ exit 1
30+ fi
31+
32+ container_id=$( echo " $goalstate " | grep ContainerId | sed ' s/\s*<\/*ContainerId>//g' | sed ' s/\r$//' )
33+ instance_id=$( echo " $goalstate " | grep InstanceId | sed ' s/\s*<\/*InstanceId>//g' | sed ' s/\r$//' )
34+
35+ ready_doc=$( cat << EOF
36+ <?xml version="1.0" encoding="utf-8"?>
37+ <Health xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
38+ <GoalStateIncarnation>1</GoalStateIncarnation>
39+ <Container>
40+ <ContainerId>$container_id </ContainerId>
41+ <RoleInstanceList>
42+ <Role>
43+ <InstanceId>$instance_id </InstanceId>
44+ <Health>
45+ <State>Ready</State>
46+ </Health>
47+ </Role>
48+ </RoleInstanceList>
49+ </Container>
50+ </Health>
51+ EOF
52+ )
53+
54+ attempts=1
55+ until [ " $attempts " -gt 5 ]
56+ do
57+ echo " registering with Azure - attempt $attempts "
58+ curl --fail -v -X ' POST' -H " x-ms-agent-name: azure-vm-register" \
59+ -H " Content-Type: text/xml;charset=utf-8" \
60+ -H " x-ms-version: 2012-11-30" \
61+ -d " $ready_doc " \
62+ " http://168.63.129.16/machine?comp=health"
63+ if [ $? -eq 0 ]
64+ then
65+ echo " successfully register with Azure"
66+ exit 0
67+ fi
68+ sleep 5 # sleep to prevent throttling from wire server
69+ attempts=$(( attempts+ 1 ))
70+ done
71+
72+ echo " failed to register with Azure after $attempts attempts"
73+ exit 1
0 commit comments