问题导读
1、这样通过编程访问OpenStack?
2、如何使用urllib2访问Keystone?
3、如何使用novaclient访问Nova?
1. 使用novaclient访问Nova
- #!/usr/bin/env python
- import logging
- import novaclient
- from novaclient.v1_1 import client
- # enable debug logging
- logger = logging.getLogger('novaclient.client')
- logger.setLevel(logging.DEBUG)
- debug_stream = logging.StreamHandler()
- logger.addHandler(debug_stream)
- auth_url = 'http://182.168.61.21:5000/v2.0'
- user = 'admin'
- password = 'Galax8800'
- project = 'admin'
- region = 'RegionOne'
- service = 'compute'
-
- nova = client.Client(user, password, project, auth_url,
- region_name=region, service_type=service)
- results = nova.images.list(detailed=True)
-
- for image in results:
- print image.id, image.name, image.status
复制代码
输出:
- root@openstack:~# python novatest.py
- d06482b4-beda-4f7e-b63f-deffc6e8072d winxp-mini ACTIVE
- b1efe625-cb81-4754-a46d-6e00c13c9d05 Ubuntu 12.04 cloudimg amd64 ACTIVE
- a0dbf801-e919-4cb5-be79-c8b9dbc2bbf1 cirros-0.3.0-x86_64 ACTIVE
复制代码
2. 使用urllib2访问Keystone
- import urllib2
- import json
-
- def get_keystone_token():
- """authenticate against keystone identity service
- returns an auth token, and the service url
- """
- user = 'admin'
- password = 'Galax8800'
- project = 'admin'
- auth_url = 'http://182.168.61.21:5000/v2.0/tokens'
- auth_request = urllib2.Request(auth_url)
- auth_request.add_header('Content-Type', 'application/json;charset=utf8')
- auth_request.add_header('Accept', 'application/json')
- auth_request.add_header('User-Agent', 'python-mikeyp')
- auth_data = {"auth":
- {
- "tenantName": project,
- "passwordCredentials": {
- "username": user,
- "password": password}
- }
- }
-
- auth_request.add_data(json.dumps(auth_data))
- auth_response = urllib2.urlopen(auth_request)
- response_data = json.loads(auth_response.read())
-
- token = response_data['access']['token']['id']
- service_list = response_data['access']['serviceCatalog']
-
- for s in service_list:
- if s['type'] == 'compute' and s['name'] == "'Compute Service'":
- break
- nova_url = s['endpoints'][0]['publicURL']
-
- return (token, nova_url)
-
- if __name__ == '__main__':
- print get_keystone_token()
复制代码
输出(G版的keystone中,token变长了……):
- root@openstack:~# python keystonetest.py
- (u'MIILBAYJKoZIhvcNAQcCoIIK9TCCCvECAQExCTAHBgUrDgMCGjCCCd0GCSqGSIb3DQEHAaCCCc4EggnKeyJhY2Nlc3MiOiB7InRva2VuIjogeyJpc3N1ZWRfYXQiOiAiMjAxMy0wNC0xOFQwMzowMTozMS4wMjM0NTIiLCAiZXhwaXJlcyI6ICIyMDEzLTA0LTE5VDAzOjAxOjMwWiIsICJpZCI6ICJwbGFjZWhvbGRlciIsICJ0ZW5hbnQiOiB7ImRlc2NyaXB0aW9uIjogbnVsbCwgImVuYWJsZWQiOiB0cnVlLCAiaWQiOiAiMGJmZTZhMDk0ZGIxNGFiNjg1OTE0NmIzYWQwNGFhMGIiLCAibmFtZSI6ICJhZG1pbiJ9fSwgInNlcnZpY2VDYXRhbG9nIjogW3siZW5kcG9pbnRzIjogW3siYWRtaW5VUkwiOiAiaHR0cDovLzE4Mi4xNjguNjEuMjE6ODc3NC92Mi8wYmZlNmEwOTRkYjE0YWI2ODU5MTQ2YjNhZDA0YWEwYiIsICJyZWdpb24iOiAiUmVnaW9uT25lIiwgImludGVybmFsVVJMIjogImh0dHA6Ly8xODIuMTY4LjYxLjIxOjg3NzQvdjIvMGJmZTZhMDk0ZGIxNGFiNjg1OTE0NmIzYWQwNGFhMGIiLCAiaWQiOiAiNzY4OTY2NzgxMTI0NDVlZDljNWM4YTExODRlYzAwMGYiLCAicHVibGljVVJMIjogImh0dHA6Ly8xODIuMTY4LjYxLjUwOjg3NzQvdjIvMGJmZTZhMDk0ZGIxNGFiNjg1OTE0NmIzYWQwNGFhMGIifV0sICJlbmRwb2ludHNfbGlua3MiOiBbXSwgInR5cGUiOiAiY29tcHV0ZSIsICJuYW1lIjogIm5vdmEifSwgeyJlbmRwb2ludHMiOiBbeyJhZG1pblVSTCI6ICJodHRwOi8vMTgyLjE2OC42MS4yMTo5Njk2LyIsICJyZWdpb24iOiAiUmVnaW9uT25lIiwg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iS2V5c3RvbmVBZG1pbiJ9XSwgIm5hbWUiOiAiYWRtaW4ifSwgIm1ldGFkYXRhIjogeyJpc19hZG1pbiI6IDAsICJyb2xlcyI6IFsiMzNhNzAxZTJlMTU1NDFjZWEwMzExMDcwYWQ2ZGIxYmUiLCAiYjE3NWRiZDdiYjZmNGRlZTk0NjgwZmQ2MzdkYmU5ZmUiLCAiNjk1YTVjN2JlMGY4NDlkZThhNDBhM2I1ZTcxMmE2N2UiXX19fTGB-zCB-AIBATBcMFcxCzAJBgNVBAYTAlVTMQ4wDAYDVQQIEwVVbnNldDEOMAwGA1UEBxMFVW5zZXQxDjAMBgNVBAoTBVVuc2V0MRgwFgYDVQQDEw93d3cuZXhhbXBsZS5jb20CAQEwBwYFKw4DAhowDQYJKoZIhvcNAQEBBQAEgYAH+FJcMZzORMRyK3efj8VPa2XgArhBRLdwefmbleT3ntWOSDpaf-rYXd-klAVlqUQVcMzSuTnP17acCuE7KSsyYipyLUZ++t3IwYb2Z+MT0k1E9VxrVTbiJIw53AWQYBnILS29Qe0PAIuyenyT5cXEAUQ73WD2B3McOSJnPH0gjg==', u'http://182.168.61.50:8774/v2/0bfe6a094db14ab6859146b3ad04aa0b')
复制代码
3. 使用urllib2通过Nova调用Glance
这里会用到上面调用Keystone的代码,因为要获取token。
- #!/usr/bin/env python
- import urllib2
- import json
-
- def get_keystone_token():
- """authenticate against keystone identity service
- returns an auth token, and the service url
- """
- user = 'admin'
- password = 'Galax8800'
- project = 'admin'
- auth_url = 'http://182.168.61.21:5000/v2.0/tokens'
- auth_request = urllib2.Request(auth_url)
- auth_request.add_header('Content-Type', 'application/json;charset=utf8')
- auth_request.add_header('Accept', 'application/json')
- auth_request.add_header('User-Agent', 'python-mikeyp')
- auth_data = {"auth":
- {
- "tenantName": project,
- "passwordCredentials": {
- "username": user,
- "password": password}
- }
- }
-
- auth_request.add_data(json.dumps(auth_data))
- auth_response = urllib2.urlopen(auth_request)
- response_data = json.loads(auth_response.read())
-
- token = response_data['access']['token']['id']
- service_list = response_data['access']['serviceCatalog']
-
- for s in service_list:
- if s['type'] == 'compute':
- service_url = s['endpoints'][0]['publicURL']
- break
-
- return (token, service_url)
-
- token, service_url = get_keystone_token()
- image_api = service_url + '/images/detail'
- print image_api
- images_request = urllib2.Request(image_api)
- images_request.add_header('Content-Type', 'application/json;charset=utf8')
- images_request.add_header('Accept', 'application/json')
- images_request.add_header('User-Agent', 'python-mikeyp')
- images_request.add_header('X-Auth-Token', token)
- images_request.add_header('X-Auth-Project-Id', 'admin')
- image_response = urllib2.urlopen(images_request)
- image_data = json.loads(image_response.read())
- print json.dumps(image_data, indent=4)
复制代码
输出:
- root@openstack:~# python glancetest.py
- http://182.168.61.50:8774/v2/0bfe6a094db14ab6859146b3ad04aa0b/images/detail
- {
- "images": [
- {
- "status": "ACTIVE",
- "updated": "2013-04-10T03:33:03Z",
- "name": "winxp-mini",
- "links": [
- {
- "href": "http://182.168.61.50:8774/v2/0bfe6a094db14ab6859146b3ad04aa0b/images/d06482b4-beda-4f7e-b63f-deffc6e8072d",
- "rel": "self"
- },
- {
- "href": "http://182.168.61.50:8774/0bfe6a094db14ab6859146b3ad04aa0b/images/d06482b4-beda-4f7e-b63f-deffc6e8072d",
- "rel": "bookmark"
- },
- {
- "href": "http://182.168.61.21:9292/0bfe6a094db14ab6859146b3ad04aa0b/images/d06482b4-beda-4f7e-b63f-deffc6e8072d",
- "type": "application/vnd.openstack.image",
- "rel": "alternate"
- }
- ],
- "created": "2013-04-10T03:32:48Z",
- "minDisk": 0,
- "progress": 100,
- "minRam": 0,
- "metadata": {},
- "id": "d06482b4-beda-4f7e-b63f-deffc6e8072d",
- "OS-EXT-IMG-SIZE:size": 2147483648
- },
- {
- "status": "ACTIVE",
- "updated": "2013-04-08T13:34:19Z",
- "name": "Ubuntu 12.04 cloudimg amd64",
- "links": [
- {
- "href": "http://182.168.61.50:8774/v2/0bfe6a094db14ab6859146b3ad04aa0b/images/b1efe625-cb81-4754-a46d-6e00c13c9d05",
- "rel": "self"
- },
- {
- "href": "http://182.168.61.50:8774/0bfe6a094db14ab6859146b3ad04aa0b/images/b1efe625-cb81-4754-a46d-6e00c13c9d05",
- "rel": "bookmark"
- },
- {
- "href": "http://182.168.61.21:9292/0bfe6a094db14ab6859146b3ad04aa0b/images/b1efe625-cb81-4754-a46d-6e00c13c9d05",
- "type": "application/vnd.openstack.image",
- "rel": "alternate"
- }
- ],
- "created": "2013-04-08T13:34:17Z",
- "minDisk": 0,
- "progress": 100,
- "minRam": 0,
- "metadata": {},
- "id": "b1efe625-cb81-4754-a46d-6e00c13c9d05",
- "OS-EXT-IMG-SIZE:size": 213123072
- },
- {
- "status": "ACTIVE",
- "updated": "2013-04-08T13:16:49Z",
- "name": "cirros-0.3.0-x86_64",
- "links": [
- {
- "href": "http://182.168.61.50:8774/v2/0bfe6a094db14ab6859146b3ad04aa0b/images/a0dbf801-e919-4cb5-be79-c8b9dbc2bbf1",
- "rel": "self"
- },
- {
- "href": "http://182.168.61.50:8774/0bfe6a094db14ab6859146b3ad04aa0b/images/a0dbf801-e919-4cb5-be79-c8b9dbc2bbf1",
- "rel": "bookmark"
- },
- {
- "href": "http://182.168.61.21:9292/0bfe6a094db14ab6859146b3ad04aa0b/images/a0dbf801-e919-4cb5-be79-c8b9dbc2bbf1",
- "type": "application/vnd.openstack.image",
- "rel": "alternate"
- }
- ],
- "created": "2013-04-08T13:16:48Z",
- "minDisk": 0,
- "progress": 100,
- "minRam": 0,
- "metadata": {},
- "id": "a0dbf801-e919-4cb5-be79-c8b9dbc2bbf1",
- "OS-EXT-IMG-SIZE:size": 9159168
- }
- ]
- }
复制代码
本文转载自:孔令贤
|