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, 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)
# # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>. import logging from flask import abort, jsonify, current_app from cmdb.exportd.exportd_job.exportd_job_manager import ExportdJobManagement from cmdb.utils.helpers import load_class, get_module_classes from cmdb.interface.route_utils import make_response, login_required from cmdb.interface.blueprint import RootBlueprint from cmdb.utils.error import CMDBError with current_app.app_context(): exportd_manager = ExportdJobManagement(current_app.database_manager) LOGGER = logging.getLogger(__name__) external_system = RootBlueprint('external_system', __name__, url_prefix='/externalsystem') # DEFAULT ROUTES @external_system.route('/', methods=['GET']) @login_required def get_external_system_list(): return make_response( get_module_classes('cmdb.exportd.externals.external_systems'))
from cmdb.exportd.exportd_logs.exportd_log import ExportdJobLog, LogAction, ExportdMetaLog 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)
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