2021-08-04 15:07:59 +00:00
|
|
|
from flask import current_app
|
2021-02-01 11:51:07 +00:00
|
|
|
from ..models import Corpus
|
2020-11-09 15:14:19 +00:00
|
|
|
import docker
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
|
2021-02-01 11:51:07 +00:00
|
|
|
class CheckCorporaMixin:
|
|
|
|
def check_corpora(self):
|
|
|
|
corpora = Corpus.query.all()
|
2021-07-16 11:07:01 +00:00
|
|
|
queued_corpora = list(filter(lambda corpus: corpus.status == 'queued', corpora)) # noqa
|
|
|
|
running_corpora = list(filter(lambda corpus: corpus.status == 'running', corpora)) # noqa
|
|
|
|
start_analysis_corpora = list(filter(lambda corpus: corpus.status == 'start analysis', corpora)) # noqa
|
|
|
|
analysing_corpora = list(filter(lambda corpus: corpus.status == 'analysing', corpora)) # noqa
|
|
|
|
stop_analysis_corpora = list(filter(lambda corpus: corpus.status == 'stop analysis', corpora)) # noqa
|
|
|
|
submitted_corpora = list(filter(lambda corpus: corpus.status == 'submitted', corpora)) # noqa
|
2021-02-01 11:51:07 +00:00
|
|
|
for corpus in submitted_corpora:
|
|
|
|
self.create_build_corpus_service(corpus)
|
|
|
|
for corpus in queued_corpora + running_corpora:
|
|
|
|
self.checkout_build_corpus_service(corpus)
|
|
|
|
for corpus in start_analysis_corpora:
|
|
|
|
self.create_cqpserver_container(corpus)
|
2021-07-16 11:07:01 +00:00
|
|
|
for corpus in analysing_corpora:
|
|
|
|
self.checkout_analysing_corpus_container(corpus)
|
2021-02-01 11:51:07 +00:00
|
|
|
for corpus in stop_analysis_corpora:
|
|
|
|
self.remove_cqpserver_container(corpus)
|
2020-11-09 15:14:19 +00:00
|
|
|
|
2021-02-01 11:51:07 +00:00
|
|
|
def create_build_corpus_service(self, corpus):
|
2021-08-04 15:07:59 +00:00
|
|
|
''' # Docker service settings # '''
|
|
|
|
''' ## Command ## '''
|
|
|
|
command = 'docker-entrypoint.sh build-corpus'
|
|
|
|
''' ## Constraints ## '''
|
|
|
|
constraints = ['node.role==worker']
|
|
|
|
''' ## Image ## '''
|
|
|
|
image = current_app.config['DOCKER_IMAGE_PREFIX'] + 'cqpserver:latest'
|
|
|
|
''' ## Labels ## '''
|
|
|
|
labels = {
|
|
|
|
'origin': current_app.config['SERVER_NAME'],
|
|
|
|
'type': 'build-corpus',
|
|
|
|
'corpus_id': str(corpus.id)
|
2021-02-01 11:51:07 +00:00
|
|
|
}
|
2021-08-04 15:07:59 +00:00
|
|
|
''' ## Mounts ## '''
|
|
|
|
''' ### Corpus file mount ### '''
|
|
|
|
corpus_file_source = os.path.join(corpus.path, 'merged', 'corpus.vrt')
|
|
|
|
corpus_file_target = '/root/files/corpus.vrt'
|
|
|
|
corpus_file_mount = \
|
|
|
|
corpus_file_source + ':' + corpus_file_target + ':ro'
|
|
|
|
''' ### Corpus data mount ### '''
|
|
|
|
corpus_data_source = os.path.join(corpus.path, 'data')
|
|
|
|
corpus_data_target = '/corpora/data'
|
|
|
|
corpus_data_mount = \
|
|
|
|
corpus_data_source + ':' + corpus_data_target + ':rw'
|
|
|
|
# Make sure that their is no data in the corpus data directory
|
|
|
|
shutil.rmtree(corpus_data_source, ignore_errors=True)
|
|
|
|
os.mkdir(corpus_data_source)
|
|
|
|
''' ### Corpus registry mount ### '''
|
|
|
|
corpus_registry_source = os.path.join(corpus.path, 'registry')
|
|
|
|
corpus_registry_target = '/usr/local/share/cwb/registry'
|
|
|
|
corpus_registry_mount = \
|
|
|
|
corpus_registry_source + ':' + corpus_registry_target + ':rw'
|
|
|
|
# Make sure that their is no data in the corpus registry directory
|
|
|
|
shutil.rmtree(corpus_registry_source, ignore_errors=True)
|
|
|
|
os.mkdir(corpus_registry_source)
|
|
|
|
mounts = [corpus_file_mount, corpus_data_mount, corpus_registry_mount]
|
|
|
|
''' ## Name ## '''
|
|
|
|
name = 'build-corpus_{}'.format(corpus.id)
|
|
|
|
''' ## Restart policy ## '''
|
|
|
|
restart_policy = docker.types.RestartPolicy()
|
2021-02-01 11:51:07 +00:00
|
|
|
try:
|
2021-08-04 15:07:59 +00:00
|
|
|
self.docker.services.create(
|
|
|
|
image,
|
|
|
|
command=command,
|
|
|
|
constraints=constraints,
|
|
|
|
labels=labels,
|
|
|
|
mounts=mounts,
|
|
|
|
name=name,
|
|
|
|
restart_policy=restart_policy
|
|
|
|
)
|
2021-02-01 11:51:07 +00:00
|
|
|
except docker.errors.APIError as e:
|
|
|
|
logging.error(
|
2021-08-04 15:07:59 +00:00
|
|
|
'Create "{}" service raised '.format(name)
|
2021-02-01 11:51:07 +00:00
|
|
|
+ '"docker.errors.APIError" The server returned an error. '
|
|
|
|
+ 'Details: {}'.format(e)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
corpus.status = 'queued'
|
|
|
|
|
|
|
|
def checkout_build_corpus_service(self, corpus):
|
|
|
|
service_name = 'build-corpus_{}'.format(corpus.id)
|
|
|
|
try:
|
|
|
|
service = self.docker.services.get(service_name)
|
|
|
|
except docker.errors.NotFound:
|
|
|
|
logging.error(
|
|
|
|
'Get "{}" service raised '.format(service_name)
|
|
|
|
+ '"docker.errors.NotFound" The service does not exist. '
|
|
|
|
+ '(corpus.status: {} -> failed)'.format(corpus.status)
|
|
|
|
)
|
|
|
|
corpus.status = 'failed'
|
|
|
|
except docker.errors.APIError as e:
|
|
|
|
logging.error(
|
|
|
|
'Get "{}" service raised '.format(service_name)
|
|
|
|
+ '"docker.errors.APIError" The server returned an error. '
|
|
|
|
+ 'Details: {}'.format(e)
|
|
|
|
)
|
|
|
|
except docker.errors.InvalidVersion:
|
|
|
|
logging.error(
|
|
|
|
'Get "{}" service raised '.format(service_name)
|
|
|
|
+ '"docker.errors.InvalidVersion" One of the arguments is '
|
|
|
|
+ 'not supported with the current API version.'
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
service_tasks = service.tasks()
|
|
|
|
if not service_tasks:
|
|
|
|
return
|
|
|
|
task_state = service_tasks[0].get('Status').get('State')
|
|
|
|
if corpus.status == 'queued' and task_state != 'pending':
|
|
|
|
corpus.status = 'running'
|
|
|
|
elif (corpus.status == 'running'
|
|
|
|
and task_state in ['complete', 'failed']):
|
|
|
|
try:
|
|
|
|
service.remove()
|
|
|
|
except docker.errors.APIError as e:
|
|
|
|
logging.error(
|
|
|
|
'Remove "{}" service raised '.format(service_name)
|
2021-08-04 15:07:59 +00:00
|
|
|
+ '"docker.errors.APIError" The server returned an error. ' # noqa
|
2021-02-01 11:51:07 +00:00
|
|
|
+ 'Details: {}'.format(e)
|
|
|
|
)
|
|
|
|
return
|
|
|
|
else:
|
2021-08-04 15:07:59 +00:00
|
|
|
corpus.status = \
|
|
|
|
'prepared' if task_state == 'complete' else 'failed'
|
2020-11-09 15:14:19 +00:00
|
|
|
|
2021-02-01 11:51:07 +00:00
|
|
|
def create_cqpserver_container(self, corpus):
|
2021-08-04 15:07:59 +00:00
|
|
|
''' # Docker container settings # '''
|
|
|
|
''' ## Command ## '''
|
|
|
|
command = 'cqpserver'
|
|
|
|
''' ## Detach ## '''
|
|
|
|
detach = True
|
|
|
|
''' ## Image ## '''
|
|
|
|
image = current_app.config['DOCKER_IMAGE_PREFIX'] + 'cqpserver:latest'
|
|
|
|
''' ## Name ## '''
|
2021-08-16 10:09:50 +00:00
|
|
|
name = 'cqpserver_{}'.format(corpus.id)
|
2021-08-04 15:07:59 +00:00
|
|
|
''' ## Network ## '''
|
|
|
|
network = 'nopaque_default'
|
|
|
|
''' ## Volumes ## '''
|
|
|
|
''' ### Corpus data volume ### '''
|
|
|
|
corpus_data_source = os.path.join(corpus.path, 'data')
|
|
|
|
corpus_data_target = '/corpora/data'
|
|
|
|
corpus_data_volume = \
|
|
|
|
corpus_data_source + ':' + corpus_data_target + ':rw'
|
|
|
|
''' ### Corpus registry volume ### '''
|
|
|
|
corpus_registry_source = os.path.join(corpus.path, 'registry')
|
|
|
|
corpus_registry_target = '/usr/local/share/cwb/registry'
|
|
|
|
corpus_registry_volume = \
|
|
|
|
corpus_registry_source + ':' + corpus_registry_target + ':rw'
|
|
|
|
volumes = [corpus_data_volume, corpus_registry_volume]
|
2021-02-01 11:51:07 +00:00
|
|
|
# Check if a cqpserver container already exists. If this is the case,
|
|
|
|
# remove it and create a new one
|
|
|
|
try:
|
2021-08-04 15:07:59 +00:00
|
|
|
container = self.docker.containers.get(name)
|
2021-02-01 11:51:07 +00:00
|
|
|
except docker.errors.NotFound:
|
|
|
|
pass
|
|
|
|
except docker.errors.APIError as e:
|
|
|
|
logging.error(
|
2021-08-04 15:07:59 +00:00
|
|
|
'Get "{}" container raised '.format(name)
|
2021-02-01 11:51:07 +00:00
|
|
|
+ '"docker.errors.APIError" The server returned an error. '
|
|
|
|
+ 'Details: {}'.format(e)
|
|
|
|
)
|
2020-11-09 15:14:19 +00:00
|
|
|
return
|
2021-02-01 11:51:07 +00:00
|
|
|
else:
|
2020-11-13 09:01:51 +00:00
|
|
|
try:
|
2021-02-01 11:51:07 +00:00
|
|
|
container.remove(force=True)
|
2020-11-13 09:01:51 +00:00
|
|
|
except docker.errors.APIError as e:
|
2020-11-19 11:31:29 +00:00
|
|
|
logging.error(
|
2021-08-04 15:07:59 +00:00
|
|
|
'Remove "{}" container raised '.format(name)
|
2020-11-19 11:31:29 +00:00
|
|
|
+ '"docker.errors.APIError" The server returned an error. '
|
|
|
|
+ 'Details: {}'.format(e)
|
|
|
|
)
|
2020-11-13 09:01:51 +00:00
|
|
|
return
|
|
|
|
try:
|
2021-08-04 15:07:59 +00:00
|
|
|
self.docker.containers.run(image, command=command, detach=detach,
|
|
|
|
volumes=volumes, name=name,
|
|
|
|
network=network)
|
2021-02-01 11:51:07 +00:00
|
|
|
except docker.errors.ContainerError:
|
|
|
|
# This case should not occur, because detach is True.
|
|
|
|
logging.error(
|
2021-08-04 15:07:59 +00:00
|
|
|
'Run "{}" container raised '.format(name)
|
2021-02-01 11:51:07 +00:00
|
|
|
+ '"docker.errors.ContainerError" The container exits with a '
|
|
|
|
+ 'non-zero exit code and detach is False.'
|
|
|
|
)
|
|
|
|
corpus.status = 'failed'
|
|
|
|
except docker.errors.ImageNotFound:
|
|
|
|
logging.error(
|
2021-08-04 15:07:59 +00:00
|
|
|
'Run "{}" container raised '.format(name)
|
2021-02-01 11:51:07 +00:00
|
|
|
+ '"docker.errors.ImageNotFound" The specified image does not '
|
|
|
|
+ 'exist.'
|
|
|
|
)
|
|
|
|
corpus.status = 'failed'
|
2020-11-13 09:01:51 +00:00
|
|
|
except docker.errors.APIError as e:
|
2020-11-19 11:31:29 +00:00
|
|
|
logging.error(
|
2021-08-04 15:07:59 +00:00
|
|
|
'Run "{}" container raised '.format(name)
|
2020-11-19 11:31:29 +00:00
|
|
|
+ '"docker.errors.APIError" The server returned an error. '
|
|
|
|
+ 'Details: {}'.format(e)
|
|
|
|
)
|
2021-02-01 11:51:07 +00:00
|
|
|
else:
|
|
|
|
corpus.status = 'analysing'
|
2020-11-09 15:14:19 +00:00
|
|
|
|
2021-07-16 11:07:01 +00:00
|
|
|
def checkout_analysing_corpus_container(self, corpus):
|
|
|
|
container_name = 'cqpserver_{}'.format(corpus.id)
|
|
|
|
try:
|
|
|
|
self.docker.containers.get(container_name)
|
|
|
|
except docker.errors.NotFound:
|
|
|
|
logging.error('Could not find "{}" but the corpus state is "analysing".') # noqa
|
|
|
|
corpus.status = 'prepared'
|
|
|
|
except docker.errors.APIError as e:
|
|
|
|
logging.error(
|
|
|
|
'Get "{}" container raised '.format(container_name)
|
|
|
|
+ '"docker.errors.APIError" The server returned an error. '
|
|
|
|
+ 'Details: {}'.format(e)
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
2021-02-01 11:51:07 +00:00
|
|
|
def remove_cqpserver_container(self, corpus):
|
|
|
|
container_name = 'cqpserver_{}'.format(corpus.id)
|
2020-11-13 09:01:51 +00:00
|
|
|
try:
|
2021-02-01 11:51:07 +00:00
|
|
|
container = self.docker.containers.get(container_name)
|
|
|
|
except docker.errors.NotFound:
|
|
|
|
pass
|
2020-11-13 09:01:51 +00:00
|
|
|
except docker.errors.APIError as e:
|
2020-11-19 11:31:29 +00:00
|
|
|
logging.error(
|
2021-02-01 11:51:07 +00:00
|
|
|
'Get "{}" container raised '.format(container_name)
|
2020-11-19 11:31:29 +00:00
|
|
|
+ '"docker.errors.APIError" The server returned an error. '
|
|
|
|
+ 'Details: {}'.format(e)
|
|
|
|
)
|
2020-11-13 09:01:51 +00:00
|
|
|
return
|
2021-02-01 11:51:07 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
container.remove(force=True)
|
|
|
|
except docker.errors.APIError as e:
|
|
|
|
logging.error(
|
|
|
|
'Remove "{}" container raised '.format(container_name)
|
|
|
|
+ '"docker.errors.APIError" The server returned an error. '
|
|
|
|
+ 'Details: {}'.format(e)
|
|
|
|
)
|
|
|
|
return
|
|
|
|
corpus.status = 'prepared'
|