priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.GANETI_RUNAS,
mkdir=True)
except errors.OpExecError, err:
- logging.exception("Error while processing user ssh files")
- return False
+ msg = "Error while processing user ssh files"
+ logging.exception(msg)
+ return (False, "%s: %s" % (msg, err))
for name, content in [(priv_key, sshkey), (pub_key, sshpub)]:
utils.WriteFile(name, data=content, mode=0600)
utils.RunCmd([constants.SSH_INITD_SCRIPT, "restart"])
- return True
+ return (True, "Node added successfully")
def LeaveCluster():
result[constants.NV_VGLIST] = ListVolumeGroups()
if constants.NV_VERSION in what:
- result[constants.NV_VERSION] = constants.PROTOCOL_VERSION
+ result[constants.NV_VERSION] = (constants.PROTOCOL_VERSION,
+ constants.RELEASE_VERSION)
if constants.NV_HVINFO in what:
hyper = hypervisor.GetHypervisor(what[constants.NV_HVINFO])
return output
-def AddOSToInstance(instance):
+def InstanceOsAdd(instance):
"""Add an OS to an instance.
@type instance: L{objects.Instance}
@return: the success of the operation
"""
- inst_os = OSFromDisk(instance.os)
+ try:
+ inst_os = OSFromDisk(instance.os)
+ except errors.InvalidOS, err:
+ os_name, os_dir, os_err = err.args
+ if os_dir is None:
+ return (False, "Can't find OS '%s': %s" % (os_name, os_err))
+ else:
+ return (False, "Error parsing OS '%s' in directory %s: %s" %
+ (os_name, os_dir, os_err))
create_env = OSEnvironment(instance)
logging.error("os create command '%s' returned error: %s, logfile: %s,"
" output: %s", result.cmd, result.fail_reason, logfile,
result.output)
- lines = [val.encode("string_escape")
+ lines = [utils.SafeEncode(val)
for val in utils.TailFile(logfile, lines=20)]
return (False, "OS create script failed (%s), last lines in the"
" log file:\n%s" % (result.fail_reason, "\n".join(lines)))
if result.failed:
logging.error("os create command '%s' returned error: %s output: %s",
result.cmd, result.fail_reason, result.output)
- lines = [val.encode("string_escape")
+ lines = [utils.SafeEncode(val)
for val in utils.TailFile(logfile, lines=20)]
return (False, "OS rename script failed (%s), last lines in the"
" log file:\n%s" % (result.fail_reason, "\n".join(lines)))
return (True, "Migration successfull")
-def CreateBlockDevice(disk, size, owner, on_primary, info):
+def BlockdevCreate(disk, size, owner, on_primary, info):
"""Creates a block device for an instance.
@type disk: L{objects.Disk}
return True, physical_id
-def RemoveBlockDevice(disk):
+def BlockdevRemove(disk):
"""Remove a block device.
@note: This is intended to be called recursively.
@return: the success of the operation
"""
+ msgs = []
+ result = True
try:
rdev = _RecursiveFindBD(disk)
except errors.BlockDeviceError, err:
rdev = None
if rdev is not None:
r_path = rdev.dev_path
- result = rdev.Remove()
+ try:
+ rdev.Remove()
+ except errors.BlockDeviceError, err:
+ msgs.append(str(err))
+ result = False
if result:
DevCacheManager.RemoveCache(r_path)
- else:
- result = True
+
if disk.children:
for child in disk.children:
- result = result and RemoveBlockDevice(child)
- return result
+ c_status, c_msg = BlockdevRemove(child)
+ result = result and c_status
+ if c_msg: # not an empty message
+ msgs.append(c_msg)
+
+ return (result, "; ".join(msgs))
def _RecursiveAssembleBD(disk, owner, as_primary):
if children.count(None) >= mcn:
raise
cdev = None
- logging.debug("Error in child activation: %s", str(err))
+ logging.error("Error in child activation: %s", str(err))
children.append(cdev)
if as_primary or disk.AssembleOnSecondary():
return result
-def AssembleBlockDevice(disk, owner, as_primary):
+def BlockdevAssemble(disk, owner, as_primary):
"""Activate a block device for an instance.
This is a wrapper over _RecursiveAssembleBD.
C{True} for secondary nodes
"""
- result = _RecursiveAssembleBD(disk, owner, as_primary)
- if isinstance(result, bdev.BlockDev):
- result = result.dev_path
- return result
+ status = False
+ result = "no error information"
+ try:
+ result = _RecursiveAssembleBD(disk, owner, as_primary)
+ if isinstance(result, bdev.BlockDev):
+ result = result.dev_path
+ status = True
+ if result == True:
+ status = True
+ except errors.BlockDeviceError, err:
+ result = "Error while assembling disk: %s" % str(err)
+ return (status, result)
-def ShutdownBlockDevice(disk):
+def BlockdevShutdown(disk):
"""Shut down a block device.
First, if the device is assembled (Attach() is successfull), then
@return: the success of the operation
"""
+ msgs = []
+ result = True
r_dev = _RecursiveFindBD(disk)
if r_dev is not None:
r_path = r_dev.dev_path
- result = r_dev.Shutdown()
- if result:
+ try:
+ r_dev.Shutdown()
DevCacheManager.RemoveCache(r_path)
- else:
- result = True
+ except errors.BlockDeviceError, err:
+ msgs.append(str(err))
+ result = False
+
if disk.children:
for child in disk.children:
- result = result and ShutdownBlockDevice(child)
- return result
+ c_status, c_msg = BlockdevShutdown(child)
+ result = result and c_status
+ if c_msg: # not an empty message
+ msgs.append(c_msg)
+ return (result, "; ".join(msgs))
-def MirrorAddChildren(parent_cdev, new_cdevs):
+
+def BlockdevAddchildren(parent_cdev, new_cdevs):
"""Extend a mirrored block device.
@type parent_cdev: L{objects.Disk}
return True
-def MirrorRemoveChildren(parent_cdev, new_cdevs):
+def BlockdevRemovechildren(parent_cdev, new_cdevs):
"""Shrink a mirrored block device.
@type parent_cdev: L{objects.Disk}
return True
-def GetMirrorStatus(disks):
+def BlockdevGetmirrorstatus(disks):
"""Get the mirroring status of a list of devices.
@type disks: list of L{objects.Disk}
return bdev.FindDevice(disk.dev_type, disk.physical_id, children)
-def FindBlockDevice(disk):
+def BlockdevFind(disk):
"""Check if a device is activated.
If it is, return informations about the real device.
estimated_time, is_degraded)
"""
- rbd = _RecursiveFindBD(disk)
+ try:
+ rbd = _RecursiveFindBD(disk)
+ except errors.BlockDeviceError, err:
+ return (False, str(err))
if rbd is None:
- return rbd
- return (rbd.dev_path, rbd.major, rbd.minor) + rbd.GetSyncStatus()
+ return (True, None)
+ return (True, (rbd.dev_path, rbd.major, rbd.minor) + rbd.GetSyncStatus())
def UploadFile(file_name, data, mode, uid, gid, atime, mtime):
result = {}
result['OS_API_VERSION'] = '%d' % constants.OS_API_VERSION
result['INSTANCE_NAME'] = instance.name
+ result['INSTANCE_OS'] = instance.os
result['HYPERVISOR'] = instance.hypervisor
result['DISK_COUNT'] = '%d' % len(instance.disks)
result['NIC_COUNT'] = '%d' % len(instance.nics)
real_disk.Open()
result['DISK_%d_PATH' % idx] = real_disk.dev_path
# FIXME: When disks will have read-only mode, populate this
- result['DISK_%d_ACCESS' % idx] = 'W'
+ result['DISK_%d_ACCESS' % idx] = disk.mode
if constants.HV_DISK_TYPE in instance.hvparams:
result['DISK_%d_FRONTEND_TYPE' % idx] = \
instance.hvparams[constants.HV_DISK_TYPE]
return result
-def GrowBlockDevice(disk, amount):
+def BlockdevGrow(disk, amount):
"""Grow a stack of block devices.
This function is called recursively, with the childrens being the
return True, None
-def SnapshotBlockDevice(disk):
+def BlockdevSnapshot(disk):
"""Create a snapshot copy of a block device.
This function is called recursively, and the snapshot is actually created
if disk.children:
if len(disk.children) == 1:
# only one child, let's recurse on it
- return SnapshotBlockDevice(disk.children[0])
+ return BlockdevSnapshot(disk.children[0])
else:
# more than one child, choose one that matches
for child in disk.children:
if child.size == disk.size:
# return implies breaking the loop
- return SnapshotBlockDevice(child)
+ return BlockdevSnapshot(child)
elif disk.dev_type == constants.LD_LV:
r_dev = _RecursiveFindBD(disk)
if r_dev is not None:
return True
-def RenameBlockDevices(devlist):
+def BlockdevRename(devlist):
"""Rename a list of block devices.
@type devlist: list of tuples
return True
-def CloseBlockDevices(instance_name, disks):
+def BlockdevClose(instance_name, disks):
"""Closes the given block devices.
This means they will be switched to secondary mode (in case of
if multimaster:
# change to primary mode
for rd in bdevs:
- rd.Open()
+ try:
+ rd.Open()
+ except errors.BlockDeviceError, err:
+ return (False, "Can't change to primary mode: %s" % str(err))
if multimaster:
msg = "multi-master and primary"
else:
#logging.exception("Error while closing fd %s", fd)
pass
- return result == 0, output
+ return result == 0, utils.SafeEncode(output.strip())
def RunHooks(self, hpath, phase, env):
"""Run the scripts in the hooks directory.