async.py 14.6 KB
Newer Older
André Anjos's avatar
André Anjos committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

###############################################################################
#                                                                             #
# Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/           #
# Contact: beat.support@idiap.ch                                              #
#                                                                             #
# This file is part of the beat.core module of the BEAT platform.             #
#                                                                             #
# Commercial License Usage                                                    #
# Licensees holding valid commercial BEAT licenses may use this file in       #
# accordance with the terms contained in a written agreement between you      #
# and Idiap. For further information contact tto@idiap.ch                     #
#                                                                             #
# Alternatively, this file may be used under the terms of the GNU Affero      #
# Public License version 3 as published by the Free Software and appearing    #
# in the file LICENSE.AGPL included in the packaging of this file.            #
# The BEAT platform is distributed in the hope that it will be useful, but    #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  #
# or FITNESS FOR A PARTICULAR PURPOSE.                                        #
#                                                                             #
# You should have received a copy of the GNU Affero Public License along      #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/.           #
#                                                                             #
###############################################################################


29
'''Implementation of subprocess-based asynchronous running with greenlets
André Anjos's avatar
André Anjos committed
30
31
32
'''

import os
33
import tarfile
André Anjos's avatar
André Anjos committed
34
35
36
37

import logging
logger = logging.getLogger(__name__)

38
39
40
41
import six
import docker
import requests
import simplejson
André Anjos's avatar
André Anjos committed
42
43
44
45

from . import stats


46
47
class Host(object):
  '''An object of this class can connect to the docker host and resolve stuff'''
48

André Anjos's avatar
André Anjos committed
49

50
  def __init__(self, use_machine=None, **kwargs):
André Anjos's avatar
André Anjos committed
51

52
53
54
55
56
    self.machine = None #normally, no need to use docker-machine
    self.use_machine = None
    self.started_machine = False
    self.client = None
    self.containers = []
André Anjos's avatar
André Anjos committed
57

58
59
60
61
    if use_machine is not None:
      import machine
      self.machine = machine.Machine()
      self.use_machine = use_machine
André Anjos's avatar
André Anjos committed
62

63
64
65
      if not self.machine.exists(self.use_machine):
        raise RuntimeError("no docker machine named `%s' found " \
            " - perhaps you forgot to create it?" % self.use_machine)
André Anjos's avatar
André Anjos committed
66

67
68
69
70
71
72
73
74
    elif 'host' in kwargs and 'port' in kwargs:
      host = kwargs.get('host')
      del kwargs['host']
      port = kwargs.get('port')
      del kwargs['port']
      kwargs['base_url'] = "http://%s:%s" % (host, port)

    self.kwargs = kwargs
75
    self.beat_environments = {}
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92


  def setup(self):

    if self.use_machine is not None:
      # requires a bootstrapping machine to run
      if not self.machine.status(self.use_machine):
        logger.info("Starting docker machine `%s'...", self.use_machine)
        self.machine.start(self.use_machine)
        self.started_machine = True

      self.client = \
          docker.Client(**self.machine.config(machine=self.use_machine))

    else:
      self.client = docker.Client(**self.kwargs)

93
94
    self.beat_environments = self._discover_environments(raise_on_errors=True)

95
96
97
98
99
100
101
102
103
104
105
106
107

  def teardown(self):

    for container in self.containers: self.rm(container)

    if self.use_machine is not None and self.started_machine:
      logger.info("Stopping docker machine `%s'...", self.use_machine)
      self.machine.stop(self.use_machine)
      self.started_machine = False

    self.client = None


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  def _discover_environments(self, raise_on_errors=True):
    '''Returns a dictionary containing information about docker environments

    Parameters:

      raise_on_errors (bool, Optional): If we should raise an exception
        (``RuntimeError``) in case installed environments override each other
        and we can't know which to use.


    Raises:

      RuntimeError: if you set ``raise_on_errors`` and I found environments
        that override each other for their description keys (``<name>
        (<version>)``), which should be unique.

    '''

    def _describe(image):
      '''Tries to run the "describe" app on the image, collect results'''

      status, output = self.get_statusoutput(image, 'describe')
      if status == 0:
        try:
          return simplejson.loads(output)
        except Exception as e:
          logger.warn("Ignoring potential environment at `%s' since " \
                  "`describe' output cannot be parsed: %s", image, str(e))
      return {}

    retval = {}

    for image in self.client.images():
      # call the "describe" application on each existing image
      tag = image['RepoTags'][0] if image['RepoTags'] else None
      id = image['Id'].split(':')[1][:12]
      logger.debug("Checking image `%s' (%s)...", tag, id)
      description = _describe(image['Id'])
      if not description: continue

      key = description['name'] + ' (' + description['version'] + ')'

      if key in retval:

        # this check avoids we do a new environment and, by mistake, override
        # it with a previous version or the contrary.
        if raise_on_errors:
          raise RuntimeError("Environments at `%s' and `%s' have the " \
                  "same name (`%s'). Distinct environments must be " \
                  "uniquely named. Fix this and re-start." % \
                  (envdir, retval[key]['image'], key))
        else:
          logger.warn("Overriding **existing** environment `%s' image " \
                  "with `%s' (it was `%s). To avoid this warning make " \
                  "sure your docker images do not contain environments " \
                  "with the same names", key, retval[key]['image'],
                  image['Id'])

      retval[key] = description
      retval[key]['image'] = image['Id']
      retval[key]['tag'] = tag
      retval[key]['short_id'] = id
      logger.info("Registered `%s' -> `%s (%s)'", key, tag, id)

    logger.debug("Found %d environments", len(retval))
    return retval


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  def create_container(self, image, command, tmp_archive=None, **args):
    """Prepares the docker container for running the user code


    Parameters:

      image (str): A unique identifier for the image to create

      command (list): A list of strings with the command to run inside the
        container.

      tmp_archive (bytes): An archive to copy into the temporary directory of
        the container (``/tmp``), supposedly with information that is used by
        the command.

      args (dict): A list of extra arguments to pass to the underlying
        ``create_container()`` call from the docker Python API.
André Anjos's avatar
André Anjos committed
193
194


195
196
197
198
199
200
201
    Returns:

      docker.Container: The container, already preloaded and ready to run. The
        container is not started by this method.

    """

