From 350b9339606f8f3ef202556a4ca959602e6d7c7a Mon Sep 17 00:00:00 2001 From: Francis Giraldeau Date: Sat, 7 Jul 2018 17:46:47 -0400 Subject: [PATCH] Add basic dockerfile The docker image include osrm and evnav. Signed-off-by: Francis Giraldeau --- .gitmodules | 3 + 3rdparty/osrm-backend | 1 + docker/.make-release-support | 105 ++++++++++++++++++++++++++++++++ docker/.release | 2 + docker/Dockerfile | 80 +++++++++++++++++++++++++ docker/Makefile | 13 ++++ docker/Makefile.mk | 112 +++++++++++++++++++++++++++++++++++ 7 files changed, 316 insertions(+) create mode 160000 3rdparty/osrm-backend create mode 100644 docker/.make-release-support create mode 100644 docker/.release create mode 100644 docker/Dockerfile create mode 100644 docker/Makefile create mode 100644 docker/Makefile.mk diff --git a/.gitmodules b/.gitmodules index cfb67a0..01c6e0c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "3rdparty/rapidjson"] path = 3rdparty/rapidjson url = https://github.com/miloyip/rapidjson +[submodule "3rdparty/osrm-backend"] + path = 3rdparty/osrm-backend + url = https://github.com/Project-OSRM/osrm-backend.git diff --git a/3rdparty/osrm-backend b/3rdparty/osrm-backend new file mode 160000 index 0000000..3a7b377 --- /dev/null +++ b/3rdparty/osrm-backend @@ -0,0 +1 @@ +Subproject commit 3a7b377586a7019105c469b4c77158b956021bfd diff --git a/docker/.make-release-support b/docker/.make-release-support new file mode 100644 index 0000000..06ecbcc --- /dev/null +++ b/docker/.make-release-support @@ -0,0 +1,105 @@ +#!/bin/bash +# +# Copyright 2015 Xebia Nederland B.V. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +function hasChanges() { + test -n "$(git status -s .)" +} + +function getRelease() { + awk -F= '/^release=/{print $2}' .release +} + +function getBaseTag() { + sed -n -e "s/^tag=\(.*\)$(getRelease)\$/\1/p" .release +} + +function getTag() { + if [ -z "$1" ] ; then + awk -F= '/^tag/{print $2}' .release + else + echo "$(getBaseTag)$1" + fi +} + +function setRelease() { + if [ -n "$1" ] ; then + sed -i.x -e "s/^tag=.*/tag=$(getTag $1)/" .release + sed -i.x -e "s/^release=.*/release=$1/g" .release + rm -f .release.x + runPreTagCommand "$1" + else + echo "ERROR: missing release version parameter " >&2 + return 1 + fi +} + +function runPreTagCommand() { + if [ -n "$1" ] ; then + COMMAND=$(sed -n -e "s/@@RELEASE@@/$1/g" -e 's/^pre_tag_command=\(.*\)/\1/p' .release) + if [ -n "$COMMAND" ] ; then + if ! OUTPUT=$(bash -c "$COMMAND" 2>&1) ; then echo $OUTPUT >&2 && exit 1 ; fi + fi + else + echo "ERROR: missing release version parameter " >&2 + return 1 + fi +} + +function tagExists() { + tag=${1:-$(getTag)} + test -n "$tag" && test -n "$(git tag | grep "^$tag\$")" +} + +function differsFromRelease() { + tag=$(getTag) + ! tagExists $tag || test -n "$(git diff --shortstat -r $tag .)" +} + +function getVersion() { + result=$(getRelease) + + if differsFromRelease; then + result="$result-$(git log -n 1 --format=%h .)" + fi + + if hasChanges ; then + result="$result-dirty" + fi + echo $result +} + +function nextPatchLevel() { + version=${1:-$(getRelease)} + major_and_minor=$(echo $version | cut -d. -f1,2) + patch=$(echo $version | cut -d. -f3) + version=$(printf "%s.%d" $major_and_minor $(($patch + 1))) + echo $version +} + +function nextMinorLevel() { + version=${1:-$(getRelease)} + major=$(echo $version | cut -d. -f1); + minor=$(echo $version | cut -d. -f2); + version=$(printf "%d.%d.0" $major $(($minor + 1))) ; + echo $version +} + +function nextMajorLevel() { + version=${1:-$(getRelease)} + major=$(echo $version | cut -d. -f1); + version=$(printf "%d.0.0" $(($major + 1))) + echo $version +} diff --git a/docker/.release b/docker/.release new file mode 100644 index 0000000..157c9fd --- /dev/null +++ b/docker/.release @@ -0,0 +1,2 @@ +release=1.0.0 +tag=evnav-1.0.0 diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..48a21bd --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,80 @@ +FROM ubuntu:bionic as buildstage + +RUN apt-get update && apt-get install -y \ + curl \ + build-essential \ + git \ + qt5-default \ + cmake \ + pkg-config \ + libbz2-dev \ + libxml2-dev \ + libzip-dev \ + libboost-all-dev \ + lua5.3 \ + liblua5.3-dev \ + libtbb-dev + +ARG DOCKER_TAG +RUN mkdir -p /src && mkdir -p /opt +COPY . /src + +# Build OSRM first +# The last step must not delete the sources +WORKDIR /src/3rdparty/osrm-backend +RUN NPROC=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) && \ + echo "Building OSRM ${DOCKER_TAG}" && \ + git show --format="%H" | head -n1 > /opt/OSRM_GITSHA && \ + echo "Building OSRM gitsha $(cat /opt/OSRM_GITSHA)" && \ + mkdir -p build && \ + cd build && \ + BUILD_TYPE="Release" && \ + ENABLE_ASSERTIONS="Off" && \ + BUILD_TOOLS="Off" && \ + case ${DOCKER_TAG} in *"-debug"*) BUILD_TYPE="Debug";; esac && \ + case ${DOCKER_TAG} in *"-assertions"*) BUILD_TYPE="RelWithDebInfo" && ENABLE_ASSERTIONS="On" && BUILD_TOOLS="On";; esac && \ + echo "Building ${BUILD_TYPE} with ENABLE_ASSERTIONS=${ENABLE_ASSERTIONS} BUILD_TOOLS=${BUILD_TOOLS}" && \ + cmake .. -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DENABLE_ASSERTIONS=${ENABLE_ASSERTIONS} -DBUILD_TOOLS=${BUILD_TOOLS} -DENABLE_LTO=On && \ + make -j${NPROC} install && \ + cd ../profiles && \ + cp -r * /opt && \ + strip /usr/local/bin/* + +# Build evnav +WORKDIR /src +RUN NPROC=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) && \ + git show --format="%H" | head -n1 > /opt/EVNAV_GITSHA && \ + echo "Building EVNAV gitsha $(cat /opt/EVNAV_GITSHA)" && \ + mkdir -p build && \ + cd build && \ + BUILD_TYPE="Release" && \ + case ${DOCKER_TAG} in *"-debug"*) BUILD_TYPE="Debug";; esac && \ + cmake .. -DCMAKE_BUILD_TYPE=${BUILD_TYPE} && \ + make -j${NPROC} install && \ + strip /usr/local/bin/* + +# Multistage build to reduce image size - https://docs.docker.com/engine/userguide/eng-image/multistage-build/#use-multi-stage-builds +# Only the content below ends up in the image, this helps remove /src from the image (which is large) +FROM ubuntu:bionic as runstage +RUN apt-get update && apt-get install -y \ + curl \ + libboost-filesystem1.65.1 \ + libboost-iostreams1.65.1 \ + libboost-program-options1.65.1 \ + libboost-regex1.65.1 \ + libboost-system1.65.1 \ + libboost-thread1.65.1 \ + libbz2-1.0 \ + libpcre3 \ + libqt5core5a \ + libqt5network5 \ + libtbb2 \ + zlib1g \ + lua5.3 +RUN mkdir -p /opt +COPY --from=buildstage /usr/local /usr/local +COPY --from=buildstage /opt /opt +WORKDIR /opt + +EXPOSE 5000 +EXPOSE 8080 diff --git a/docker/Makefile b/docker/Makefile new file mode 100644 index 0000000..4b8f281 --- /dev/null +++ b/docker/Makefile @@ -0,0 +1,13 @@ +# Ref: https://stackoverflow.com/questions/18136918/how-to-get-current-relative-directory-of-your-makefile +mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) +current_dir := $(notdir $(patsubst %/,%,$(dir $(mkfile_path)))) + +# The working directory must be the directory containing the Dockerfile + +include Makefile.mk + +REGISTRY_HOST=registry.gitlab.com +USERNAME=giraldeauf +NAME=evnav + +DOCKER_BUILD_ARGS=--build-arg DOCKER_TAG=release diff --git a/docker/Makefile.mk b/docker/Makefile.mk new file mode 100644 index 0000000..f07f043 --- /dev/null +++ b/docker/Makefile.mk @@ -0,0 +1,112 @@ +# +# Copyright 2015 Xebia Nederland B.V. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +REGISTRY_HOST=docker.io +USERNAME=$(USER) +NAME=$(shell basename $(PWD)) + +RELEASE_SUPPORT := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))/.make-release-support +IMAGE=$(REGISTRY_HOST)/$(USERNAME)/$(NAME) + +VERSION=$(shell . $(RELEASE_SUPPORT) ; getVersion) +TAG=$(shell . $(RELEASE_SUPPORT); getTag) + +SHELL=/bin/bash + +.PHONY: pre-build docker-build post-build build release patch-release minor-release major-release tag check-status check-release showver \ + push pre-push do-push post-push + +build: pre-build docker-build post-build + +pre-build: + + +post-build: + + +pre-push: + + +post-push: + + + +docker-build: .release + docker build $(DOCKER_BUILD_ARGS) -t $(IMAGE):$(VERSION) -f Dockerfile .. + @DOCKER_MAJOR=$(shell docker -v | sed -e 's/.*version //' -e 's/,.*//' | cut -d\. -f1) ; \ + DOCKER_MINOR=$(shell docker -v | sed -e 's/.*version //' -e 's/,.*//' | cut -d\. -f2) ; \ + if [ $$DOCKER_MAJOR -eq 1 ] && [ $$DOCKER_MINOR -lt 10 ] ; then \ + echo docker tag -f $(IMAGE):$(VERSION) $(IMAGE):latest ;\ + docker tag -f $(IMAGE):$(VERSION) $(IMAGE):latest ;\ + else \ + echo docker tag $(IMAGE):$(VERSION) $(IMAGE):latest ;\ + docker tag $(IMAGE):$(VERSION) $(IMAGE):latest ; \ + fi + +.release: + @echo "release=0.0.0" > .release + @echo "tag=$(NAME)-0.0.0" >> .release + @echo INFO: .release created + @cat .release + + +release: check-status check-release build push + + +push: pre-push do-push post-push + +do-push: + docker push $(IMAGE):$(VERSION) + docker push $(IMAGE):latest + +snapshot: build push + +showver: .release + @. $(RELEASE_SUPPORT); getVersion + +tag-patch-release: VERSION := $(shell . $(RELEASE_SUPPORT); nextPatchLevel) +tag-patch-release: .release tag + +tag-minor-release: VERSION := $(shell . $(RELEASE_SUPPORT); nextMinorLevel) +tag-minor-release: .release tag + +tag-major-release: VERSION := $(shell . $(RELEASE_SUPPORT); nextMajorLevel) +tag-major-release: .release tag + +patch-release: tag-patch-release release + @echo $(VERSION) + +minor-release: tag-minor-release release + @echo $(VERSION) + +major-release: tag-major-release release + @echo $(VERSION) + + +tag: TAG=$(shell . $(RELEASE_SUPPORT); getTag $(VERSION)) +tag: check-status + @. $(RELEASE_SUPPORT) ; ! tagExists $(TAG) || (echo "ERROR: tag $(TAG) for version $(VERSION) already tagged in git" >&2 && exit 1) ; + @. $(RELEASE_SUPPORT) ; setRelease $(VERSION) + git add . + git commit -m "bumped to version $(VERSION)" ; + git tag $(TAG) ; + @ if [ -n "$(shell git remote -v)" ] ; then git push --tags ; else echo 'no remote to push tags to' ; fi + +check-status: + @. $(RELEASE_SUPPORT) ; ! hasChanges || (echo "ERROR: there are still outstanding changes" >&2 && exit 1) ; + +check-release: .release + @. $(RELEASE_SUPPORT) ; tagExists $(TAG) || (echo "ERROR: version not yet tagged in git. make [minor,major,patch]-release." >&2 && exit 1) ; + @. $(RELEASE_SUPPORT) ; ! differsFromRelease $(TAG) || (echo "ERROR: current directory differs from tagged $(TAG). make [minor,major,patch]-release." ; exit 1)