Development Background:

Because OpenStack has records of overheated migration failure, an error occurs when the virtual machine is remounted to the disk, and the data disk cannot be mounted. Therefore, you need to locate all the virtual machines that have overheated migration and failed to migrate on OpenStack to avoid disk mounting failure during subsequent reinstallation.

Development environment:

Centos, Python 2.7.5, OpenStack

Specific ideas:

Shell filter commands nova list – all find all virtual machine nova instance – action – the list virtual machine ID | egrep – in ‘live – migration | error’ screen with appropriate action of the virtual machine. Use a shell script to filter, but since nova instance-action-list is too slow to execute, consider using Python to find this part of the virtual machine through the OS interface. Invoke OpenStack APIS to find all virtual machines, query all actions of each virtual machine, and output the virtual machine name and ID if the virtual machine has live-migration actions and message is Error.

Key steps:

1. Login authentication: It is primarily through keystoneauth components to provide certification service, I through keystoneauth component keystoneauth1 session. The session class initializes a session object, This object can store information such as user name, password, and token. 2, shell command corresponding novaclient library query, mainly through it is the official document: docs.openstack.org/python-nova… Query the desired method and its parameters.

Concrete source code

#! /usr/bin/python2
# -*- coding: utf-8 -*-  
from novaclient import client
from keystoneauth1 import session
from keystoneauth1.identity import v3
import 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'})
# cycle
for instance in instances:
        # Query the action of each VIRTUAL machine
        for i in nova.instance_action.list(instance.id):
                Select live-migration actions and the result is Error
                if i.to_dict()['action'] = ='live-migration' and i.to_dict()['message'] = ='Error':
                        # Output the virtual machine name and ID
                        print instance.name ,instance.id
                        break   Copy the code

conclusion

Python’s Novaclient library and KeystoneAuth1 library have encapsulated the specific request process. Basically, you only need to fill in the key information to directly call the API interface to obtain virtual machine information. The main problem is that there are few OpenStack API interfaces on the web. Basically, you need to read official documents.

I hope you can support me and share with me