Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
Already on GitHub? Sign in to your account
Bring Your Own Resources #205
Merged
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
905ed11
add BYOR support
StrausMG 1c7a454
set docker-client in spawner.start
StrausMG 6515f43
fix typo
StrausMG 0e700e6
Merge branch 'master' into byor
StrausMG fec015c
avoid code duplication
StrausMG c4ea301
add animation and handling bad Docker daemons
StrausMG cb2c263
add BYOR config
StrausMG 23a3530
fix critical bug in resetting byor settings
StrausMG d6beda0
minor fix
StrausMG 5d48c5f
minor code improvemenets
StrausMG 4606899
minor code fix
StrausMG 87980a6
fix critical bug
StrausMG 92abe86
fix typo
StrausMG be55158
Merge branch 'master' into byor
StrausMG 1b2c9d5
fix loading byor options form, return the old one
StrausMG bb8ae87
Merge branch 'byor' of https://github.com/StrausMG/everware into byor
StrausMG 46a8ac7
minor fixes
StrausMG 92835db
fix BYOR config
StrausMG da9d883
move BYOR functionality to a separate class
StrausMG ec76581
fix BYOR config
StrausMG 08807ea
move initialization of options_form to __init__
StrausMG 183c60f
change the way of the initialization check
StrausMG d579b5f
Merge branch 'master' into byor
anaderi
Jump to file or symbol
Failed to load files and symbols.
Viewing a subset of changes. View all
move BYOR functionality to a separate class
commit da9d883b01a2a1095aa308a2e92f7710df768f49
StrausMG
committed
Jun 25, 2017
| @@ -0,0 +1,82 @@ | ||
| +import docker | ||
| +from docker.errors import DockerException | ||
| +from traitlets import Int | ||
| +from tornado import gen | ||
| + | ||
| +from .spawner import CustomDockerSpawner | ||
| + | ||
| + | ||
| +class ByorDockerSpawner(CustomDockerSpawner): | ||
| + def __init__(self, **kwargs): | ||
| + self._byor_client = None | ||
| + CustomDockerSpawner.__init__(self, **kwargs) | ||
| + | ||
| + @property | ||
| + def client(self): | ||
| + if self._byor_client is not None: | ||
| + return self._byor_client | ||
| + return super(ByorDockerSpawner, self).client | ||
| + | ||
| + @property | ||
| + def byor_is_used(self): | ||
| + return self.user_options.get('byor_is_needed', False) | ||
| + | ||
| + def _reset_byor(self): | ||
| + self.container_ip = str(self.__class__.container_ip) | ||
| + self._byor_client = None | ||
| + | ||
| + byor_timeout = Int(20, min=1, config=True, | ||
| + help='Timeout for connection to BYOR Docker daemon') | ||
| + | ||
| + def options_from_form(self, formdata): | ||
| + options = {} | ||
| + options['byor_is_needed'] = formdata.pop('byor_is_needed', [''])[0].strip() == 'on' | ||
| + for field in ('byor_docker_ip', 'byor_docker_port'): | ||
| + options[field] = formdata.pop(field, [''])[0].strip() | ||
| + options.update( | ||
| + super(ByorDockerSpawner, self).options_from_form(formdata) | ||
| + ) | ||
| + return options | ||
| + | ||
| + @gen.coroutine | ||
| + def _configure_byor(self): | ||
| + """Configure BYOR settings or reset them if BYOR is not needed.""" | ||
| + if not self.byor_is_used: | ||
| + self._reset_byor() | ||
| + return | ||
| + byor_ip = self.user_options['byor_docker_ip'] | ||
| + byor_port = self.user_options['byor_docker_port'] | ||
| + try: | ||
| + # version='auto' causes a connection to the daemon. | ||
| + # That's why the method must be a coroutine. | ||
| + self._byor_client = docker.Client('{}:{}'.format(byor_ip, byor_port), | ||
| + version='auto', | ||
| + timeout=self.byor_timeout) | ||
| + except DockerException as e: | ||
| + self._is_failed = True | ||
| + message = str(e) | ||
| + if 'ConnectTimeoutError' in message: | ||
| + log_message = 'Connection to the Docker daemon took too long (> {} secs)'.format( | ||
| + self.byor_timeout | ||
| + ) | ||
| + notification_message = 'BYOR timeout limit {} exceeded'.format(self.byor_timeout) | ||
| + else: | ||
| + log_message = "Failed to establish connection with the Docker daemon" | ||
| + notification_message = log_message | ||
| + self._add_to_log(log_message, level=2) | ||
| + yield self.notify_about_fail(notification_message) | ||
| + self._is_building = False | ||
| + raise | ||
| + | ||
| + self.container_ip = byor_ip | ||
| + | ||
| + @gen.coroutine | ||
| + def _prepare_for_start(self): | ||
| + super(ByorDockerSpawner, self)._prepare_for_start() | ||
| + yield self._configure_byor() | ||
| + | ||
| + @gen.coroutine | ||
| + def start(self, image=None): | ||
| + yield self._prepare_for_start() | ||
| + ip_port = yield self._start(image) | ||
| + return ip_port |
move to ByorDockerSpawner class