You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gbuild 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. #!/usr/bin/ruby
  2. require 'optparse'
  3. require 'yaml'
  4. require 'fileutils'
  5. require 'pathname'
  6. @options = {:num_procs => 2, :memory => 2000}
  7. @bitness = {
  8. 'i386' => 32,
  9. 'amd64' => 64,
  10. }
  11. @arches = {
  12. 'i386' => 'i386',
  13. 'amd64' => 'x86_64',
  14. }
  15. def system!(cmd)
  16. system(cmd) or raise "failed to run #{cmd}"
  17. end
  18. def sanitize(str, where)
  19. raise "unsanitary string in #{where}" if (str =~ /[^\w.-]/)
  20. str
  21. end
  22. def sanitize_path(str, where)
  23. raise "unsanitary string in #{where}" if (str =~ /[^@\w\/.:+-]/)
  24. str
  25. end
  26. def info(str)
  27. puts str unless @options[:quiet]
  28. end
  29. def build_one_configuration(suite, arch, build_desc)
  30. FileUtils.rm_f("var/install.log")
  31. FileUtils.rm_f("var/build.log")
  32. bits = @bitness[arch] or raise "unknown architecture ${arch}"
  33. if ENV["USE_LXC"]
  34. ENV["LXC_ARCH"] = arch
  35. ENV["LXC_SUITE"] = suite
  36. end
  37. suitearch = "#{suite}-#{arch}"
  38. info "Stopping target if it is up"
  39. system "stop-target"
  40. sleep 1
  41. unless @options[:skip_image]
  42. info "Making a new image copy"
  43. system! "make-clean-vm --suite #{suite} --arch #{arch}"
  44. end
  45. info "Starting target"
  46. system! "start-target #{bits} #{suitearch}&"
  47. $stdout.write "Checking if target is up"
  48. (1..30).each do
  49. system "on-target true 2> /dev/null" and break
  50. sleep 2
  51. $stdout.write '.'
  52. end
  53. info ''
  54. system! "on-target true"
  55. system! "on-target -u root tee -a /etc/sudoers.d/#{ENV['DISTRO'] || 'ubuntu'} > /dev/null << EOF
  56. %#{ENV['DISTRO'] || 'ubuntu'} ALL=(ALL) NOPASSWD: ALL
  57. EOF" if build_desc["sudo"] and @options[:allow_sudo]
  58. info "Preparing build environment"
  59. system! "on-target setarch #{@arches[arch]} bash < target-bin/init-build.sh"
  60. build_desc["files"].each do |filename|
  61. filename = sanitize(filename, "files section")
  62. system! "copy-to-target #{@quiet_flag} inputs/#{filename} build/"
  63. end
  64. if build_desc["enable_cache"]
  65. if File.directory?("cache/#{build_desc["name"]}")
  66. system! "copy-to-target #{@quiet_flag} cache/#{build_desc["name"]}/ cache/"
  67. end
  68. if File.directory?("cache/common")
  69. system! "copy-to-target #{@quiet_flag} cache/common/ cache/"
  70. end
  71. end
  72. if build_desc["multiarch"]
  73. info "Adding multiarch support (log in var/install.log)"
  74. for a in build_desc["multiarch"]
  75. system! "on-target -u root dpkg --add-architecture #{a} >> var/install.log 2>&1"
  76. end
  77. end
  78. info "Updating apt-get repository (log in var/install.log)"
  79. system! "on-target -u root apt-get update >> var/install.log 2>&1"
  80. info "Installing additional packages (log in var/install.log)"
  81. system! "on-target -u root -e DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends -y install #{build_desc["packages"].join(" ")} >> var/install.log 2>&1"
  82. if build_desc["alternatives"]
  83. info "Set alternatives (log in var/install.log)"
  84. for a in build_desc["alternatives"]
  85. system! "on-target -u root update-alternatives --set #{a["package"]} #{a["path"]} >> var/install.log 2>&1"
  86. end
  87. end
  88. if @options[:upgrade] || system("on-target -u root '[ ! -e /var/cache/gitian/initial-upgrade ]'")
  89. info "Upgrading system, may take a while (log in var/install.log)"
  90. system! "on-target -u root bash < target-bin/upgrade-system.sh >> var/install.log 2>&1"
  91. end
  92. info "Creating package manifest"
  93. system! "on-target -u root bash < target-bin/grab-packages.sh > var/base-#{suitearch}.manifest"
  94. info "Creating build script (var/build-script)"
  95. File.open("var/build-script", "w") do |script|
  96. script.puts "#!/bin/bash"
  97. script.puts "set -e"
  98. script.puts "export LANG='en_US.UTF-8'"
  99. script.puts "export LC_ALL='en_US.UTF-8'"
  100. script.puts "umask 002"
  101. script.puts "export OUTDIR=$HOME/out"
  102. script.puts "GBUILD_BITS=#{bits}"
  103. if build_desc["enable_cache"]
  104. script.puts "GBUILD_CACHE_ENABLED=1"
  105. script.puts "GBUILD_PACKAGE_CACHE=$HOME/cache/#{build_desc["name"]}"
  106. script.puts "GBUILD_COMMON_CACHE=$HOME/cache/common"
  107. end
  108. script.puts "MAKEOPTS=(-j#{@options[:num_procs]})"
  109. script.puts
  110. author_date = nil
  111. build_desc["remotes"].each do |remote|
  112. dir = sanitize(remote["dir"], remote["dir"])
  113. author_date = `cd inputs/#{dir} && git log --format=@%at -1 | date +"%F %T" -u -f -`.strip
  114. raise "error looking up author date in #{dir}" unless $?.exitstatus == 0
  115. system! "copy-to-target #{@quiet_flag} inputs/#{dir} build/"
  116. script.puts "(cd build/#{dir} && git reset -q --hard && git clean -q -f -d)"
  117. end
  118. script.puts
  119. ref_datetime = build_desc["reference_datetime"] || author_date
  120. (ref_date, ref_time) = ref_datetime.split
  121. script.puts "REFERENCE_DATETIME='#{ref_datetime}'"
  122. script.puts "REFERENCE_DATE='#{ref_date}'"
  123. script.puts "REFERENCE_TIME='#{ref_time}'"
  124. script.puts
  125. script.puts "cd build"
  126. script.puts build_desc["script"]
  127. end
  128. info "Running build script (log in var/build.log)"
  129. system! "on-target setarch #{@arches[arch]} bash -x < var/build-script > var/build.log 2>&1"
  130. end
  131. ################################
  132. OptionParser.new do |opts|
  133. opts.banner = "Usage: build [options] <build-description>.yml"
  134. opts.on("--allow-sudo", "override SECURITY on the target VM and allow the use of sudo with no password for the default user") do |v|
  135. @options[:allow_sudo] = v
  136. end
  137. opts.on("-i", "--skip-image", "reuse current target image") do |v|
  138. @options[:skip_image] = v
  139. end
  140. opts.on("--upgrade", "upgrade guest with latest packages") do |v|
  141. @options[:upgrade] = v
  142. end
  143. opts.on("-q", "--quiet", "be quiet") do |v|
  144. @options[:quiet] = v
  145. end
  146. opts.on("-j PROCS", "--num-make PROCS", "number of processes to use") do |v|
  147. @options[:num_procs] = v
  148. end
  149. opts.on("-m MEM", "--memory MEM", "memory to allocate in MiB") do |v|
  150. @options[:memory] = v
  151. end
  152. opts.on("-c PAIRS", "--commit PAIRS", "comma separated list of DIRECTORY=COMMIT pairs") do |v|
  153. @options[:commit] = v
  154. end
  155. opts.on("-u PAIRS", "--url PAIRS", "comma separated list of DIRECTORY=URL pairs") do |v|
  156. @options[:url] = v
  157. end
  158. opts.on("-o", "--cache-read-only", "only use existing cache files, do not update them") do |v|
  159. @options[:cache_ro] = v
  160. end
  161. opts.on("--skip-fetch", "skip fetching the latest git objects and refs from the remote source") do |v|
  162. @options[:skip_fetch] = v
  163. end
  164. end.parse!
  165. if !ENV["USE_LXC"] and !ENV["USE_DOCKER"] and !ENV["USE_VBOX"] and !File.exist?("/dev/kvm")
  166. $stderr.puts "\n************* WARNING: kvm not loaded, this will probably not work out\n\n"
  167. end
  168. base_dir = Pathname.new(__FILE__).expand_path.dirname.parent
  169. libexec_dir = base_dir + 'libexec'
  170. ENV['PATH'] = libexec_dir.to_s + ":" + ENV['PATH']
  171. ENV['GITIAN_BASE'] = base_dir.to_s
  172. ENV['NPROCS'] = @options[:num_procs].to_s
  173. ENV['VMEM'] = @options[:memory].to_s
  174. @quiet_flag = @options[:quiet] ? "-q" : ""
  175. build_desc_file = ARGV.shift or raise "must supply YAML build description file"
  176. build_desc = YAML.load_file(build_desc_file)
  177. in_sums = []
  178. build_dir = 'build'
  179. result_dir = 'result'
  180. cache_dir = 'cache'
  181. enable_cache = build_desc["enable_cache"]
  182. FileUtils.rm_rf(build_dir)
  183. FileUtils.mkdir(build_dir)
  184. FileUtils.mkdir_p(result_dir)
  185. package_name = build_desc["name"] or raise "must supply name"
  186. package_name = sanitize(package_name, "package name")
  187. if enable_cache
  188. FileUtils.mkdir_p(File.join(cache_dir, "common"))
  189. FileUtils.mkdir_p(File.join(cache_dir, package_name))
  190. end
  191. distro = build_desc["distro"] || "ubuntu"
  192. suites = build_desc["suites"] or raise "must supply suites"
  193. archs = build_desc["architectures"] or raise "must supply architectures"
  194. build_desc["reference_datetime"] or build_desc["remotes"].size > 0 or raise "must supply `reference_datetime` or `remotes`"
  195. docker_image_digests = build_desc["docker_image_digests"] || []
  196. # if docker_image_digests are supplied, it must be the same length as suites
  197. if docker_image_digests.size > 0 and suites.size != docker_image_digests.size
  198. raise "`suites` and `docker_image_digests` must both be the same size if both are supplied"
  199. elsif ENV["USE_DOCKER"] and docker_image_digests.size > 0 and suites.size == docker_image_digests.size
  200. suites = docker_image_digests
  201. end
  202. ENV['DISTRO'] = distro
  203. desc_sum = `sha256sum #{build_desc_file}`
  204. desc_sum = desc_sum.sub(build_desc_file, "#{package_name}-desc.yml")
  205. in_sums << desc_sum
  206. build_desc["files"].each do |filename|
  207. filename = sanitize(filename, "files section")
  208. in_sums << `cd inputs && sha256sum #{filename}`
  209. end
  210. commits = {}
  211. if @options[:commit]
  212. @options[:commit].split(',').each do |pair|
  213. (dir, commit) = pair.split('=')
  214. commits[dir] = commit
  215. end
  216. end
  217. urls = {}
  218. if @options[:url]
  219. @options[:url].split(',').each do |pair|
  220. (dir, url) = pair.split('=')
  221. urls[dir] = url
  222. end
  223. end
  224. build_desc["remotes"].each do |remote|
  225. if !remote["commit"]
  226. remote["commit"] = commits[remote["dir"]]
  227. raise "must specify a commit for directory #{remote["dir"]}" unless remote["commit"]
  228. end
  229. if urls[remote["dir"]]
  230. remote["url"] = urls[remote["dir"]]
  231. end
  232. dir = sanitize(remote["dir"], remote["dir"])
  233. commit = sanitize(remote["commit"], remote["commit"])
  234. unless File.exist?("inputs/#{dir}")
  235. system!("git init inputs/#{dir}")
  236. end
  237. if !@options[:skip_fetch]
  238. system!("cd inputs/#{dir} && git fetch --update-head-ok #{sanitize_path(remote["url"], remote["url"])} +refs/tags/*:refs/tags/* +refs/heads/*:refs/heads/*")
  239. system!("cd inputs/#{dir} && git checkout -q #{commit}")
  240. system!("cd inputs/#{dir} && git submodule update --init --recursive --force")
  241. end
  242. commit = `cd inputs/#{dir} && git log --format=%H -1 #{commit}`.strip
  243. raise "error looking up commit for tag #{remote["commit"]}" unless $?.exitstatus == 0
  244. in_sums << "git:#{commit} #{dir}"
  245. end
  246. base_manifests = YAML::Omap.new
  247. suites.each do |suite|
  248. suite = sanitize(suite, "suite")
  249. archs.each do |arch|
  250. info "--- Building for #{suite} #{arch} ---"
  251. arch = sanitize(arch, "architecture")
  252. # Build!
  253. build_one_configuration(suite, arch, build_desc)
  254. info "Grabbing results from target"
  255. system! "copy-from-target #{@quiet_flag} out #{build_dir}"
  256. if enable_cache && !@options[:cache_ro]
  257. info "Grabbing cache from target"
  258. system! "copy-from-target #{@quiet_flag} cache/#{package_name}/ #{cache_dir}"
  259. system! "copy-from-target #{@quiet_flag} cache/common/ #{cache_dir}"
  260. end
  261. base_manifest = File.read("var/base-#{suite}-#{arch}.manifest")
  262. base_manifests["#{suite}-#{arch}"] = base_manifest
  263. end
  264. end
  265. out_dir = File.join(build_dir, "out")
  266. out_sums = {}
  267. cache_common_dir = File.join(cache_dir, "common")
  268. cache_package_dir = File.join(cache_dir, "#{package_name}")
  269. cache_common_sums = {}
  270. cache_package_sums = {}
  271. info "Generating report"
  272. Dir.glob(File.join(out_dir, '**', '*'), File::FNM_DOTMATCH).sort.each do |file_in_out|
  273. next if File.directory?(file_in_out)
  274. file = file_in_out.sub(out_dir + File::SEPARATOR, '')
  275. file = sanitize_path(file, file_in_out)
  276. out_sums[file] = `cd #{out_dir} && sha256sum #{file}`
  277. raise "failed to sum #{file}" unless $? == 0
  278. puts out_sums[file] unless @options[:quiet]
  279. end
  280. if enable_cache
  281. Dir.glob(File.join(cache_common_dir, '**', '*'), File::FNM_DOTMATCH).sort.each do |file_in_out|
  282. next if File.directory?(file_in_out)
  283. file = file_in_out.sub(cache_common_dir + File::SEPARATOR, '')
  284. file = sanitize_path(file, file_in_out)
  285. cache_common_sums[file] = `cd #{cache_common_dir} && sha256sum #{file}`
  286. raise "failed to sum #{file}" unless $? == 0
  287. end
  288. Dir.glob(File.join(cache_package_dir, '**', '*'), File::FNM_DOTMATCH).sort.each do |file_in_out|
  289. next if File.directory?(file_in_out)
  290. file = file_in_out.sub(cache_package_dir + File::SEPARATOR, '')
  291. file = sanitize_path(file, file_in_out)
  292. cache_package_sums[file] = `cd #{cache_package_dir} && sha256sum #{file}`
  293. raise "failed to sum #{file}" unless $? == 0
  294. end
  295. end
  296. out_manifest = out_sums.keys.sort.map { |key| out_sums[key] }.join('')
  297. in_manifest = in_sums.join('')
  298. cache_common_manifest = cache_common_sums.keys.sort.map { |key| cache_common_sums[key] }.join('')
  299. cache_package_manifest = cache_package_sums.keys.sort.map { |key| cache_package_sums[key] }.join('')
  300. # Use Omap to keep result deterministic
  301. report = YAML::Omap[
  302. 'out_manifest', out_manifest,
  303. 'in_manifest', in_manifest,
  304. 'base_manifests', base_manifests,
  305. 'cache_common_manifest', cache_common_manifest,
  306. 'cache_package_manifest', cache_package_manifest,
  307. ]
  308. result_file = "#{package_name}-res.yml"
  309. File.open(File.join(result_dir, result_file), "w") do |io|
  310. io.write report.to_yaml
  311. end
  312. system!("cd #{result_dir} && sha256sum #{result_file}") unless @options[:quiet]
  313. info "Done."