From: Rafael dos Santos Silva Date: Wed, 19 Oct 2016 20:31:18 +0000 (-0200) Subject: Adds Makefile functions to build.rb X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=7d0d783d63974e7405d97c27a39cec1c42bcb5ba;p=discourse_docker.git Adds Makefile functions to build.rb --- diff --git a/image/Makefile b/image/Makefile deleted file mode 100644 index 731b5ea..0000000 --- a/image/Makefile +++ /dev/null @@ -1,81 +0,0 @@ -IMAGE_VERSION := 1.3.7 - -SHELL := /bin/bash -OS := $(shell uname | tr '[:upper:]' '[:lower:]') - -SQUASH ?= YES - -DOCKER_SQUASH_URL := https://github.com/jwilder/docker-squash/releases/download/v0.2.0/docker-squash-${OS}-amd64-v0.2.0.tar.gz - - - -# omitting discourse_fast_switch from 'all' as it seems obsolete. -all: base discourse discourse_dev discourse_test discourse_bench -.PHONY: all base discourse discourse_dev discourse_test discourse_bench discourse_fast_switch - -base: base-deps -# discourse: discourse-deps -discourse_dev: discourse_dev-deps -# discourse_test: discourse_test-deps -# discourse_bench: discourse_bench-deps -# discourse_fast_switch: discourse_fast_switch-deps - -base: - @IMAGE_DIR=$@ IS_BASE=YES ${MAKE} build-image - -discourse discourse_dev discourse_test discourse_bench discourse_fast_switch: - @IMAGE_DIR=$@ ${MAKE} build-image - - -base-deps: base/phantomjs -.PHONY: base-deps - -base/phantomjs: - @echo "Fetching phantomjs..." - cd base && ./download_phantomjs - -discourse_dev-deps: - @echo "Copying postgres and redis templates..." - sed -e 's/\(db_name: discourse\)/\1_development/' ../templates/postgres.template.yml > discourse_dev/postgres.template.yml - cp ../templates/redis.template.yml discourse_dev/redis.template.yml -.PHONY: discourse_dev-deps - -# If you don't want to docker-squash the image, prefix your command line with -# SQUASH=NO, like: -# SQUASH=NO make discourse_dev -build-image: docker-squash update-dockerfile - @echo "----- building image: discourse/${IMAGE_DIR}:${IMAGE_VERSION} -----" -ifeq (${SQUASH}, YES) - docker build ${IMAGE_DIR} | tee .build.out - tail -1 .build.out | awk '/^Successfully built / {print $$3}' > .build.hash - @echo "squashing $$(cat .build.hash)..." - docker save -o img.tar $$(cat .build.hash) - sudo ./docker-squash -verbose -i img.tar -o squash.tar $(if $(IS_BASE),-from root) - docker load -i squash.tar - docker tag $$(cat .build.hash) discourse/${IMAGE_DIR}:${IMAGE_VERSION} - rm -f squash.tar - rm -f img.tar - rm -f .build.hash - rm -f .build.out -else - docker build -t discourse/${IMAGE_DIR}:${IMAGE_VERSION} ${IMAGE_DIR} -endif -.PHONY: build-image - -docker-squash: - wget ${DOCKER_SQUASH_URL} --output-document=$@.tar.gz - tar -xzvf $@.tar.gz - rm -f $@.tar.gz - -# We want to ensure that the Dockerfiles all have an appropriate header -# comment, and any FROM line refers to the version we're currently building. -# Also, if there's a line that echoes into VERSION, replace the value with the -# current version. (Dockerfiles support environment expansion, but not for -# FROM or RUN statements.) -update-dockerfile: - @echo "----- updating ${IMAGE_DIR}/Dockerfile for ${IMAGE_VERSION} -----" - @sed -i '' -e 's/^\(# NAME:\).*$$/\1 discourse\/${IMAGE_DIR}/' ${IMAGE_DIR}/Dockerfile - @sed -i '' -e 's/^\(# VERSION:\).*$$/\1 ${IMAGE_VERSION}/' ${IMAGE_DIR}/Dockerfile - @sed -i '' -e 's/^\(FROM discourse\/[^:]*:\).*/\1${IMAGE_VERSION}/' ${IMAGE_DIR}/Dockerfile - @sed -i '' -e 's/^\(RUN echo "\)[^"]*\(" > \/VERSION\)$$/\1${IMAGE_VERSION}\2/' ${IMAGE_DIR}/Dockerfile -.PHONY: update-dockerfile diff --git a/image/base/Dockerfile b/image/base/Dockerfile index 7429610..3a4e0c8 100644 --- a/image/base/Dockerfile +++ b/image/base/Dockerfile @@ -7,7 +7,7 @@ ENV PG_VERSION 9.5.4-1.pgdg16.04+2 MAINTAINER Sam Saffron "https://twitter.com/samsaffron" -RUN echo "1.3.7" > /VERSION +RUN echo "${version}" > /VERSION RUN apt-get update && apt-get install -y lsb-release sudo curl RUN echo "debconf debconf/frontend select Teletype" | debconf-set-selections diff --git a/image/build.rb b/image/build.rb index 8069d5b..30a0420 100644 --- a/image/build.rb +++ b/image/build.rb @@ -1,19 +1,7 @@ # simple build file to be used locally by Sam # require 'pty' - -$version = "1.3.7" - -$docker_squash = "https://github.com/goldmann/docker-squash/archive/master.zip" - -$base_image = "discourse/base:#{$version}" -$image = "discourse/discourse:#{$version}" -$test = "discourse/discourse_test:#{$version}" - -if ENV["USER"] != "root" - STDERR.puts "Build script must be ran as root due to docker-squash" - exit 1 -end +require 'optparse' def run(command) lines = [] @@ -32,26 +20,71 @@ def run(command) end def ensure_docker_squash - run ("pip install --user #{$docker_squash}") + docker_squash = "https://github.com/goldmann/docker-squash/archive/master.zip" + run ("pip install --user #{$docker_squash} --upgrade") end -ensure_docker_squash -def build(path, tag, layers_to_keep = nil) - lines = run("cd #{path} && docker build .") +def build(image, version) + lines = run("cd #{image[:name]} && docker build --build-arg version=#{version} .") img = lines[-1]["successfully built ".length..-1].strip - layers_to_squash = run("docker history #{img} | wc -l").first.to_i - (1 + layers_to_keep) if layers_to_keep - if layers_to_keep != nil - puts "docker-squash -t #{tag} --verbose -f #{layers_to_squash} #{img}" - run("docker-squash -t #{tag} --verbose -f #{layers_to_squash} #{img}") + + if image[:squash] + + layers_to_squash = run("docker history #{img} | wc -l").first.to_i - (1 + image[:layers_to_keep]) + + if layers_to_keep != nil + run("docker-squash -t #{image[:tag]} --verbose -f #{layers_to_squash} #{img}") + else + run("docker-squash -t #{image[:tag]} --verbose #{img}") + end + else - puts "docker-squash -t #{tag} --verbose #{img}" - run("docker-squash -t #{tag} --verbose #{img}") + run("docker tag #{img} #{image[:tag]}") end end -run "(cd base && ./download_phantomjs)" +def bump(image, image_version) + run("sed -i '' -e 's/^\(# NAME:\).*$$/\1 discourse\/#{image_dir}/' #{image_dir}/Dockerfile") + run("sed -i '' -e 's/^\(# VERSION:\).*$$/\1 #{image_version}/' #{image_dir}/Dockerfile") + run("sed -i '' -e 's/^\(FROM discourse\/[^:]*:\).*/\1#{image_version}/' #{image_dir}/Dockerfile") +end -build("base",$base_image) -build("discourse",$image,1) -build("discourse_test",$test,2) +options = {} +OptionParser.new do |parser| + parser.on("-i", "--image image", + "Build the image. No parameter means [base discourse discourse_test].") do |i| + options[:image] = [i] + end + parser.on("-b", "--bump version", + "Bumps the version in the Dockerfiles specified by --image") do |v| + options[:version] = [v] + end +end.parse! + +DEFAULT_IMAGES = %i[base discourse discourse_test discourse_dev discourse_bench] + +todo = options[:image] || DEFAULT_IMAGES +version = options[:version] || '1.3.7' + +if ENV["USER"] != "root" + STDERR.puts "Build script must be ran as root due to docker-squash" + exit 1 +end + +ensure_docker_squash + +images = { + base: { name: 'base', tag: "discourse/base:#{version}", squash: true }, + discourse: { name: 'discourse', tag: "discourse/discourse:#{version}", squash: true, layers_to_keep: 1 }, + discourse_test: { name: 'discourse_test', tag: "discourse/discourse_test:#{version}", squash: true, layers_to_keep: 2 }, + discourse_dev: { name: 'discourse_dev', tag: "discourse/discourse_dev:#{version}", squash: false }, + discourse_bench: { name: 'discourse_bench', tag: "discourse/discourse_bench:#{version}", squash: false } +} + +todo.each do |image| + puts images[image] + bump(images[image][:name], options[:version]) if options[:version] + run "(cd base && ./download_phantomjs)" if image == 'base' + build(images[image], version) +end diff --git a/image/discourse_dev/build b/image/discourse_dev/build deleted file mode 100755 index 6dfd5a4..0000000 --- a/image/discourse_dev/build +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -docker build -t discourse/dev:1.0.13 . -docker save discourse/dev:1.0.13 | ../docker-squash -t discourse/dev:1.0.13 -verbose -from root | docker load -docker tag -f discourse/dev:1.0.13 discourse/dev:latest -