Bumps ImageMagick version
[discourse_docker.git] / image / build.rb
1 # simple build file to be used locally by Sam
2 #
3 require 'pty'
4 require 'optparse'
5
6 def run(command)
7 lines = []
8 PTY.spawn(command) do |stdin, stdout, pid|
9 begin
10 stdin.each do |line|
11 lines << line
12 puts line
13 end
14 rescue Errno::EIO
15 # we are done
16 end
17 end
18
19 lines
20 end
21
22 def ensure_docker_squash
23 docker_squash = "https://github.com/goldmann/docker-squash/archive/master.zip"
24 run ("apt install python-pip")
25 run ("pip install '#{docker_squash}' --upgrade")
26 end
27
28
29 def build(image)
30 lines = run("cd #{image[:name]} && docker build .")
31 img = lines[-1]["successfully built ".length..-1].strip
32
33 if image[:squash]
34
35 if image[:layers_to_keep] == nil
36 run("docker-squash -t #{image[:tag]} --cleanup --verbose #{img}")
37 else
38 layers_to_squash = run("docker history #{img} | wc -l").first.to_i - (1 + image[:layers_to_keep])
39 run("docker-squash -t #{image[:tag]} --cleanup --verbose -f #{layers_to_squash} #{img}")
40 end
41
42 run("docker rmi #{img}")
43
44 else
45 run("docker tag #{img} #{image[:tag]}")
46 end
47 end
48
49 def bump(image, image_version)
50 run("echo #{image_version} > base/VERSION") if image == 'base'
51 run("sed -i '' -e 's/^\(# NAME:\).*$$/\1 discourse\/#{image}/' #{image}/Dockerfile")
52 run("sed -i '' -e 's/^\(# VERSION:\).*$$/\1 #{image_version}/' #{image}/Dockerfile")
53 run("sed -i '' -e 's/^\(FROM discourse\/[^:]*:\).*/\1#{image_version}/' #{image}/Dockerfile")
54 end
55
56 def dev_deps()
57 run("sed -e 's/\(db_name: discourse\)/\1_development/' ../templates/postgres.template.yml > discourse_dev/postgres.template.yml")
58 run("cp ../templates/redis.template.yml discourse_dev/redis.template.yml")
59 end
60
61 options = {}
62 OptionParser.new do |parser|
63 parser.on("-i", "--image image",
64 "Build the image. No parameter means [base discourse discourse_test].") do |i|
65 options[:image] = [i.to_sym]
66 end
67 parser.on("-b", "--bump version",
68 "Bumps the version in the Dockerfiles specified by --image") do |v|
69 options[:version] = [v]
70 end
71 end.parse!
72
73 DEFAULT_IMAGES = %i[base discourse discourse_test discourse_dev discourse_bench]
74
75 todo = options[:image] || DEFAULT_IMAGES
76 version = options[:version] || '1.3.10'
77
78 if ENV["USER"] != "root"
79 STDERR.puts "Build script must be ran as root due to docker-squash"
80 exit 1
81 end
82
83 ensure_docker_squash
84
85 images = {
86 base: { name: 'base', tag: "discourse/base:#{version}", squash: true },
87 discourse: { name: 'discourse', tag: "discourse/discourse:#{version}", squash: true, layers_to_keep: 1 },
88 discourse_test: { name: 'discourse_test', tag: "discourse/discourse_test:#{version}", squash: true, layers_to_keep: 2 },
89 discourse_dev: { name: 'discourse_dev', tag: "discourse/discourse_dev:#{version}", squash: false },
90 discourse_bench: { name: 'discourse_bench', tag: "discourse/discourse_bench:#{version}", squash: false }
91 }
92
93 todo.each do |image|
94 puts images[image]
95 bump(images[image][:name], options[:version]) if options[:version]
96
97 dev_deps() if image == :discourse_dev
98 run "(cd base && ./download_phantomjs)" if image == :base
99
100 build(images[image])
101 end