Skip to content

Commit 34d808f

Browse files
author
PPB InfoSec Engineering
committed
Initial commit
0 parents  commit 34d808f

38 files changed

+1289
-0
lines changed

.dockerignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# file set with Dockerfile.mysql (for run_tests script) in mind - review if used for anything else
2+
.jenkins
3+
**/.coverage
4+
**/__pycache__
5+
**/*.pyc
6+
dist
7+
*.egg-info
8+
.git
9+
.reports
10+
.dockerignore
11+
**/.pytest_cache

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.pyc
2+
__pycache__
3+
/dist
4+
*.egg-info
5+
.coverage
6+
.reports
7+
.pytest_cache
8+
.vscode

.jenkins/Dockerfile.tests

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM docker.app.betfair/appsec/base/python:3.9-slim-buster
2+
3+
RUN apt update
4+
RUN apt install -y libmariadb-dev build-essential
5+
COPY . /app
6+
7+
WORKDIR /app/testapp
8+
RUN pip install -r requirements.txt

.jenkins/ci_build

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
cd $(dirname $0)
6+
cd ..
7+
8+
if [ ! -e /.dockerenv ]; then
9+
# not in container
10+
.jenkins/run_tests
11+
docker run -v $(pwd):/work \
12+
-w /work \
13+
-e ARTIFACTORY_USER \
14+
-e ARTIFACTORY_PASSWORD \
15+
docker.app.betfair/appsec/base/python:3.9-slim-buster \
16+
/work/.jenkins/ci_build
17+
exit 0
18+
fi
19+
20+
export TWINE_USERNAME=${ARTIFACTORY_USER}
21+
export TWINE_PASSWORD=${ARTIFACTORY_PASSWORD}
22+
export TWINE_NON_INTERACTIVE=1
23+
24+
# check if package-version already uploaded (pypi.org blocks but ppb artifactory does not)
25+
PN=$(python setup.py --name)
26+
PV=$(python setup.py --version)
27+
# requests required by twine anyway, not a waste :)
28+
pip install 'requests<3'
29+
python <<EOF
30+
import requests, sys
31+
c=requests.get('https://artifactory-prd.prd.betfair/artifactory/api/pypi/pypi/$PN/$PV/').status_code
32+
if c != 404:
33+
print(f'ERROR: version already exists? [{c}]', file=sys.stderr)
34+
exit(2)
35+
EOF
36+
37+
pip install twine
38+
39+
40+
python setup.py sdist
41+
python -m twine upload --repository-url https://artifactory-prd.prd.betfair/artifactory/api/pypi/pypi dist/*

.jenkins/mr_test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
cd $(dirname $0)
6+
./run_tests

.jenkins/run_tests

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
cd $(dirname $0)
6+
cd ..
7+
8+
create_mysql() {
9+
# to make sure the latest is in JKS cache... can comment out sometime in the future :P
10+
MYSQLDOCKER=$(docker run \
11+
-e MYSQL_ROOT_PASSWORD=root \
12+
--rm -d \
13+
--health-cmd "mysqladmin ping" \
14+
--health-interval 10s \
15+
--health-timeout 5s \
16+
--health-retries=5 \
17+
mysql:5)
18+
trap "rm_mysql" EXIT
19+
}
20+
21+
wait_mysql() {
22+
echo "Waiting for mysql container (${MYSQLDOCKER})..."
23+
while [ 1 ]; do
24+
docker inspect --format='{{.State.Health}}' ${MYSQLDOCKER} | grep -q '^{healthy' && break
25+
sleep 0.5
26+
done
27+
}
28+
29+
rm_mysql() {
30+
echo "Removing mysql container"
31+
docker rm -f ${MYSQLDOCKER} > /dev/null
32+
}
33+
34+
if [ ! -e /.dockerenv ]; then
35+
# not in container
36+
create_mysql
37+
PACKAGE_NAME=$(cat setup.cfg | grep ^"name = " | cut -d ' ' -f3-)
38+
docker build -t ppb_${PACKAGE_NAME}_tests_image -f .jenkins/Dockerfile.tests .
39+
wait_mysql
40+
docker run -v $(pwd):/work \
41+
-w /work \
42+
--link ${MYSQLDOCKER}:mysql \
43+
-e DBCLEANUP_TEST_MYSQL_HOST=mysql \
44+
-e DBCLEANUP_TEST_MYSQL_PORT=3306 \
45+
-e COVERAGE_REPORT_XML=/work/.reports/coverage.xml \
46+
-e JUNIT_REPORT_XML=/work/.reports/junit.xml \
47+
ppb_${PACKAGE_NAME}_tests_image \
48+
/work/.jenkins/run_tests
49+
exit 0
50+
fi
51+
52+
python -m pytest testapp \
53+
--cov \
54+
--no-cov-on-fail \
55+
--junit-xml ${JUNIT_REPORT_XML:-/dev/null} \
56+
--cov-report xml:${COVERAGE_REPORT_XML:-/dev/null} \
57+
--cov-report term-missing \
58+
--ds testapp.settings_mysql

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2021 PaddyPowerBetfair
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include README.md
2+
include LICENSE

