感谢朋友支持本博客,欢迎共同探讨交流,由于能力和时间有限,错误之处在所难免,欢迎指正!
如果转载,请保留作者信息。
博客地址:http://blog.csdn.net/gaoxingnengjisuan
邮箱地址:[email protected]
我们接续上一片博客,来继续解析cinderclient的源代码。
上篇博客中说到在/python-cinderclient/cinderclient/shell.py----mian方法的最后,执行了语句args.func(self.cs, args),实现根据命令行参数的解析调用具体的方法,输出示例为args.func = do_list,说明当执行命令行cinder list时,这里调用的方法为do_list。
具体来看代码,/python-cinderclient/cinderclient/v1/shell.py----do_list(cs, args):
@utils.service_type('volume')
def do_list(cs, args):
"""
List all the volumes.
实现列出所有的卷操作;
"""
all_tenants = int(os.environ.get("ALL_TENANTS", args.all_tenants))
search_opts = {
'all_tenants': all_tenants,
'display_name': args.display_name,
'status': args.status,
'metadata': _extract_metadata(args) if args.metadata else None,
}
volumes = cs.volumes.list(search_opts=search_opts)
_translate_volume_keys(volumes)
# Create a list of servers to which the volume is attached
for vol in volumes:
servers = [s.get('server_id') for s in vol.attachments]
setattr(vol, 'attached_to', ','.join(map(str, servers)))
utils.print_list(volumes, ['ID', 'Status', 'Display Name',
'Size', 'Volume Type', 'Bootable', 'Attached to'])
首先来看语句volumes = cs.volumes.list(search_opts=search_opts),其中cs已经定位了类/python-cinderclient/cinderclient/v1/client.py----class Client(object),在其类的初始化方法中我们可以看到变量self.volumes = volumes.VolumeManager(self),定位到类/python-cinderclient/cinderclient/v1/volumes.py----class VolumeManager(base.ManagerWithFind),从而可以知道,语句volumes = cs.volumes.list(search_opts=search_opts)所实现调用的方法为/python-cinderclient/cinderclient/v1/volumes.py----class VolumeManager(base.ManagerWithFind)----def list(self, detailed=True, search_opts=None),我们来看方法list的具体代码:
def list(self, detailed=True, search_opts=None):
"""
***********************************************
Get a list of all volumes.
获取包含所有卷的列表;
:rtype: list of :class:`Volume`
"""
if search_opts is None:
search_opts = {}
qparams = {}
for opt, val in six.iteritems(search_opts):
if val:
qparams[opt] = val
query_string = "?%s" % urlencode(qparams) if qparams else ""
detail = ""
if detailed:
detail = "/detail"
# /cinderclient/base.py----class Manager(utils.HookableMixin):
# def _list(self, url, response_key, obj_class=None, body=None):
return self._list("/volumes%s%s" % (detail, query_string),
"volumes")
在这个方法中,前面都是进行的一些列参数解析操作,重点来看语句:
return self._list("/volumes%s%s" % (detail, query_string),
"volumes")
这里调用了方法/python-cinderclient/cinderclient/base.py----class Manager(utils.HookableMixin)----def _list(self, url, response_key, obj_class=None, body=None),具体来看代码:
def _list(self, url, response_key, obj_class=None, body=None):
resp = None
if body:
resp, body = self.api.client.post(url, body=body)
else:
resp, body = self.api.client.get(url)
if obj_class is None:
obj_class = self.resource_class
data = body[response_key]
# NOTE(ja): keystone returns values as list as {'values': [ ... ]}
# unlike other services which just return the list...
if isinstance(data, dict):
try:
data = data['values']
except KeyError:
pass
with self.completion_cache('human_id', obj_class, mode="w"):
with self.completion_cache('uuid', obj_class, mode="w"):
return [obj_class(self, res, loaded=True)
for res in data if res]
这里来看代码:
if body:
resp, body = self.api.client.post(url, body=body)
else:
resp, body = self.api.client.get(url)
对于resp, body = self.api.client.post(url, body=body),这里调用了方法/python-cinderclient/cinderclient/client.py----class HTTPClient(object)----def post(self, url, **kwargs),详细代码如下:
def post(self, url, **kwargs):
return self._cs_request(url, 'POST', **kwargs)
对于resp, body = self.api.client.get(url),这里调用了方法/python-cinderclient/cinderclient/client.py----class HTTPClient(object)----def get(self, url, **kwargs):,详细代码如下:
def get(self, url, **kwargs):
return self._cs_request(url, 'GET', **kwargs)
可见,在post方法和get方法中都进一步调用了方法_cs_request,并且对应的传入可参数POST或GET,具体来看方法/python-cinderclient/cinderclient/client.py----class HTTPClient(object)----def _cs_request(self, url, method, **kwargs)的实现:
def _cs_request(self, url, method, **kwargs):
auth_attempts = 0
attempts = 0
backoff = 1
while True:
attempts += 1
if not self.management_url or not self.auth_token:
self.authenticate()
kwargs.setdefault('headers', {})['X-Auth-Token'] = self.auth_token
if self.projectid:
kwargs['headers']['X-Auth-Project-Id'] = self.projectid
try:
resp, body = self.request(self.management_url + url, method, **kwargs)
return resp, body
except exceptions.BadRequest as e:
if attempts > self.retries:
raise
except exceptions.Unauthorized:
if auth_attempts > 0:
raise
self._logger.debug("Unauthorized, reauthenticating.")
self.management_url = self.auth_token = None
# First reauth. Discount this attempt.
attempts -= 1
auth_attempts += 1
continue
except exceptions.ClientException as e:
if attempts > self.retries:
raise
if 500 <= e.code <= 599:
pass
else:
raise
except requests.exceptions.ConnectionError as e:
# Catch a connection refused from requests.request
self._logger.debug("Connection refused: %s" % e)
msg = 'Unable to establish connection: %s' % e
raise exceptions.ConnectionError(msg)
self._logger.debug(
"Failed attempt(%s of %s), retrying in %s seconds" %
(attempts, self.retries, backoff))
sleep(backoff)
backoff *= 2
来看最重要的一条语句:
resp, body = self.request(self.management_url + url, method, **kwargs)
这里调用了方法request,并传入了相关参数,执行相应的操作,并从服务器端获取相应的响应返回值。有输出示例如:
self.management_url + url: http://172.21.5.164:8776/v1/55d34f8573ed4ac19379a0d80afca4bf/volumes/detail
method: GET
kwargs: {'headers': {'X-Auth-Project-Id': 'admin', 'User-Agent': 'python-cinderclient', 'Accept': 'application/json', 'X-Auth-Token': u'MIISwwYJKoZIhvcNAQcCoIIStDCCErA......PQ=='}}
具体来看方法/python-cinderclient/cinderclient/client.py----class HTTPClient(object)----def request(self, url, method, **kwargs):
def request(self, url, method, **kwargs):
kwargs.setdefault('headers', kwargs.get('headers', {}))
kwargs['headers']['User-Agent'] = self.USER_AGENT
kwargs['headers']['Accept'] = 'application/json'
if 'body' in kwargs:
kwargs['headers']['Content-Type'] = 'application/json'
kwargs['data'] = json.dumps(kwargs['body'])
del kwargs['body']
if self.timeout:
kwargs.setdefault('timeout', self.timeout)
self.http_log_req((url, method,), kwargs)
resp = requests.request(
method,
url,
verify=self.verify_cert,
**kwargs)
self.http_log_resp(resp)
if resp.text:
try:
body = json.loads(resp.text)
except ValueError:
pass
body = None
else:
body = None
if resp.status_code >= 400:
raise exceptions.from_response(resp, body)
return resp, body
来看这里最重要的一段代码:
resp = requests.request(
method,
url,
verify=self.verify_cert,
**kwargs)
来看输出示例:
method = GET
url = http://172.21.5.164:8776/v1/55d34f8573ed4ac19379a0d80afca4bf/volumes/detail
verify = True
kwargs = {'headers': {'X-Auth-Project-Id': 'admin', 'User-Agent': 'python-cinderclient', 'Accept': 'application/json', 'X-Auth-Token': u'MIISwwYJKoZIhvcNAQcCoIIStDCCErA......PQ=='}}
这里应用了python中的requests库,具体调用的方法是/requests/api.py----def request(method, url, **kwargs):
def request(method, url, **kwargs):
"""Constructs and sends a :class:`Request <Request>`.
Returns :class:`Response <Response>` object.
:param method: method for the new :class:`Request` object.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
:param files: (optional) Dictionary of 'name': file-like-objects (or {'name': ('filename', fileobj)}) for multipart encoding upload.
:param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
:param timeout: (optional) Float describing the timeout of the request.
:param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) if ``True``, the SSL cert will be verified. A CA_BUNDLE path can also be provided.
:param stream: (optional) if ``False``, the response content will be immediately downloaded.
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
Usage::
>>> import requests
>>> req = requests.request('GET', 'http://httpbin.org/get')
<Response [200]>
"""
session = sessions.Session()
return session.request(method=method, url=url, **kwargs)
这个库遵循HTTP协议,实现了访问远程服务器,并获取相应的响应信息的功能,本文在这里就不深入展开了。
本文是以命令行cinder list为例,所以从服务器端获取相关卷的信息的返回值后,会在方法/python-cinderclient/cinderclient/base.py----class Manager(utils.HookableMixin)----def _list(self, url, response_key, obj_class=None, body=None)中进行解析并进行打印输出,得到卷的列表信息。
至此,卷的查询命令cinder list在cinderclient中执行过程分析完成,后面一片博客我将会简单总结cinderclient中的源码结构。