• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 Google LLC
2#
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6
7import posixpath
8from recipe_engine import recipe_api
9
10
11MOUNT_SRC = '/SRC'
12MOUNT_OUT = '/OUT'
13
14
15class DockerApi(recipe_api.RecipeApi):
16  def _chmod(self, filepath, mode, recursive=False):
17    cmd = ['chmod']
18    if recursive:
19      cmd.append('-R')
20    cmd.extend([mode, filepath])
21    name = ' '.join([str(elem) for elem in cmd])
22    self.m.step(name, cmd=cmd, infra_step=True)
23
24  def mount_src(self):
25    return MOUNT_SRC
26
27  def mount_out(self):
28    return MOUNT_OUT
29
30  def run(self, name, docker_image, src_dir, out_dir, script, args=None, docker_args=None, copies=None, recursive_read=None, attempts=1):
31    # Setup. Docker runs as a different user, so we need to give it access to
32    # read, write, and execute certain files.
33    with self.m.step.nest('Docker setup'):
34      # Make sure out_dir exists, otherwise mounting will fail.
35      # (Note that the docker --mount option, unlike the --volume option, does
36      # not create this dir as root if it doesn't exist.)
37      self.m.file.ensure_directory('mkdirs out_dir', out_dir, mode=0777)
38      # ensure_directory won't change the permissions if the dir already exists,
39      # so we need to do that explicitly.
40      self._chmod(out_dir, '777')
41
42      # chmod the src_dir, but not recursively; Swarming writes some files which
43      # we can't access, so "chmod -R" will fail if this is the root workdir.
44      self._chmod(src_dir, '755')
45
46      # Need to make the script executable, or Docker can't run it.
47      self._chmod(script, '0755')
48
49      # Copy any requested files.
50      if copies:
51        for src, dest in copies.iteritems():
52          dirname = self.m.path.dirname(dest)
53          self.m.file.ensure_directory(
54              'mkdirs %s' % dirname, dirname, mode=0777)
55          self.m.file.copy('cp %s %s' % (src, dest), src, dest)
56          self._chmod(dest, '644')
57
58      # Recursive chmod any requested directories.
59      if recursive_read:
60        for elem in recursive_read:
61          self._chmod(elem, 'a+r', recursive=True)
62
63    # Run.
64    cmd = [
65      'docker', 'run', '--shm-size=2gb', '--rm',
66      '--mount', 'type=bind,source=%s,target=%s' % (src_dir, MOUNT_SRC),
67      '--mount', 'type=bind,source=%s,target=%s' % (out_dir, MOUNT_OUT),
68    ]
69    if docker_args:
70      cmd.extend(docker_args)
71    script_rel = posixpath.relpath(str(script), str(self.m.path['start_dir']))
72    cmd.extend([docker_image, MOUNT_SRC + '/' + script_rel])
73    if args:
74      cmd.extend(args)
75
76    env = {'DOCKER_CONFIG': '/home/chrome-bot/.docker'}
77    with self.m.env(env):
78      self.m.run.with_retry(self.m.step, name, attempts, cmd=cmd)
79