Makefile

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# test setup based on https://github.com/fopina/django-bulk-update-or-create/blob/master/Makefile
2+
TEST_CONTAINER := django-dbcleanup-test
3+
4+
.PHONY: style
5+
style:
6+
black --target-version=py36 \
7+
--line-length=120 \
8+
--skip-string-normalization \
9+
dbcleanup testapp setup.py
10+
11+
.PHONY: style_check
12+
style_check:
13+
black --target-version=py36 \
14+
--line-length=120 \
15+
--skip-string-normalization \
16+
--check \
17+
dbcleanup testapp setup.py
18+
19+
.PHONY: startmysql
20+
startmysql:
21+
@docker inspect ${TEST_CONTAINER}-mysql | grep -q '"Running": true' || \
22+
docker run --name ${TEST_CONTAINER}-mysql \
23+
-e MYSQL_ROOT_PASSWORD=root \
24+
--rm -p 8877:3306 -d \
25+
--health-cmd "mysqladmin ping" \
26+
--health-interval 10s \
27+
--health-timeout 5s \
28+
--health-retries=5 \
29+
mysql:8
30+
until [ "`docker inspect -f {{.State.Health.Status}} ${TEST_CONTAINER}-mysql`" == "healthy" ]; do sleep 0.1; done;
31+
32+
startpg:
33+
@docker inspect ${TEST_CONTAINER}-pg | grep -q '"Running": true' || \
34+
docker run --name ${TEST_CONTAINER}-pg \
35+
-e POSTGRES_USER=postgres \
36+
-e POSTGRES_PASSWORD=postgres \
37+
-e POSTGRES_DB=postgres \
38+
--rm -p 8878:5432 -d \
39+
--health-cmd pg_isready \
40+
--health-interval 10s \
41+
--health-timeout 5s \
42+
--health-retries 5 \
43+
postgres:10
44+
until [ "`docker inspect -f {{.State.Health.Status}} ${TEST_CONTAINER}-pg`" == "healthy" ]; do sleep 0.1; done;
45+
46+
testpg: startpg
47+
DJANGO_SETTINGS_MODULE="testapp.settings_postgresql" \
48+
testapp/manage.py test $${TEST_ARGS:-tests}
49+
50+
test: startmysql
51+
testapp/manage.py test $${TEST_ARGS:-tests}
52+
53+
coverage: startmysql
54+
PYTHONPATH="testapp" \
55+
python -b -W always -m coverage run testapp/manage.py test $${TEST_ARGS:-tests}
56+
coverage report

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# django-dbcleanup
2+
3+
Easily monitor database usage - and clean it up (based on your django models)
4+
5+
## ToDo
6+
7+
Gitlab Issues used to track it

0 commit comments

Comments
 (0)