def __init__(self, job: ExportdJob): self.job = job self.exportvars = self.__get_exportvars() self.destinations = self.__get__destinations() scr = SystemConfigReader() database_manager = DatabaseManagerMongo( **scr.get_all_values_from_section('Database')) self.__object_manager = CmdbObjectManager( database_manager=database_manager) self.log_manager = ExportdLogManager(database_manager=database_manager) self.sources = self.__get_sources() super(ExportdManagerBase, self).__init__(database_manager)
def __init__(self, event, state=False): super(ExportdThread, self).__init__() self.job = None self.job_id = event.get_param("id") self.type_id = event.get_param("type_id") self.user_id = event.get_param("user_id") self.event = event self.is_active = state self.exception_handling = None scr = SystemConfigReader() database_options = scr.get_all_values_from_section('Database') self.__dbm = DatabaseManagerMongo(**database_options) self.log_manager = ExportdLogManager(database_manager=self.__dbm) self.exportd_job_manager = ExportdJobManagement( database_manager=self.__dbm) self.user_manager = UserManager(database_manager=self.__dbm)
def __init__(self): scr = SystemConfigReader() self.database_manager = DatabaseManagerMongo( **scr.get_all_values_from_section('Database')) self.object_manager = CmdbObjectManager( database_manager=self.database_manager) self.log_manager = ExportdLogManager( database_manager=self.database_manager) super().__init__(self.database_manager)
def __init__(self, event: Event, state: bool = False): scr = SystemConfigReader() database_options = scr.get_all_values_from_section('Database') database = DatabaseManagerMongo(**database_options) super(ExportdThread, self).__init__() self.job = None self.job_id = int(event.get_param("id")) self.type_id = int( event.get_param("type_id")) if event.get_param("type_id") else None self.user_id = int(event.get_param("user_id")) self.event = event self.is_active = state self.exception_handling = None self.object_manager = CmdbObjectManager(database_manager=database) self.log_manager = ExportdLogManager(database_manager=database) self.exportd_job_manager = ExportdJobManagement( database_manager=database) self.user_manager = UserManager(database_manager=database)
from cmdb.framework.results import IterationResult from cmdb.interface.api_parameters import CollectionParameters from cmdb.interface.response import GetMultiResponse from cmdb.interface.rest_api.exportd_routes import exportd_blueprint from cmdb.interface.route_utils import make_response, insert_request_user, login_required, right_required from cmdb.interface.blueprint import RootBlueprint from cmdb.manager import ManagerIterationError, ManagerGetError from cmdb.user_management import UserModel from cmdb.utils.error import CMDBError LOGGER = logging.getLogger(__name__) exportd_log_blueprint = RootBlueprint('exportd_log_rest', __name__, url_prefix='/exportdlog') with current_app.app_context(): exportd_manager = ExportdJobManagement(current_app.database_manager, current_app.event_queue) log_manager = ExportdLogManager(current_app.database_manager) @exportd_blueprint.route('/logs', methods=['GET', 'HEAD']) @exportd_blueprint.protect(auth=True, right='base.exportd.log.view') @exportd_blueprint.parse_collection_parameters() def get_exportd_logs(params: CollectionParameters): """Iterate route for exportd logs""" log_manager = ExportDLogManager(current_app.database_manager) body = request.method == 'HEAD' try: iteration_result: IterationResult[ExportdJobLog] = log_manager.iterate( filter=params.filter, limit=params.limit, skip=params.skip, sort=params.sort, order=params.order) types = [ExportdJobLog.to_json(type) for type in iteration_result.results]
class ExportdManagerBase(ExportdJobManagement): def __init__(self, job: ExportdJob): self.job = job self.exportvars = self.__get_exportvars() self.destinations = self.__get__destinations() scr = SystemConfigReader() database_manager = DatabaseManagerMongo( **scr.get_all_values_from_section('Database')) self.__object_manager = CmdbObjectManager( database_manager=database_manager) self.log_manager = ExportdLogManager(database_manager=database_manager) self.sources = self.__get_sources() super(ExportdManagerBase, self).__init__(database_manager) def __get_exportvars(self): exportvars = {} for variable in self.job.get_variables(): exportvars.update({ variable["name"]: ExportVariable(variable["name"], variable["default"], variable["templates"]) }) return exportvars def __get_sources(self): sources = [] sources.append( ExportSource(self.job, object_manager=self.__object_manager)) return sources def __get__destinations(self): destinations = [] destination_params = {} for destination in self.job.get_destinations(): for param in destination["parameter"]: destination_params.update({param["name"]: param["value"]}) export_destination = ExportDestination( "cmdb.exportd.externals.external_systems.{}".format( destination["className"]), destination_params, self.exportvars) destinations.append(export_destination) return destinations def execute(self, event, user_id: int, user_name: str, log_flag: bool = True) -> ExportdHeader: # get cmdb objects from all sources cmdb_objects = set() exportd_header = ExportdHeader() for source in self.sources: cmdb_objects.update(source.get_objects()) # for every destination: do export for destination in self.destinations: external_system = destination.get_external_system() external_system.prepare_export() for cmdb_object in cmdb_objects: # setup objectdata for use in ExportVariable templates template_data = ObjectTemplateData( self.__object_manager, cmdb_object).get_template_data() external_system.add_object(cmdb_object, template_data) exportd_header = external_system.finish_export() if log_flag: try: log_params = { 'job_id': self.job.get_public_id(), 'state': True, 'user_id': user_id, 'user_name': user_name, 'event': event.get_type(), 'message': external_system.msg_string, } self.log_manager.insert_log( action=LogAction.EXECUTE, log_type=ExportdJobLog.__name__, **log_params) except LogManagerInsertError as err: LOGGER.error(err) return exportd_header
class ExportdThread(Thread): def __init__(self, event, state=False): super(ExportdThread, self).__init__() self.job = None self.job_id = event.get_param("id") self.type_id = event.get_param("type_id") self.user_id = event.get_param("user_id") self.event = event self.is_active = state self.exception_handling = None scr = SystemConfigReader() database_options = scr.get_all_values_from_section('Database') self.__dbm = DatabaseManagerMongo(**database_options) self.log_manager = ExportdLogManager(database_manager=self.__dbm) self.exportd_job_manager = ExportdJobManagement( database_manager=self.__dbm) self.user_manager = UserManager(database_manager=self.__dbm) def run(self): try: if self.type_id: for obj in self.exportd_job_manager.get_job_by_event_based( True): if next((item for item in obj.get_sources() if item["type_id"] == self.type_id), None): if obj.get_active( ) and obj.scheduling["event"]["active"]: self.job = obj self.worker() elif self.is_active: self.job = self.exportd_job_manager.get_job(self.job_id) self.worker() except Exception as ex: LOGGER.error(ex) return ex def worker(self): cur_user = None try: # update job for UI self.job.state = ExecuteState.RUNNING.name self.job.last_execute_date = datetime.utcnow() # get current user cur_user = self.user_manager.get(self.user_id) self.exportd_job_manager.update_job(self.job, self.user_manager.get( self.user_id), event_start=False) # execute Exportd job job = cmdb.exportd.exporter_base.ExportdManagerBase(self.job) job.execute(self.event, cur_user.get_public_id(), cur_user.get_display_name()) except Exception as err: LOGGER.error(err) self.exception_handling = err # Generate Error log try: log_params = { 'job_id': self.job.get_public_id(), 'state': False, 'user_id': cur_user.get_public_id(), 'user_name': cur_user.get_display_name(), 'event': self.event.get_type(), 'message': ['Successful'] if not err else err.args, } self.log_manager.insert_log(action=LogAction.EXECUTE, log_type=ExportdJobLog.__name__, **log_params) except LogManagerInsertError as err: LOGGER.error(err) finally: # update job for UI self.job.state = ExecuteState.SUCCESSFUL.name if not self.exception_handling else ExecuteState.FAILED.name self.exportd_job_manager.update_job(self.job, self.user_manager.get( self.user_id), event_start=False)
def create_rest_api(event_queue): from cmdb.interface.config import app_config from cmdb.utils.system_config import SystemConfigReader system_config_reader = SystemConfigReader() # Create managers from cmdb.data_storage.database_manager import DatabaseManagerMongo app_database = DatabaseManagerMongo( **system_config_reader.get_all_values_from_section('Database')) object_manager = CmdbObjectManager(database_manager=app_database, event_queue=event_queue) log_manager = CmdbLogManager(database_manager=app_database) security_manager = SecurityManager(database_manager=app_database) user_manager = UserManager(database_manager=app_database) exportd_job_manager = ExportdJobManagement(database_manager=app_database, event_queue=event_queue) exportd_log_manager = ExportdLogManager(database_manager=app_database) media_file_manager = MediaFileManagement(database_manager=app_database) docapi_tpl_manager = DocapiTemplateManager(database_manager=app_database) # Create APP from cmdb.interface.cmdb_app import BaseCmdbApp app = BaseCmdbApp(__name__, database_manager=app_database, docapi_tpl_manager=docapi_tpl_manager, media_file_manager=media_file_manager, exportd_manager=exportd_job_manager, exportd_log_manager=exportd_log_manager, object_manager=object_manager, log_manager=log_manager, user_manager=user_manager, security_manager=security_manager) app.url_map.strict_slashes = True # Import App Extensions from flask_cors import CORS CORS(app, expose_headers=['X-API-Version', 'X-Total-Count']) import cmdb if cmdb.__MODE__ == 'DEBUG': config = app_config['development'] config.APPLICATION_ROOT = '/rest/' app.config.from_object(config) else: config = app_config['production'] config.APPLICATION_ROOT = '/rest/' app.config.from_object(config) with app.app_context(): register_converters(app) register_error_pages(app) register_blueprints(app) return app
def create_rest_api(event_queue): from cmdb.interface.config import app_config from cmdb.utils.system_config import SystemConfigReader system_config_reader = SystemConfigReader() try: cache_config = { 'DEBUG': True, 'CACHE_TYPE': system_config_reader.get_value('name', 'Cache'), 'CACHE_REDIS_HOST': system_config_reader.get_value('host', 'Cache'), 'CACHE_REDIS_PORT': system_config_reader.get_value('port', 'Cache'), 'CACHE_REDIS_PASSWORD': system_config_reader.get_value('password', 'Cache'), } except (ImportError, CMDBError) as e: LOGGER.debug(e.message) cache_config = {'CACHE_TYPE': 'simple'} from flask_caching import Cache cache = Cache(config=cache_config) # Create manager from cmdb.data_storage.database_manager import DatabaseManagerMongo app_database = DatabaseManagerMongo( **system_config_reader.get_all_values_from_section('Database')) object_manager = CmdbObjectManager(database_manager=app_database, event_queue=event_queue) log_manager = CmdbLogManager(database_manager=app_database) security_manager = SecurityManager(database_manager=app_database) user_manager = UserManager(database_manager=app_database) exportd_job_manager = ExportdJobManagement(database_manager=app_database, event_queue=event_queue) exportd_log_manager = ExportdLogManager(database_manager=app_database) media_file_manager = MediaFileManagement(database_manager=app_database) docapi_tpl_manager = DocapiTemplateManager(database_manager=app_database) # Create APP from cmdb.interface.cmdb_app import BaseCmdbApp app = BaseCmdbApp(__name__, database_manager=app_database, docapi_tpl_manager=docapi_tpl_manager, media_file_manager=media_file_manager, exportd_manager=exportd_job_manager, exportd_log_manager=exportd_log_manager, object_manager=object_manager, log_manager=log_manager, user_manager=user_manager, security_manager=security_manager) # Import App Extensions from flask_cors import CORS CORS(app) import cmdb cache.init_app(app) cache.clear() app.cache = cache if cmdb.__MODE__ == 'DEBUG': app.config.from_object(app_config['rest_development']) LOGGER.info('RestAPI starting with config mode {}'.format( app.config.get("ENV"))) elif cmdb.__MODE__ == 'TESTING': app.config.from_object(app_config['testing']) else: app.config.from_object(app_config['rest']) LOGGER.info('RestAPI starting with config mode {}'.format( app.config.get("ENV"))) with app.app_context(): register_converters(app) register_error_pages(app) register_blueprints(app) return app