Commit | Line | Data |
---|---|---|
bc33afd9 S |
1 | # simple build file to be used locally by Sam |
2 | # | |
3 | require 'pty' | |
7d0d783d | 4 | require 'optparse' |
bc33afd9 S |
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 | |
7d0d783d | 23 | docker_squash = "https://github.com/goldmann/docker-squash/archive/master.zip" |
4159764d RSS |
24 | run ("apt install python-pip") |
25 | run ("pip install --user '#{docker_squash}' --upgrade") | |
bc33afd9 S |
26 | end |
27 | ||
bc33afd9 | 28 | |
ad5b2d96 RSS |
29 | def build(image) |
30 | lines = run("cd #{image[:name]} && docker build .") | |
bc33afd9 | 31 | img = lines[-1]["successfully built ".length..-1].strip |
7d0d783d RSS |
32 | |
33 | if image[:squash] | |
34 | ||
ad5b2d96 RSS |
35 | if image[:layers_to_keep] == nil |
36 | run("docker-squash -t #{image[:tag]} --cleanup --verbose #{img}") | |
7d0d783d | 37 | else |
ad5b2d96 RSS |
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}") | |
7d0d783d RSS |
40 | end |
41 | ||
ad5b2d96 RSS |
42 | run("docker rmi #{img}") |
43 | ||
1959166b | 44 | else |
7d0d783d | 45 | run("docker tag #{img} #{image[:tag]}") |
1959166b | 46 | end |
bc33afd9 S |
47 | end |
48 | ||
7d0d783d | 49 | def bump(image, image_version) |
ad5b2d96 RSS |
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") | |
7d0d783d | 59 | end |
e8b9677b | 60 | |
7d0d783d RSS |
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| | |
ad5b2d96 | 65 | options[:image] = [i.to_sym] |
7d0d783d RSS |
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.7' | |
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] | |
ad5b2d96 RSS |
96 | |
97 | dev_deps() if image == 'discourse_dev' | |
98 | run "(cd base && ./download_phantomjs)" if image == 'base' | |
99 | ||
100 | build(images[image]) | |
7d0d783d | 101 | end |