diff --git a/everware/spawner.py b/everware/spawner.py index 0eb461f..eaa974d 100755 --- a/everware/spawner.py +++ b/everware/spawner.py @@ -6,6 +6,7 @@ from concurrent.futures import ThreadPoolExecutor +import docker from docker.errors import APIError from smtplib import SMTPException from jupyterhub.utils import wait_for_http_server @@ -42,9 +43,54 @@ def __init__(self, **kwargs): self._image_handler = ImageHandler() self._cur_waiter = None self._is_empty = False + self._initialize_global_client() + # self.client may change if a BYOR-user will come + # However, it has to be set since it's used when the application starts + self.client = self._global_client ContainerHandler.__init__(self, **kwargs) EmailNotificator.__init__(self) + _global_client = None + def _initialize_global_client(self): + """Build single global client instance for non-BYOR users.""" + cls = self.__class__ + if cls._global_client is not None: + return + if self.use_docker_client_env: + kwargs = kwargs_from_env( + assert_hostname=self.tls_assert_hostname + ) + client = docker.Client(version='auto', **kwargs) + else: + if self.tls: + tls_config = True + elif self.tls_verify or self.tls_ca or self.tls_client: + tls_config = docker.tls.TLSConfig( + client_cert=self.tls_client, + ca_cert=self.tls_ca, + verify=self.tls_verify, + assert_hostname=self.tls_assert_hostname) + else: + tls_config = None + + docker_host = os.environ.get('DOCKER_HOST', 'unix://var/run/docker.sock') + client = docker.Client(base_url=docker_host, tls=tls_config, version='auto') + cls._global_client = client + + @property + def client(self): + return self._client + + @client.setter + def client(self, value): + self._client = value + + @gen.coroutine + def _make_byor_client(self): + """Prepare a client for the user. For a non-BYOR user just use the global client.""" + byor_docker_url = self.user_options['byor_docker_url'] + self.container_ip = byor_docker_url.split(':')[0] + self.client = docker.Client(base_url=byor_docker_url, tls=None, version='auto') # We override the executor here to increase the number of threads @property @@ -126,6 +172,24 @@ def _options_form_default(self): style="margin-bottom: 3px;" /> + +
+ (optionally) If you want to launch a container on your own machine, enter ip and port of the docker-host + running on it