Get VM status using Azure Python SDK - python

I have a list of VMs and I'd like to get each VM's status (ReadyRole/Stopped/StoppedDeallocated) using Azure's Python SDK.
I have done this in a bash terminal using azure cli commands and a combination of grep,tail and such utils but I'd like to do that in python script using Azure's SDK.
With the help of azure cli I run in a shell script azure vm list and then grep my way to get the status of the VMs.
I've been looking into servicemanagementservice.py of Azure SDK but I can't find a function like get_role_status(). list_hosted_services() and get_hosted_service_properties don't seem to provide the info I want, unless I'm missing something.
Can anyone point me towards a solution?

Base on my experience, we can get every instances status using Azure REST API.
So Azure SDK for python should have similar method, because the functions in Azure SDK use the same URL as REST API.
I tried to use this method get_deployment_by_name to get the instances status:
subscription_id = '****-***-***-**'
certificate_path = 'CURRENT_USER\\my\\***'
sms = ServiceManagementService(subscription_id, certificate_path)
result=sms.get_deployment_by_name("your service name","your deployment name")
You can get the role list and check the every role property, please see this picture:

Related

Is there a way to check on past Pivot Cloud Foundry (PCF) CLI buildpacks?

I'm currently attempting to stop utilizing a web proxy which allows internet access from an AWS Virtual Private Cloud as it won't be in use anymore soon. I also use the internet access to fetch data from an API endpoint which has past buildpack data such as the version and name of the buildpack itself. (https://buildpacks.cloudfoundry.org/#/buildpacks) General information is that I'm currently using python and AWS to do what I am doing.
Despite my research, I haven't been able to find such a CLI command which allows me to get this data without usage of this PCF API. Is there any way to do this without internet access?

Azure Python SDK: How to set auto-shutdown task on VMs?

On Azure Portal I can set auto-shutdown for a VM but can't find the API command for the Python SDK anywhere. Is this possible at the moment? Do I have to use DevTestLab? The SDK in question: https://github.com/Azure/azure-sdk-for-python
1.The auto-shutdown in the portal is configured as part of the VM's deployment template. If you updated the template, you could certainly deploy it in whatever way you choose.
2.You could schedule a script using the REST API in Azure to start/stop the VM at any schedule of your choosing. The script could deployed on Webjobs, Azure Functions or Azure Scheduler.

How to authorize Azure Python SDK on VM instance?

In AWS, you can assign a role to a VM, which then authorizes the instance when it makes queries to the AWS SDK. I am looking for similar functionality in Azure, or something that would enable me to do close to that.
I found this post which suggests that this is not possible in the way AWS does it. Are there any workarounds for this? I really don't want the system administrator to have to login to the instance and give their Azure Active Directory credentials to authorize it.
Excellent question :). I would suggest to wait a few days, we have something in progress that seems to fit your need. I created this issue for tracking.
The most simple would be to create a Service Principal credentials for these VMs. To do that, execute a post deployment script to install the CLI and "az ad sp create-for-rbac --sdk-auth >~/mycredentials.json". Then, you can start SDK script reading this credential file.
The "create-for-rbac" commands already exists if you want to look at it (--sdk-auth is the new option coming), so you can see that you can specify all scope and permissions needed in this command.
(I own the Azure SDK for Python at MS)

How to get Azure VM uptime/runtime (time in running status) with Azure Python SDK

Is it any way to get how long time a Azure VM have been in running status (uptime/runtime) with use of the Azure Python SDK?
A code example would be verry helpfull.
Or any hint om where I should look to find out how to do this.
Br. Rune
As I known, there is not an existing API to get the uptime value using Azure Python SDK for Azure VM. There is a feedback about uptime which you can follow up for this feature.
Per my experience, a workaround way is that you can install some third-party libraries like uptime in Python on Azure VM to get the system uptime value to publish it via HTTP or sending to other Azure stores on schedule, then you can call or retrieve to get it when you need.
Hope it helps.

Python Azure SDK Equivalent of CloudConfigurationManager

Is there a Python equivalent of:
Microsoft.Azure.CloudConfigurationManager.GetSetting
Currently I locate the ServiceConfiguration.cscfg file, and parse it!!
I reviewed the source codes & api documents of Azure SDK for Python, I didn't find any information about the feature in Python which be equivalent of Microsoft.Azure.CloudConfigurationManager.GetSetting in C#.
Meanwhile, it seems that Azure Python SDK only support for reading the .cscfg file to create a Cloud Service deployment via service management client as the code configuration = base64.b64encode(open(file_path, 'rb').read('path_to_.cscfg_file')) which from here.
So seems that parsing the .cscfg file is the only way to get the properties of cloud service.

Categories

Resources