Get the App
SLTechnology News&Howtos  ›  Servers  › 

Python filters virtual machines through OpenStack API interface

Shulou Source: shulou.com Published: 2022-06-03 08:40:37 10月04日 Update

Development background:

Due to the record of overheated migration failure on OpenStack, the virtual machine remount disk will report an error, and the data disk will not be mounted, so you need to find all virtual machine processing on OpenStack that has overheated migration and failed migration, so as to avoid subsequent remount failure.

Development environment:

Centos,Python 2.7.5,OpenStack

Specific ideas:

Shell filter command

Nova list-all, find all virtual machines.

Nova instance-action-list virtual machine ID | egrep-in 'live-migration | error' filters out virtual machines with corresponding actions.

Use shell script filtering, but because the execution of the nova instance-action-list command is too slow, consider using Python to find this part of the virtual machine through the interface of OS.

Through the API interface call of OpenStack, find out all the virtual machines, and query all the actions of each virtual machine, filter out the virtual machine with live-migration action and message as Error, then output the virtual machine name and virtual machine ID.

Key steps:

1. Login authentication implementation: OpenStack mainly provides authentication services through keystoneauth components. I initialize a Session object through the keystoneauth2.session.Session class of keystoneauth components, which can store user name, password, token and other information.

2. The shell command corresponds to the method query of the novaclient library, mainly through the official document of OpenStack: https://docs.openstack.org/python-novaclient/latest/reference/index.html to query the desired method and the parameters of the method.

Specific source code #! / usr/bin/python2#-*-coding: utf-8-*-from novaclient import clientfrom keystoneauth2 import sessionfrom keystoneauth2.identity import v3import requests# login authentication auth = v3.Password (user_domain_name='Default', username='*', password='*', project_domain_name='Default' Project_name='admin', auth_url=' http://IP:35357/v3')sess = session.Session (auth=auth) nova = client.Client (2 Session=sess) # query all virtual machines instances = nova.servers.list (search_opts= {'all_tenants':'1'}) # Loop for instance in instances: # query the actions of each virtual machine for i in nova.instance_action.list (instance.id): # filter out the actions of live-migration and the result is Error if i.to_dict () [' action'] = =' Live-migration' and i.to_dict () ['message'] = =' Error': # output virtual machine name and virtual machine ID print instance.name Instance.id break summary

Python's novaclient library and keystoneauth2 library have encapsulated the specific request process. Basically, you only need to fill in the key information and then you can directly call the API API to obtain virtual machine information. The main problem encountered is that there are few API interface materials of OpenStack, and basically you need to read official documents.

Tags: Virtual machine action query interface information command method selection authentication key name official object document disk component development login output parameter Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Technology Xiaomi vpn Docker