-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazure_spot_vm_monitor.py
46 lines (35 loc) · 1.35 KB
/
azure_spot_vm_monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Import the needed credential and management objects from the libraries.
import os
from dotenv import load_dotenv
from time import sleep
from azure.identity import DefaultAzureCredential
from azure.mgmt.compute import ComputeManagementClient
import requests
load_dotenv()
# Acquire a credential object using CLI-based authentication.
credential = DefaultAzureCredential()
# Make sure to set AZURE_SUBSCRIPTION_ID, AZURE_TENANT_ID, AZURE_CLIENT_ID, and AZURE_CLIENT_SECRET environment variables for authentication
# Retrieve subscription ID from environment variable.
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
while(True):
# do this forever...
timeout = False
# check if VM is up
URL = os.environ["URL"]
try:
r = requests.get(url = URL,timeout=3)
except:
timeout = True
if (timeout or r.status_code != 200):
# Obtain the management object for virtual machines
compute_client = ComputeManagementClient(credential, subscription_id)
VM_NAME = os.environ["VM_NAME"]
RESOURCE_GROUP_NAME = os.environ["RESOURCE_GROUP_NAME"]
poller = compute_client.virtual_machines.begin_start(
RESOURCE_GROUP_NAME,
VM_NAME
)
vm_result = poller.result()
else:
print("The server is up.")
sleep(10)