Debian Codegraph @

contrib/gitian/gitian-build.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 import argparse
4 import os
5 import subprocess
6 import sys
7
8 gsigs = 'https://github.com/monero-project/gitian.sigs.git'
9 gbrepo = 'https://github.com/devrandom/gitian-builder.git'
10
11 platforms = {'l': ['Linux', 'linux', 'tar.bz2'],
12 'a': ['Android', 'android', 'tar.bz2'],
13 'f': ['FreeBSD', 'freebsd', 'tar.bz2'],
14 'w': ['Windows', 'win', 'zip'],
15 'm': ['MacOS', 'osx', 'tar.bz2'] }
16
17 def setup():
18 global args, workdir
19 programs = ['apt-cacher-ng', 'ruby', 'git', 'make', 'wget']
20 if args.kvm:
21 programs += ['python-vm-builder', 'qemu-kvm', 'qemu-utils']
22 else:
23 programs += ['lxc', 'debootstrap']
24 if not args.no_apt:
25 subprocess.check_call(['sudo', 'apt-get', 'install', '-qq'] + programs)
26 if not os.path.isdir('sigs'):
27 subprocess.check_call(['git', 'clone', gsigs, 'sigs'])
28 if not os.path.isdir('builder'):
29 subprocess.check_call(['git', 'clone', gbrepo, 'builder'])
30 os.chdir('builder')
31 subprocess.check_call(['git', 'checkout', 'c0f77ca018cb5332bfd595e0aff0468f77542c23'])
32 os.makedirs('inputs', exist_ok=True)
33 os.chdir('inputs')
34 if os.path.isdir('monero'):
35 # Remove the potentially stale monero dir. Otherwise you might face submodule mismatches.
36 subprocess.check_call(['rm', 'monero', '-fR'])
37 subprocess.check_call(['git', 'clone', args.url, 'monero'])
38 os.chdir('..')
39 make_image_prog = ['bin/make-base-vm', '--suite', 'bionic', '--arch', 'amd64']
40 if args.docker:
41 try:
42 subprocess.check_output(['docker', '--help'])
43 except:
44 print("ERROR: Could not find 'docker' command. Ensure this is in your PATH.")
45 sys.exit(1)
46 make_image_prog += ['--docker']
47 elif not args.kvm:
48 make_image_prog += ['--lxc']
49 subprocess.check_call(make_image_prog)
50 os.chdir(workdir)
51 if args.is_bionic and not args.kvm and not args.docker:
52 subprocess.check_call(['sudo', 'sed', '-i', 's/lxcbr0/br0/', '/etc/default/lxc-net'])
53 print('Reboot is required')
54 sys.exit(0)
55
56 def rebuild():
57 global args, workdir
58
59 print('\nBuilding Dependencies\n')
60 os.makedirs('../out/' + args.version, exist_ok=True)
61
62
63 for i in args.os:
64 os_name = platforms[i][0]
65 tag_name = platforms[i][1]
66 suffix = platforms[i][2]
67
68 print('\nCompiling ' + args.version + ' ' + os_name)
69 infile = 'inputs/monero/contrib/gitian/gitian-' + tag_name + '.yml'
70 subprocess.check_call(['bin/gbuild', '-j', args.jobs, '-m', args.memory, '--commit', 'monero='+args.commit, '--url', 'monero='+args.url, infile])
71 subprocess.check_call(['bin/gsign', '-p', args.sign_prog, '--signer', args.signer, '--release', args.version+'-'+tag_name, '--destination', '../sigs/', infile])
72 subprocess.check_call('mv build/out/monero-*.' + suffix + ' ../out/'+args.version, shell=True)
73 print('Moving var/install.log to var/install-' + tag_name + '.log')
74 subprocess.check_call('mv var/install.log var/install-' + tag_name + '.log', shell=True)
75 print('Moving var/build.log to var/build-' + tag_name + '.log')
76 subprocess.check_call('mv var/build.log var/build-' + tag_name + '.log', shell=True)
77
78 os.chdir(workdir)
79
80 if args.commit_files:
81 print('\nCommitting '+args.version+' Unsigned Sigs\n')
82 os.chdir('sigs')
83 for i, v in platforms:
84 subprocess.check_call(['git', 'add', args.version+'-'+v[1]+'/'+args.signer])
85 subprocess.check_call(['git', 'commit', '-m', 'Add '+args.version+' unsigned sigs for '+args.signer])
86 os.chdir(workdir)
87
88
89 def build():
90 global args, workdir
91
92 print('\nChecking Depends Freshness\n')
93 os.chdir('builder')
94 os.makedirs('inputs', exist_ok=True)
95
96 subprocess.check_call(['make', '-C', 'inputs/monero/contrib/depends', 'download', 'SOURCES_PATH=' + os.getcwd() + '/cache/common'])
97
98 rebuild()
99
100
101 def verify():
102 global args, workdir
103 os.chdir('builder')
104
105 for i, v in platforms:
106 print('\nVerifying v'+args.version+' '+v[0]+'\n')
107 subprocess.check_call(['bin/gverify', '-v', '-d', '../sigs/', '-r', args.version+'-'+v[1], 'inputs/monero/contrib/gitian/gitian-'+v[1]+'.yml'])
108 os.chdir(workdir)
109
110 def main():
111 global args, workdir
112
113 parser = argparse.ArgumentParser(description='Script for running full Gitian builds.', usage='%(prog)s [options] signer version')
114 parser.add_argument('-c', '--commit', action='store_true', dest='commit', help='Indicate that the version argument is for a commit or branch')
115 parser.add_argument('-p', '--pull', action='store_true', dest='pull', help='Indicate that the version argument is the number of a github repository pull request')
116 parser.add_argument('-u', '--url', dest='url', default='https://github.com/monero-project/monero', help='Specify the URL of the repository. Default is %(default)s')
117 parser.add_argument('-v', '--verify', action='store_true', dest='verify', help='Verify the Gitian build')
118 parser.add_argument('-b', '--build', action='store_true', dest='build', help='Do a Gitian build')
119 parser.add_argument('-B', '--buildsign', action='store_true', dest='buildsign', help='Build both signed and unsigned binaries')
120 parser.add_argument('-o', '--os', dest='os', default='lafwm', help='Specify which Operating Systems the build is for. Default is %(default)s. l for Linux, a for Android, f for FreeBSD, w for Windows, m for MacOS')
121 parser.add_argument('-r', '--rebuild', action='store_true', dest='rebuild', help='Redo a Gitian build')
122 parser.add_argument('-R', '--rebuildsign', action='store_true', dest='rebuildsign', help='Redo and sign a Gitian build')
123 parser.add_argument('-j', '--jobs', dest='jobs', default='2', help='Number of processes to use. Default %(default)s')
124 parser.add_argument('-m', '--memory', dest='memory', default='2000', help='Memory to allocate in MiB. Default %(default)s')
125 parser.add_argument('-k', '--kvm', action='store_true', dest='kvm', help='Use KVM instead of LXC')
126 parser.add_argument('-d', '--docker', action='store_true', dest='docker', help='Use Docker instead of LXC')
127 parser.add_argument('-S', '--setup', action='store_true', dest='setup', help='Set up the Gitian building environment. Uses LXC. If you want to use KVM, use the --kvm option. If you run this script on a non-debian based system, pass the --no-apt flag')
128 parser.add_argument('-D', '--detach-sign', action='store_true', dest='detach_sign', help='Create the assert file for detached signing. Will not commit anything.')
129 parser.add_argument('-n', '--no-commit', action='store_false', dest='commit_files', help='Do not commit anything to git')
130 parser.add_argument('signer', nargs='?', help='GPG signer to sign each build assert file')
131 parser.add_argument('version', nargs='?', help='Version number, commit, or branch to build.')
132 parser.add_argument('-a', '--no-apt', action='store_true', dest='no_apt', help='Indicate that apt is not installed on the system')
133
134 args = parser.parse_args()
135 workdir = os.getcwd()
136
137 args.is_bionic = b'bionic' in subprocess.check_output(['lsb_release', '-cs'])
138
139 if args.buildsign:
140 args.build = True
141 args.sign = True
142
143 if args.rebuildsign:
144 args.rebuild = True
145 args.sign = True
146
147 if args.kvm and args.docker:
148 raise Exception('Error: cannot have both kvm and docker')
149
150 args.sign_prog = 'true' if args.detach_sign else 'gpg --detach-sign'
151
152 # Set enviroment variable USE_LXC or USE_DOCKER, let gitian-builder know that we use lxc or docker
153 if args.docker:
154 os.environ['USE_DOCKER'] = '1'
155 elif not args.kvm:
156 os.environ['USE_LXC'] = '1'
157 if not 'GITIAN_HOST_IP' in os.environ.keys():
158 os.environ['GITIAN_HOST_IP'] = '10.0.2.2'
159 if not 'LXC_GUEST_IP' in os.environ.keys():
160 os.environ['LXC_GUEST_IP'] = '10.0.2.5'
161
162 script_name = os.path.basename(sys.argv[0])
163 # Signer and version shouldn't be empty
164 if args.signer == '':
165 print(script_name+': Missing signer.')
166 print('Try '+script_name+' --help for more information')
167 sys.exit(1)
168 if args.version == '':
169 print(script_name+': Missing version.')
170 print('Try '+script_name+' --help for more information')
171 sys.exit(1)
172
173 # Add leading 'v' for tags
174 if args.commit and args.pull:
175 raise Exception('Cannot have both commit and pull')
176 args.commit = args.commit if args.commit else args.version
177
178 if args.setup:
179 setup()
180
181 os.makedirs('builder/inputs/monero', exist_ok=True)
182 os.chdir('builder/inputs/monero')
183 if args.pull:
184 subprocess.check_call(['git', 'fetch', args.url, 'refs/pull/'+args.version+'/merge'])
185 args.commit = subprocess.check_output(['git', 'show', '-s', '--format=%H', 'FETCH_HEAD'], universal_newlines=True).strip()
186 args.version = 'pull-' + args.version
187 print(args.commit)
188 subprocess.check_call(['git', 'fetch'])
189 subprocess.check_call(['git', 'checkout', args.commit])
190 os.chdir(workdir)
191
192 if args.build:
193 build()
194
195 if args.rebuild:
196 os.chdir('builder')
197 rebuild()
198
199 if args.verify:
200 verify()
201
202 if __name__ == '__main__':
203 main()