Created
June 17, 2019 18:34
-
-
Save zduymz/e3df0427ce9227b248a252fcd02ddfd1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import adal | |
import requests | |
import os | |
import json | |
import logging | |
import sys | |
import time | |
log = logging.getLogger(__name__) | |
log.setLevel(logging.INFO) | |
handler = logging.StreamHandler(sys.stdout) | |
handler.setLevel(logging.INFO) | |
formatter = logging.Formatter( | |
'%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
handler.setFormatter(formatter) | |
log.addHandler(handler) | |
# setup found here - https://lnx.azurewebsites.net/non-interactive-login-in-azure-cli-2-0/ | |
tenant_id = '7a32773c-a86e-490d-ae40-xxxxx' | |
app_id = '177cfdd0-8fb1-44ca-910c-xxxxx' | |
app_secret = '4fe53c89-2198-440b-86e5-xxxx' | |
authority_url = 'https://login.microsoftonline.com/' + tenant_id | |
resource = 'https://management.azure.com/' | |
context = adal.AuthenticationContext(authority_url) | |
token = context.acquire_token_with_client_credentials(resource, app_id, app_secret) | |
headers = {'Authorization': 'Bearer ' + token['accessToken'], 'Content-Type': 'application/json'} | |
def restart_vm(subscriptionId, resourceGroupName, vmName): | |
url = 'https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}/start?api-version=2018-06-01'.format(subscriptionId=subscriptionId, resourceGroupName=resourceGroupName, vmName=vmName) | |
resp = requests.post(url, headers=headers) | |
print(resp.text) | |
if resp.status_code == 200: | |
return True | |
else: | |
return False | |
def get_vm_status(subscriptionId, resourceGroupName, vmName): | |
url = 'https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}/instanceView?api-version=2018-06-01'.format(subscriptionId=subscriptionId, resourceGroupName=resourceGroupName, vmName=vmName) | |
# params = {'api-version': '2018-06-01'} | |
resp = requests.get(url, headers=headers) | |
# print(resp) | |
if resp.status_code != 200: | |
return 'unknown' | |
info = json.loads(resp.text) | |
vm_status = 'running' | |
# print(map(lambda x: x.get('code'), info.get('statuses', []))) | |
for status in map(lambda x: x.get('code'), info.get('statuses', [])): | |
if vm_status in status: | |
return vm_status | |
return 'stopped' | |
def check_vm(subscriptionId, resourceGroupName, vmName): | |
status = get_vm_status(subscriptionId, resourceGroupName, vmName) | |
if status == 'stopped': | |
# try to restart 3 times | |
for i in range(3): | |
done = restart_vm(subscriptionId, resourceGroupName, vmName) | |
if done == False: | |
log.warn('Restart failed') | |
time.sleep(90) | |
else: | |
log.info('Restart successfully') | |
break | |
elif status == 'unknown': | |
log.error('Can not request API') | |
elif status == 'running': | |
log.info('VM {} running'.format(vmName)) | |
vm1 = ['fd7d53ef-e290-4ab1-937e-xxxxxxx', 'rg-01', 'test-01'] | |
vm2 = ['fd7d53ef-e290-4ab1-937e-xxxxxxx', 'rg-02', 'test-02'] | |
check_vm(*vm1) | |
check_vm(*vm2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment