Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,243 additions
and
530 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# | ||
# Copyright (c) 2013-2014 QuarksLab. | ||
# This file is part of IRMA project. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License in the top-level directory | ||
# of this distribution and at: | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# No part of the project, including this file, may be copied, | ||
# modified, propagated, or distributed except according to the | ||
# terms contained in the LICENSE file. | ||
|
||
import celery | ||
import config.parser as config | ||
from brain.helpers.celerytasks import async_call | ||
|
||
frontend_app = celery.Celery('frontendtasks') | ||
config.conf_frontend_celery(frontend_app) | ||
config.configure_syslog(frontend_app) | ||
|
||
|
||
def scan_launched(frontend_scanid): | ||
async_call(frontend_app, | ||
"frontend.tasks", | ||
"scan_launched", | ||
args=[frontend_scanid]) | ||
|
||
|
||
def scan_result(frontend_scanid, filename, probe, result): | ||
async_call(frontend_app, | ||
"frontend.tasks", | ||
"scan_result", | ||
args=[frontend_scanid, filename, probe, result]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# | ||
# Copyright (c) 2013-2014 QuarksLab. | ||
# This file is part of IRMA project. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License in the top-level directory | ||
# of this distribution and at: | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# No part of the project, including this file, may be copied, | ||
# modified, propagated, or distributed except according to the | ||
# terms contained in the LICENSE file. | ||
|
||
from lib.irma.ftp.handler import FtpTls | ||
import config.parser as config | ||
|
||
|
||
def flush_dir(ftpuser, scanid): | ||
print("Flushing dir {0}".format(scanid)) | ||
conf_ftp = config.brain_config['ftp_brain'] | ||
with FtpTls(conf_ftp.host, | ||
conf_ftp.port, | ||
conf_ftp.username, | ||
conf_ftp.password) as ftps: | ||
ftps.deletepath("{0}/{1}".format(ftpuser, scanid), deleteParent=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# | ||
# Copyright (c) 2013-2014 QuarksLab. | ||
# This file is part of IRMA project. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License in the top-level directory | ||
# of this distribution and at: | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# No part of the project, including this file, may be copied, | ||
# modified, propagated, or distributed except according to the | ||
# terms contained in the LICENSE file. | ||
|
||
from brain.controllers import scanctrl | ||
from brain.models.sqlobjects import Job | ||
from brain.helpers.sql import session_query, session_transaction | ||
from lib.common.compat import timestamp | ||
|
||
|
||
def new(scan_id, filename, probe): | ||
with session_transaction() as session: | ||
j = Job(filename, probe, scan_id) | ||
j.save(session) | ||
session.commit() | ||
return j.id | ||
|
||
|
||
def _finish(job_id, status): | ||
with session_transaction() as session: | ||
job = Job.load(job_id, session) | ||
job.status = status | ||
job.ts_end = timestamp() | ||
job.update(['status', 'ts_end'], session) | ||
scan_id = job.scan.id | ||
scanctrl.check_finished(scan_id) | ||
|
||
|
||
def success(job_id): | ||
_finish(job_id, Job.success) | ||
|
||
|
||
def error(job_id): | ||
_finish(job_id, Job.error) | ||
|
||
|
||
def set_taskid(job_id, task_id): | ||
with session_transaction() as session: | ||
job = Job.load(job_id, session) | ||
job.task_id = task_id | ||
|
||
|
||
def info(job_id): | ||
with session_query() as session: | ||
job = Job.load(job_id, session) | ||
frontend_scan_id = job.scan.scan_id | ||
filename = job.filename | ||
probe = job.probename | ||
return (frontend_scan_id, filename, probe) | ||
|
||
|
||
def duration(job_id): | ||
with session_query() as session: | ||
job = Job.load(job_id, session) | ||
return (job.ts_end - job.ts_start) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# | ||
# Copyright (c) 2013-2014 QuarksLab. | ||
# This file is part of IRMA project. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License in the top-level directory | ||
# of this distribution and at: | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# No part of the project, including this file, may be copied, | ||
# modified, propagated, or distributed except according to the | ||
# terms contained in the LICENSE file. | ||
|
||
import celery | ||
import time | ||
import config.parser as config | ||
from brain.helpers.celerytasks import route, async_call | ||
|
||
|
||
results_app = celery.Celery('resultstasks') | ||
config.conf_results_celery(results_app) | ||
|
||
probe_app = celery.Celery('probetasks') | ||
config.conf_results_celery(probe_app) | ||
|
||
# Time to cache the probe list | ||
# to avoid asking to rabbitmq | ||
PROBELIST_CACHE_TIME = 60 | ||
cache_probelist = {'list': None, 'time': None} | ||
|
||
|
||
def get_probelist(): | ||
# get active queues list from probe celery app | ||
now = time.time() | ||
result_queue = config.brain_config['broker_probe'].queue | ||
if cache_probelist['time'] is not None: | ||
cache_time = now - cache_probelist['time'] | ||
if cache_probelist['time'] is None or cache_time > PROBELIST_CACHE_TIME: | ||
slist = list() | ||
i = probe_app.control.inspect() | ||
queues = i.active_queues() | ||
if queues: | ||
for infolist in queues.values(): | ||
for info in infolist: | ||
if info['name'] not in slist: | ||
# exclude only predefined result queue | ||
if info['name'] != result_queue: | ||
slist.append(info['name']) | ||
if len(slist) != 0: | ||
# activate cache only on non empty list | ||
cache_probelist['time'] = now | ||
cache_probelist['list'] = slist | ||
return cache_probelist['list'] | ||
|
||
|
||
# ============ | ||
# Task calls | ||
# ============ | ||
|
||
def job_launch(ftpuser, frontend_scanid, filename, probe, job_id): | ||
""" send a task to the brain to flush the scan files""" | ||
hook_success = route( | ||
results_app.signature("brain.tasks.job_success", | ||
[job_id])) | ||
hook_error = route( | ||
results_app.signature("brain.tasks.job_error", | ||
[job_id])) | ||
task = async_call(probe_app, | ||
"probe.tasks", | ||
"probe_scan", | ||
args=(ftpuser, frontend_scanid, filename), | ||
queue=probe, | ||
link=hook_success, | ||
link_error=hook_error) | ||
return task.id | ||
|
||
|
||
def job_cancel(job_list): | ||
probe_app.control.revoke(job_list, terminate=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# | ||
# Copyright (c) 2013-2014 QuarksLab. | ||
# This file is part of IRMA project. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License in the top-level directory | ||
# of this distribution and at: | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# No part of the project, including this file, may be copied, | ||
# modified, propagated, or distributed except according to the | ||
# terms contained in the LICENSE file. | ||
|
||
from brain.models.sqlobjects import Scan | ||
from brain.helpers.sql import session_query, session_transaction | ||
from lib.irma.common.utils import IrmaScanStatus | ||
from lib.irma.common.exceptions import IrmaValueError | ||
|
||
|
||
def new(frontend_scan_id, user_id, nb_files): | ||
with session_transaction() as session: | ||
scan = Scan(frontend_scan_id, user_id, nb_files) | ||
scan.save(session) | ||
session.commit() | ||
return scan.id | ||
|
||
|
||
def get_scan_id(frontend_scan_id, user_id): | ||
with session_query() as session: | ||
scan = Scan.get_scan(frontend_scan_id, user_id, session) | ||
return scan.id | ||
|
||
|
||
def get_nbjobs(scan_id): | ||
with session_query() as session: | ||
scan = Scan.load(scan_id, session) | ||
return scan.nb_jobs | ||
|
||
|
||
def _set_status(scan_id, code): | ||
with session_transaction() as session: | ||
scan = Scan.load(scan_id, session) | ||
scan.status = code | ||
session.commit() | ||
|
||
|
||
def cancelling(scan_id): | ||
_set_status(scan_id, IrmaScanStatus.cancelling) | ||
|
||
|
||
def cancelled(scan_id): | ||
_set_status(scan_id, IrmaScanStatus.cancelled) | ||
|
||
|
||
def launched(scan_id): | ||
_set_status(scan_id, IrmaScanStatus.launched) | ||
|
||
|
||
def progress(scan_id): | ||
with session_query() as session: | ||
scan = Scan.load(scan_id, session) | ||
if IrmaScanStatus.is_error(scan.status): | ||
status_str = IrmaScanStatus.label[scan.status] | ||
raise IrmaValueError(status_str) | ||
status = IrmaScanStatus.label[scan.status] | ||
progress_details = None | ||
if scan.status == IrmaScanStatus.launched: | ||
(total, finished, success) = scan.progress() | ||
progress_details = {} | ||
progress_details['total'] = total | ||
progress_details['finished'] = finished | ||
progress_details['successful'] = success | ||
return (status, progress_details) | ||
|
||
|
||
def get_pending_jobs(scan_id): | ||
with session_query() as session: | ||
scan = Scan.load(scan_id, session) | ||
return scan.get_pending_jobs_taskid() | ||
|
||
|
||
def check_finished(scan_id): | ||
with session_transaction() as session: | ||
scan = Scan.load(scan_id, session) | ||
if scan.status == IrmaScanStatus.processed: | ||
return True | ||
if scan.finished(): | ||
scan.status = IrmaScanStatus.processed | ||
return True | ||
return False | ||
|
||
|
||
def flush(scan_id): | ||
with session_transaction() as session: | ||
scan = Scan.load(scan_id, session) | ||
if scan.status == IrmaScanStatus.flushed: | ||
return | ||
for job in scan.jobs: | ||
session.delete(job) | ||
scan.status = IrmaScanStatus.flushed | ||
|
||
|
||
def error(scan_id, code): | ||
with session_transaction() as session: | ||
scan = Scan.load(scan_id, session) | ||
scan.status = code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# | ||
# Copyright (c) 2013-2014 QuarksLab. | ||
# This file is part of IRMA project. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License in the top-level directory | ||
# of this distribution and at: | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# No part of the project, including this file, may be copied, | ||
# modified, propagated, or distributed except according to the | ||
# terms contained in the LICENSE file. | ||
|
||
from brain.models.sqlobjects import User | ||
from brain.helpers.sql import session_query, session_transaction | ||
|
||
|
||
def get_userid(rmqvhost): | ||
with session_query() as session: | ||
user = User.get_by_rmqvhost(rmqvhost, session) | ||
return user.id | ||
|
||
|
||
def get_quota(user_id): | ||
with session_query() as session: | ||
user = User.load(user_id, session) | ||
quota = user.quota | ||
remaining = None | ||
if quota is not None: | ||
remaining = user.remaining_quota(session) | ||
return (remaining, quota) | ||
|
||
|
||
def get_ftpuser(user_id): | ||
with session_query() as session: | ||
user = User.load(user_id, session) | ||
return user.ftpuser |
Empty file.
Oops, something went wrong.