Skip to content

Commit

Permalink
Merge pull request #25 from Amsterdam/release/2.0.0
Browse files Browse the repository at this point in the history
Release/2.0.0
  • Loading branch information
kramer65 authored May 4, 2020
2 parents 79838e2 + c3a1edb commit fadc272
Show file tree
Hide file tree
Showing 59 changed files with 1,323 additions and 771 deletions.
52 changes: 52 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
FROM amsterdam/python:3.8-buster as app

ENV PYTHONUNBUFFERED 1
ENV CONSUL_HOST=${CONSUL_HOST:-notset}
ENV CONSUL_PORT=${CONSUL_PORT:-8500}
ENV DATAPUNT_API_URL=${DATAPUNT_API_URL:-https://api.data.amsterdam.nl/}

ARG https_proxy=http://10.240.2.1:8080/
ENV https_proxy=$https_proxy

WORKDIR /app_install
ADD requirements.txt requirements.txt
RUN pip install -r requirements.txt

ADD deploy /deploy

WORKDIR /src
ADD src .

ARG SECRET_KEY=collectstatic
RUN python manage.py collectstatic --no-input

USER datapunt

CMD ["/deploy/docker-run.sh"]

# devserver
FROM app as dev

USER root
WORKDIR /app_install
ADD requirements_dev.txt requirements_dev.txt
RUN pip install -r requirements_dev.txt
RUN chmod -R a+r /app_install

WORKDIR /src
USER datapunt

# Any process that requires to write in the home dir
# we write to /tmp since we have no home dir
ENV HOME /tmp

CMD ["python manage.py runserver 0.0.0.0"]

# tests
FROM dev as tests

USER datapunt

ENV COVERAGE_FILE=/tmp/.coverage

CMD ["pytest"]
164 changes: 85 additions & 79 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,104 +1,110 @@
#!groovy
def PROJECT_NAME = "stadsarchief"
def SLACK_CHANNEL = '#opdrachten-deployments'
def PLAYBOOK = 'deploy-stadsarchief.yml'
def SLACK_MESSAGE = [
"title_link": BUILD_URL,
"fields": [
["title": "Project","value": PROJECT_NAME],
["title":"Branch", "value": BRANCH_NAME, "short":true],
["title":"Build number", "value": BUILD_NUMBER, "short":true]
]
]

def tryStep(String message, Closure block, Closure tearDown = null) {
try {
block()
}
catch (Throwable t) {
slackSend message: "${env.JOB_NAME}: ${message} failure ${env.BUILD_URL}", channel: '#ci-channel', color: 'danger'

throw t
}
finally {
if (tearDown) {
tearDown()
}
}
}

pipeline {
agent any

node {
stage("Checkout") {
checkout scm
environment {
SHORT_UUID = sh( script: "head /dev/urandom | tr -dc A-Za-z0-9 | head -c10", returnStdout: true).trim()
COMPOSE_PROJECT_NAME = "${PROJECT_NAME}-${env.SHORT_UUID}"
VERSION = env.BRANCH_NAME.replace('/', '-').toLowerCase().replace(
'master', 'latest'
)
IS_RELEASE = "${env.BRANCH_NAME ==~ "release/.*"}"
}

stage('Test') {
tryStep "test", {
withCredentials([[$class: 'StringBinding', credentialsId: 'BOUWDOSSIERS_OBJECTSTORE_PASSWORD', variable: 'BOUWDOSSIERS_OBJECTSTORE_PASSWORD']]) {
sh "docker-compose -p stadsarchief -f src/.jenkins/test/docker-compose.yml build && " +
"docker-compose -p stadsarchief -f src/.jenkins/test/docker-compose.yml run -u root --rm tests"
}
}, {
sh "docker-compose -p stadsarchief -f src/.jenkins/test/docker-compose.yml down"
stages {
stage('Test') {
steps {
sh 'make test'
}
}
}

stage("Build image") {
tryStep "build", {
docker.withRegistry('https://repo.data.amsterdam.nl','docker-registry') {
def image = docker.build("datapunt/stadsarchief:${env.BUILD_NUMBER}", "src")
image.push()
stage('Build') {
steps {
sh 'make build'
}
}
}
}


String BRANCH = "${env.BRANCH_NAME}"

if (BRANCH == "master") {

node {
stage('Push acceptance image') {
tryStep "image tagging", {
docker.withRegistry('https://repo.data.amsterdam.nl','docker-registry') {
def image = docker.image("datapunt/stadsarchief:${env.BUILD_NUMBER}")
image.pull()
image.push("acceptance")
stage('Push and deploy') {
when {
anyOf {
branch 'master'
buildingTag()
environment name: 'IS_RELEASE', value: 'true'
}
}
}
}
stages {
stage('Push') {
steps {
retry(3) {
sh 'make push_semver'
}
}
}

node {
stage("Deploy to ACC") {
tryStep "deployment", {
build job: 'Subtask_Openstack_Playbook',
parameters: [
[$class: 'StringParameterValue', name: 'INVENTORY', value: 'acceptance'],
[$class: 'StringParameterValue', name: 'PLAYBOOK', value: 'deploy-stadsarchief.yml'],
]
}
}
}
stage('Deploy to acceptance') {
when { environment name: 'IS_RELEASE', value: 'true' }
steps {
build job: 'Subtask_Openstack_Playbook', parameters: [
string(name: 'PLAYBOOK', value: PLAYBOOK),
string(name: 'INVENTORY', value: "acceptance"),
string(
name: 'PLAYBOOKPARAMS',
value: "-e deployversion=${VERSION}"
)
], wait: true
}
}

stage('Waiting for approval') {
slackSend channel: '#ci-channel', color: 'warning', message: 'Stadsarchief is waiting for Production Release - please confirm'
input "Deploy to Production?"
}
stage('Deploy to production') {
when { buildingTag() }
steps {
build job: 'Subtask_Openstack_Playbook', parameters: [
string(name: 'PLAYBOOK', value: PLAYBOOK),
string(name: 'INVENTORY', value: "production"),
string(
name: 'PLAYBOOKPARAMS',
value: "-e deployversion=${VERSION}"
)
], wait: true

node {
stage('Push production image') {
tryStep "image tagging", {
docker.withRegistry('https://repo.data.amsterdam.nl','docker-registry') {
def image = docker.image("datapunt/stadsarchief:${env.BUILD_NUMBER}")
image.pull()
image.push("production")
image.push("latest")
slackSend(channel: SLACK_CHANNEL, attachments: [SLACK_MESSAGE <<
[
"color": "#36a64f",
"title": "Deploy to production succeeded :rocket:",
]
])
}
}
}
}
}

node {
stage("Deploy") {
tryStep "deployment", {
build job: 'Subtask_Openstack_Playbook',
parameters: [
[$class: 'StringParameterValue', name: 'INVENTORY', value: 'production'],
[$class: 'StringParameterValue', name: 'PLAYBOOK', value: 'deploy-stadsarchief.yml'],
}
post {
always {
sh 'make clean'
}
failure {
slackSend(channel: SLACK_CHANNEL, attachments: [SLACK_MESSAGE <<
[
"color": "#D53030",
"title": "Build failed :fire:",
]
}
])
}
}
}

63 changes: 63 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# This Makefile is based on the Makefile defined in the Python Best Practices repository:
# https://git.datapunt.amsterdam.nl/Datapunt/python-best-practices/blob/master/dependency_management/
#
# VERSION = 2020.01.29
.PHONY = help pip-tools install requirements update test init
dc = docker-compose

help: ## Show this help.
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'

pip-tools:
pip install pip-tools

install: pip-tools ## Install requirements and sync venv with expected state as defined in requirements.txt
pip-sync requirements_dev.txt

requirements: pip-tools ## Upgrade requirements (in requirements.in) to latest versions and compile requirements.txt
pip-compile --upgrade --output-file requirements.txt requirements.in
pip-compile --upgrade --output-file requirements_dev.txt requirements_dev.in

upgrade: requirements install ## Run 'requirements' and 'install' targets

migrations: ## Make migrations
$(dc) run --rm app python manage.py makemigrations

migrate: ## Migrate
$(dc) run --rm app python manage.py migrate

build: ## Build docker image
$(dc) build

push: build ## Push docker image to registry
$(dc) push

push_semver:
VERSION=$${VERSION} $(MAKE) push
VERSION=$${VERSION%\.*} $(MAKE) push
VERSION=$${VERSION%%\.*} $(MAKE) push

app: ## Run app
$(dc) run --service-ports app

bash: ## Run the container and start bash
$(dc) run --rm app bash

test: ## Execute tests
$(dc) run --rm test pytest $(ARGS)
$(dc) run --rm test flake8 --config=./flake8.cfg

clean: ## Clean docker stuff
$(dc) down -v

env: ## Print current env
env | sort

import_bag: ## Populate database with Bag data
${dc} exec database update-table.sh bag bag_verblijfsobject public stadsarchief
${dc} exec database update-table.sh bag bag_ligplaats public stadsarchief
${dc} exec database update-table.sh bag bag_standplaats public stadsarchief
${dc} exec database update-table.sh bag bag_nummeraanduiding public stadsarchief
${dc} exec database update-table.sh bag bag_pand public stadsarchief
${dc} exec database update-table.sh bag bag_verblijfsobjectpandrelatie public stadsarchief
${dc} exec database update-table.sh bag bag_openbareruimte public stadsarchief
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
Provide API and import for datasets from stadsarchief such as bouwdossiers


# Pre_wabo and wabo dossiers

wabo dossiers are dossiers after the law in 2008 came in to effect (https://wetten.overheid.nl/BWBR0024779/2018-07-28)
Dossiers before this law are called pre_wabo in the context of this repository.

It has been decided to import both kinds of dossiers in the same model (`BouwDossier`)
and add `source` field to differentiate between them.

The differences between pre_wabo and wabo are as follows:

- They reside in different cloud storage.
- The metadata for each are provided in different xml files.
- The xml files have slightly different structure thus are imported differently.
- WABO dossiers are not only bouwdossiers can also be other kinds.
(The model `BouwDossier` name may change in the future)


# Local development

Start database
Expand All @@ -23,13 +40,7 @@ Manual import
Before import BAG tables need to be loaded to be able to map BAG ids :

```
docker-compose exec database update-table.sh bag bag_verblijfsobject public stadsarchief <your_login_name>
docker-compose exec database update-table.sh bag bag_ligplaats public stadsarchief <your_login_name>
docker-compose exec database update-table.sh bag bag_standplaats public stadsarchief <your_login_name>
docker-compose exec database update-table.sh bag bag_nummeraanduiding public stadsarchief <your_login_name>
docker-compose exec database update-table.sh bag bag_pand public stadsarchief <your_login_name>
docker-compose exec database update-table.sh bag bag_verblijfsobjectpandrelatie public stadsarchief <your_login_name>
docker-compose exec database update-table.sh bag bag_openbareruimte public stadsarchief <your_login_name>
(cd .. && make import_bag)
```

Then we van run the import:
Expand Down Expand Up @@ -77,4 +88,4 @@ Test API login in acceptance with SWAGGER :
# Import database from acceptance


docker-compose exec database update-db.sh stadsarchief <your username>
docker-compose exec database update-db.sh stadsarchief <your username>
7 changes: 7 additions & 0 deletions deploy/docker-migrate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -u # crash on missing env variables
set -e # stop on any error
set -x # print what we are doing

python manage.py migrate --noinput
4 changes: 2 additions & 2 deletions src/.jenkins/docker-migrate.sh → deploy/docker-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

set -u # crash on missing env variables
set -e # stop on any error
set -x # print what we are doing

echo "Migrating db"
yes yes | python ./manage.py migrate --noinput
uwsgi --ini main/uwsgi.ini
Loading

0 comments on commit fadc272

Please sign in to comment.