Skip to content

Commit

Permalink
precommit: Introduce new server-based framework
Browse files Browse the repository at this point in the history
  • Loading branch information
oschuett committed Aug 20, 2020
1 parent f64bee7 commit 444cb76
Show file tree
Hide file tree
Showing 8 changed files with 524 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tools/precommit/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
FROM ubuntu:20.04

# author: Ole Schuett

# Install Ubuntu packages.
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && \
apt-get update && apt-get install -y --no-install-recommends \
clang-format \
git \
less \
python3 \
python3-pip \
python3-wheel \
python3-setuptools \
rubygems \
shellcheck \
vim \
&& rm -rf /var/lib/apt/lists/*

# Install Markdownlint.
RUN gem install mdl

# Install Black Python Formatter.
RUN pip3 install black

# Clone cp2k repository (needed for CI mode).
RUN git clone --quiet --recursive --single-branch -b master https://github.com/cp2k/cp2k.git /workspace/cp2k

# Install Flask app.
RUN pip3 install flask gunicorn
WORKDIR /opt/cp2k-precommit
COPY precommit_server.py .

ARG REVISION
ENV REVISION=${REVISION}

COPY entrypoint.sh .
CMD ["./entrypoint.sh"]

#EOF
41 changes: 41 additions & 0 deletions tools/precommit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# CP2K Precommit

The precommit system consists of the following tools that analyze and format the source code:

- [doxify](../doxify/) to add Doxygen templates.
- [prettify](../prettify/) to format Fortran files.
- [analyze_src](../conventions/analyze_src.py) to check copyright banners and a few other things.
- [ast.parse](https://docs.python.org/3/library/ast.html) to check Python syntax.
- [clang-format](https://clang.llvm.org/docs/ClangFormat.html) to format C and Cuda files.
- [black](https://github.com/psf/black) to format Python scripts.
- [shellcheck](https://github.com/koalaman/shellcheck) to analyze Shell scripts.
- [markdownlint](https://github.com/markdownlint/markdownlint) to analyze Markdown files.

In contrast to the [CP2K-CI](https://github.com/cp2k/cp2k-ci) these tools process each file individually, which makes them much more lightweight.

## Install Git Hook

The [precommit.py](./precommit.py) script can be readily installed as git hook:
```
ln -fs ../../tools/precommit/precommit.py .git/hooks/pre-commit
```

## Server

Many of the tools listed above require more than just Python. To avoid their tedious installation a remote server is used by default.
It is hosted at https://precommit.cp2k.org via [Cloud Run](https://cloud.google.com/run).

The same server can also be started locally when Docker is available:
```
./start_local_server.sh
```
The server can also be installed without Docker by following the steps in the [Dockerfile](./Dockerfile).

Once the server is up and running it can be used like this:
```
$ export CP2K_PRECOMMIT_SERVER="http://127.0.0.1:8080"
$ ./precommit.py
Running precommit checks using 8 workers and server: http://127.0.0.1:8080
Searching for files...
```

28 changes: 28 additions & 0 deletions tools/precommit/cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# author: Ole Schuett

steps:
- name: 'gcr.io/cloud-builders/docker'
args: ["build", "--build-arg ", "REVISION=$SHORT_SHA", "-t", "img_cp2kprecommit", "."]

- name: 'gcr.io/cloud-builders/docker'
args: ["tag", "img_cp2kprecommit", "gcr.io/$PROJECT_ID/img_cp2kprecommit:$SHORT_SHA"]

- name: 'gcr.io/cloud-builders/docker'
args: ["push", "gcr.io/$PROJECT_ID/img_cp2kprecommit:$SHORT_SHA"]

- name: 'gcr.io/cloud-builders/docker'
args: ["tag", "img_cp2kprecommit", "gcr.io/$PROJECT_ID/img_cp2kprecommit:latest"]

- name: 'gcr.io/cloud-builders/docker'
args: ["push", "gcr.io/$PROJECT_ID/img_cp2kprecommit:latest"]

- name: "gcr.io/cloud-builders/gcloud"
args:
- "run"
- "deploy"
- "cp2k-precommit"
- "--platform=managed"
- "--region=us-central1"
- "--image=gcr.io/$PROJECT_ID/img_cp2kprecommit:$SHORT_SHA"

#EOF
18 changes: 18 additions & 0 deletions tools/precommit/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash -e

# author: Ole Schuett

set -x

SHORT_SHA=$(git rev-parse --short HEAD)
docker build --build-arg "REVISION=${SHORT_SHA}" -t cp2k-precommit .

docker tag cp2k-precommit gcr.io/cp2k-org-project/img_cp2kprecommit:${SHORT_SHA}
docker tag cp2k-precommit gcr.io/cp2k-org-project/img_cp2kprecommit:latest
docker push gcr.io/cp2k-org-project/img_cp2kprecommit:${SHORT_SHA}
docker push gcr.io/cp2k-org-project/img_cp2kprecommit:latest

gcloud run deploy cp2k-precommit --platform=managed --region=us-central1 \
--image=gcr.io/cp2k-org-project/img_cp2kprecommit:${SHORT_SHA}

#EOF
28 changes: 28 additions & 0 deletions tools/precommit/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash -e

# author: Ole Schuett

trap "exit" INT

if [ -z "${GIT_REF}" ]; then
# Server mode.
gunicorn --bind=:8080 --workers=1 --threads=8 --timeout=0 precommit_server:app &
wait # Can be interrupted with ctrl-C.
else
# CI mode.
echo -e "\n========== Starting Precommit Server =========="
gunicorn --bind=:8080 --workers=1 --threads=8 --timeout=0 precommit_server:app &> server.logs &
sleep 3
cat server.logs
echo -e "\n========== Fetching Git Commit =========="
cd /workspace/cp2k
git fetch --quiet origin "${GIT_BRANCH}"
git reset --quiet --hard "${GIT_REF}"
git submodule update --init --recursive
git --no-pager log -1 --pretty='%nCommitSHA: %H%nCommitTime: %ci%nCommitAuthor: %an%nCommitSubject: %s%n'
echo -e "\n========== Running Precommit Checks =========="
export CP2K_PRECOMMIT_SERVER="http://127.0.0.1:8080"
./tools/precommit/precommit.py --no-cache --progressbar-wait=10 || true
fi

#EOF

0 comments on commit 444cb76

Please sign in to comment.