plugins/xenserver: Resolve PEP8 issues

Before enabling PEP8 checks for these files, the actual PEP8 issues
need to be resolved. There are a number of "types" of issues that need
to be fixed:

* E112 expected an indented block
* E241 multiple spaces after ','
* E265 block comment should start with '# '
* E302 expected 2 blank lines, found 1
* H102 Apache 2.0 license header not found
* H237 module module is removed in Python 3
* H303 No wildcard (*) import.
* H404 multi line docstring should start without a leading new line
* H231 Python 3.x incompatible 'except x,y:' construct
* F403 from module import *' used; unable to detect undefined names
* F841 local variable 'name' is assigned to but never used
* N344 Use six.iteritems() instead of dict.iteritems().

The fixes for the above are all pretty self-explanatory, but there is
one exception. The 'N344' issues are not ignorable due to how the
"hacking" check is implemented. Rather than ignoring them, skip the
issue altogether by replacing 'iteritems' with 'items'. The difference
in memory usage that this incurs should not be noticable.

Change-Id: Ie26e3daca3b408c9ea43613bcb9996618b9abc69
Partial-Bug: #1302831
This commit is contained in:
Stephen Finucane 2016-03-07 17:46:54 +00:00
parent 3d7e403cc7
commit 79805827bb
11 changed files with 97 additions and 55 deletions

View File

@ -17,6 +17,8 @@
# NOTE: XenServer still only supports Python 2.4 in it's dom0 userspace
# which means the Nova xenapi plugins must use only Python 2.4 features
# TODO(sfinucan): Resolve all 'noqa' items once the above is no longer true
"""Seed a bittorent image. This file should not be executed directly, rather it
should be kicked off by the `bittorent` dom0 plugin."""
@ -34,9 +36,10 @@ logging = pluginlib_nova.logging
def _daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
"""
do the UNIX double-fork magic, see Stevens' "Advanced
Programming in the UNIX Environment" for details (ISBN 0201563177)
"""Daemonize the current process.
Do the UNIX double-fork magic, see Stevens' "Advanced Programming
in the UNIX Environment" for details (ISBN 0201563177).
Source: http://www.jejik.com/articles/2007/02/
a_simple_unix_linux_daemon_in_python/
@ -47,7 +50,7 @@ def _daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
if pid > 0:
# first parent returns
return False
except OSError, e:
except OSError, e: # noqa
logging.error("fork #1 failed: %d (%s)" % (
e.errno, e.strerror))
return
@ -63,7 +66,7 @@ def _daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
if pid > 0:
# second parent exits
sys.exit(0)
except OSError, e:
except OSError, e: # noqa
logging.error("fork #2 failed: %d (%s)" % (
e.errno, e.strerror))
return

View File

@ -21,12 +21,14 @@
# NOTE: XenServer still only supports Python 2.4 in it's dom0 userspace
# which means the Nova xenapi plugins must use only Python 2.4 features
# TODO(sfinucan): Resolve all 'noqa' items once the above is no longer true
#
# XenAPI plugin for reading/writing information to xenstore
#
import base64
import commands
import commands # noqa
try:
import json
except ImportError:
@ -62,7 +64,7 @@ def version(self, arg_dict):
xenstore.write_record(self, arg_dict)
try:
resp = _wait_for_agent(self, request_id, arg_dict, timeout)
except TimeoutError, e:
except TimeoutError, e: # noqa
raise PluginError(e)
return resp
@ -83,7 +85,7 @@ def key_init(self, arg_dict):
xenstore.write_record(self, arg_dict)
try:
resp = _wait_for_agent(self, request_id, arg_dict, timeout)
except TimeoutError, e:
except TimeoutError, e: # noqa
raise PluginError(e)
return resp
@ -103,7 +105,7 @@ def password(self, arg_dict):
xenstore.write_record(self, arg_dict)
try:
resp = _wait_for_agent(self, request_id, arg_dict, timeout)
except TimeoutError, e:
except TimeoutError, e: # noqa
raise PluginError(e)
return resp
@ -119,7 +121,7 @@ def resetnetwork(self, arg_dict):
xenstore.write_record(self, arg_dict)
try:
resp = _wait_for_agent(self, request_id, arg_dict, timeout)
except TimeoutError, e:
except TimeoutError, e: # noqa
raise PluginError(e)
return resp
@ -163,7 +165,7 @@ def inject_file(self, arg_dict):
xenstore.write_record(self, arg_dict)
try:
resp = _wait_for_agent(self, request_id, arg_dict, timeout)
except TimeoutError, e:
except TimeoutError, e: # noqa
raise PluginError(e)
return resp
@ -182,7 +184,7 @@ def agent_update(self, arg_dict):
xenstore.write_record(self, arg_dict)
try:
resp = _wait_for_agent(self, request_id, arg_dict, timeout)
except TimeoutError, e:
except TimeoutError, e: # noqa
raise PluginError(e)
return resp
@ -198,7 +200,7 @@ def _get_agent_features(self, arg_dict):
xenstore.write_record(self, dct)
try:
resp = _wait_for_agent(self, tmp_id, dct, timeout)
except TimeoutError, e:
except TimeoutError, e: # noqa
raise PluginError(e)
response = json.loads(resp)
if response['returncode'] != 0:

