From afd46ba01a83371dbdbf9af34950ad66969c19e5 Mon Sep 17 00:00:00 2001
From: Bastien Abadie <bastien@nextcairn.com>
Date: Fri, 17 Jul 2020 11:51:13 +0000
Subject: [PATCH] Revert "Run a test CI"

This reverts commit 2c7dd1f7683b4c67efd73e860329e6ba3d4fec86.
---
 .gitlab-ci.yml               | 22 +++++++++-
 Dockerfile.binary            | 85 ++++++++++++++++++++++++++++++++++++
 arkindex/project/settings.py |  3 +-
 ci/build.sh                  | 14 +++++-
 4 files changed, 121 insertions(+), 3 deletions(-)
 create mode 100644 Dockerfile.binary

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 09d23d6efc..7af4325579 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -129,7 +129,27 @@ backend-build:
     - when: never
 
   script:
-    - ci/build.sh
+    - ci/build.sh Dockerfile
+
+backend-build-binary:
+  stage: build
+  image: docker:19.03.1
+  services:
+    - docker:dind
+  variables:
+    DOCKER_DRIVER: overlay2
+    DOCKER_HOST: tcp://docker:2375/
+
+  # Run this on master and tags except base tags
+  rules:
+    - if: '$CI_COMMIT_BRANCH == "master"'
+      when: on_success
+    - if: '$CI_COMMIT_TAG && $CI_COMMIT_TAG !~ /^base-.*/'
+      when: on_success
+    - when: never
+
+  script:
+    - ci/build.sh Dockerfile.binary "-binary"
 
 backend-static-deploy:
   stage: deploy
