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 |
| 18 | import sys |
| 19 | import shutil |
| 20 | import subprocess |
| 21 | import os |
| 22 | |
| 23 | REPO_ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 24 | sys.path.append(os.path.join(REPO_ROOT, 'infra', 'ci')) |
| 25 | from config import JOB_CONFIGS, SANDBOX_IMG |
| 26 | |
| 27 | |
| 28 | def main(): |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame^] | 29 | parser = argparse.ArgumentParser() |
| 30 | parser.add_argument('config', choices=JOB_CONFIGS.keys()) |
| 31 | args = parser.parse_args() |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 32 | |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame^] | 33 | # Check that the directory is clean. |
| 34 | git_cmd = ['git', '-C', REPO_ROOT, 'status', '--porcelain'] |
| 35 | modified_files = subprocess.check_output(git_cmd) |
| 36 | if modified_files: |
| 37 | print('The current Git repo has modified/untracked files.') |
| 38 | print('The sandboxed VM will fetch the HEAD of your current git repo.') |
| 39 | print('This is probably not the state you want to be in.') |
| 40 | print('I suggest you press CTRL+C, commit and then re-run this script') |
| 41 | print('Modified files:\n' + modified_files) |
| 42 | raw_input('If you think you know what you are doing, press Enter instead') |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 43 | |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame^] | 44 | env = { |
| 45 | 'PERFETTO_TEST_GIT_REF': 'file:///ci/source', |
| 46 | } |
| 47 | env.update(JOB_CONFIGS[args.config]) |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 48 | |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame^] | 49 | workdir = os.path.join(REPO_ROOT, 'out', 'tmp.ci') |
| 50 | cmd = [ |
| 51 | 'sudo', '--', 'docker', 'run', '-it', '--name', 'perfetto_ci', |
| 52 | '--cap-add', 'SYS_PTRACE', '--rm', '--volume', |
| 53 | '%s:/ci/ramdisk' % workdir, '--tmpfs', '/tmp:exec', |
| 54 | '--volume=%s:/ci/source:ro' % REPO_ROOT |
| 55 | ] |
| 56 | for kv in env.items(): |
| 57 | cmd += ['--env', '%s=%s' % kv] |
| 58 | cmd += [SANDBOX_IMG] |
| 59 | cmd += [ |
| 60 | 'bash', '-c', |
| 61 | 'cd /ci/ramdisk; bash /ci/init.sh || sudo -u perfetto -EH bash -i' |
| 62 | ] |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 63 | |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame^] | 64 | print('About to run\n', ' '.join(cmd)) |
| 65 | print('') |
| 66 | print('The VM will have read-only acess to: %s, mounted as /ci/source' % |
| 67 | REPO_ROOT) |
| 68 | print('The VM workdir /ci/ramdisk will be mounted into: %s' % workdir) |
| 69 | print('The contents of %s will be deleted before starting the VM' % workdir) |
| 70 | raw_input('Press a key to continue') |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 71 | |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame^] | 72 | shutil.rmtree(workdir, ignore_errors=True) |
| 73 | os.makedirs(workdir) |
| 74 | os.execvp(cmd[0], cmd[1:]) |
Primiano Tucci | f764739 | 2019-10-04 00:42:11 +0100 | [diff] [blame] | 75 | |
| 76 | |
| 77 | if __name__ == '__main__': |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame^] | 78 | sys.exit(main()) |