blob: e569b98b93bf74f740cc8671d95f2a3077906cb1 [file] [log] [blame]
Primiano Tuccif7647392019-10-04 00:42:11 +01001#!/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
16from __future__ import print_function
17import argparse
18import sys
19import shutil
20import subprocess
21import os
22
23REPO_ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
24sys.path.append(os.path.join(REPO_ROOT, 'infra', 'ci'))
25from config import JOB_CONFIGS, SANDBOX_IMG
26
27
28def main():
Matthew Clarkson63028d62019-10-10 15:48:23 +010029 parser = argparse.ArgumentParser()
30 parser.add_argument('config', choices=JOB_CONFIGS.keys())
31 args = parser.parse_args()
Primiano Tuccif7647392019-10-04 00:42:11 +010032
Matthew Clarkson63028d62019-10-10 15:48:23 +010033 # 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 Tuccif7647392019-10-04 00:42:11 +010043
Matthew Clarkson63028d62019-10-10 15:48:23 +010044 env = {
45 'PERFETTO_TEST_GIT_REF': 'file:///ci/source',
46 }
47 env.update(JOB_CONFIGS[args.config])
Primiano Tuccif7647392019-10-04 00:42:11 +010048
Matthew Clarkson63028d62019-10-10 15:48:23 +010049 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 Tuccif7647392019-10-04 00:42:11 +010063
Matthew Clarkson63028d62019-10-10 15:48:23 +010064 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 Tuccif7647392019-10-04 00:42:11 +010071
Matthew Clarkson63028d62019-10-10 15:48:23 +010072 shutil.rmtree(workdir, ignore_errors=True)
73 os.makedirs(workdir)
74 os.execvp(cmd[0], cmd[1:])
Primiano Tuccif7647392019-10-04 00:42:11 +010075
76
77if __name__ == '__main__':
Matthew Clarkson63028d62019-10-10 15:48:23 +010078 sys.exit(main())