Commit 3ecf6786 authored by Iustin Pop's avatar Iustin Pop
Browse files

Style changes for pep-8 and python-3000 compliance.

This changes the raising of exceptions from:
  raise Exception, value
  raise Exception(value)

as the first form will be removed in python-3000 and the second form is
preferred now.

The changes also involve a few cases of changing from raising standard
exceptions and use our own ones.

The new version also fixes many pylint-generated warnings, especially in
ganeti-noded where I changed many methods to @staticmethod.

There is no functionality changed (barring any bugs).
parent 5c947f38
......@@ -39,7 +39,6 @@ import socket
from optparse import OptionParser
from ganeti import logger
from ganeti import constants
from ganeti import errors
from ganeti import ssconf
......@@ -111,7 +110,7 @@ def StartMaster(master_netdev, master_ip, debug):
result = utils.RunCmd(["fping", "-q", master_ip])
if not result.failed:
r2 = utils.RunCmd(["fping", "-q", "-S127.0.0.1", master_ip])
if not result.failed:
if not r2.failed:
# we already have the ip:
if debug:
sys.stderr.write("Notice: already started.\n")
......@@ -168,9 +167,9 @@ def main():
fn = StopMaster
return fn(master_netdev, master_ip, debug)
result = fn(master_netdev, master_ip, debug)
if __name__=='__main__':
exit_code = main()
if __name__ == '__main__':
......@@ -21,6 +21,9 @@
"""Ganeti node daemon"""
# functions in this module need to have a given name structure, so:
# pylint: disable-msg=C0103
import os
import sys
import resource
......@@ -43,26 +46,35 @@ from OpenSSL import SSL
class ServerContextFactory:
def getContext(self):
"""SSL context factory class that uses a given certificate.
def getContext():
"""Return a customized context.
The context will be set to use our certificate.
ctx = SSL.Context(SSL.TLSv1_METHOD)
return ctx
class ServerObject(pb.Avatar):
"""The server implementation.
This class holds all methods exposed over the RPC interface.
def __init__(self, name): = name
def perspectiveMessageReceived(self, broker, message, args, kw):
"""This method is called when a network message is received.
I will call::
"""Custom message dispatching function.
| self.perspective_%(message)s(*broker.unserialize(args),
| **broker.unserialize(kw))
to handle the method; subclasses of Avatar are expected to
implement methods of this naming convention.
This function overrides the pb.Avatar function in order to provide
a simple form of exception passing (as text only).
args = broker.unserialize(args, self)
......@@ -79,33 +91,56 @@ class ServerObject(pb.Avatar):
# the new block devices --------------------------
def perspective_blockdev_create(self, params):
def perspective_blockdev_create(params):
"""Create a block device.
bdev_s, size, on_primary, info = params
bdev = objects.ConfigObject.Loads(bdev_s)
if bdev is None:
raise ValueError("can't unserialize data!")
return backend.CreateBlockDevice(bdev, size, on_primary, info)
def perspective_blockdev_remove(self, params):
def perspective_blockdev_remove(params):
"""Remove a block device.
bdev_s = params[0]
bdev = objects.ConfigObject.Loads(bdev_s)
return backend.RemoveBlockDevice(bdev)
def perspective_blockdev_assemble(self, params):
def perspective_blockdev_assemble(params):
"""Assemble a block device.
bdev_s, on_primary = params
bdev = objects.ConfigObject.Loads(bdev_s)
if bdev is None:
raise ValueError("can't unserialize data!")
return backend.AssembleBlockDevice(bdev, on_primary)
def perspective_blockdev_shutdown(self, params):
def perspective_blockdev_shutdown(params):
"""Shutdown a block device.
bdev_s = params[0]
bdev = objects.ConfigObject.Loads(bdev_s)
if bdev is None:
raise ValueError("can't unserialize data!")
return backend.ShutdownBlockDevice(bdev)
def perspective_blockdev_addchild(self, params):
def perspective_blockdev_addchild(params):
"""Add a child to a mirror device.
Note: this is only valid for mirror devices. It's the caller's duty
to send a correct disk, otherwise we raise an error.
bdev_s, ndev_s = params
bdev = objects.ConfigObject.Loads(bdev_s)
ndev = objects.ConfigObject.Loads(ndev_s)
......@@ -113,7 +148,14 @@ class ServerObject(pb.Avatar):
raise ValueError("can't unserialize data!")
return backend.MirrorAddChild(bdev, ndev)
def perspective_blockdev_removechild(self, params):
def perspective_blockdev_removechild(params):
"""Remove a child from a mirror device.
This is only valid for mirror devices, of course. It's the callers
duty to send a correct disk, otherwise we raise an error.
bdev_s, ndev_s = params
bdev = objects.ConfigObject.Loads(bdev_s)
ndev = objects.ConfigObject.Loads(ndev_s)
......@@ -121,130 +163,258 @@ class ServerObject(pb.Avatar):
raise ValueError("can't unserialize data!")
return backend.MirrorRemoveChild(bdev, ndev)
def perspective_blockdev_getmirrorstatus(self, params):
def perspective_blockdev_getmirrorstatus(params):
"""Return the mirror status for a list of disks.
disks = [objects.ConfigObject.Loads(dsk_s)
for dsk_s in params]
return backend.GetMirrorStatus(disks)
def perspective_blockdev_find(self, params):
def perspective_blockdev_find(params):
"""Expose the FindBlockDevice functionality for a disk.
This will try to find but not activate a disk.
disk = objects.ConfigObject.Loads(params[0])
return backend.FindBlockDevice(disk)
def perspective_blockdev_snapshot(self, params):
def perspective_blockdev_snapshot(params):
"""Create a snapshot device.
Note that this is only valid for LVM disks, if we get passed
something else we raise an exception. The snapshot device can be
remove by calling the generic block device remove call.
cfbd = objects.ConfigObject.Loads(params[0])
return backend.SnapshotBlockDevice(cfbd)
# export/import --------------------------
def perspective_snapshot_export(self, params):
def perspective_snapshot_export(params):
"""Export a given snapshot.
disk = objects.ConfigObject.Loads(params[0])
dest_node = params[1]
instance = objects.ConfigObject.Loads(params[2])
return backend.ExportSnapshot(disk,dest_node,instance)
return backend.ExportSnapshot(disk, dest_node, instance)
def perspective_finalize_export(params):
"""Expose the finalize export functionality.
def perspective_finalize_export(self, params):
instance = objects.ConfigObject.Loads(params[0])
snap_disks = [objects.ConfigObject.Loads(str_data)
for str_data in params[1]]
return backend.FinalizeExport(instance, snap_disks)
def perspective_export_info(self, params):
dir = params[0]
einfo = backend.ExportInfo(dir)
def perspective_export_info(params):
"""Query information about an existing export on this node.
The given path may not contain an export, in which case we return
path = params[0]
einfo = backend.ExportInfo(path)
if einfo is None:
return einfo
return einfo.Dumps()
def perspective_export_list(self, params):
def perspective_export_list(params):
"""List the available exports on this node.
Note that as opposed to export_info, which may query data about an
export in any path, this only queries the standard Ganeti path
return backend.ListExports()
def perspective_export_remove(self, params):
def perspective_export_remove(params):
"""Remove an export.
export = params[0]
return backend.RemoveExport(export)
# volume --------------------------
def perspective_volume_list(self, params):
def perspective_volume_list(params):
"""Query the list of logical volumes in a given volume group.
vgname = params[0]
return backend.GetVolumeList(vgname)
def perspective_vg_list(self, params):
def perspective_vg_list(params):
"""Query the list of volume groups.
return backend.ListVolumeGroups()
# bridge --------------------------
def perspective_bridges_exist(self, params):
def perspective_bridges_exist(params):
"""Check if all bridges given exist on this node.
bridges_list = params[0]
return backend.BridgesExist(bridges_list)
# instance --------------------------
def perspective_instance_os_add(self, params):
def perspective_instance_os_add(params):
"""Install an OS on a given instance.
inst_s, os_disk, swap_disk = params
inst = objects.ConfigObject.Loads(inst_s)
return backend.AddOSToInstance(inst, os_disk, swap_disk)
def perspective_instance_os_import(self, params):
def perspective_instance_os_import(params):
"""Run the import function of an OS onto a given instance.
inst_s, os_disk, swap_disk, src_node, src_image = params
inst = objects.ConfigObject.Loads(inst_s)
return backend.ImportOSIntoInstance(inst, os_disk, swap_disk,
src_node, src_image)
def perspective_instance_shutdown(self, params):
def perspective_instance_shutdown(params):
"""Shutdown an instance.
instance = objects.ConfigObject.Loads(params[0])
return backend.ShutdownInstance(instance)
def perspective_instance_start(self, params):
def perspective_instance_start(params):
"""Start an instance.
instance = objects.ConfigObject.Loads(params[0])
extra_args = params[1]
return backend.StartInstance(instance, extra_args)
def perspective_instance_info(self, params):
def perspective_instance_info(params):
"""Query instance information.
return backend.GetInstanceInfo(params[0])
def perspective_all_instances_info(self, params):
def perspective_all_instances_info(params):
"""Query information about all instances.
return backend.GetAllInstancesInfo()
def perspective_instance_list(self, params):
def perspective_instance_list(params):
"""Query the list of running instances.
return backend.GetInstanceList()
# node --------------------------
def perspective_node_info(self, params):
def perspective_node_info(params):
"""Query node information.
vgname = params[0]
return backend.GetNodeInfo(vgname)
def perspective_node_add(self, params):
def perspective_node_add(params):
"""Complete the registration of this node in the cluster.
return backend.AddNode(params[0], params[1], params[2],
params[3], params[4], params[5])
def perspective_node_verify(self, params):
def perspective_node_verify(params):
"""Run a verify sequence on this node.
return backend.VerifyNode(params[0])
def perspective_node_start_master(self, params):
def perspective_node_start_master(params):
"""Promote this node to master status.
return backend.StartMaster()
def perspective_node_stop_master(self, params):
def perspective_node_stop_master(params):
"""Demote this node from master status.
return backend.StopMaster()
def perspective_node_leave_cluster(self, params):
def perspective_node_leave_cluster(params):
"""Cleanup after leaving a cluster.
return backend.LeaveCluster()
def perspective_node_volumes(self, params):
def perspective_node_volumes(params):
"""Query the list of all logical volume groups.
return backend.NodeVolumes()
# cluster --------------------------
def perspective_version(self, params):
def perspective_version(params):
"""Query version information.
return constants.PROTOCOL_VERSION
def perspective_upload_file(self, params):
def perspective_upload_file(params):
"""Upload a file.
Note that the backend implementation imposes strict rules on which
files are accepted.
return backend.UploadFile(*params)
# os -----------------------
def perspective_os_diagnose(self, params):
def perspective_os_diagnose(params):
"""Query detailed information about existing OSes.
os_list = backend.DiagnoseOS()
if not os_list:
# this catches also return values of 'False',
......@@ -257,31 +427,46 @@ class ServerObject(pb.Avatar):
elif isinstance(data, errors.InvalidOS):
raise errors.ProgrammerError, ("Invalid result from backend.DiagnoseOS"
" (class %s, %s)" %
(str(data.__class__), data))
raise errors.ProgrammerError("Invalid result from backend.DiagnoseOS"
" (class %s, %s)" %
(str(data.__class__), data))
return result
def perspective_os_get(self, params):
def perspective_os_get(params):
"""Query information about a given OS.
name = params[0]
os = backend.OSFromDisk(name).Dumps()
os_obj = backend.OSFromDisk(name).Dumps()
except errors.InvalidOS, err:
os = err.args
return os
os_obj = err.args
return os_obj
# hooks -----------------------
def perspective_hooks_runner(self, params):
def perspective_hooks_runner(params):
"""Run hook scripts.
hpath, phase, env = params
hr = backend.HooksRunner()
return hr.RunHooks(hpath, phase, env)
class MyRealm:
"""Simple realm that forwards all requests to a ServerObject.
__implements__ = portal.IRealm
def requestAvatar(self, avatarId, mind, *interfaces):
"""Return an avatar based on our ServerObject class.
if pb.IPerspective not in interfaces:
raise NotImplementedError
return pb.IPerspective, ServerObject(avatarId), lambda:None
......@@ -310,6 +495,9 @@ def ParseOptions():
def main():
"""Main function for the node daemon.
options, args = ParseOptions()
for fname in (constants.SSL_CERT_FILE,):
if not os.path.isfile(fname):
......@@ -361,13 +549,13 @@ def createDaemon():
pid = os.fork()
except OSError, e:
raise Exception, "%s [%d]" % (e.strerror, e.errno)
raise Exception("%s [%d]" % (e.strerror, e.errno))
if (pid == 0): # The first child.
pid = os.fork() # Fork a second child.
except OSError, e:
raise Exception, "%s [%d]" % (e.strerror, e.errno)
raise Exception("%s [%d]" % (e.strerror, e.errno))
if (pid == 0): # The second child.
......@@ -393,5 +581,5 @@ def createDaemon():
if __name__=='__main__':
if __name__ == '__main__':
......@@ -117,7 +117,7 @@ class RestarterState(object):
fcntl.flock(f.fileno(), fcntl.LOCK_EX|fcntl.LOCK_NB)
except IOError, x:
if x.errno == errno.EAGAIN:
raise StandardError('State file already locked')
raise StandardError("State file already locked")
self.statefile = f
......@@ -202,6 +202,12 @@ class Instance(object):
self.state = state
def Restart(self):
"""Encapsulates the start of an instance.
This is currently done using the command line interface and not
the Ganeti modules.
DoCmd(['gnt-instance', 'startup', '--lock-retries=15',])
......@@ -258,7 +264,7 @@ class Restarter(object):
sstore = ssconf.SimpleStore()
master = sstore.GetMasterNode()
if master != socket.gethostname():
raise NotMasterError, ("This is not the master node")
raise NotMasterError("This is not the master node")
self.instances = InstanceList()
self.messages = []
......@@ -346,7 +346,7 @@ def GetAllInstancesInfo():
iinfo = hypervisor.GetHypervisor().GetAllInstancesInfo()
if iinfo:
for name, id, memory, vcpus, state, times in iinfo:
for name, inst_id, memory, vcpus, state, times in iinfo:
output[name] = {
'memory': memory,
'vcpus': vcpus,
......@@ -729,7 +729,7 @@ def GetMirrorStatus(disks):
for dsk in disks:
rbd = _RecursiveFindBD(dsk)
if rbd is None:
raise errors.BlockDeviceError, "Can't find device %s" % str(dsk)
raise errors.BlockDeviceError("Can't find device %s" % str(dsk))
return stats
......@@ -847,12 +847,12 @@ def _OSOndiskVersion(name, os_dir=None):
st = os.stat(api_file)
except EnvironmentError, err:
raise errors.InvalidOS, (name, "'ganeti_api_version' file not"
" found (%s)" % _ErrnoOrStr(err))
raise errors.InvalidOS(name, "'ganeti_api_version' file not"
" found (%s)" % _ErrnoOrStr(err))
if not stat.S_ISREG(stat.S_IFMT(st.st_mode)):
raise errors.InvalidOS, (name, "'ganeti_api_version' file is not"
" a regular file")
raise errors.InvalidOS(name, "'ganeti_api_version' file is not"
" a regular file")
f = open(api_file)
......@@ -861,15 +861,14 @@ def _OSOndiskVersion(name, os_dir=None):
except EnvironmentError, err:
raise errors.InvalidOS, (name, "error while reading the"
" API version (%s)" % _ErrnoOrStr(err))
raise errors.InvalidOS(name, "error while reading the"
" API version (%s)" % _ErrnoOrStr(err))
api_version = api_version.strip()
api_version = int(api_version)
except (TypeError, ValueError), err:
raise errors.InvalidOS, (name, "API version is not integer (%s)" %
raise errors.InvalidOS(name, "API version is not integer (%s)" % str(err))
return api_version
......@@ -920,8 +919,8 @@ def OSFromDisk(name, os_dir=None):
api_version = _OSOndiskVersion(name, os_dir)
if api_version != constants.OS_API_VERSION:
raise errors.InvalidOS, (name, "API version mismatch (found %s want %s)"
% (api_version, constants.OS_API_VERSION))
raise errors.InvalidOS(name, "API version mismatch (found %s want %s)"
% (api_version, constants.OS_API_VERSION))