View File

@ -30,7 +30,7 @@ pluginlib_nova.configure_logging('bandwidth')
def _read_proc_net():
devs = [l.strip() for l in open('/proc/net/dev', 'r').readlines()]
#ignore headers
# Ignore headers
devs = devs[2:]
dlist = [d.split(':', 1) for d in devs if d.startswith('vif')]
devmap = dict()

View File

@ -17,6 +17,8 @@
# NOTE: XenServer still only supports Python 2.4 in it's dom0 userspace
# which means the Nova xenapi plugins must use only Python 2.4 features
# TODO(sfinucan): Resolve all 'noqa' items once the above is no longer true
"""Download images via BitTorrent."""
import errno
@ -242,7 +244,7 @@ def _active_seeder_processes():
for pid in pids:
try:
cmdline = open(os.path.join('/proc', pid, 'cmdline'), 'rb').read()
except IOError, e:
except IOError, e: # noqa
if e.errno != errno.ENOENT:
raise

View File

@ -1,5 +1,17 @@
#!/usr/bin/env python
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# NOTE: XenServer still only supports Python 2.4 in it's dom0 userspace
# which means the Nova xenapi plugins must use only Python 2.4 features

View File

@ -1,4 +1,5 @@
#!/usr/bin/python
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
@ -14,6 +15,8 @@
# NOTE: XenServer still only supports Python 2.4 in it's dom0 userspace
# which means the Nova xenapi plugins must use only Python 2.4 features
# TODO(sfinucan): Resolve all 'noqa' items once the above is no longer true
"""
To configure this plugin, you must set the following xenstore key:
/local/logconsole/@ = "/var/log/xen/guest/console.%d"
@ -50,7 +53,7 @@ SEEK_END = 2
def _last_bytes(file_like_object):
try:
file_like_object.seek(-MAX_CONSOLE_BYTES, SEEK_END)
except IOError, e:
except IOError, e: # noqa
if e.errno == 22:
file_like_object.seek(0, SEEK_SET)
else:
@ -72,7 +75,7 @@ def get_console_log(session, arg_dict):
try:
try:
log_content = _last_bytes(logfile)
except IOError, e:
except IOError, e: # noqa
msg = "Error reading console: %s" % e
logging.debug(msg)
raise pluginlib_nova.PluginError(msg)

View File

@ -21,6 +21,8 @@
# NOTE: XenServer still only supports Python 2.4 in it's dom0 userspace
# which means the Nova xenapi plugins must use only Python 2.4 features
# TODO(sfinucan): Resolve all 'noqa' items once the above is no longer true
"""Handle the uploading and downloading of images via Glance."""
try:
@ -28,7 +30,7 @@ try:
except ImportError:
from six.moves import http_client as httplib
import md5
import md5 # noqa
import socket
import urllib2
from urlparse import urlparse
@ -58,11 +60,11 @@ def _download_tarball_and_verify(request, staging_path):
try:
response = urllib2.urlopen(request)
except urllib2.HTTPError, error:
except urllib2.HTTPError, error: # noqa
raise RetryableError(error)
except urllib2.URLError, error:
except urllib2.URLError, error: # noqa
raise RetryableError(error)
except httplib.HTTPException, error:
except httplib.HTTPException, error: # noqa
# httplib.HTTPException and derivatives (BadStatusLine in particular)
# don't have a useful __repr__ or __str__
raise RetryableError('%s: %s' % (error.__class__.__name__, error))
@ -80,7 +82,7 @@ def _download_tarball_and_verify(request, staging_path):
try:
try:
utils.extract_tarball(response, staging_path, callback=update_md5)
except Exception, error:
except Exception, error: # noqa
raise RetryableError(error)
finally:
bytes_read = callback_data['bytes_read']
@ -151,9 +153,11 @@ def _upload_tarball(staging_path, image_id, glance_host, glance_port,
_upload_tarball_by_url(staging_path, image_id, url,
extra_headers, properties)
def _upload_tarball_by_url(staging_path, image_id, glance_endpoint,
extra_headers, properties):
"""
"""Upload an image to Glance.
Create a tarball of the image and then stream that into Glance
using chunked-transfer-encoded HTTP.
"""
@ -182,7 +186,7 @@ def _upload_tarball_by_url(staging_path, image_id, glance_endpoint,
else:
conn = httplib.HTTPConnection(parts[1])
conn.connect()
except Exception, error:
except Exception, error: # noqa
logging.exception('Failed to connect %(url)s' % {'url': url})
raise RetryableError(error)
@ -217,14 +221,14 @@ def _upload_tarball_by_url(staging_path, image_id, glance_endpoint,
headers.update(**extra_headers)
for key, value in properties.iteritems():
for key, value in properties.items():
header_key = "x-image-meta-property-%s" % key.replace('_', '-')
headers[header_key] = str(value)
for header, value in headers.iteritems():
for header, value in headers.items():
conn.putheader(header, value)
conn.endheaders()
except Exception, error:
except Exception, error: # noqa
logging.exception('Failed to upload %(url)s' % {'url': url})
raise RetryableError(error)
@ -235,7 +239,7 @@ def _upload_tarball_by_url(staging_path, image_id, glance_endpoint,
callback_data['bytes_written'] += chunk_len
try:
conn.send("%x\r\n%s\r\n" % (chunk_len, chunk))
except Exception, error:
except Exception, error: # noqa
logging.exception('Failed to upload when sending chunks')
raise RetryableError(error)
@ -343,7 +347,7 @@ def validate_image_status_before_upload(conn, url, extra_headers):
else:
head_resp.read()
except Exception, error:
except Exception, error: # noqa
logging.exception('Failed to HEAD the image %(image_id)s while '
'checking image status before attempting to '
'upload %(url)s' % {'image_id': image_id,

View File

@ -17,16 +17,19 @@
# NOTE: XenServer still only supports Python 2.4 in it's dom0 userspace
# which means the Nova xenapi plugins must use only Python 2.4 features
# TODO(sfinucan): Resolve all 'noqa' items once the above is no longer true
"""Inject network configuration into iPXE ISO for boot."""
import logging
import os
import shutil
import utils
#FIXME(sirp): should this use pluginlib from 5.6?
from pluginlib_nova import *
configure_logging('ipxe')
# FIXME(sirp): should this use pluginlib from 5.6?
import pluginlib_nova
pluginlib_nova.configure_logging('ipxe')
ISOLINUX_CFG = """SAY iPXE ISO boot image
@ -96,7 +99,7 @@ def _create_iso(mkisofs_cmd, filename, path):
orig_dir = os.getcwd()
os.chdir(path)
try:
utils.run_command([mkisofs_cmd, '-quiet', '-l', '-o', filename,
utils.run_command([mkisofs_cmd, '-quiet', '-l', '-o', filename,
'-c', 'boot.cat', '-b', 'isolinux.bin',
'-no-emul-boot', '-boot-load-size', '4',
'-boot-info-table', '.'])

View File

@ -21,6 +21,8 @@
# NOTE: XenServer still only supports Python 2.4 in it's dom0 userspace
# which means the Nova xenapi plugins must use only Python 2.4 features
# TODO(sfinucan): Resolve all 'noqa' items once the above is no longer true
"""Handle the manipulation of kernel images."""
import errno
@ -119,7 +121,7 @@ def create_kernel_ramdisk(session, args):
def _remove_file(filepath):
try:
os.remove(filepath)
except OSError, exc:
except OSError, exc: # noqa
if exc.errno != errno.ENOENT:
raise

View File

@ -31,9 +31,9 @@ import utils
# 1.3 - Add vhd2 functions for doing glance operations by url
PLUGIN_VERSION = "1.3"
def get_version(session):
return PLUGIN_VERSION
if __name__ == '__main__':
utils.register_plugin_calls(get_version)

View File

@ -20,6 +20,8 @@
# NOTE: XenServer still only supports Python 2.4 in it's dom0 userspace
# which means the Nova xenapi plugins must use only Python 2.4 features
# TODO(sfinucan): Resolve all 'noqa' items once the above is no longer true
#
# XenAPI plugin for host operations
#
@ -70,24 +72,27 @@ def _run_command(cmd, cmd_input=None):
"""
try:
return utils.run_command(cmd, cmd_input=cmd_input)
except utils.SubprocessException, e:
except utils.SubprocessException, e: # noqa
raise pluginlib.PluginError(e.err)
def _resume_compute(session, compute_ref, compute_uuid):
"""Resume compute node on slave host after pool join. This has to
happen regardless of the success or failure of the join operation."""
"""Resume compute node on slave host after pool join.
This has to happen regardless of the success or failure of the join
operation.
"""
try:
# session is valid if the join operation has failed
session.xenapi.VM.start(compute_ref, False, True)
except XenAPI.Failure, e:
except XenAPI.Failure:
# if session is invalid, e.g. xapi has restarted, then the pool
# join has been successful, wait for xapi to become alive again
for c in range(0, DEFAULT_TRIES):
try:
_run_command(["xe", "vm-start", "uuid=%s" % compute_uuid])
return
except pluginlib.PluginError, e:
except pluginlib.PluginError:
logging.exception('Waited %d seconds for the slave to '
'become available.' % (c * DEFAULT_SLEEP))
time.sleep(DEFAULT_SLEEP)
@ -156,7 +161,7 @@ def get_config(self, arg_dict):
params = arg_dict["params"]
try:
dct = json.loads(params)
except Exception, e:
except Exception:
dct = params
key = dct["key"]
ret = conf.get(key)
@ -173,7 +178,7 @@ def set_config(self, arg_dict):
params = arg_dict["params"]
try:
dct = json.loads(params)
except Exception, e:
except Exception:
dct = params
key = dct["key"]
val = dct["value"]
@ -204,12 +209,11 @@ def iptables_config(session, args):
'ip6tables-save',
'ip6tables-restore'):
result = _run_command(cmd, process_input)
ret_str = json.dumps(dict(out=result,
err=''))
ret_str = json.dumps(dict(out=result, err=''))
logging.debug("iptables_config:exit")
return ret_str
else:
# else don't do anything and return an error
else:
raise pluginlib.PluginError(_("Invalid iptables command"))
@ -224,9 +228,9 @@ def _power_action(action, arg_dict):
"resident-on=%s" % host_uuid])
if result:
raise pluginlib.PluginError(result)
cmds = {"reboot": "host-reboot",
"startup": "host-power-on",
"shutdown": "host-shutdown",}
cmds = {"reboot": "host-reboot",
"startup": "host-power-on",
"shutdown": "host-shutdown"}
result = _run_command(["xe", cmds[action], "uuid=%s" % host_uuid])
# Should be empty string
if result:
@ -248,22 +252,27 @@ def host_shutdown(self, arg_dict):
@jsonify
def host_start(self, arg_dict):
"""Starts the host. Currently not feasible, since the host
runs on the same machine as Xen.
"""Starts the host.
Currently not feasible, since the host runs on the same machine as
Xen.
"""
return _power_action("startup", arg_dict)
@jsonify
def host_join(self, arg_dict):
"""Join a remote host into a pool whose master is the host
where the plugin is called from. The following constraints apply:
"""Join a remote host into a pool.
- The host must have no VMs running, except nova-compute, which will be
shut down (and restarted upon pool-join) automatically,
The pool's master is the host where the plugin is called from. The
following constraints apply:
- The host must have no VMs running, except nova-compute, which
will be shut down (and restarted upon pool-join) automatically,
- The host must have no shared storage currently set up,
- The host must have the same license of the master,
- The host must have the same supplemental packs as the master."""
- The host must have the same supplemental packs as the master.
"""
session = XenAPI.Session(arg_dict.get("url"))
session.login_with_password(arg_dict.get("user"),
arg_dict.get("password"))
@ -404,12 +413,14 @@ def cleanup(dct):
# "external-auth-service-name", "")
return out
def query_gc(session, sr_uuid, vdi_uuid):
result = _run_command(["/opt/xensource/sm/cleanup.py",
"-q", "-u", sr_uuid])
# Example output: "Currently running: True"
return result[19:].strip() == "True"
def get_pci_device_details(session):
"""Returns a string that is a list of pci devices with details.