Commit f3e8194d authored by Samuel GAIST's avatar Samuel GAIST

[backend][models] Implemented EnvironmentParameter

The environment parameters allow to pass additional arguments
to docker when starting a container.
parent e108f234
......@@ -30,6 +30,7 @@ from django import forms
from .models import Environment as EnvironmentModel
from .models import EnvironmentLanguage as EnvironmentLanguageModel
from .models import EnvironmentParameter as EnvironmentParameterModel
from .models import Worker as WorkerModel
from .models import Queue as QueueModel
from .models import Slot as SlotModel
......@@ -69,6 +70,10 @@ class EnvironmentLanguageInline(admin.TabularInline):
model = EnvironmentLanguageModel
class EnvironmentParameterInline(admin.TabularInline):
model = EnvironmentParameterModel
class Environment(admin.ModelAdmin):
list_display = (
......@@ -93,7 +98,8 @@ class Environment(admin.ModelAdmin):
)
inlines = [
EnvironmentLanguageInline
EnvironmentLanguageInline,
EnvironmentParameterInline
]
form = EnvironmentModelForm
......
# -*- coding: utf-8 -*-
# Generated by Django 1.9.13 on 2017-09-13 16:13
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('backend', '0006_localschedulerprocesses'),
]
operations = [
migrations.CreateModel(
name='EnvironmentParameter',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('parameter_type', models.CharField(help_text='Type of environment parameter', choices=[(b'ENV', b'Environment'), (b'VOL', b'Volume'), (b'MAC', b'Mac address')], max_length=3)),
('key', models.CharField(help_text='Key of the parameter if applicable i.e. environment variable', unique=True, max_length=512, blank=True)),
('value', models.CharField(help_text='Value of the parameter', max_length=512)),
('environment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='parameters', to='backend.Environment')),
],
),
]
......@@ -29,6 +29,7 @@
from .environment import EnvironmentManager
from .environment import Environment
from .environment import EnvironmentLanguage
from .environment import EnvironmentParameter
from .job import Job
from .job import JobSplit
from .local_scheduler import LocalSchedulerProcesses
......
......@@ -149,6 +149,28 @@ class Environment(Shareable):
)
def build_run_arguments(self):
""""
Build the arguments to pass to Docker when running the container
"""
run_arguments = {}
for parameter in self.parameters.all():
type_key = parameter.type_to_key()
value = parameter.to_value()
if isinstance(value, dict):
if not type_key in run_arguments:
run_arguments[type_key] = value
else:
run_arguments[type_key].update(value)
else:
run_arguments[type_key] = value
return run_arguments
#----------------------------------------------------------
......@@ -158,3 +180,79 @@ class EnvironmentLanguage(models.Model):
language = models.CharField(max_length=1, choices=Code.CODE_LANGUAGE,
default=Code.PYTHON)
#----------------------------------------------------------
class EnvironmentParameter(models.Model):
ENVIRONMENT = 'ENV'
VOLUME = 'VOL'
MAC_ADDRESS = 'MAC'
PARAMETER_TYPES = [
(ENVIRONMENT, 'Environment'),
(VOLUME, 'Volume'),
(MAC_ADDRESS, 'Mac address')
]
environment = models.ForeignKey(Environment,
related_name='parameters'
)
parameter_type = models.CharField(
max_length=3,
choices=PARAMETER_TYPES,
help_text='Type of environment parameter'
)
key = models.CharField(
max_length=512,
blank=True,
unique=True,
help_text='Key of the parameter if applicable i.e. environment variable'
)
value = models.CharField(
max_length=512,
help_text='Value of the parameter'
)
def type_to_key(self):
result = None
if self.parameter_type == EnvironmentParameter.ENVIRONMENT:
result = 'environment'
elif self.parameter_type == EnvironmentParameter.VOLUME:
result = 'volumes'
elif self.parameter_type == EnvironmentParameter.MAC_ADDRESS:
result = 'mac_address'
return result
def to_value(self):
result = None
if self.parameter_type == EnvironmentParameter.ENVIRONMENT:
result = { self.key: self.value }
elif self.parameter_type == EnvironmentParameter.VOLUME:
path = self.value
try:
column_index = path.index(":")
mode = path[column_index + 1:]
path = path[:column_index]
except ValueError:
mode = 'ro'
result = {
self.key: {
'bind': path,
'mode': mode
}
}
elif self.parameter_type == EnvironmentParameter.MAC_ADDRESS:
result = self.value
return result
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment