Ask Your Question
0

Get Instance Status via Python API

asked 2017-07-03 05:55:33 -0500

FilipGontko gravatar image

Hello guys!

Please, is there a way to get a status of an instance via Python API but not using cmd in source code? I mean original Python API function. I was looking everywhere and I can not find it, but I am not sure wheter I am not searching good enough or it does not exist.

I created this:

def get_node_state(name):
# create a session
nova_client = Client_nova(session=get_session(), version=2)
# creates a list of servers
servers_list = nova_client.servers.list()
server_state = name
server_exists = False
# search the server in the list
for s in servers_list:
    if s.name == server_state:
        server_exists = True
        break
if not server_exists:
    print("server %s does not exist" % server_state)
    status = None
else:
    # runs a command to find status
    status = os.popen("openstack server show -c status --format value %s" % name).read()
status = status.strip("\r\n")
return status

But I need to exchange the os.popen command.

Thank you!

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2017-07-03 09:13:35 -0500

nuriel77 gravatar image

Have a look here: https://docs.openstack.org/developer/python-novaclient/api.html (novaclient Python API bindings)

And check the novaclient v2 library on github where you can inspect how you should construct your commands/queries, for example the list() function which can accept search options.

https://github.com/openstack/python-novaclient/blob/master/novaclient/v2/servers.py#L801 (python-novaclient)

edit flag offensive delete link more
1

answered 2017-07-03 07:03:08 -0500

dbaxps gravatar image

Check https://ask.openstack.org/en/question...
Version of nova client might be higher, syntax a bit different. But core idea would work.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Get to know Ask OpenStack

Resources for moderators

Question Tools

1 follower

Stats

Asked: 2017-07-03 05:55:33 -0500

Seen: 405 times

Last updated: Jul 03 '17