Openstack: login as admin and retrieve server data from different tenants via python -
i'm writing cron job in python openstack should read server ids database , servers api using python-novaclient.
in pseudo code things should work this:
session = login_to_keystone(user="admin", password="something") #or use token nova_client = get_nova_client(session) #the servers array holds dictionaries server, user , tenant ids strings # e.g. {"server_id": "1-2-3", "tentant_id": "456", user_id: "11111-2222"} server in servers: server_obj = nova_client.servers.get(server.server_id) ...do stuff server_obj (read data it, delete,...)... what i've come following, it's not right endpointnotfound exception. i'm using devstack juno.
from keystoneclient.v2_0 import client keystone_client keystoneclient import session novaclient import client nova_client #the url admin endpoint keystone = keystone_client.client(token="my-admin-token", auth_url="http://192.168.1.1:35357/v2.0", endpoint="http://192.168.1.1:35357/v2.0") key_session = session.session(auth=keystone) nova = nova_client.client(2, session=key_session) #let's assume servers array populated server in servers: server_obj = nova.servers.get(server.server_id) #exception happens here i need run admin servers can belong tenant , might deleted cron job.
thanks help!
update: information need can use admin tenant retrieving servers (regardless of owner). allows me use publicurl.
my current solution looks this:
from keystoneclient.auth.identity import v2 keystoneclient import session novaclient import client nova_client auth = v2.password(auth_url="http://192.168.1.1:5000/v2.0", username="admin", password="my_secrete", tenant_name="admin") # admin's tenant auth_session = session.session(auth=auth) nova = nova_client.client(2, session=auth_session) server in servers: ... stuff nova.servers.get("some id")
in order list of servers tenants, need perform 2 tasks:
- log in user admin privileges, and
- tell nova api want list of servers tenants.
logging in admin user
it looks you're trying use admin_token defined in keystone.conf authentication. may work, mechanism meant means of bootstrapping keystone. when interacting other services, meant log in using username/password pair has been defined in keystone admin credentials. think @anoop.babu has given work fine:
>>> nova = nova_client.client('2', username, password, project_id, auth_url) where:
username=adminpassword=password_for_admin_userproject_id=adminauth_url=http://your_api_server:5000/v2.0
we can test client out using like:
>>> nova.hypervisors.list() [<hypervisor: 2>] that tells we've authenticated successfully.
listing servers
if called nova.servers.list(), asking list of nova servers owned admin tenant, should empty:
>>> nova.servers.list() [] in order see servers other tenants, need pass all_tenants search option:
>>> nova.servers.list(search_opts={'all_tenants':1}) [<server: cirros0>] and should want be.
Comments
Post a Comment