202
203
204
205
206
207
    if image in self.beat_environments: #replace by a real image name
      attrs = self.beat_environments[image]
      if attrs['tag'] is not None: image = attrs['tag']
      else: image = attrs['short_id']

    logger.info("[docker] create_container %s %s", image, ' '.join(command))
208
209
210
211
212
213
    container = self.client.create_container(image=image, command=command,
        **args)
    self.containers.append(container)

    if tmp_archive is not None:
      # Place the tarball into the container
214
      logger.info("[docker] archive -> %s@/tmp", container['Id'][:12])
215
216
217
218
219
220
221
222
      self.client.put_archive(container, '/tmp', tmp_archive)

    return container


  def start(self, container):
    '''Starts a given container'''

223
    logger.info("[docker] start %s", container['Id'][:12])
224
225
226
227
228
229
230
231
    self.client.start(container)


  def status(self, container):
    '''Checks the status of a given container

    '''

André Anjos's avatar
André Anjos committed
232
    try:
233
      logger.info("[docker] inspect %s", container['Id'][:12])
234
235
236
237
      z = self.client.inspect_container(container)
      return z['State']['Status']
    except Exception as e:
      return None
André Anjos's avatar
André Anjos committed
238
239


240
241
  def rm(self, container):
    '''Removes a given container. If it is not done, kill it first'''
André Anjos's avatar
André Anjos committed
242

243
244
245
246
247
248
249
    status = self.status(container)

    if status not in ('created', 'exited'):
      logger.warn("Killing container `%s' which is on state `%s'",
          container['Id'], status)
      self.client.kill(container)
    else:
250
      logger.info("[docker] rm %s", container['Id'][:12])
251
      self.client.remove_container(container)
André Anjos's avatar
André Anjos committed
252

253
    self.containers.remove(container)
André Anjos's avatar
André Anjos committed
254
255


256
257
  def get_statusoutput(self, image, command, **kwargs):
    '''Runs a command and retrieves status and output'''
André Anjos's avatar
André Anjos committed
258

259
260
    container = self.create_container(image=image, command=command, **kwargs)
    try:
261
      self.start(container)
262
263
264
265
266
      status = self.client.wait(container)
      output = self.client.logs(container)
    except Exception:
      return 1, None
    finally:
267
      self.rm(container)
268
269

    return status, output
André Anjos's avatar
André Anjos committed
270
271


272
273
274
275
def make_inmemory_tarball(path):
  '''Creates an in-memory tarball of the contents of path

    Parameters:
André Anjos's avatar
André Anjos committed
276

277
278
279
280
281
282
      path (str): The path to a file or directory that will be tarred.


    Returns:

      bytes: A byte-string representing the packaged object(s).
André Anjos's avatar
André Anjos committed
283

284
  '''
André Anjos's avatar
André Anjos committed
285

286
287
288
289
290
  # Prepare the tarball, remove the temporary directory
  c = six.moves.cStringIO()
  with tarfile.open(mode='w', fileobj=c) as tar:
    tar.add(path, arcname=os.path.basename(path))
  return c.getvalue()
André Anjos's avatar
André Anjos committed
291
292


