def connect(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = ConnectRsp() diskPaths = set() def config_lvm(host_id): lvm.backup_lvm_config() lvm.reset_lvm_conf_default() lvm.config_lvm_by_sed("use_lvmlockd", "use_lvmlockd=1", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("use_lvmetad", "use_lvmetad=0", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("host_id", "host_id=%s" % host_id, ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("sanlock_lv_extend", "sanlock_lv_extend=%s" % DEFAULT_SANLOCK_LV_SIZE, ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("lvmlockd_lock_retries", "lvmlockd_lock_retries=6", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("issue_discards", "issue_discards=1", ["lvm.conf", "lvmlocal.conf"]) lvm.config_sanlock_by_sed("sh_retries", "sh_retries=20") lvm.config_sanlock_by_sed("logfile_priority", "logfile_priority=7") lvm.config_sanlock_by_sed("renewal_read_extend_sec", "renewal_read_extend_sec=24") lvm.config_sanlock_by_sed("debug_renew", "debug_renew=1") lvm.config_sanlock_by_sed("use_watchdog", "use_watchdog=0") config_lvm(cmd.hostId) for diskUuid in cmd.sharedBlockUuids: disk = CheckDisk(diskUuid) diskPaths.add(disk.get_path()) lvm.start_lvmlockd() lvm.check_gl_lock() rsp.isFirst = self.create_vg_if_not_found(cmd.vgUuid, diskPaths, cmd.hostUuid, cmd.forceWipe) lvm.start_vg_lock(cmd.vgUuid) lvm.clean_vg_exists_host_tags(cmd.vgUuid, cmd.hostUuid, HEARTBEAT_TAG) lvm.add_vg_tag(cmd.vgUuid, "%s::%s::%s" % (HEARTBEAT_TAG, cmd.hostUuid, time.time())) rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) rsp.hostId = lvm.get_running_host_id(cmd.vgUuid) return jsonobject.dumps(rsp)
def connect(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = ConnectRsp() diskPaths = set() def config_lvm(host_id, enableLvmetad=False): lvm.backup_lvm_config() lvm.reset_lvm_conf_default() lvm.config_lvm_by_sed("use_lvmlockd", "use_lvmlockd=1", ["lvm.conf", "lvmlocal.conf"]) if enableLvmetad: lvm.config_lvm_by_sed("use_lvmetad", "use_lvmetad=1", ["lvm.conf", "lvmlocal.conf"]) else: lvm.config_lvm_by_sed("use_lvmetad", "use_lvmetad=0", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("host_id", "host_id=%s" % host_id, ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("sanlock_lv_extend", "sanlock_lv_extend=%s" % DEFAULT_SANLOCK_LV_SIZE, ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("lvmlockd_lock_retries", "lvmlockd_lock_retries=6", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("issue_discards", "issue_discards=1", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("reserved_stack", "reserved_stack=256", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("reserved_memory", "reserved_memory=131072", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_filter(["lvm.conf", "lvmlocal.conf"]) lvm.config_sanlock_by_sed("sh_retries", "sh_retries=20") lvm.config_sanlock_by_sed("logfile_priority", "logfile_priority=7") lvm.config_sanlock_by_sed("renewal_read_extend_sec", "renewal_read_extend_sec=24") lvm.config_sanlock_by_sed("debug_renew", "debug_renew=1") lvm.config_sanlock_by_sed("use_watchdog", "use_watchdog=0") sanlock_hostname = "%s-%s-%s" % (cmd.vgUuid[:8], cmd.hostUuid[:8], bash.bash_o("hostname").strip()[:20]) lvm.config_sanlock_by_sed("our_host_name", "our_host_name=%s" % sanlock_hostname) config_lvm(cmd.hostId, cmd.enableLvmetad) for diskUuid in cmd.sharedBlockUuids: disk = CheckDisk(diskUuid) diskPaths.add(disk.get_path()) lvm.start_lvmlockd() lvm.check_gl_lock() logger.debug("find/create vg %s lock..." % cmd.vgUuid) rsp.isFirst = self.create_vg_if_not_found(cmd.vgUuid, diskPaths, cmd.hostUuid, cmd.forceWipe) lvm.check_stuck_vglk() logger.debug("starting vg %s lock..." % cmd.vgUuid) lvm.start_vg_lock(cmd.vgUuid) if lvm.lvm_vgck(cmd.vgUuid, 60)[0] is False and lvm.lvm_check_operation(cmd.vgUuid) is False: lvm.drop_vg_lock(cmd.vgUuid) logger.debug("restarting vg %s lock..." % cmd.vgUuid) lvm.check_gl_lock() lvm.start_vg_lock(cmd.vgUuid) lvm.clean_vg_exists_host_tags(cmd.vgUuid, cmd.hostUuid, HEARTBEAT_TAG) lvm.add_vg_tag(cmd.vgUuid, "%s::%s::%s::%s" % (HEARTBEAT_TAG, cmd.hostUuid, time.time(), bash.bash_o('hostname').strip())) self.clear_stalled_qmp_socket() rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) rsp.hostId = lvm.get_running_host_id(cmd.vgUuid) rsp.vgLvmUuid = lvm.get_vg_lvm_uuid(cmd.vgUuid) rsp.hostUuid = cmd.hostUuid return jsonobject.dumps(rsp)
def connect(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = ConnectRsp() diskPaths = set() def config_lvm(host_id): lvm.backup_lvm_config() lvm.reset_lvm_conf_default() lvm.config_lvm_by_sed("use_lvmlockd", "use_lvmlockd=1", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("use_lvmetad", "use_lvmetad=0", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("host_id", "host_id=%s" % host_id, ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("sanlock_lv_extend", "sanlock_lv_extend=%s" % DEFAULT_SANLOCK_LV_SIZE, ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("lvmlockd_lock_retries", "lvmlockd_lock_retries=6", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("issue_discards", "issue_discards=1", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("reserved_memory", "reserved_memory=65536", ["lvm.conf", "lvmlocal.conf"]) lvm.config_sanlock_by_sed("sh_retries", "sh_retries=20") lvm.config_sanlock_by_sed("logfile_priority", "logfile_priority=7") lvm.config_sanlock_by_sed("renewal_read_extend_sec", "renewal_read_extend_sec=24") lvm.config_sanlock_by_sed("debug_renew", "debug_renew=1") lvm.config_sanlock_by_sed("use_watchdog", "use_watchdog=0") sanlock_hostname = "%s-%s-%s" % (cmd.vgUuid[:8], cmd.hostUuid[:8], bash.bash_o("hostname").strip()[:20]) lvm.config_sanlock_by_sed("our_host_name", "our_host_name=%s" % sanlock_hostname) config_lvm(cmd.hostId) for diskUuid in cmd.sharedBlockUuids: disk = CheckDisk(diskUuid) diskPaths.add(disk.get_path()) lvm.start_lvmlockd() lvm.check_gl_lock() logger.debug("find/create vg %s lock..." % cmd.vgUuid) rsp.isFirst = self.create_vg_if_not_found(cmd.vgUuid, diskPaths, cmd.hostUuid, cmd.forceWipe) lvm.check_stuck_vglk() logger.debug("starting vg %s lock..." % cmd.vgUuid) lvm.start_vg_lock(cmd.vgUuid) if lvm.lvm_vgck(cmd.vgUuid, 15)[0] is False: lvm.drop_vg_lock(cmd.vgUuid) logger.debug("restarting vg %s lock..." % cmd.vgUuid) lvm.check_gl_lock() lvm.start_vg_lock(cmd.vgUuid) lvm.clean_vg_exists_host_tags(cmd.vgUuid, cmd.hostUuid, HEARTBEAT_TAG) lvm.add_vg_tag(cmd.vgUuid, "%s::%s::%s::%s" % (HEARTBEAT_TAG, cmd.hostUuid, time.time(), bash.bash_o('hostname').strip())) rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) rsp.hostId = lvm.get_running_host_id(cmd.vgUuid) return jsonobject.dumps(rsp)
def disconnect(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = AgentRsp() @linux.retry(times=3, sleep_time=random.uniform(0.1, 3)) def find_vg(vgUuid): cmd = shell.ShellCmd("vgs --nolocking %s -otags | grep %s" % (vgUuid, INIT_TAG)) cmd(is_exception=False) if cmd.return_code == 0: return True logger.debug("can not find vg %s with tag %s" % (vgUuid, INIT_TAG)) cmd = shell.ShellCmd("vgs %s" % vgUuid) cmd(is_exception=False) if cmd.return_code == 0: logger.warn("found vg %s without tag %s" % (vgUuid, INIT_TAG)) return True raise RetryException("can not find vg %s with or without tag %s" % (vgUuid, INIT_TAG)) @linux.retry(times=3, sleep_time=random.uniform(0.1, 3)) def deactive_drbd_resouces_on_vg(vgUuid): active_lvs = lvm.list_local_active_lvs(vgUuid) if len(active_lvs) == 0: return drbd_resources = [ drbd.DrbdResource(lv.split("/")[-1]) for lv in active_lvs ] for r in drbd_resources: r.destroy() logger.warn("active lvs %s will be deactivate" % active_lvs) lvm.deactive_lv(vgUuid) active_lvs = lvm.list_local_active_lvs(vgUuid) if len(active_lvs) != 0: raise RetryException( "lvs [%s] still active, retry deactive again" % active_lvs) try: find_vg(cmd.vgUuid) except RetryException: logger.debug("can not find vg %s; return success" % cmd.vgUuid) return jsonobject.dumps(rsp) except Exception as e: raise e deactive_drbd_resouces_on_vg(cmd.vgUuid) lvm.clean_vg_exists_host_tags(cmd.vgUuid, cmd.hostUuid, HEARTBEAT_TAG) return jsonobject.dumps(rsp)
def disconnect(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = AgentRsp() @linux.retry(times=3, sleep_time=random.uniform(0.1, 3)) def find_vg(vgUuid): cmd = shell.ShellCmd("vgs --nolocking %s -otags | grep %s" % (vgUuid, INIT_TAG)) cmd(is_exception=False) if cmd.return_code == 0: return True logger.debug("can not find vg %s with tag %s" % (vgUuid, INIT_TAG)) cmd = shell.ShellCmd("vgs %s" % vgUuid) cmd(is_exception=False) if cmd.return_code == 0: logger.warn("found vg %s without tag %s" % (vgUuid, INIT_TAG)) return True raise RetryException("can not find vg %s with or without tag %s" % (vgUuid, INIT_TAG)) try: find_vg(cmd.vgUuid) except RetryException: logger.debug("can not find vg %s; return success" % cmd.vgUuid) return jsonobject.dumps(rsp) except Exception as e: raise e @linux.retry(times=3, sleep_time=random.uniform(0.1, 3)) def deactive_lvs_on_vg(vgUuid): active_lvs = lvm.list_local_active_lvs(vgUuid) if len(active_lvs) == 0: return logger.warn("active lvs %s will be deactivate" % active_lvs) lvm.deactive_lv(vgUuid) active_lvs = lvm.list_local_active_lvs(vgUuid) if len(active_lvs) != 0: raise RetryException("lvs [%s] still active, retry deactive again" % active_lvs) deactive_lvs_on_vg(cmd.vgUuid) lvm.clean_vg_exists_host_tags(cmd.vgUuid, cmd.hostUuid, HEARTBEAT_TAG) lvm.stop_vg_lock(cmd.vgUuid) if cmd.stopServices: lvm.quitLockServices() return jsonobject.dumps(rsp)
def disconnect(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = AgentRsp() @linux.retry(times=3, sleep_time=random.uniform(0.1, 3)) def find_vg(vgUuid): cmd = shell.ShellCmd("vgs %s -otags | grep %s" % (vgUuid, INIT_TAG)) cmd(is_exception=False) if cmd.return_code == 0: return True logger.debug("can not find vg %s with tag %s" % (vgUuid, INIT_TAG)) cmd = shell.ShellCmd("vgs %s" % vgUuid) cmd(is_exception=False) if cmd.return_code == 0: logger.warn("found vg %s without tag %s" % (vgUuid, INIT_TAG)) return True raise RetryException("can not find vg %s with or without tag %s" % (vgUuid, INIT_TAG)) try: find_vg(cmd.vgUuid) except RetryException: logger.debug("can not find vg %s; return success" % cmd.vgUuid) return jsonobject.dumps(rsp) except Exception as e: raise e active_lvs = lvm.list_local_active_lvs(cmd.vgUuid) if len(active_lvs) != 0: logger.warn("active lvs %s will be deactivate" % active_lvs) lvm.deactive_lv(cmd.vgUuid) lvm.clean_vg_exists_host_tags(cmd.vgUuid, cmd.hostUuid, HEARTBEAT_TAG) lvm.stop_vg_lock(cmd.vgUuid) return jsonobject.dumps(rsp)
def connect(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = ConnectRsp() diskPaths = set() def config_lvm(enableLvmetad=False): lvm.backup_lvm_config() lvm.reset_lvm_conf_default() if enableLvmetad: lvm.config_lvm_by_sed("use_lvmetad", "use_lvmetad=1", ["lvm.conf", "lvmlocal.conf"]) else: lvm.config_lvm_by_sed("use_lvmetad", "use_lvmetad=0", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("issue_discards", "issue_discards=1", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("reserved_stack", "reserved_stack=256", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("reserved_memory", "reserved_memory=131072", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("thin_pool_autoextend_threshold", "thin_pool_autoextend_threshold=80", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("snapshot_autoextend_threshold", "snapshot_autoextend_threshold=80", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_filter(["lvm.conf", "lvmlocal.conf"], True) def config_drbd(): bash.bash_r( "sed -i 's/usage-count yes/usage-count no/g' /etc/drbd.d/global_common.conf" ) bash.bash_r( "iptables -I INPUT -p tcp -m tcp --dport 20000:30000 -j ACCEPT" ) drbd.install_drbd() config_lvm() config_drbd() for diskId in cmd.diskIdentifiers: disk = CheckDisk(diskId) diskPaths.add(disk.get_path()) logger.debug("find/create vg %s ..." % cmd.vgUuid) self.create_vg_if_not_found(cmd.vgUuid, diskPaths, cmd.hostUuid, cmd.forceWipe) self.create_thin_pool_if_not_found(cmd.vgUuid, INIT_POOL_RATIO) drbd.up_all_resouces() if lvm.lvm_check_operation(cmd.vgUuid) is False: logger.warn("lvm operation test failed!") lvm.clean_vg_exists_host_tags(cmd.vgUuid, cmd.hostUuid, HEARTBEAT_TAG) lvm.add_vg_tag( cmd.vgUuid, "%s::%s::%s::%s" % (HEARTBEAT_TAG, cmd.hostUuid, time.time(), bash.bash_o('hostname').strip())) if cmd.fencerAddress: lvm.clean_vg_exists_host_tags(cmd.vgUuid, '\'\'', FENCER_TAG) lvm.add_vg_tag(cmd.vgUuid, "%s::%s" % (FENCER_TAG, cmd.fencerAddress)) lvm.clean_vg_exists_host_tags(cmd.vgUuid, '\'\'', MANAGEMENT_TAG) lvm.add_vg_tag(cmd.vgUuid, "%s::%s" % (MANAGEMENT_TAG, cmd.magementAddress)) self.generate_fencer(cmd.peerManagementAddress, cmd.peerSshUsername, cmd.peerSshPassword) if cmd.storageNetworkCidr is not None: nics = linux.get_nics_by_cidr(cmd.storageNetworkCidr) if len(nics) != 0: rsp.storageNetworkAddress = nics[0].values()[0] rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) rsp.vgLvmUuid = lvm.get_vg_lvm_uuid(cmd.vgUuid) rsp.hostUuid = cmd.hostUuid return jsonobject.dumps(rsp)
def connect(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = ConnectRsp() diskPaths = set() def config_lvm(host_id): lvm.backup_lvm_config() lvm.reset_lvm_conf_default() lvm.config_lvm_by_sed("use_lvmlockd", "use_lvmlockd=1", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("use_lvmetad", "use_lvmetad=0", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("host_id", "host_id=%s" % host_id, ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed( "sanlock_lv_extend", "sanlock_lv_extend=%s" % DEFAULT_SANLOCK_LV_SIZE, ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("lvmlockd_lock_retries", "lvmlockd_lock_retries=6", ["lvm.conf", "lvmlocal.conf"]) lvm.config_lvm_by_sed("issue_discards", "issue_discards=1", ["lvm.conf", "lvmlocal.conf"]) lvm.config_sanlock_by_sed("sh_retries", "sh_retries=20") lvm.config_sanlock_by_sed("logfile_priority", "logfile_priority=7") lvm.config_sanlock_by_sed("renewal_read_extend_sec", "renewal_read_extend_sec=24") lvm.config_sanlock_by_sed("debug_renew", "debug_renew=1") def create_vg_if_not_found(vgUuid, diskPaths, hostUuid): @linux.retry(times=3, sleep_time=random.uniform(0.1, 3)) def find_vg(vgUuid): cmd = shell.ShellCmd("vgs %s -otags | grep %s" % (vgUuid, INIT_TAG)) cmd(is_exception=False) if cmd.return_code != 0: raise RetryException("can not find vg %s with tag %s" % (vgUuid, INIT_TAG)) return True try: find_vg(vgUuid) except RetryException: cmd = shell.ShellCmd( "vgcreate --shared --addtag '%s::%s::%s' --metadatasize %s %s %s" % (INIT_TAG, hostUuid, time.time(), DEFAULT_VG_METADATA_SIZE, vgUuid, " ".join(diskPaths))) cmd(is_exception=False) if cmd.return_code == 0: return True if find_vg(vgUuid) is False: raise Exception( "can not find vg %s with disks: %s and create failed for %s " % (vgUuid, diskPaths, cmd.stderr)) except Exception as e: raise e return False config_lvm(cmd.hostId) for diskUuid in cmd.sharedBlockUuids: disk = CheckDisk(diskUuid) diskPaths.add(disk.get_path()) lvm.start_lvmlockd() rsp.isFirst = create_vg_if_not_found(cmd.vgUuid, diskPaths, cmd.hostUuid) lvm.start_vg_lock(cmd.vgUuid) lvm.clean_vg_exists_host_tags(cmd.vgUuid, cmd.hostUuid, HEARTBEAT_TAG) lvm.add_vg_tag( cmd.vgUuid, "%s::%s::%s" % (HEARTBEAT_TAG, cmd.hostUuid, time.time())) rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) rsp.hostId = lvm.get_running_host_id(cmd.vgUuid) return jsonobject.dumps(rsp)