Primiano Tucci | c8be681 | 2021-02-09 18:08:49 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 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 |
Matthew Clarkson | b4645a2 | 2019-11-06 13:34:39 +0000 | [diff] [blame] | 19 | import errno |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame] | 20 | import grp |
| 21 | import os |
| 22 | import readline |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 23 | import sys |
| 24 | import shutil |
| 25 | import subprocess |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame] | 26 | from pipes import quote |
Matthew Clarkson | b4645a2 | 2019-11-06 13:34:39 +0000 | [diff] [blame] | 27 | from subprocess import check_call |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame] | 28 | |
| 29 | try: |
| 30 | from shutil import which as find_executable |
| 31 | except AttributeError: |
| 32 | from distutils.spawn import find_executable |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 33 | |
| 34 | REPO_ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 35 | sys.path.append(os.path.join(REPO_ROOT, 'infra', 'ci')) |
| 36 | from config import JOB_CONFIGS, SANDBOX_IMG |
| 37 | |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame] | 38 | try: |
| 39 | input = raw_input |
| 40 | except NameError: |
| 41 | pass |
| 42 | |
| 43 | |
| 44 | def user_in_docker_group(): |
| 45 | try: |
| 46 | group = grp.getgrnam('docker') |
| 47 | except KeyError: |
| 48 | return False |
| 49 | else: |
| 50 | return group.gr_gid in os.getgroups() |
| 51 | |
| 52 | |
| 53 | def decision(question='Would you like to continue', confirm=True, default='n'): |
| 54 | default = default.lower().strip() |
| 55 | yes = default in {'y', 'yes'} |
| 56 | no = default in {'n', 'no'} |
| 57 | default = 'y' if yes else 'n' |
| 58 | prompt = '%s? [%s/%s]: ' % (question, 'Y' if yes else 'y', 'N' if no else 'n') |
| 59 | if not confirm: |
| 60 | print('%sy' % prompt) |
| 61 | return |
| 62 | while True: |
| 63 | choice = input(prompt).lower().strip() |
| 64 | if not choice: |
| 65 | choice = default |
| 66 | if choice in {'y', 'yes'}: |
| 67 | return |
| 68 | elif choice in {'n', 'no'}: |
| 69 | sys.exit(3) |
| 70 | |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 71 | |
| 72 | def main(): |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame] | 73 | parser = argparse.ArgumentParser( |
| 74 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 75 | parser.add_argument('config', choices=JOB_CONFIGS.keys()) |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame] | 76 | parser.add_argument( |
| 77 | '--runner', |
| 78 | help='The container runner executable to use', |
| 79 | choices=('podman', 'docker'), |
| 80 | default='podman' if find_executable('podman') else 'docker') |
| 81 | parser.add_argument( |
Matthew Clarkson | b4645a2 | 2019-11-06 13:34:39 +0000 | [diff] [blame] | 82 | '--build', |
| 83 | action='store_true', |
| 84 | help='Will perform a build of sandbox image') |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame] | 85 | group = parser.add_mutually_exclusive_group() |
| 86 | group.add_argument( |
| 87 | '--confirm', |
| 88 | action='store_true', |
| 89 | default=True, |
| 90 | help='User confirmation of decision prompts') |
| 91 | group.add_argument( |
| 92 | '--no-confirm', |
| 93 | dest='confirm', |
| 94 | action='store_false', |
| 95 | help='Forces confirmation of decision prompts') |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 96 | args = parser.parse_args() |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 97 | |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 98 | # Check that the directory is clean. |
| 99 | git_cmd = ['git', '-C', REPO_ROOT, 'status', '--porcelain'] |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame] | 100 | modified_files = subprocess.check_output(git_cmd).decode() |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 101 | if modified_files: |
| 102 | print('The current Git repo has modified/untracked files.') |
| 103 | print('The sandboxed VM will fetch the HEAD of your current git repo.') |
| 104 | print('This is probably not the state you want to be in.') |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame] | 105 | print('I suggest you stop, commit and then re-run this script') |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 106 | print('Modified files:\n' + modified_files) |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame] | 107 | decision('Do you know what you are doing', confirm=args.confirm) |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 108 | |
Matthew Clarkson | b4645a2 | 2019-11-06 13:34:39 +0000 | [diff] [blame] | 109 | if args.build: |
| 110 | print('') |
| 111 | print('About to build %r locally with %r' % (args.image, args.runner)) |
| 112 | decision(confirm=args.confirm) |
Hector Dearman | b1989b0 | 2022-07-05 20:11:07 +0100 | [diff] [blame] | 113 | check_call(('make', '-C', os.path.join(REPO_ROOT, 'infra', 'ci'), |
Matthew Clarkson | b4645a2 | 2019-11-06 13:34:39 +0000 | [diff] [blame] | 114 | 'BUILDER=%s' % args.runner, 'build-sandbox')) |
| 115 | |
Primiano Tucci | e9790ab | 2021-02-22 14:58:01 +0100 | [diff] [blame] | 116 | bundle_path = '/tmp/perfetto-ci.bundle' |
Hector Dearman | b1989b0 | 2022-07-05 20:11:07 +0100 | [diff] [blame] | 117 | check_call(['git', '-C', REPO_ROOT, 'bundle', 'create', bundle_path, 'HEAD']) |
Primiano Tucci | e9790ab | 2021-02-22 14:58:01 +0100 | [diff] [blame] | 118 | os.chmod(bundle_path, 0o664) |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 119 | env = { |
Primiano Tucci | e9790ab | 2021-02-22 14:58:01 +0100 | [diff] [blame] | 120 | 'PERFETTO_TEST_GIT_REF': bundle_path, |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 121 | } |
| 122 | env.update(JOB_CONFIGS[args.config]) |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 123 | |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 124 | workdir = os.path.join(REPO_ROOT, 'out', 'tmp.ci') |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame] | 125 | cmd = [] |
| 126 | if args.runner == 'docker' and not user_in_docker_group(): |
| 127 | cmd += ['sudo', '--'] |
| 128 | cmd += [ |
| 129 | args.runner, 'run', '-it', '--name', 'perfetto_ci', '--cap-add', |
| 130 | 'SYS_PTRACE', '--rm', '--volume', |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 131 | '%s:/ci/ramdisk' % workdir, '--tmpfs', '/tmp:exec', |
Primiano Tucci | e9790ab | 2021-02-22 14:58:01 +0100 | [diff] [blame] | 132 | '--volume=%s:%s:ro' % (bundle_path, bundle_path) |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 133 | ] |
| 134 | for kv in env.items(): |
| 135 | cmd += ['--env', '%s=%s' % kv] |
Matthew Clarkson | b4645a2 | 2019-11-06 13:34:39 +0000 | [diff] [blame] | 136 | cmd += [SANDBOX_IMG] |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 137 | cmd += [ |
| 138 | 'bash', '-c', |
| 139 | 'cd /ci/ramdisk; bash /ci/init.sh || sudo -u perfetto -EH bash -i' |
| 140 | ] |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 141 | |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame] | 142 | print( |
| 143 | 'About to run\n', |
| 144 | ' '.join('\n ' + c if c.startswith('--') or c == 'bash' else quote(c) |
| 145 | for c in cmd)) |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 146 | print('') |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 147 | print('The VM workdir /ci/ramdisk will be mounted into: %s' % workdir) |
| 148 | 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] | 149 | decision(confirm=args.confirm) |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 150 | |
Matthew Clarkson | b4645a2 | 2019-11-06 13:34:39 +0000 | [diff] [blame] | 151 | try: |
| 152 | shutil.rmtree(workdir) |
| 153 | except EnvironmentError as e: |
| 154 | if e.errno == errno.ENOENT: |
| 155 | pass |
| 156 | elif e.errno == errno.EACCES: |
| 157 | print('') |
| 158 | print('Removing previous volume %r' % workdir) |
| 159 | check_call(('sudo', 'rm', '-r', quote(workdir))) |
| 160 | else: |
| 161 | raise |
| 162 | |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 163 | os.makedirs(workdir) |
Matthew Clarkson | 1ed6a04 | 2019-10-25 11:18:05 +0100 | [diff] [blame] | 164 | os.execvp(cmd[0], cmd) |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 165 | |
| 166 | |
| 167 | if __name__ == '__main__': |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 168 | sys.exit(main()) |