2

I am trying to convert my BASH script to Python and am having difficulties in finding the equivalent code for openstack server show or openstack server list --long. I would like to know what host is my server currently located and use this information for a check before migrating it to another host.

Looking through the latest novaclient documentation and its servers module, I have found two potential commands that I was hoping would accomplish the task, but does not do so:

list(detailed=True)

  • Gets a list servers
  • detailed=True should return detailed server info (optional).
  • This returns a regular list of servers with their names.

get(server)

  • Get a server
  • This returns only the name of the server.

I have been researching for the past two days, and I could not find the same / similar problem here in stack overflow so I have decided to ask and I am hoping that someone can help me with this.

2 Answers2

2

Either list or get should be fine here.

As an example get would be used like this.

instance = nova_client.servers.get('my-server')
print(instance.name)
print(instance.addresses)
print(instance.status)

Or using list.

for instance in nova_client.servers.list():
    print(instance.name)
    print(instance.addresses)
    print(instance.status)

If you want an easy way of understanding the type of data you can get, you can simply use the Python inbuilt dir.

instance = nova_client.servers.get('my-server')
print(dir(instance))
eandersson
  • 25,781
  • 8
  • 89
  • 110
  • Hi eanderson, using dir(), I was able to find the host attribute of the server. I also used the Python inbuilt getattr() to display it's value. Thank you very much! – CheddarChiz Oct 11 '18 at 15:47
0

'my-server' needs to be the id as in instance.id, the name of the server is not valid.
I cant yet comment, so i wrote an answer.

James Baker
  • 107
  • 2
  • 11