def remove_file(filename): """ removes a given file, when the file exists """ if os.path.exists(filename): log_verbose(options, "Remove %s" % filename) if os.path.isdir(filename): shutil.rmtree(filename) else: os.remove(filename)
def register_targets(monitor, options, config): """ Register SCST targets """ for (tid, t) in target.read_all_targets(monitor).items(): try: iscsi_scst.register_target(t) if len(t.volumes()) > 0: scst.add_group("Default_" + t.name()) log_verbose(options, "Target %s registered" % t.name()) except scst.ScstException as e: handle_exception(options, e)
def register_volumes(monitor, options, config): """ Registers SCST volumes """ for (volume_id, vol) in volume.read_all_volumes(monitor).items(): try: if not vol: # the volume is currently in detaching mode and is only still listed without a value continue scst.register_volume(vol) log_verbose(options, "Volume %s registered" % vol.name()) except scst.ScstException as e: handle_exception(options, e)
def unregister_groups(monitor, options, config): """ unregisters the groups of the dedupv1d """ for (group_name, g) in group.read_all_groups(monitor).items(): try: for pattern in g.initiator_pattern(): scst.rm_initiator_pattern_from_group(pattern, group_name) if not group_name == "Default" and scst.exists_group(group_name): # The Default group cannot be deleted scst.rm_group(group_name) log_verbose(options, "Group %s unregistered" % group_name) except scst.ScstException as e: handle_exception(options, e)
def register_groups(monitor, options, config): """ register SCST groups """ for (group_name, g) in group.read_all_groups(monitor).items(): try: if not group_name == "Default": # The Default group is always there scst.add_group(group_name) for pattern in g.initiator_pattern(): scst.add_initiator_pattern_to_group(pattern, group_name) log_verbose(options, "Group %s registered" % group_name) except scst.ScstException as e: handle_exception(options, e)
def unregister_volumes(monitor, options, config): """ unregisters the volumes of the dedupv1d not used anymore """ try: for (volume_id, volume) in monitor.read("volume").items(): try: if not volume: continue log_verbose(options, "Device %s unregistered" % volume["name"]) except scst.ScstException as e: handle_exception(options, e) except IOError: handle_exception(options, e)
def unregister_targets_direct(options, config): """ unregister a given configured at SCST. This function bypasses the information given by the monitor. Therefore this function can be used to unregister a target if dedupv1d is not available """ try: for (tid, t) in iscsi_scst.get_targets().items(): try: log_verbose(options, "Remove target %s" % (t.name())) iscsi_scst.unregister_target(t) except scst.ScstException as e: log_warning(options, "Failed to unregister target %s: %s" % (t.name(), str(e))) except scst.ScstException as e: log_warning("Failed to unregister targets: %s" % (str(e)))
def register_users(monitor, options): """ registers a SCST user """ for (user_name, u) in scst_user.read_all_users(monitor).items(): try: for target_name in u.targets(): t = target.read_target_by_name(monitor, target_name) if not iscsi_scst.is_target_registered(t): raise scst.ScstException("Target %s not registered" % target_name) iscsi_scst.add_user_to_target(u, t) log_verbose(options, "User %s registered" % user_name) except scst.ScstException as e: handle_exception(options, e)
def unregister_users_direct(options, config): """ unregisters the users configured at SCST. This function bypasses the information given by the monitor. Therefore this function can be used to unregister the users if dedupv1d is not available """ try: for (tid, t) in iscsi_scst.get_targets().items(): target_users = iscsi_scst.get_users_in_target(t) for u in target_users: try: log_verbose(options, "Remove user %s from %s" % (u.name, t.name())) iscsi_scst.rm_user_from_target(u, t) except scst.ScstException as e: log_error(options, "Failed to remove user %s from %s: %s" % (u.name, t.name(), str(e))) except scst.ScstException as e: log_error(options, "Failed to remove users: %s" % (str(e)))
def unregister_users(monitor, options, config): """ unregisters the users of the dedupv1d """ for (user_name, u) in scst_user.read_all_users(monitor).items(): try: for target_name in u.targets(): t = target.read_target_by_name(monitor, target_name) if not iscsi_scst.is_target_registered(t): continue if not iscsi_scst.is_user_in_target(user_name, t): continue iscsi_scst.rm_user_from_target(u, t) log_verbose(options, "User %s unregistered" % user_name) except scst.ScstException as e: handle_exception(options, e)
def unregister_group_direct(option, config, groups): try: all_groups = scst.get_scst_groups() for (group_name) in groups: try: if options.verbose: log_verbose(options, "Remove group %s" % (group_name)) if not group_name in all_groups: raise scst.ScstException("Group %s not existing" % (group_name)) for ip in scst.get_initiator_pattern_in_group(group_name): scst.rm_initiator_pattern_from_group(ip, group_name) if group_name != "Default": scst.rm_group(group_name) except scst.ScstException as e: log_error(options, "Failed to remove group %s: %s" % (group_name, e)) except scst.ScstException as e: log_error("Failed to remove groups: %s" % (e))
def unregister_targets(monitor, options, config): """ unregisters the targets configured at SCST. This function bypasses the information given by the monitor. Therefore this function can be used to unregister the targets if dedupv1d is not available """ try: for (tid, t) in target.read_all_targets(monitor).items(): try: if iscsi_scst.is_target_registered(t): iscsi_scst.unregister_target(t) group_name = "Default_" + t.name() if len(t.volumes()) > 0 and scst.exists_group(group_name): scst.rm_group(group_name) log_verbose(options, "Target %s unregistered" % t.name()) except scst.ScstException as e: if options.force: log_warning("Failed to unregister target %s: %s" % (t.name(), str(e))) else: raise scst.ScstException("Failed to unregister target %s" % (t.name()), e) except IOError as e: handle_exception(options, e)
def stop_device(dedupv1_root, monitor, options, config, writeback_stop = False): """ stops dedupv1d. This function can take a very long time to finish when writeback_stop is True. In this case the dedup1d is not existing before all open chunk and block index data is written into the persistent index """ def on_stop(): dirty_file = config.get("daemon.dirtyfile") if os.path.exists(dirty_file): dirty_data = DirtyFileData() content = open(dirty_file, "r").read() read_sized_message(dirty_data, content) if not dirty_data.stopped: raise Exception("dedupv1d stopped with errors") log_info(options, "\ndedupv1d stopped") lock_filename = config.get("daemon.lockfile") pid = None not_running = not is_running(config) if not_running: if options.force: log_warning(options, "dedupv1d not running") else: raise Exception("dedupv1d not running") else: pid = get_daemon_pid(config) try: session_count = 0 for (tid, t) in iscsi_scst.get_targets().items(): session_count = session_count + len(t["sessions"]) for session in t["sessions"]: log_warning(options, "Target %s has still open session with initiator %s" % (t["name"], session["initiator"])) if session_count > 0: if options.force: log_warning(options, "iSCSI targets have still open sessions") else: raise Exception("iSCSI targets have still open sessions") except scst.ScstException as e: handle_exception(options, e) if options.force: try: unregister_users(monitor, options, config) except Exception as e: if not_running: log_verbose(options, "Unregister users failed") else: log_warning(options, "Unregister users failed") try: unregister_volumes(monitor, options, config) except Exception as e: if not_running: log_verbose(options, "Unregister volumes failed") else: log_warning(options, "Unregister volumes failed") try: unregister_targets(monitor, options, config) except: if not_running: log_verbose(options, "Unregister targets failed") else: log_warning(options, "Unregister targets failed") try: unregister_groups(monitor, options, config) except: if not_running: log_verbose(options, "Unregister groups failed") else: log_warning(options, "Unregister groups failed") else: unregister_users(monitor, options, config) unregister_volumes(monitor, options, config) unregister_targets(monitor, options, config) unregister_groups(monitor, options, config) try: new_state = "stop" if writeback_stop: new_state = "writeback-stop" try: monitor.read("status", [("change-state", new_state)]) except MonitorException: if not options.force: raise except OSError: if options.force: log_warning(options, "dedupv1d crashed") try: if pid: # Here we do trick with with the dots so we dont # se log_info, but it should only be done when not in # raw mode if not options.raw: print "dedupv1d stopping", for i in xrange(128): if not is_process_running(pid): on_stop() break if not options.raw: sys.stdout.write(".") sys.stdout.flush() time.sleep(2 * i) #exp backoff finally: # We have to remove the lock file here, cause the daemon # might not have the permission to do it. The file is created by # the daemon starting process with the uid of this script, but the # daemon might run under a different uid. if os.path.exists(lock_filename): os.remove(lock_filename)
def remove_file(filename): """ removes a given file, when the file exists """ if os.path.exists(filename): log_verbose(options, "Remove %s" % filename) os.remove(filename)