293
294
class Popen:
  '''Process-like manager for asynchronously executing user code in a container
André Anjos's avatar
André Anjos committed
295

296
297
298
  The process will be run in the background, and its standard output and
  standard error will be read after it finishes, into a limited size circular
  buffer.
André Anjos's avatar
André Anjos committed
299
300


301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
  Parameters:

    host (:py:class:`Host`): The docker host where to start the container.
      The host must be properly initialized, including starting the
      appropriate docker-machine, if that is the case. This normally implies
      including calls to this object inside a protected ``with host``
      section.

    image (str): A string uniquely identifying the image from which to start
      the container

    command (list): A list of strings with the command to run inside the
      container.

    tmp_archive (str, bytes, Optional): An archive to copy into the temporary
      directory of the container, supposedly with information that is used by
      the command. If a string is given, than it is considered as a path from
      which the archive is going to be created.

    virtual_memory_in_megabytes (int, Optional): The maximum amount of memory
      the user process can consume on the host. If not specified, a memory
      limit is not set.

    max_cpu_percent (float, Optional): The maximum amount of CPU the user
      process may consume on the host. The value ``100`` equals to using 100%
      of a single core. If not specified, then a CPU limitation is not put in
      place.

  '''

  def __init__(self, host, image, command, tmp_archive=None,
      virtual_memory_in_megabytes=0, max_cpu_percent=0, **args):

    self.host = host

    args.update({
        'user': 'nobody',
        'ports': ['5555'],
        'tty': False,
        'detach': True,
        'stdin_open': False,
        })

    # creates the log configuration, limiting output size kept on the image
    host_config = self.host.client.create_host_config(
        log_config = docker.utils.LogConfig(type='',
          config={'max-size': '1M', 'max-file': '1'}),
        )

    if isinstance(tmp_archive, six.string_types) and \
        os.path.exists(tmp_archive):
        tmp_archive = make_inmemory_tarball(tmp_archive)

    # creates the container
    self.container = self.host.create_container(image=image,
        command=command, tmp_archive=tmp_archive, host_config=host_config,
        **args)

    update_args = {}
André Anjos's avatar
André Anjos committed
360
361

    if max_cpu_percent:
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
      update_args['cpu_period'] = 100000
      update_args['cpu_quota'] = int(update_args['cpu_period'] * \
          (max_cpu_percent/100.))

    if virtual_memory_in_megabytes:
      update_args['mem_limit'] = str(virtual_memory_in_megabytes) + 'm'
      update_args['memswap_limit'] = update_args['mem_limit']

    self.host.client.update_container(self.container, **update_args)

    # Starts the container
    self.host.start(self.container)

    # Gets start point statistics
    self.previous_stats = None


  def __enter__(self):
    return self


  def __exit__(self, *exc):
    self.rm()
André Anjos's avatar
André Anjos committed
385
386


387
388
389
390
391
392
393
394
395
396
397
398
  def wait(self, timeout=None):
    '''Reads stdout and stderr until the underlying processes finishes

    Implements a modified version of :py:meth:`subprocess.Popen.wait`, in which
    we read the stdout and stderr data into a circular buffer, keep only the
    last N bytes of each stream.

    This method will call :py:meth:`file.readline` on both stdout and stderr
    streams attached to the process. These methods are "green". They will yield
    once they are blocked.


399
400
401
402
403
404
    Parameters:

      timeout (float, Optional): A timeout in seconds to wait for the user
        process to finish. If a timeout value is not given, waits forever.


405
406
407
408
409
410
411
    Returns:

      int: Returns the status code of the process


    Raises:

412
      requests.exceptions.ReadTimeout: if the process times out
413
414
415

    '''

416
    return self.host.client.wait(self.container, timeout)
417

418
419
420
  @property
  def stdout(self):
    '''Returns the stdout'''
421

422
423
424
425
426
427
428
429
    return self.host.client.logs(self.container, stdout=True, stderr=False)


  @property
  def stderr(self):
    '''Returns the stderr'''

    return self.host.client.logs(self.container, stdout=False, stderr=True)
430
431


432
433
  def rm(self):
    '''Remove the container. If it is not stopped yet, kill it first'''
434

435
    self.host.rm(self.container)
436
437


438
439
  def status(self):
    '''Returns my own "docker" status'''
440

441
    return self.host.status(self.container)
André Anjos's avatar
André Anjos committed
442
443
444


  def kill(self):
445
    '''Stop container'''
André Anjos's avatar
André Anjos committed
446

447
448
449
450
451
452
453
    if self.status() == 'running':
        self.host.client.kill(self.container)


  def _stats(self):

    return self.host.client.stats(self.container, decode=True, stream=False)
André Anjos's avatar
André Anjos committed
454
455
456


  def statistics(self):
457
    '''If the process is still active, returns usage statistics by ``pusutil``
André Anjos's avatar
André Anjos committed
458
459
460
461
462
463
464
465
466
467
468


    Returns:

      stats (dict): A dictionary object with all collected statistics


    Raises:

      RuntimeError: In case the process is not active anymore.

469
    '''
André Anjos's avatar
André Anjos committed
470

471
    data = self._stats()
André Anjos's avatar
André Anjos committed
472

473
474
    previous_cpu = self.previous_stats['cpu_stats'] \
        if self.previous_stats else None
André Anjos's avatar
André Anjos committed
475
476
477

    # merge statistics
    retval = dict(
478
479
            cpu=stats.cpu_statistics(previous_cpu, data['cpu_stats']),
            memory=stats.memory_statistics(data['memory_stats']),
André Anjos's avatar
André Anjos committed
480
            )
481
482
483

    self.previous_stats = data

André Anjos's avatar
André Anjos committed
484
    return retval