def start_process(): """ 新增入库跟踪情况的表 dm2_import_step,仅用于测试监控,实际发布时,这个表就不用了: 内容:单独的调度,每隔30秒,统计各种个数 个数:目录个数,文件个数,数据个数,附属文件个数,已挂接标签个数,未挂接标签个数 """ while True: try: db = CFactory().give_me_db() sql_result_count = ''' SELECT now() as query_time, ( SELECT COUNT ( * ) FROM dm2_storage_directory ) AS count_dir, ( SELECT COUNT ( * ) FROM dm2_storage_file ) AS count_file, ( SELECT COUNT ( * ) FROM dm2_storage_object ) AS count_object, ( SELECT COUNT ( * ) FROM dm2_storage_obj_detail ) AS count_obj_detail, ( SELECT COUNT ( * ) FROM dm2_storage_object WHERE dsotags IS NULL ) AS count_object_tag, ( SELECT COUNT ( * ) FROM dm2_storage_object WHERE dsotags IS NOT NULL ) AS count_object_notag ''' count_dataset = db.one_row(sql_result_count) dis_query_time = count_dataset.value_by_name(0, 'query_time', '') dis_directory_count = count_dataset.value_by_name( 0, 'count_dir', '') dis_file_count = count_dataset.value_by_name(0, 'count_file', '') dis_object_count = count_dataset.value_by_name( 0, 'count_object', '') dis_detail_count = count_dataset.value_by_name( 0, 'count_obj_detail', '') dis_object_tag_count = count_dataset.value_by_name( 0, 'count_object_tag', '') dis_object_notag_count = count_dataset.value_by_name( 0, 'count_object_notag', '') sql_insert = ''' insert into dm2_import_step ("dis_query_time","dis_id","dis_directory_count","dis_file_count","dis_object_count","dis_detail_count", "dis_object_tag_count","dis_object_notag_count","dis_addtime") values (:query_time,:disid,:directory_count,:file_count,:object_count,:detail_count, :object_tag_count,:object_notag_count,now()) ''' db.execute( sql_insert, { 'query_time': dis_query_time, 'disid': CUtils.one_id(), 'directory_count': dis_directory_count, 'file_count': dis_file_count, 'object_count': dis_object_count, 'detail_count': dis_detail_count, 'object_tag_count': dis_object_tag_count, 'object_notag_count': dis_object_notag_count }) time.sleep(30) except Exception as error: raise Exception(error.__str__())
def execute(self) -> str: try: db = CFactory().give_me_db() sql_result_count = ''' SELECT now() as query_time, ( SELECT COUNT ( * ) FROM dm2_storage_directory ) AS count_dir, ( SELECT COUNT ( * ) FROM dm2_storage_file ) AS count_file, ( SELECT COUNT ( * ) FROM dm2_storage_object ) AS count_object, ( SELECT COUNT ( * ) FROM dm2_storage_obj_detail ) AS count_obj_detail, ( SELECT COUNT ( * ) FROM dm2_storage_object WHERE dsotags IS NULL ) AS count_object_tag, ( SELECT COUNT ( * ) FROM dm2_storage_object WHERE dsotags IS NOT NULL ) AS count_object_notag ''' count_dataset = db.one_row(sql_result_count) dis_query_time = count_dataset.value_by_name(0, 'query_time', '') dis_directory_count = count_dataset.value_by_name( 0, 'count_dir', '') dis_file_count = count_dataset.value_by_name(0, 'count_file', '') dis_object_count = count_dataset.value_by_name( 0, 'count_object', '') dis_detail_count = count_dataset.value_by_name( 0, 'count_obj_detail', '') dis_object_tag_count = count_dataset.value_by_name( 0, 'count_object_tag', '') dis_object_notag_count = count_dataset.value_by_name( 0, 'count_object_notag', '') sql_insert = ''' insert into dm2_import_step ("dis_query_time","dis_id","dis_directory_count","dis_file_count","dis_object_count","dis_detail_count", "dis_object_tag_count","dis_object_notag_count","dis_addtime") values (:query_time,:disid,:directory_count,:file_count,:object_count,:detail_count, :object_tag_count,:object_notag_count,now()) ''' db.execute( sql_insert, { 'query_time': dis_query_time, 'disid': CUtils.one_id(), 'directory_count': dis_directory_count, 'file_count': dis_file_count, 'object_count': dis_object_count, 'detail_count': dis_detail_count, 'object_tag_count': dis_object_tag_count, 'object_notag_count': dis_object_notag_count }) return CResult.merge_result(self.Success, '本次分析定时扫描任务成功结束!') except Exception as error: raise Exception(error.__str__())
def create_inbound_mission(self, storage_id): database = CFactory().give_me_db(self.get_mission_db_id()) new_batch_no = database.seq_next_value(self.Seq_Type_Date_AutoInc) database.execute( ''' insert into dm2_storage_inbound(dsiid, dsistorageid, dsidirectory, dsibatchno, dsidirectoryid, dsistatus) VALUES(:dsiid, :storageid, :directory, :batch_no, :directory_id, :status) ''', { 'dsiid': CUtils.one_id(), 'storageid': storage_id, 'directory': '', 'batch_no': new_batch_no, 'directory_id': CUtils.one_id(), 'status': self.IB_Status_QI_InQueue })
def process_mission(self, dataset): storage_id = dataset.value_by_name(0, 'root_directory_id', '') storage_root_path = dataset.value_by_name(0, 'root_directory', '') CLogger().debug('storage_id: {0}'.format(storage_id)) ds_retry_times = dataset.value_by_name(0, 'retry_times', 0) if ds_retry_times >= self.abnormal_job_retry_times(): ds_last_process_memo = CUtils.any_2_str( dataset.value_by_name(0, 'last_process_memo', None)) process_result = CResult.merge_result( self.Failure, '{0}, \n系统已经重试{1}次, 仍然未能解决, 请人工检查修正后重试!'.format( ds_last_process_memo, ds_retry_times)) self.update_status(storage_id, process_result, self.ProcStatus_Error) return process_result sql_check_root_storage_dir_exist = ''' select dsdid from dm2_storage_directory where dsdid = :dsdid ''' sql_update_root_storage_dir = ''' update dm2_storage_directory set dsdParentID = '-1', dsdDirectory = '', dsdDirtype = {1} , dsdDirectoryName = '', dsdPath = '' , dsdDirCreateTime = :dsddircreatetime, dsdDirLastModifyTime = :dsddirlastmodifytime , dsdLastModifyTime = Now(), dsd_directory_valid = {0} where dsdid = :dsdid '''.format(self.File_Status_Unknown, self.Dir_Type_Root) sql_insert_root_storage_dir = ''' insert into dm2_storage_directory( dsdid, dsdparentid, dsdstorageid, dsddirectory, dsddirtype, dsdlastmodifytime , dsddirectoryname, dsd_directory_valid, dsdpath, dsddircreatetime, dsddirlastmodifytime) values(:dsdid, '-1', :dsdStorageID, '', {1}, Now() , '', {0}, '', :dsddircreatetime, :dsddirlastmodifytime ) '''.format(self.File_Status_Unknown, self.Dir_Type_Root) try: db = CFactory().give_me_db(self.get_mission_db_id()) params = dict() params['dsdid'] = storage_id params['dsdStorageID'] = storage_id if CFile.file_or_path_exist(storage_root_path): params['dsdDirCreateTime'] = CFile.file_modify_time( storage_root_path) params['dsddirlastmodifytime'] = CFile.file_modify_time( storage_root_path) if db.if_exists(sql_check_root_storage_dir_exist, params): db.execute(sql_update_root_storage_dir, params) else: db.execute(sql_insert_root_storage_dir, params) process_result = CResult.merge_result(CResult.Success, '存储扫描处理成功') self.update_status(storage_id, process_result) return process_result except DBException as err: process_result = CResult.merge_result( CResult.Exception, '存储扫描失败, 原因为{0}'.format(err.__str__)) self.update_status(storage_id, process_result) return process_result
class CTable(CResource): """ 数据表记录更新类 . 可以自动提取数据表中的字段信息 . 通过简单的方式, 设置每一个字段的内容 . 数据表将根据结构中的主键等信息, 自动进行更新\插入\删除等操作 """ __database: CDataBase = None __db_id: str = None __table_name: str = None __column_list: CColumnList = None def __init__(self): self.__column_list = CColumnList() def load_info(self, db_id, table_name): self.__db_id = db_id self.__database = CFactory().give_me_db(self.__db_id) self.__table_name = table_name self.__load_table_info() @property def column_list(self): return self.__column_list @property def db_id(self) -> str: return self.__db_id @property def table_name(self): return self.__table_name @abstractmethod def __load_table_info(self): """ 在这里加载数据表的信息, 并初始化__column_list对象 :return: """ dict_table_info = self.__database.table_info(self.__table_name) table_column_list = CUtils.dict_value_by_name(dict_table_info, self.Name_Columns, None) self.__column_list.clear() if table_column_list is None: return for table_column in table_column_list: column_name = CUtils.dict_value_by_name(table_column, self.Name_Name, '') if CUtils.equal_ignore_case(column_name, ''): continue column_data_type = CUtils.dict_value_by_name( table_column, self.Name_DataType, '') column_is_primary_key = CUtils.dict_value_by_name( table_column, self.Name_PrimaryKey, 0) self.__column_list.add( CColumn(column_name, column_data_type, column_is_primary_key == self.DB_True)) def __param_name(self, column_name: str) -> str: return '{0}'.format(column_name.strip().lower()) def __prepare_where_condition(self, column: CColumn, sql_text: str, sql_params: dict): column_type = self.__database.db_column_type_by_name( column.db_column_type) if CUtils.equal_ignore_case(column_type.set_value_method, self.DB_Column_Set_Method_Param): sql_text = CUtils.str_append( sql_text, '{0}=:{1}'.format(column.name, self.__param_name(column.name)), ' and ') sql_params[self.__param_name(column.name)] = CUtils.any_2_str( CUtils.dict_value_by_name(column.value, self.Name_Text, '')) elif CUtils.equal_ignore_case(column_type.set_value_method, self.DB_Column_Set_Method_Function): sql_text = CUtils.str_append( sql_text, '{0}=:{1}'.format(column.name, self.__param_name(column.name)), ' and ') sql_params[self.__param_name( column.name)] = CUtils.replace_placeholder( column_type.set_value_template, dict({ self.Name_Value, CUtils.any_2_str( CUtils.dict_value_by_name(column.value, self.Name_Text, '')) })) return sql_text, sql_params def __prepare_where(self): sql_text = '' sql_params = {} for column_index in range(self.__column_list.size()): column = self.__column_list.column_by_index(column_index) if column.is_primary_key: sql_text, sql_params = self.__prepare_where_condition( column, sql_text, sql_params) return sql_text, sql_params def __prepare_if_exists(self): sql_from = self.__column_list.column_by_index(0).name for column_index in range(self.__column_list.size()): column = self.__column_list.column_by_index(column_index) if column.is_primary_key: sql_from = column.name break sql_text, sql_params = self.__prepare_where() sql_text = CUtils.str_append( 'select {0} from {1}'.format(sql_from, self.__table_name), sql_text, ' where ') return sql_text, sql_params def __prepare_insert_data(self) -> list: sql_list = [] temp_helper_code_list = [] sql_insert_field = '' sql_insert_data = '' sql_insert_params = dict() for column_index in range(self.__column_list.size()): column = self.__column_list.column_by_index(column_index) if column.value is None: continue try: column_type = self.__database.db_column_type_by_name( column.db_column_type) column_insert_field, column_insert_data = '', '' column_value_type = CUtils.dict_value_by_name( column.value, self.Name_Type, self.DataValueType_SQL) column_value_as_text = CUtils.any_2_str( CUtils.dict_value_by_name(column.value, self.Name_Text, '')) # 如果值为原生sql, 则不管字段类型为何值, 都直接把sql存入insert_data语句中 if CUtils.equal_ignore_case(column_value_type, self.DataValueType_SQL): column_insert_field = column.name column_insert_data = column_value_as_text elif CUtils.equal_ignore_case(column_value_type, self.DataValueType_File): column_insert_field = column.name column_insert_data = ':{0}'.format( self.__param_name(column.name)) self.__database.file2param(sql_insert_params, self.__param_name(column.name), column_value_as_text) else: if CUtils.equal_ignore_case( column_type.set_value_method, self.DB_Column_Set_Method_Function): column_insert_field = column.name if len(column_value_as_text ) > column_type.function_param_max_size >= 0: column_data_id = CUtils.one_id() temp_helper_code_list.append(column_data_id) sql_exchange = ''' insert into ro_global_spatialhandle(code, data) values(:code, :data) ''' param_exchange = { 'code': column_data_id, 'data': column_value_as_text } sql_list.append((sql_exchange, param_exchange)) column_insert_data = CUtils.replace_placeholder( column_type.set_value_template, dict({ self.Name_Value: "(select data from ro_global_spatialhandle where code = '{0}')" .format(column_data_id) })) else: if column_type.function_param_quoted: column_value_as_text = CUtils.quote( column_value_as_text) column_insert_data = CUtils.replace_placeholder( column_type.set_value_template, dict({self.Name_Value: column_value_as_text})) elif CUtils.equal_ignore_case( column_type.set_value_method, self.DB_Column_Set_Method_Geometry): column_insert_field = column.name if len(column_value_as_text ) > column_type.function_param_max_size >= 0: column_data_id = CUtils.one_id() temp_helper_code_list.append(column_data_id) sql_exchange = ''' insert into ro_global_spatialhandle(code, data) values(:code, :data) ''' param_exchange = { 'code': column_data_id, 'data': column_value_as_text } sql_list.append((sql_exchange, param_exchange)) column_insert_data = CUtils.replace_placeholder( column_type.set_value_template, dict({ self.Name_Value: "(select data from ro_global_spatialhandle where code = '{0}')" .format(column_data_id), self.Name_Srid: CUtils.dict_value_by_name( column.value, self.Name_Srid, settings.application.xpath_one( self.Path_Setting_Spatial_Srid, self.SRID_WGS84)) })) else: if column_type.function_param_quoted: column_value_as_text = CUtils.quote( column_value_as_text) column_insert_data = CUtils.replace_placeholder( column_type.set_value_template, dict({ self.Name_Value: column_value_as_text, self.Name_Srid: CUtils.dict_value_by_name( column.value, self.Name_Srid, settings.application.xpath_one( self.Path_Setting_Spatial_Srid, self.SRID_WGS84)) })) else: # if CUtils.equal_ignore_case(column_type.set_value_method, self.DB_Column_Set_Method_Param): column_insert_field = column.name column_insert_data = ':{0}'.format( self.__param_name(column.name)) sql_insert_params[self.__param_name( column.name)] = column_value_as_text sql_insert_field = CUtils.str_append(sql_insert_field, column_insert_field, ', ') sql_insert_data = CUtils.str_append(sql_insert_data, column_insert_data, ', ') except Exception as error: print(error.__str__()) raise sql_insert = 'insert into {0}({1}) values({2})'.format( self.__table_name, sql_insert_field, sql_insert_data) sql_list.append((sql_insert, sql_insert_params)) for temp_helper_code in temp_helper_code_list: sql_list.append( ("delete from ro_global_spatialhandle where code = '{0}'". format(temp_helper_code), None)) return sql_list def __prepare_update_data(self) -> list: sql_list = [] temp_helper_code_list = [] sql_update_set = '' sql_update_params = dict() for column_index in range(self.__column_list.size()): column = self.__column_list.column_by_index(column_index) if column.is_primary_key or (column.value is None): continue try: column_type = self.__database.db_column_type_by_name( column.db_column_type) column_value_type = CUtils.dict_value_by_name( column.value, self.Name_Type, self.DataValueType_SQL) column_value_as_text = CUtils.any_2_str( CUtils.dict_value_by_name(column.value, self.Name_Text, '')) # 如果值为原生sql, 则不管字段类型为何值, 都直接把sql存入insert_data语句中 if CUtils.equal_ignore_case(column_value_type, self.DataValueType_SQL): column_update_set = '{0}={1}'.format( column.name, column_value_as_text) elif CUtils.equal_ignore_case(column_value_type, self.DataValueType_File): column_update_set = '{0}={1}'.format( column.name, ':{0}'.format(self.__param_name(column.name))) self.__database.file2param(sql_update_params, self.__param_name(column.name), column_value_as_text) else: if CUtils.equal_ignore_case( column_type.set_value_method, self.DB_Column_Set_Method_Function): if len(column_value_as_text ) > column_type.function_param_max_size >= 0: column_data_id = CUtils.one_id() temp_helper_code_list.append(column_data_id) sql_exchange = ''' insert into ro_global_spatialhandle(code, data) values(:code, :data) ''' param_exchange = { 'code': column_data_id, 'data': column_value_as_text } sql_list.append((sql_exchange, param_exchange)) column_update_set = '{0}={1}'.format( column.name, CUtils.replace_placeholder( column_type.set_value_template, dict({ self.Name_Value: "(select data from ro_global_spatialhandle where code = '{0}')" .format(column_data_id) }))) else: if column_type.function_param_quoted: column_value_as_text = CUtils.quote( column_value_as_text) column_update_set = '{0}={1}'.format( column.name, CUtils.replace_placeholder( column_type.set_value_template, dict({ self.Name_Value: column_value_as_text }))) elif CUtils.equal_ignore_case( column_type.set_value_method, self.DB_Column_Set_Method_Geometry): if len(column_value_as_text ) > column_type.function_param_max_size >= 0: column_data_id = CUtils.one_id() temp_helper_code_list.append(column_data_id) sql_exchange = ''' insert into ro_global_spatialhandle(code, data) values(:code, :data) ''' param_exchange = { 'code': column_data_id, 'data': column_value_as_text } sql_list.append((sql_exchange, param_exchange)) column_update_set = '{0}={1}'.format( column.name, CUtils.replace_placeholder( column_type.set_value_template, dict({ self.Name_Value: "(select data from ro_global_spatialhandle where code = '{0}')" .format(column_data_id), self.Name_Srid: CUtils.dict_value_by_name( column.value, self.Name_Srid, settings.application.xpath_one( self.Path_Setting_Spatial_Srid, self.SRID_WGS84)) }))) else: if column_type.function_param_quoted: column_value_as_text = CUtils.quote( column_value_as_text) column_update_set = '{0}={1}'.format( column.name, CUtils.replace_placeholder( column_type.set_value_template, dict({ self.Name_Value: column_value_as_text, self.Name_Srid: CUtils.dict_value_by_name( column.value, self.Name_Srid, settings.application.xpath_one( self.Path_Setting_Spatial_Srid, self.SRID_WGS84)) }))) else: # if CUtils.equal_ignore_case(column_type.set_value_method, self.DB_Column_Set_Method_Param): column_update_set = '{0}={1}'.format( column.name, ':{0}'.format(self.__param_name(column.name))) sql_update_params[self.__param_name( column.name)] = column_value_as_text sql_update_set = CUtils.str_append(sql_update_set, column_update_set, ', ') except Exception as error: print(error.__str__()) raise sql_where = '' for column_index in range(self.__column_list.size()): column = self.__column_list.column_by_index(column_index) if column.is_primary_key: sql_where, sql_update_params = self.__prepare_where_condition( column, sql_where, sql_update_params) if not CUtils.equal_ignore_case(sql_where, ''): sql_where = CUtils.str_append(' where ', sql_where, ' ') sql_update = 'update {0} set {1} {2}'.format(self.__table_name, sql_update_set, sql_where) sql_list.append((sql_update, sql_update_params)) for temp_helper_code in temp_helper_code_list: sql_list.append( ("delete from ro_global_spatialhandle where code = '{0}'". format(temp_helper_code), None)) return sql_list def __prepare_delete(self): sql_text, sql_params = self.__prepare_where() sql_text = CUtils.str_append( 'delete from {0}'.format(self.__table_name), sql_text, ' where ') return sql_text, sql_params def sql_of_insert(self) -> list: return self.__prepare_insert_data() def sql_of_update(self) -> list: return self.__prepare_update_data() def sql_of_delete(self) -> list: sql_text, sql_params = self.__prepare_delete() return [(sql_text, sql_params)] def save_data(self, session: Session = None) -> str: if self.if_exists(session): return self.update_data(session) else: return self.insert_data(session) def if_exists(self, session: Session = None) -> bool: sql_text, sql_params = self.__prepare_if_exists() if session is None: return self.__database.if_exists(sql_text, sql_params) else: return self.__database.session_if_exists(session, sql_text, sql_params) def insert_data(self, session: Session = None) -> str: try: sql_list = self.__prepare_insert_data() if session is None: self.__database.execute_batch(sql_list) else: self.__database.session_execute_batch(session, sql_list) return CResult.merge_result(CResult.Success) except Exception as error: return CResult.merge_result(CResult.Failure, error.__str__()) def update_data(self, session: Session = None) -> str: try: sql_list = self.__prepare_update_data() if session is None: self.__database.execute_batch(sql_list) else: self.__database.session_execute_batch(session, sql_list) return CResult.merge_result(CResult.Success) except Exception as error: return CResult.merge_result(CResult.Failure, error.__str__()) def delete_data(self, session: Session = None) -> str: try: sql_text, sql_params = self.__prepare_delete() if session is None: self.__database.execute(sql_text, sql_params) else: self.__database.session_execute(session, sql_text, sql_params) return CResult.merge_result(CResult.Success) except Exception as error: return CResult.merge_result(CResult.Failure, error.__str__())
def execute(self) -> str: dm2_storage_list = CFactory().give_me_db(self.get_mission_db_id()).all_row( ''' SELECT dstid, dsttitle, dstunipath, dstscanlasttime, dstlastmodifytime, dstotheroption, dstotheroption -> 'mount' ->> 'username' AS dm2_username, dstotheroption -> 'mount' ->> 'password' AS dm2_password FROM dm2_storage WHERE dstscanstatus = 0 AND ( dsttype = 'mix' OR dsttype = 'core' ) ''' ) gis_server_list = CFactory().give_me_db(self.get_mission_db_id()).all_row( ''' select dgsid, dgstitle, dgslastmodifytime from dp_gis_server ''' ) if dm2_storage_list.is_empty(): return CResult.merge_result(CResult.Success, '本次没有需要检查的入库存储!') if gis_server_list.is_empty(): return CResult.merge_result(CResult.Success, '本次没有需要检查的服务!') hostname = settings.application.xpath_one('data2service.system.connect.host', None) port = settings.application.xpath_one('data2service.system.connect.port', None) username = settings.application.xpath_one('data2service.system.connect.username', None) password = settings.application.xpath_one('data2service.system.connect.password', None) for data_index in range(dm2_storage_list.size()): storage_id = dm2_storage_list.value_by_name(data_index, 'dstid', '') storage_title = dm2_storage_list.value_by_name(data_index, 'dsttitle', '') storage_dstscanlasttime = dm2_storage_list.value_by_name(data_index, 'dstscanlasttime', None) storage_dstunipath = dm2_storage_list.value_by_name(data_index, 'dstunipath', '') storage_username = dm2_storage_list.value_by_name(data_index, 'dm2_username', '') storage_password = dm2_storage_list.value_by_name(data_index, 'dm2_password', '') CLogger().debug('正在检查和启动存储[{0}.{1}]的定时扫描...'.format(storage_id, storage_title)) for data_index in range(gis_server_list.size()): server_id = gis_server_list.value_by_name(data_index, 'dgsid', '') server_title = gis_server_list.value_by_name(data_index, 'dgstitle', '') server_dgslastmodifytime = gis_server_list.value_by_name(data_index, 'dgslastmodifytime', '') CLogger().debug('正在检查和启动存储[{0}.{1}]的定时扫描...'.format(server_id, server_title)) if storage_dstscanlasttime is not None: gis_storage_list = CFactory().give_me_db(self.get_mission_db_id()).all_row( ''' select dgsid, dgsserverid, dgsstorageid, dgsstoragelastcfgtime from dp_gis_storage where dgsstorageid = :storage_id and dgsserverid = :server_id ''', {'storage_id': storage_id, 'server_id': server_id} ) gis_storage_id = gis_storage_list.value_by_name(data_index, 'dgsid', '') # gis_storage_server_id = gis_storage_list.value_by_name(data_index, 'dgsserverid', '') # gis_storage_storage_id = gis_storage_list.value_by_name(data_index, 'dgsstorageid', '') gis_storage_dgslastmodifytime = gis_storage_list.value_by_name(data_index, 'dgslastmodifytime', '') if gis_storage_list.is_empty(): gis_storage_id = CUtils.one_id() database = CFactory().give_me_db(self.get_mission_db_id()) database.execute( ''' insert into dp_gis_storage( dgsid, dgsserverid, dgsstorageid, dgsstoragelastcfgtime, dgsstatus, dgsmountprocid, dgsmounturl, dgsmountmemo, dgsmemo, dgsdefinetype, dgsdefine, dgslastmodifytime ) VALUES( :dgsid, :dgsserverid, :dgsstorageid, :dgsstoragelastcfgtime, :dgsstatus, :dgsmountprocid, :dgsmounturl, :dgsmountmemo, :dgsmemo, :dgsdefinetype, :dgsdefine, :dgslastmodifytime ) ''', { 'dgsid': gis_storage_id, 'dgsserverid': server_id, 'dgsstorageid': storage_id, 'dgsstoragelastcfgtime': None, 'dgsstatus': 2, 'dgsmountprocid': None, 'dgsmounturl': None, 'dgsmountmemo': None, 'dgsmemo': None, 'dgsdefinetype': None, 'dgsdefine': None, 'dgslastmodifytime': None } ) CLogger().info("--------------begin mount---------------") try: mountcmd = "mount -t cifs {0} {1} -o username={2},password={3} " # 挂接 fstab_line = "echo '{0} {1} cifs defaults,username={2},password={3} 0 0' >> /etc/fstab" # 挂接 client = paramiko.SSHClient() # 启动ssh客户端 client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接linux服务器 client.connect(hostname=hostname, port=port, username=username, password=password) # 创建挂载点 win_path = storage_dstunipath.replace('\\', '/') linux_path = '/mnt/store_' + storage_id if not os.path.exists(linux_path): CLogger().info("to make dir " + linux_path) cmdin, cmdout, cmderr = client.exec_command("mkdir " + linux_path) if cmderr != None: error_msg = cmderr.read().decode('utf-8') if len(error_msg) > 1: CLogger().error(error_msg) # check if the linux_path mounted 看是否挂接 CLogger().info("check if the linux_path mounted " + linux_path) cmdin, cmdout, cmderr = client.exec_command('mount') if cmdout != None: mount_info = cmdout.read().decode('utf-8') if linux_path in mount_info: CLogger().info("the linux_path mounted " + linux_path) continue CLogger().info("to " + mountcmd.format(win_path, linux_path, storage_username, storage_password)) # 开始挂接 cmdin, cmdout, cmderr = client.exec_command( mountcmd.format(win_path, linux_path, storage_username, storage_password)) if cmderr != None: error_msg = cmderr.read().decode('utf-8') if len(error_msg) > 1: CLogger().error(error_msg) message = error_msg # echo into fstab CLogger().info(fstab_line.format(win_path, linux_path, username, password)) # 向fstab中写配置文件 cmdin, cmdout, cmderr = client.exec_command( fstab_line.format(win_path, linux_path, username, password)) message = 'mount success' CLogger().info('mount success') # 更新挂接状态 lastmodifytime = CTime.now() CFactory().give_me_db(self.get_mission_db_id()).execute( ''' update dp_gis_storage set dgsstatus = 1, dgsserverid = :server_id, dgsmounturl = :linux_path, dgslastmodifytime = :lastmodifytime, dgsmountmemo = :message where dgsid = :gis_storage_id ''', {'server_id': server_id, 'linux_path': linux_path, 'gis_storage_id': gis_storage_id, 'lastmodifytime': lastmodifytime, 'message': message} ) CFactory().give_me_db(self.get_mission_db_id()).execute( ''' update dm2_storage set dstlastmodifytime = :lastmodifytime where dstid = :storage_id ''', {'storage_id': storage_id, 'lastmodifytime': lastmodifytime} ) except Exception as error: message = 'mount服务[{0}.{1}]的状态过程出现异常! 错误信息为: {2}'.format(server_id, server_title, error.__str__()) CFactory().give_me_db(self.get_mission_db_id()).execute( ''' update dp_gis_storage set dgsstatus = 3, dgsmountmemo = :message where dgsid = :gis_storage_id ''', {'gis_storage_id': gis_storage_id, 'message': message} ) CLogger().error(message) else: if server_dgslastmodifytime is not None: if CUtils.equal_ignore_case(storage_dstscanlasttime, gis_storage_dgslastmodifytime): pass else: CFactory().give_me_db(self.get_mission_db_id()).execute( ''' update dp_gis_storage set dgsstatus = 2 where dgsid = :gis_storage_id ''', {'gis_storage_id': gis_storage_id} ) try: mountcmd = "mount -t cifs {0} {1} -o username={2},password={3} " # 挂接 fstab_line = "echo '{0} {1} cifs defaults,username={2},password={3} 0 0' >> /etc/fstab" # 挂接 client = paramiko.SSHClient() # 启动ssh客户端 client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接linux服务器 client.connect(hostname=hostname, port=port, username=username, password=password) # 创建挂载点 win_path = storage_dstunipath.replace('\\', '/') linux_path = '/mnt/store_' + storage_id if not os.path.exists(linux_path): CLogger().info("to make dir " + linux_path) cmdin, cmdout, cmderr = client.exec_command("mkdir " + linux_path) if cmderr != None: error_msg = cmderr.read().decode('utf-8') if len(error_msg) > 1: CLogger().debug(error_msg) # self.report_error(start_response, error_msg) # check if the linux_path mounted 看是否挂接 CLogger().info("check if the linux_path mounted " + linux_path) cmdin, cmdout, cmderr = client.exec_command('mount') if cmdout != None: mount_info = cmdout.read().decode('utf-8') if linux_path in mount_info: CLogger().info("the linux_path mounted " + linux_path) continue CLogger().info("to " + mountcmd.format(win_path, linux_path, storage_username, storage_password)) # 开始挂接 cmdin, cmdout, cmderr = client.exec_command( mountcmd.format(win_path, linux_path, storage_username, storage_password)) if cmderr != None: error_msg = cmderr.read().decode('utf-8') if len(error_msg) > 1: CLogger().error(error_msg) # echo into fstab CLogger().info(fstab_line.format(win_path, linux_path, username, password)) # 向fstab中写配置文件 cmdin, cmdout, cmderr = client.exec_command( fstab_line.format(win_path, linux_path, username, password)) message = 'mount success' # 更新挂接状态 lastmodifytime = CTime.now() CFactory().give_me_db(self.get_mission_db_id()).execute( ''' update dp_gis_storage set dgsstatus = 1, dgsserverid = :server_id, dgsmounturl = :linux_path, dgslastmodifytime = :lastmodifytime, dgsmountmemo = :message where dgsid = :gis_storage_id ''', {'server_id': server_id, 'linux_path': linux_path, 'gis_storage_id': gis_storage_id, 'lastmodifytime': lastmodifytime, 'message': message} ) CFactory().give_me_db(self.get_mission_db_id()).execute( ''' update dm2_storage set dstlastmodifytime = :lastmodifytime where dstid = :storage_id ''', {'storage_id': storage_id, 'lastmodifytime': lastmodifytime} ) CLogger().info(message) except Exception as error: message = 'mount服务[{0}.{1}]的状态过程出现异常! 错误信息为: {2}'.format(server_id, server_title, error.__str__()) CFactory().give_me_db(self.get_mission_db_id()).execute( ''' update dp_gis_storage set dgsstatus = 3, dgsmountmemo = :message where dgsid = :gis_storage_id ''', {'gis_storage_id': gis_storage_id, 'message': message} ) CLogger().debug(message) return CResult.merge_result(CResult.Success, '服务mount监控任务执行成功结束!')