def main()

in docker/executor.py [0:0]


def main():
    _LOG.debug(f'Creating directories')
    work_dir, metrics_dir, reports_dir = \
        os_service.create_work_dirs(job_id=JOB_ID)

    _LOG.debug(f'Describing job with id \'{JOB_ID}\'')
    job: Job = job_service.get_by_id(object_id=JOB_ID)

    if not job:
        _LOG.error(f'Job with id \'{JOB_ID}\' does not exist')
        raise ExecutorException(
            step_name=JOB_STEP_INITIALIZATION,
            reason=f'Job with id \'{JOB_ID}\' does not exist'
        )

    _LOG.debug(f'Setting job status to RUNNING')
    job = job_service.set_status(
        job=job,
        status=JobStatusEnum.JOB_RUNNING_STATUS.value)

    scan_tenants = job_service.get_scan_tenants(job=job)
    licensed_parent_id = job.parent_id
    licensed_parent = parent_service.get_parent_by_id(
        parent_id=licensed_parent_id)
    _LOG.debug(f'Parent: \'{licensed_parent_id}\'')
    if not licensed_parent or licensed_parent.is_deleted:
        _LOG.error(f'Parent \'{licensed_parent_id}\' does not exist')
        raise ExecutorException(
            step_name=JOB_STEP_INITIALIZATION,
            reason=f'Parent \'{licensed_parent_id}\' does not exist'
        )

    licensed_parent_meta = parent_service.get_parent_meta(
        parent=licensed_parent)
    license_key = licensed_parent_meta.license_key

    application_id = licensed_parent.application_id
    application = application_service.get_application_by_id(
        application_id=application_id)
    if not application or application.is_deleted:
        _LOG.error(f'Application \'{licensed_parent_id}\' does not exist')
        raise ExecutorException(
            step_name=JOB_STEP_INITIALIZATION,
            reason=f'Application \'{licensed_parent_id}\' does not exist'
        )
    application_meta = application_service.get_application_meta(
        application=application
    )
    algorithm_name = licensed_parent_meta.algorithm
    algorithm = algorithm_service.get_by_name(name=algorithm_name)
    _LOG.debug(f'Algorithm: \'{algorithm_name}\'')
    if not algorithm:
        _LOG.error(f'Application \'{application_id}\' does not have algorithm '
                   f'specified')
        raise ExecutorException(
            step_name=JOB_STEP_INITIALIZATION,
            reason=f'Application \'{application_id}\' does not have algorithm '
                   f'specified'
        )

    input_storage_name = application_meta.input_storage
    _LOG.debug(f'Input storage: \'{input_storage_name}\'')
    input_storage: Storage = storage_service.get_by_name(
        name=input_storage_name)
    if not input_storage:
        _LOG.error(f'Input storage \'{input_storage_name}\' does not exist.')
        raise ExecutorException(
            step_name=JOB_STEP_INITIALIZATION,
            reason=f'Input storage \'{input_storage_name}\' does not exist.'
        )

    output_storage_name = application_meta.output_storage
    _LOG.debug(f'Output storage: \'{output_storage_name}\'')
    output_storage: Storage = storage_service.get_by_name(
        name=output_storage_name)
    if not output_storage:
        _LOG.error(f'Output storage \'{output_storage_name}\' does not exist.')
        raise ExecutorException(
            step_name=JOB_STEP_INITIALIZATION,
            reason=f'Output storage \'{output_storage_name}\' does not exist.'
        )

    _LOG.info(f'Resolving RIGHTSIZER parent for license')
    parent = parent_service.resolve(
        licensed_parent=licensed_parent,
        scan_tenants=scan_tenants
    )
    if not parent:
        _LOG.error(f'Can\'t resolve RIGHTSIZER parent for license '
                   f'\'{licensed_parent_id}\'. Shape rules won\'t be applied.')
        parent_meta = ParentMeta()
    else:
        parent_meta = parent_service.get_parent_meta(parent=parent)

    _LOG.debug(f'Describing License \'{license_key}\'')
    license_: License = license_service.get_license(license_id=license_key)

    for tenant in scan_tenants:
        try:
            _LOG.info(f'Processing tenant {tenant}')
            process_tenant_instances(
                metrics_dir=metrics_dir,
                reports_dir=reports_dir,
                input_storage=input_storage,
                output_storage=output_storage,
                parent_meta=parent_meta,
                licensed_parent=licensed_parent,
                algorithm=algorithm,
                license_=license_,
                customer=parent.customer_id,
                tenant=tenant,
                job=job
            )
        except LicenseForbiddenException as e:
            _LOG.error(e)
            job_service.set_licensed_job_status(
                job=job,
                tenant=tenant,
                status=JobTenantStatusEnum.TENANT_FAILED_STATUS
            )
        except Exception as e:
            _LOG.error(f'Unexpected error occurred while processing '
                       f'tenant {tenant}: {e}')
            job_service.set_licensed_job_status(
                job=job,
                tenant=tenant,
                status=JobTenantStatusEnum.TENANT_FAILED_STATUS
            )

    _LOG.debug(f'Job {JOB_ID} has finished successfully')
    _LOG.debug(f'Setting job state to SUCCEEDED')
    job_service.set_status(job=job,
                           status=JobStatusEnum.JOB_SUCCEEDED_STATUS.value)

    if os.path.exists(PROFILE_LOG_PATH):
        _LOG.debug(f'Uploading profile log')
        storage_service.upload_profile_log(
            storage=output_storage,
            job_id=JOB_ID,
            file_path=PROFILE_LOG_PATH
        )
    _LOG.debug(f'Cleaning workdir')
    os_service.clean_workdir(work_dir=work_dir)