-
Notifications
You must be signed in to change notification settings - Fork 101
/
generate_dockerfile.py
164 lines (117 loc) · 5.19 KB
/
generate_dockerfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python3.8
import argparse
DESCRIPTION="Script to build avatar2 core and its endpoints using Docker."
USAGE=""" generate_dockerfile.py [options]
Example:
./generate_dockerfile.py \\
--endpoint_list avatar-qemu panda \\
--qemu_targets arm-softmmu mips-softmmu
"""
BASE_IMAGE='ubuntu:20.04'
avatar2_runtime_dependencies=[ 'python3',
'python3-setuptools',
'libcapstone3',
'gdb',
'gdbserver',
'gdb-multiarch']
avatar2_build_dependencies=[ 'git',
'cmake',
'pkg-config',
'build-essential',
'python3-dev',
'python3-pip',
'libcapstone-dev']
TEMPLATE_CORE_BASE=f"""
# avatar2 run-time dependencies
RUN apt-get update && \\
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends {' '.join(avatar2_runtime_dependencies)} && \\
apt-get clean && \\
rm -rf /var/lib/apt/lists/*
"""
TEMPLATE_CORE_GIT_BUILD=f"""
# avatar2 build dependencies
RUN apt-get update && \\
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends {' '.join(avatar2_build_dependencies)} && \\
pip3 install --upgrade --no-cache-dir pip
RUN git clone https://github.com/avatartwo/avatar2 /root/avatar2/
RUN cd /root/avatar2 && \\
python3 setup.py install
"""
TEMPLATE_QEMU_BUILD="""
RUN sed -i '/deb-src .*-security main restricted/s/^#//g' /etc/apt/sources.list
RUN apt-get update && \\
DEBIAN_FRONTEND=noninteractive apt-get build-dep -y qemu && \\
apt-get install -y git ninja-build
RUN git clone https://github.com/avatartwo/avatar-qemu /root/avatar-qemu/
RUN cd /root/avatar-qemu/ && \\
git checkout dev/qemu-6.2
RUN mkdir -p /root/avatar-qemu/build && cd /root/avatar-qemu/build && \\
../configure \\
--disable-sdl \\
--prefix=/usr/local/ \\
--target-list="${QEMU_TARGETS}" && \\
make -j "$(nproc)"
RUN cd /root/avatar-qemu/build/ && make install
"""
TEMPLATE_QEMU_RUNTIME="""
RUN apt-get update && \\
apt-get install -y --no-install-recommends libpulse0
COPY --from=build-avatar-qemu /usr/local /usr/local
"""
TEMPLATE_PANDA=f"""
# PANDA run-time dependencies
RUN apt-get update && \\
apt-get install -y --no-install-recommends ca-certificates wget && \\
wget 'https://raw.githubusercontent.com/panda-re/panda/master/panda/dependencies/{BASE_IMAGE}_base.txt' && \\
DEBIAN_FRONTEND=noninteractive apt-get -qq install -y --no-install-recommends $(cat ./ubuntu:20.04_base.txt | grep -o '^[^#]*') && \\
rm -f ./{BASE_IMAGE}_base.txt && \\
apt-get remove -y ca-certificates wget
COPY --from=panda /usr/local /usr/local
"""
def generate(endpoint_list, qemu_targets=['arm-softmmu']):
print(f'[*] Generate avatar2 Dockerfile with the following endpoints: {endpoint_list}')
stage = 0
with open('./Dockerfile', 'w') as f:
# avatar2 base images
f.write(f'### Stage {stage}: the base avatar2-core image\n')
f.write(f'FROM {BASE_IMAGE} AS base\n')
f.write(TEMPLATE_CORE_BASE)
# Build avatar2 with the specified endpoints
stage += 1
f.write(f'\n\n\n### Stage {stage}: The avatar2-core build image\n')
f.write(f'FROM base AS build-core\n')
f.write(TEMPLATE_CORE_GIT_BUILD)
if endpoint_list is not None:
if 'avatar-qemu' in endpoint_list:
stage += 1
f.write(f'\n\n\n### Stage {stage}: Build avatar-qemu\n')
f.write(f'FROM base AS build-avatar-qemu\n')
f.write(f'ARG QEMU_TARGETS="{",".join(qemu_targets)}"\n')
f.write(TEMPLATE_QEMU_BUILD)
if 'panda' in endpoint_list:
stage += 1
f.write(f'\n\n\n### Stage {stage}: Pull official panda image\n')
f.write('FROM pandare/panda:latest AS panda\n')
pass
# Copy artifacts into the final image
stage += 1
f.write(f'\n\n\n### Stage {stage}: Assemble the final image\n')
f.write(f'FROM base AS avatar2\n\n')
f.write('COPY --from=build-core /usr/local /usr/local\n')
if endpoint_list is not None:
if 'avatar-qemu' in endpoint_list:
f.write(TEMPLATE_QEMU_RUNTIME)
if 'panda' in endpoint_list:
f.write(TEMPLATE_PANDA)
f.write('RUN apt-get clean && rm -rf /var/lib/apt/lists/*\n\n')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=DESCRIPTION, usage=USAGE)
parser.add_argument('-e', '--endpoint_list', nargs='+', default=None,
choices=['avatar-qemu', 'panda'],
help='list of endpoints to build with avatar2')
parser.add_argument('--qemu_targets', nargs='+', default=['arm-softmmu'],
choices=['arm-softmmu', 'i386-softmmu', 'mips-softmmu',
'mipsel-softmmu', 'x86_64-softmmu'],
help='the target-list argument used to build qemu')
args = parser.parse_args()
generate(args.endpoint_list, args.qemu_targets)