Fix debug feature and add --debug to swift.

- Remove PYTHON_SWIFTCLIENT and use --debug making it more consistents
  with other openstack clients libraries.
- Fix printing the curl command.
- Don't show the body of the GET objects it could get bad on large
  objects and it's an iterator anyway.
- Use -I for showing HEADs (and not -X HEAD).

Change-Id: I954e7d3d795401fc3679725440dec36cdc80af87
This commit is contained in:
Chmouel Boudjnah 2012-11-16 15:23:25 +10:00
parent e93d47a930
commit ab4282ebb4
2 changed files with 29 additions and 21 deletions
bin
swiftclient

@ -15,6 +15,7 @@
# limitations under the License. # limitations under the License.
import signal import signal
import socket import socket
import logging
from errno import EEXIST, ENOENT from errno import EEXIST, ENOENT
from hashlib import md5 from hashlib import md5
@ -1065,6 +1066,9 @@ Example:
default=False, help='Use SERVICENET internal network') default=False, help='Use SERVICENET internal network')
parser.add_option('-v', '--verbose', action='count', dest='verbose', parser.add_option('-v', '--verbose', action='count', dest='verbose',
default=1, help='Print more info') default=1, help='Print more info')
parser.add_option('--debug', action='store_true', dest='debug',
default=False, help='Show the curl commands of all http '
'queries.')
parser.add_option('-q', '--quiet', action='store_const', dest='verbose', parser.add_option('-q', '--quiet', action='store_const', dest='verbose',
const=0, default=1, help='Suppress status output') const=0, default=1, help='Suppress status output')
parser.add_option('-A', '--auth', dest='auth', parser.add_option('-A', '--auth', dest='auth',
@ -1143,7 +1147,7 @@ Example:
parser.add_option('--os-endpoint-type', parser.add_option('--os-endpoint-type',
metavar='<endpoint-type>', metavar='<endpoint-type>',
default=environ.get('OS_ENDPOINT_TYPE'), default=environ.get('OS_ENDPOINT_TYPE'),
help='Openstack Endpoint type. ' \ help='Openstack Endpoint type. '
'Defaults to env[OS_ENDPOINT_TYPE]') 'Defaults to env[OS_ENDPOINT_TYPE]')
parser.add_option('--os-cacert', parser.add_option('--os-cacert',
metavar='<ca-certificate>', metavar='<ca-certificate>',
@ -1169,6 +1173,10 @@ Example:
signal.signal(signal.SIGINT, attempt_graceful_exit) signal.signal(signal.SIGINT, attempt_graceful_exit)
if options.debug:
logger = logging.getLogger("swiftclient")
logging.basicConfig(level=logging.DEBUG)
print_queue = Queue(10000) print_queue = Queue(10000)
def _print(item): def _print(item):

@ -48,16 +48,14 @@ logger = logging.getLogger("swiftclient")
def http_log(args, kwargs, resp, body): def http_log(args, kwargs, resp, body):
if os.environ.get('SWIFTCLIENT_DEBUG', False): if not logger.isEnabledFor(logging.DEBUG):
ch = logging.StreamHandler()
logger.setLevel(logging.DEBUG)
logger.addHandler(ch)
elif not logger.isEnabledFor(logging.DEBUG):
return return
string_parts = ['curl -i'] string_parts = ['curl -i']
for element in args: for element in args:
if element in ('GET', 'POST', 'PUT', 'HEAD'): if element == 'HEAD':
string_parts.append(' -I')
elif element in ('GET', 'POST', 'PUT'):
string_parts.append(' -X %s' % element) string_parts.append(' -X %s' % element)
else: else:
string_parts.append(' %s' % element) string_parts.append(' %s' % element)
@ -240,6 +238,7 @@ def get_keystoneclient_2_0(auth_url, user, key, os_options, **kwargs):
""" """
insecure = kwargs.get('insecure', False) insecure = kwargs.get('insecure', False)
debug = logger.isEnabledFor(logging.DEBUG) and True or False
from keystoneclient.v2_0 import client as ksclient from keystoneclient.v2_0 import client as ksclient
from keystoneclient import exceptions from keystoneclient import exceptions
@ -248,6 +247,7 @@ def get_keystoneclient_2_0(auth_url, user, key, os_options, **kwargs):
password=key, password=key,
tenant_name=os_options.get('tenant_name'), tenant_name=os_options.get('tenant_name'),
tenant_id=os_options.get('tenant_id'), tenant_id=os_options.get('tenant_id'),
debug=debug,
cacert=kwargs.get('cacert'), cacert=kwargs.get('cacert'),
auth_url=auth_url, insecure=insecure) auth_url=auth_url, insecure=insecure)
except exceptions.Unauthorized: except exceptions.Unauthorized:
@ -364,11 +364,11 @@ def get_account(url, token, marker=None, limit=None, prefix=None,
qs += '&prefix=%s' % quote(prefix) qs += '&prefix=%s' % quote(prefix)
full_path = '%s?%s' % (parsed.path, qs) full_path = '%s?%s' % (parsed.path, qs)
headers = {'X-Auth-Token': token} headers = {'X-Auth-Token': token}
conn.request('GET', full_path, '', method = 'GET'
headers) conn.request(method, full_path, '', headers)
resp = conn.getresponse() resp = conn.getresponse()
body = resp.read() body = resp.read()
http_log(("%s?%s" % (url, qs), 'GET',), {'headers': headers}, resp, body) http_log(("%s?%s" % (url, qs), method,), {'headers': headers}, resp, body)
resp_headers = {} resp_headers = {}
for header, value in resp.getheaders(): for header, value in resp.getheaders():
@ -546,7 +546,7 @@ def head_container(url, token, container, http_conn=None, headers=None):
conn.request(method, path, '', req_headers) conn.request(method, path, '', req_headers)
resp = conn.getresponse() resp = conn.getresponse()
body = resp.read() body = resp.read()
http_log(('%s?%s' % (url, path), method,), http_log(('%s%s' % (url.replace(parsed.path, ''), path), method,),
{'headers': req_headers}, resp, body) {'headers': req_headers}, resp, body)
if resp.status < 200 or resp.status >= 300: if resp.status < 200 or resp.status >= 300:
@ -587,7 +587,7 @@ def put_container(url, token, container, headers=None, http_conn=None):
conn.request(method, path, '', headers) conn.request(method, path, '', headers)
resp = conn.getresponse() resp = conn.getresponse()
body = resp.read() body = resp.read()
http_log(('%s?%s' % (url, path), method,), http_log(('%s%s' % (url.replace(parsed.path, ''), path), method,),
{'headers': headers}, resp, body) {'headers': headers}, resp, body)
if resp.status < 200 or resp.status >= 300: if resp.status < 200 or resp.status >= 300:
raise ClientException('Container PUT failed', raise ClientException('Container PUT failed',
@ -621,7 +621,7 @@ def post_container(url, token, container, headers, http_conn=None):
conn.request(method, path, '', headers) conn.request(method, path, '', headers)
resp = conn.getresponse() resp = conn.getresponse()
body = resp.read() body = resp.read()
http_log(('%s?%s' % (url, path), method,), http_log(('%s%s' % (url.replace(parsed.path, ''), path), method,),
{'headers': headers}, resp, body) {'headers': headers}, resp, body)
if resp.status < 200 or resp.status >= 300: if resp.status < 200 or resp.status >= 300:
raise ClientException('Container POST failed', raise ClientException('Container POST failed',
@ -652,7 +652,7 @@ def delete_container(url, token, container, http_conn=None):
conn.request(method, path, '', headers) conn.request(method, path, '', headers)
resp = conn.getresponse() resp = conn.getresponse()
body = resp.read() body = resp.read()
http_log(('%s?%s' % (url, path), method,), http_log(('%s%s' % (url.replace(parsed.path, ''), path), method,),
{'headers': headers}, resp, body) {'headers': headers}, resp, body)
if resp.status < 200 or resp.status >= 300: if resp.status < 200 or resp.status >= 300:
raise ClientException('Container DELETE failed', raise ClientException('Container DELETE failed',
@ -692,7 +692,7 @@ def get_object(url, token, container, name, http_conn=None,
resp = conn.getresponse() resp = conn.getresponse()
if resp.status < 200 or resp.status >= 300: if resp.status < 200 or resp.status >= 300:
body = resp.read() body = resp.read()
http_log(('%s?%s' % (url, path), 'POST',), http_log(('%s%s' % (url.replace(parsed.path, ''), path), method,),
{'headers': headers}, resp, body) {'headers': headers}, resp, body)
raise ClientException('Object GET failed', http_scheme=parsed.scheme, raise ClientException('Object GET failed', http_scheme=parsed.scheme,
http_host=conn.host, http_port=conn.port, http_host=conn.host, http_port=conn.port,
@ -712,8 +712,8 @@ def get_object(url, token, container, name, http_conn=None,
resp_headers = {} resp_headers = {}
for header, value in resp.getheaders(): for header, value in resp.getheaders():
resp_headers[header.lower()] = value resp_headers[header.lower()] = value
http_log(('%s?%s' % (url, path), 'POST',), http_log(('%s%s' % (url.replace(parsed.path, ''), path), method,),
{'headers': headers}, resp, object_body) {'headers': headers}, resp, None)
return resp_headers, object_body return resp_headers, object_body
@ -741,7 +741,7 @@ def head_object(url, token, container, name, http_conn=None):
conn.request(method, path, '', headers) conn.request(method, path, '', headers)
resp = conn.getresponse() resp = conn.getresponse()
body = resp.read() body = resp.read()
http_log(('%s?%s' % (url, path), 'POST',), http_log(('%s%s' % (url.replace(parsed.path, ''), path), method,),
{'headers': headers}, resp, body) {'headers': headers}, resp, body)
if resp.status < 200 or resp.status >= 300: if resp.status < 200 or resp.status >= 300:
raise ClientException('Object HEAD failed', http_scheme=parsed.scheme, raise ClientException('Object HEAD failed', http_scheme=parsed.scheme,
@ -840,7 +840,7 @@ def put_object(url, token=None, container=None, name=None, contents=None,
resp = conn.getresponse() resp = conn.getresponse()
body = resp.read() body = resp.read()
headers = {'X-Auth-Token': token} headers = {'X-Auth-Token': token}
http_log(('%s?%s' % (url, path), 'PUT',), http_log(('%s%s' % (url.replace(parsed.path, ''), path), 'PUT',),
{'headers': headers}, resp, body) {'headers': headers}, resp, body)
if resp.status < 200 or resp.status >= 300: if resp.status < 200 or resp.status >= 300:
raise ClientException('Object PUT failed', http_scheme=parsed.scheme, raise ClientException('Object PUT failed', http_scheme=parsed.scheme,
@ -873,7 +873,7 @@ def post_object(url, token, container, name, headers, http_conn=None):
conn.request('POST', path, '', headers) conn.request('POST', path, '', headers)
resp = conn.getresponse() resp = conn.getresponse()
body = resp.read() body = resp.read()
http_log(('%s?%s' % (url, path), 'POST',), http_log(('%s%s' % (url.replace(parsed.path, ''), path), 'POST',),
{'headers': headers}, resp, body) {'headers': headers}, resp, body)
if resp.status < 200 or resp.status >= 300: if resp.status < 200 or resp.status >= 300:
raise ClientException('Object POST failed', http_scheme=parsed.scheme, raise ClientException('Object POST failed', http_scheme=parsed.scheme,
@ -919,7 +919,7 @@ def delete_object(url, token=None, container=None, name=None, http_conn=None,
conn.request('DELETE', path, '', headers) conn.request('DELETE', path, '', headers)
resp = conn.getresponse() resp = conn.getresponse()
body = resp.read() body = resp.read()
http_log(('%s?%s' % (url, path), 'POST',), http_log(('%s%s' % (url.replace(parsed.path, ''), path), 'DELETE',),
{'headers': headers}, resp, body) {'headers': headers}, resp, body)
if resp.status < 200 or resp.status >= 300: if resp.status < 200 or resp.status >= 300:
raise ClientException('Object DELETE failed', raise ClientException('Object DELETE failed',