diff --git a/Dockerfile.binary b/Dockerfile.binary
new file mode 100644
index 0000000000..c0c85386bd
--- /dev/null
+++ b/Dockerfile.binary
@@ -0,0 +1,85 @@
+FROM python:3.7-alpine AS compilation
+
+RUN pip install nuitka
+
+ARG COMMON_BRANCH=master
+ARG COMMON_ID=9855787
+ARG PONOS_BRANCH=master
+ARG PONOS_ID=10017043
+ARG GITLAB_TOKEN="gaFM7LRa9zy9QMowcUhx"
+
+# We build in /usr/share because Django will try to load some files relative to that path
+# once executed in the binary (management commands, ...)
+WORKDIR /usr/share
+
+# Add our own source code
+ADD arkindex /usr/share/arkindex
+ADD base/requirements.txt /tmp/requirements-base-arkindex.txt
+ADD requirements.txt /tmp/requirements-arkindex.txt
+
+# Retrieve common source code
+RUN mkdir /tmp/common && \
+  wget --header "PRIVATE-TOKEN: $GITLAB_TOKEN" https://gitlab.com/api/v4/projects/$COMMON_ID/repository/archive.tar.gz?sha=$COMMON_BRANCH -O /tmp/common.tar.gz && \
+  tar --strip-components=1 -xvf /tmp/common.tar.gz -C /tmp/common && \
+  mv /tmp/common/arkindex_common /usr/share
+
+# Retrieve ponos source code
+RUN mkdir /tmp/ponos && \
+  wget --header "PRIVATE-TOKEN: $GITLAB_TOKEN" https://gitlab.com/api/v4/projects/$PONOS_ID/repository/archive.tar.gz?sha=$PONOS_BRANCH -O /tmp/ponos.tar.gz && \
+  tar --strip-components=1 -xvf /tmp/ponos.tar.gz -C /tmp/ponos && \
+  mv /tmp/ponos/ponos /usr/share
+
+# Build full requirements, removing relative or remote references to arkindex projects
+# Special case for apistar, where we want to keep our own fork
+# Special case for approximate requirements from ponos, where we want to keep the versions specified from this repo
+RUN cat /tmp/common/requirements.txt /tmp/ponos/requirements-server.txt /tmp/requirements-*arkindex.txt | sort | uniq | grep -v -E '^apistar|arkindex|^#|^Django~=|^boto3~=|^cryptography~=|^django-enumfields~=|^djangorestframework~=|^pyyaml~=' > /requirements.txt
+
+# List all management commands
+RUN find /usr/share/arkindex/*/management -name '*.py' -not -name '__init__.py' > /commands.txt
+
+# Remove arkindex & ponos unit tests
+RUN find /usr/share/arkindex /usr/share/ponos -type d -name tests | xargs rm -rf
+
+# DEBUG
+RUN find /usr/share -type f
+
+# Compile all our python source code
+RUN apk --update add build-base
+RUN python -m nuitka \
+      --nofollow-imports \
+      --include-package=arkindex \
+      --include-package=arkindex_common \
+      --include-package=ponos \
+      --show-progress \
+      --python-flag=-OO \
+      --lto \
+      --output-dir=/build \
+      arkindex/manage.py
+
+# Start over from a clean setup
+FROM registry.gitlab.com/arkindex/backend/base:python-3.7 as build
+
+# Import files from compilation
+RUN mkdir /usr/share/arkindex
+COPY --from=compilation /build/manage.bin /usr/bin/arkindex
+COPY --from=compilation /requirements.txt /usr/share/arkindex
+COPY --from=compilation /commands.txt /usr/share/arkindex
+
+# Install open source Python dependencies
+# We also add gunicorn, to be able to run `arkindex gunicorn`
+RUN pip install -r /usr/share/arkindex/requirements.txt gunicorn
+
+# Setup Arkindex VERSION
+COPY VERSION /etc/arkindex.version
+
+# Copy templates in base dir for binary
+ENV BASE_DIR=/usr/share/arkindex
+COPY arkindex/templates /usr/share/arkindex/templates
+
+# Touch python files for needed management commands
+# Otherwise Django will not load the compiled module
+RUN for cmd in $(cat /usr/share/arkindex/commands.txt); do mkdir -p $(dirname $cmd); touch $cmd; done
+
+# Run gunicorn server
+ENV PORT=80
+CMD ["arkindex", "gunicorn", "--host=0.0.0.0"]
diff --git a/arkindex/project/settings.py b/arkindex/project/settings.py
index 524fc3bb0e..33a04c9582 100644
--- a/arkindex/project/settings.py
+++ b/arkindex/project/settings.py
@@ -19,7 +19,8 @@ from pathlib import Path
 from arkindex.project.config import get_settings_parser, CacheType
 
 # Build paths inside the project like this: BASE_DIR / ...
-BASE_DIR = Path(__file__).resolve().parent.parent
+_base_dir = os.environ.get('BASE_DIR')
+BASE_DIR = Path(_base_dir) if _base_dir else Path(__file__).resolve().parent.parent
 
 # Used for special cases during configuration parsing and settings loading
 TEST_ENV = 'test' in sys.argv
diff --git a/ci/build.sh b/ci/build.sh
index e84a3c97b8..246c701de9 100755
--- a/ci/build.sh
+++ b/ci/build.sh
@@ -5,6 +5,13 @@
 # Will automatically login to a registry if CI_REGISTRY, CI_REGISTRY_USER and CI_REGISTRY_PASSWORD are set.
 # Will only push an image if $CI_REGISTRY is set.
 
+DOCKERFILE=$1
+TAG_SUFFIX=$2
+if [ -z "$DOCKERFILE" ]; then
+	echo "Missing dockerfile on CLI"
+	exit 1
+fi
+
 if [ -z "$VERSION" ]; then
 	#Ensure this is not a base tag
 	case $CI_COMMIT_TAG in
@@ -15,6 +22,11 @@ if [ -z "$VERSION" ]; then
 	VERSION=${CI_COMMIT_TAG:-latest}
 fi
 
+if [ -n "$TAG_SUFFIX" ]; then
+	# Apply optional suffix to image version
+	VERSION="${VERSION}${TAG_SUFFIX}"
+fi
+
 if [ -z "$VERSION" -o -z "$CI_PROJECT_DIR" -o -z "$CI_REGISTRY_IMAGE" ]; then
 	echo Missing environment variables
 	exit 1
@@ -31,7 +43,7 @@ IMAGE_TAG="$CI_REGISTRY_IMAGE:$VERSION"
 
 cd $CI_PROJECT_DIR
 docker pull "$CI_REGISTRY_IMAGE/base:latest"
-docker build . -t "$IMAGE_TAG" --build-arg "PONOS_BRANCH=$PONOS_BRANCH" --build-arg "COMMON_BRANCH=$COMMON_BRANCH"
+docker build . -f $DOCKERFILE -t "$IMAGE_TAG" --build-arg "PONOS_BRANCH=$PONOS_BRANCH" --build-arg "COMMON_BRANCH=$COMMON_BRANCH"
 if [ -n "$CI_REGISTRY" ]; then
 	docker push "$IMAGE_TAG"
 fi
-- 
GitLab