def _agentService_initd_start(self): cmd7 = "/etc/init.d/" + self.args['AgentServiceName'] + " start" info = executeCommand(cmd7, False) if info[0] != 0: xlogging.raise_system_error( "_agentService_initd_start error", "start_agentService, error:{}".format(info[2]), 0)
def mount(self): if self._mounted: xlogging.raise_system_error(r'内部异常,多次挂载', r'MountNbdLinux mount failed', 1) else: self._mounted = True if self._read_only: self.change_gpt_guid() self.change_vgs_name_and_uuid() self._scan_device() mount_params, pvs, swap_devices = self.analyze_mount_params() if self._read_only: self.change_pvs_uuid(pvs) self.activate_lvm() # if self._read_only: # self.change_xfs_uuid(mount_params) # self.change_ext_uuid(mount_params) # self.change_btrfs_uuid(mount_params) self.mount_all(mount_params) if self._read_only: # self.change_chmod_only_read() pass else: self.fix_swap(swap_devices)
def _create_lvm(self): disks = self._args_dict['lvm_config']['disks'] code, stdout, stderr = funcs.execute_cmd('pvscan') if code != 0: xlogging.raise_system_error( '初始化数据缓存区域失败', 'execute_cmd pvscan:{}{}{}'.format(code, stdout, stderr), 1161) pvs = list() for disk in disks: for dev_path, size in self._all_blocks_size.items(): if int(disk['disk_bytes']) == int(size): disk['dev_path'] = dev_path break else: xlogging.raise_system_error( '初始化数据缓存区域失败', 'not found disk {} dev_path {}'.format( disk, self._all_blocks_size), 1162) funcs.execute_cmd('pvcreate {}'.format(disk['dev_path'])) pvs.append(disk['dev_path']) cmd = 'vgcreate {} '.format(VG_NAME) cmd += ' '.join(pvs) funcs.execute_cmd(cmd) funcs.execute_cmd('lvcreate -l 100%VG -n {} {}'.format( LV_NAME, VG_NAME)) funcs.execute_cmd( 'mkfs.ext4 /dev/mapper/{}-{}'.format(VG_NAME, LV_NAME), 60 * 30)
def _agentService_systemd_start(self): cmd3 = "systemctl start " + self.args['Service_Service'] info = executeCommand(cmd3) if info[0] != 0: xlogging.raise_system_error( "_agentService_systemd_start error", "start_agentService, error:{}".format(info[2]), 0)
def _check_path(path): if os.path.exists(path): return path else: xlogging.raise_system_error( "Register _check_path error", "Register service path not exists {}".format(path), 0)
def _agentService_initd_init(self): _logger.info("_start_agentService_init start") cmd_stop = "/etc/init.d/" + self.args['AgentServiceName'] + " stop" if self.args['AgentServiceName'] == 'ClwDRClient': rc_file_name = 'S20' + self.args['AgentServiceName'] else: rank = get_ip_service_rank(get_rc_dir()) rc_file_name = 'S{}'.format(rank) + self.args['AgentServiceName'] executeCommand(cmd_stop) rc2_full = get_rcx_full_path("rc2.d", rc_file_name) rc3_full = get_rcx_full_path("rc3.d", rc_file_name) rc5_full = get_rcx_full_path("rc5.d", rc_file_name) cmd_del_rc3 = "rm -f " + rc3_full cmd_del_rc5 = "rm -f " + rc5_full cmd_del_rc2 = "rm -f " + rc2_full cmd4 = "chmod +x /etc/init.d/" + self.args['AgentServiceName'] cmd5 = "ln -s /etc/init.d/" + self.args[ 'AgentServiceName'] + " " + rc3_full cmd6 = "ln -s /etc/init.d/" + self.args[ 'AgentServiceName'] + " " + rc5_full ubuntu_a_cmd = "ln -s /etc/init.d/" + self.args[ 'AgentServiceName'] + " " + rc2_full # cmd7 = "/etc/init.d/" + self.args['AgentServiceName'] + " start" if os.path.exists(rc3_full): _logger.info( "[_start_agentService_init] remove file={}".format(rc3_full)) executeCommand(cmd_del_rc3) if os.path.exists(rc5_full): _logger.info( "[_start_agentService_init] remove file={}".format(rc5_full)) executeCommand(cmd_del_rc5) if os.path.exists(rc2_full): _logger.info( "[_start_agentService_init] remove file={}".format(rc2_full)) executeCommand(cmd_del_rc2) wait = True for cmd in [cmd4, cmd5, cmd6, ubuntu_a_cmd]: # if cmd == cmd7: # wait = False info = executeCommand(cmd, wait) if info[0] != 0: xlogging.raise_system_error( "_agentService_initd_init error", "init_agentService, error:{}".format(info[2]), 0) retval = do_servcie_config(self.args['AgentServiceName']) if not retval: xlogging.raise_system_error("_agentService_initd_init error", "do_servcie_config failed", 0)
def get_rc_dir(): rc3_dir = r'/etc/rc3.d/' if os.path.exists(rc3_dir): return rc3_dir rc3_dir = r'/etc/init.d/rc3.d/' if os.path.exists(rc3_dir): return rc3_dir xlogging.raise_system_error("get_rc_dir error", "can not find rc3.d dir")
def mount_all(self, mount_params): # org_param = 'rw,async,relatime,noatime,nodiratime' valid_mount_points = 0 for mount_task in mount_params: if mount_task['mountPoint'] in self.mount_point: _logger.warning("{} is mounted, mount_task {}".format(mount_task['mountPoint'], mount_task)) continue mnt_succ = False if self._read_only: _get_info_from_syscmd_timeout_very_short(r'mkdir -p "{}"'.format(mount_task['mountPoint'])) else: if mount_task.get('fileSystem', '').upper() == 'EXT4': _logger.info(r'force fsck.ext4 begin : {}'.format(mount_task["device"])) r = _get_info_from_syscmd_timeout_long(r'fsck.ext4 -y "{}"'.format(mount_task["device"])) _logger.info(r'force fsck.ext4 end : {} {}'.format(mount_task["device"], r)) o_param = self.get_mount_option(self.mnt_opt, mount_task) _logger.info( "[mount_all] opt={} dev={} mntpoint={}".format(o_param, mount_task['device'], mount_task['mountPoint'])) if mount_task['mountPoint'].endswith(xdefine.CLW_BOOT_REDIRECT_GPT_LINUX_MOUNT_PATH): os.mkdir(mount_task['mountPoint']) r = _get_info_from_syscmd_timeout_long(r'mount -v -o {} {} "{}"'.format( o_param, mount_task['device'], mount_task['mountPoint'])) if r[0] == 0: self.mount_point.append(mount_task['mountPoint']) _logger.info(r'mount {} ok'.format(mount_task['mountPoint'])) valid_mount_points += 1 mnt_succ = True elif not self._read_only and mount_task.get('fileSystem', '').upper() == 'XFS' and r[0] != -2: _get_info_from_syscmd_timeout_long(r'xfs_repair -L {}'.format(mount_task["device"])) time.sleep(0.5) r = _get_info_from_syscmd_timeout_long(r'mount -v -o {} {} "{}"'.format( o_param, mount_task['device'], mount_task['mountPoint'])) if r[0] == 0: self.mount_point.append(mount_task['mountPoint']) _logger.info(r'mount {} ok'.format(mount_task['mountPoint'])) valid_mount_points += 1 mnt_succ = True elif r[0] == -2: _get_info_from_syscmd_timeout_short(r'umount -v "{}"'.format(mount_task['mountPoint'])) _get_info_from_syscmd_timeout_short(r'umount -v -l "{}"'.format(mount_task['mountPoint'])) btrfsmntopt = mount_task.get("btrfsmntopt") _logger.info(r'[mount_all] mmt_succ={} btrfsmntopt={}'.format(mnt_succ, btrfsmntopt)) if mnt_succ and btrfsmntopt: self.mount_btrfs_subvol(mount_task) if valid_mount_points == 0: xlogging.raise_system_error(r'无法加载备份快照中的文件系统', r'mount_all failed', 0)
def get_rcx_full_path(rc_x, file_name): rc_x_path = "/etc/{}/".format(rc_x) if os.path.exists(rc_x_path): return os.path.join(rc_x_path, file_name) rc_x_path = "/etc/init.d/{}/".format(rc_x) if os.path.exists(rc_x_path): return os.path.join(rc_x_path, file_name) xlogging.raise_system_error("get_rc_x_path error", "can not find {}".format(rc_x), 0) return ""
def get_systemd_path(): # default path = "/usr/lib/systemd/system/" if os.path.exists(path): return path # ubuntu 16.04 path = "/etc/systemd/system" if os.path.exists(path): return path xlogging.raise_system_error("register error", "[get_systemd_path] not found systemd path", 0) return None
def _get_exe_path(path): if path.endswith('LinuxAgentService'): dir_name = os.path.dirname(path) wdg_name = 'clerwd' wdg_path = os.path.join(dir_name, wdg_name) if not os.path.exists(wdg_path): xlogging.raise_system_error( "Register _check_path error", "Register service path not exists {}".format(wdg_path), 0) interval_times = 10 exe_cmd = "{exe_wdg} {itv_tms} {exe_agt_srv}".format( exe_wdg=wdg_path, itv_tms=interval_times, exe_agt_srv=path) return exe_cmd else: return path
def _agentService_systemd_init(self): _logger.info("_start_agentService_systemd start") # cmd1 = "chmod +x /usr/lib/systemd/system/" + self.args['Service_Service'] systemd_path = get_systemd_path() systemd_cfg = os.path.join(systemd_path, self.args['Service_Service']) cmd1 = "chmod +x {}".format(systemd_cfg) cmd2 = "systemctl daemon-reload" cmd3 = "systemctl enable " + self.args['Service_Service'] # cmd3 = "systemctl start " + self.args['Service_Service'] for cmd in [cmd1, cmd2, cmd3]: info = executeCommand(cmd) if info[0] != 0: xlogging.raise_system_error( "_agentService_systemd_init error", "init_agentService, error:{}".format(info[2]), 0)
def _init_data_area(self): if 'lvm_config' not in self._args_dict: return lvm_path = '/dev/mapper/{}-{}'.format(VG_NAME, LV_NAME) if not os.path.exists(lvm_path): self._status = ('初始化数据缓存区域', 'init_data_area') _logger.warning('not found lvm {}, start create'.format(lvm_path)) self._pause_in_debug_mod('开始创建lvm') self._create_lvm() xlogging.makedirs(MOUNT_PATH, exist_ok=True) code, stdout, stderr = funcs.execute_cmd('mount {} {}'.format( lvm_path, MOUNT_PATH)) if code != 0: xlogging.raise_system_error( '初始化数据缓存区域失败', 'execute_cmd mount:{}{}{}'.format(code, stdout, stderr), 1162)
def _get_remote_proxy(self): from service_in_kvm import app count = 12 * 5 remote_proxy = None while count > 0: count -= 1 try: remote_proxy = app.get_remote_proxy( self._args_dict['db_backup_params']['master_ident']) except Exception as e: _logger.warning( '_get_remote_proxy error: {} will retry {}'.format( e, count)) time.sleep(5) else: break if not remote_proxy: xlogging.raise_system_error('等待R端连入超时', 'waite remote timeout', 172) self.remote_proxy = remote_proxy
def _get_remote_proxy(self): from service_in_kvm import app count = 12 * 5 remote_proxy = None while count > 0 and (not self._stop_flag): count -= 1 try: remote_proxy = app.get_remote_proxy(self._args_dict['ident']) except Exception as e: _logger.warning( '_get_remote_proxy error: {} will retry {}'.format( e, count)) time.sleep(5) else: break if not remote_proxy: xlogging.raise_system_error('等待R端连入超时', 'waite remote timeout', 172) self.remote_proxy = remote_proxy self.remote_func_proxy = remote_helper.FunctionMapper( self.remote_proxy, _logger)
def get_ip_service_rank(script_dir, default='06'): rank = default find = False try: rank_re = re.compile('^S(\d+)network$') for file_name in os.listdir(script_dir): match = rank_re.match(file_name) if match: _rank = match.groups()[0] if str.isdigit(_rank) and int(_rank) < int(rank): rank = _rank find = True if find: assert int(rank) > 0 rank = '0{}'.format(int(rank) - 1) except AssertionError: xlogging.raise_system_error("get_ip_service_rank error", "rank <= 0", 0) except Exception as e: _logger.error("get_ip_service_rank error: {}".format(e), exc_info=True) _logger.info('get_ip_service_rank return {}'.format(rank)) return rank
def get_reg_file_path_imp(root_path, install_path): """ 获取clerware_meta_path 1. AgentService.config 中配置了Agent.ClwMetaPath 目录,那么使用这个 2. 也有可能 ClwMeta 存在与 install_path 目录下 2. 以上都不存在,那么就在 /boot 目录下 :return: clerware_meta_path """ bin_path = root_path + install_path if not os.path.exists(bin_path): xlogging.raise_system_error('无效的安装路径', 'invalid install path', 445) return None # from AgentService.config meta_path = get_clwmeta_path(bin_path, root_path) if meta_path: return meta_path _logger.info(r'get_reg_file_path_imp config path is not exist') # from bin_path meta_path = os.path.join(bin_path, 'ClerWareMeta') if os.path.exists(meta_path): return meta_path _logger.info(r'get_reg_file_path_imp bin path is not exist') # from boot dir meta_path = os.path.join(root_path, 'boot', 'ClerWareMeta') if os.path.exists(meta_path): return meta_path xlogging.raise_system_error( r'无法访问备份快照中的关键数据区域', r'get_reg_file_path_imp failed : {}'.format(meta_path), 1) return None
def _work(self): xlogging.raise_system_error('无效的调用类型', 'invalid action', 1047)