Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (C) 2019 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | from __future__ import print_function |
| 17 | import argparse |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame^] | 18 | import distutils |
| 19 | import grp |
| 20 | import os |
| 21 | import readline |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 22 | import sys |
| 23 | import shutil |
| 24 | import subprocess |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame^] | 25 | from pipes import quote |
| 26 | |
| 27 | try: |
| 28 | from shutil import which as find_executable |
| 29 | except AttributeError: |
| 30 | from distutils.spawn import find_executable |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 31 | |
| 32 | REPO_ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 33 | sys.path.append(os.path.join(REPO_ROOT, 'infra', 'ci')) |
| 34 | from config import JOB_CONFIGS, SANDBOX_IMG |
| 35 | |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame^] | 36 | try: |
| 37 | input = raw_input |
| 38 | except NameError: |
| 39 | pass |
| 40 | |
| 41 | |
| 42 | def user_in_docker_group(): |
| 43 | try: |
| 44 | group = grp.getgrnam('docker') |
| 45 | except KeyError: |
| 46 | return False |
| 47 | else: |
| 48 | return group.gr_gid in os.getgroups() |
| 49 | |
| 50 | |
| 51 | def decision(question='Would you like to continue', confirm=True, default='n'): |
| 52 | default = default.lower().strip() |
| 53 | yes = default in {'y', 'yes'} |
| 54 | no = default in {'n', 'no'} |
| 55 | default = 'y' if yes else 'n' |
| 56 | prompt = '%s? [%s/%s]: ' % (question, 'Y' if yes else 'y', 'N' if no else 'n') |
| 57 | if not confirm: |
| 58 | print('%sy' % prompt) |
| 59 | return |
| 60 | while True: |
| 61 | choice = input(prompt).lower().strip() |
| 62 | if not choice: |
| 63 | choice = default |
| 64 | if choice in {'y', 'yes'}: |
| 65 | return |
| 66 | elif choice in {'n', 'no'}: |
| 67 | sys.exit(3) |
| 68 | |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 69 | |
| 70 | def main(): |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame^] | 71 | parser = argparse.ArgumentParser( |
| 72 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 73 | parser.add_argument('config', choices=JOB_CONFIGS.keys()) |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame^] | 74 | parser.add_argument( |
| 75 | '--runner', |
| 76 | help='The container runner executable to use', |
| 77 | choices=('podman', 'docker'), |
| 78 | default='podman' if find_executable('podman') else 'docker') |
| 79 | parser.add_argument( |
| 80 | '--image', default=SANDBOX_IMG, help='The image to use to run the tests') |
| 81 | group = parser.add_mutually_exclusive_group() |
| 82 | group.add_argument( |
| 83 | '--confirm', |
| 84 | action='store_true', |
| 85 | default=True, |
| 86 | help='User confirmation of decision prompts') |
| 87 | group.add_argument( |
| 88 | '--no-confirm', |
| 89 | dest='confirm', |
| 90 | action='store_false', |
| 91 | help='Forces confirmation of decision prompts') |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 92 | args = parser.parse_args() |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 93 | |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 94 | # Check that the directory is clean. |
| 95 | git_cmd = ['git', '-C', REPO_ROOT, 'status', '--porcelain'] |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame^] | 96 | modified_files = subprocess.check_output(git_cmd).decode() |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 97 | if modified_files: |
| 98 | print('The current Git repo has modified/untracked files.') |
| 99 | print('The sandboxed VM will fetch the HEAD of your current git repo.') |
| 100 | print('This is probably not the state you want to be in.') |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame^] | 101 | print('I suggest you stop, commit and then re-run this script') |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 102 | print('Modified files:\n' + modified_files) |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame^] | 103 | decision('Do you know what you are doing', confirm=args.confirm) |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 104 | |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 105 | env = { |
| 106 | 'PERFETTO_TEST_GIT_REF': 'file:///ci/source', |
| 107 | } |
| 108 | env.update(JOB_CONFIGS[args.config]) |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 109 | |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 110 | workdir = os.path.join(REPO_ROOT, 'out', 'tmp.ci') |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame^] | 111 | cmd = [] |
| 112 | if args.runner == 'docker' and not user_in_docker_group(): |
| 113 | cmd += ['sudo', '--'] |
| 114 | cmd += [ |
| 115 | args.runner, 'run', '-it', '--name', 'perfetto_ci', '--cap-add', |
| 116 | 'SYS_PTRACE', '--rm', '--volume', |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 117 | '%s:/ci/ramdisk' % workdir, '--tmpfs', '/tmp:exec', |
| 118 | '--volume=%s:/ci/source:ro' % REPO_ROOT |
| 119 | ] |
| 120 | for kv in env.items(): |
| 121 | cmd += ['--env', '%s=%s' % kv] |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame^] | 122 | cmd += [args.image] |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 123 | cmd += [ |
| 124 | 'bash', '-c', |
| 125 | 'cd /ci/ramdisk; bash /ci/init.sh || sudo -u perfetto -EH bash -i' |
| 126 | ] |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 127 | |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame^] | 128 | print( |
| 129 | 'About to run\n', |
| 130 | ' '.join('\n ' + c if c.startswith('--') or c == 'bash' else quote(c) |
| 131 | for c in cmd)) |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 132 | print('') |
| 133 | print('The VM will have read-only acess to: %s, mounted as /ci/source' % |
| 134 | REPO_ROOT) |
| 135 | print('The VM workdir /ci/ramdisk will be mounted into: %s' % workdir) |
| 136 | print('The contents of %s will be deleted before starting the VM' % workdir) |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame^] | 137 | decision(confirm=args.confirm) |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 138 | |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 139 | shutil.rmtree(workdir, ignore_errors=True) |
| 140 | os.makedirs(workdir) |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame^] | 141 | os.execvp(cmd[0], cmd) |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 142 | |
| 143 | |
| 144 | if __name__ == '__main__': |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 145 | sys.exit(main()) |