Skip to content

Commit 10e0195

Browse files
authored
Add docker-compose.dev.yml and dev/test scripts (#1262)
1 parent 1ad05b7 commit 10e0195

File tree

7 files changed

+137
-21
lines changed

7 files changed

+137
-21
lines changed

.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ Dockerfile
33
docker-compose*.yml
44
.vagrant/
55
codecov/
6+
.dev/

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ lib/
1414
log/
1515
.vagrant/
1616
codecov/
17+
.dev/

Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ RUN sudo python3 prerequisites.py --yes --cmsuser=cmsuser install
5050

5151
RUN sudo sed 's|/cmsuser:your_password_here@localhost:5432/cmsdb"|/postgres@testdb:5432/cmsdbfortesting"|' ./config/cms.conf.sample \
5252
| sudo tee /usr/local/etc/cms-testdb.conf
53+
RUN sudo sed 's|/cmsuser:your_password_here@localhost:5432/cmsdb"|/postgres@devdb:5432/cmsdb"|' ./config/cms.conf.sample \
54+
| sudo tee /usr/local/etc/cms-devdb.conf
5355

5456
ENV LANG C.UTF-8
5557

cms-dev.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
set -x
3+
4+
GIT_BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
5+
6+
docker compose -p $GIT_BRANCH_NAME -f docker-compose.dev.yml run --build --rm --service-ports devcms

cms-test.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
set -x
3+
4+
GIT_BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
5+
6+
docker compose -p $GIT_BRANCH_NAME -f docker-compose.test.yml run --build --rm testcms

docker-compose.dev.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
services:
2+
devdb:
3+
image: postgres
4+
environment:
5+
POSTGRES_HOST_AUTH_METHOD: trust
6+
volumes:
7+
- "./.dev/postgres-data:/var/lib/postgresql/data"
8+
9+
devcms:
10+
build: .
11+
depends_on:
12+
- "devdb"
13+
environment:
14+
CMS_CONFIG: /usr/local/etc/cms-devdb.conf
15+
volumes:
16+
- "./.dev/home:/home/cmsuser"
17+
- ".:/home/cmsuser/cms"
18+
privileged: true
19+
cgroup: host
20+
command: >
21+
wait-for-it devdb:5432 -- bash
22+
ports:
23+
- 8888:8888
24+
- 8889:8889
25+
- 8890:8890

docs/Docker image.rst

+96-21
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,19 @@ Docker image
33

44
We provide a docker image (defined in :gh_blob:`Dockerfile`) that can be used to
55
easily get an instance of CMS running locally with all the necessary
6-
dependencies. We also provide a :gh_blob:`docker-compose.test.yml` files that
7-
uses said docker image to run the tests.
6+
dependencies. We also provide:
7+
8+
* :gh_blob:`cms-test.sh`: This file uses :gh_blob:`docker-compose.test.yml` to
9+
spawn a volatile database (not persisted on disk) as well as a CMS instance
10+
that automatically runs all tests.
11+
12+
* :gh_blob:`cms-dev.sh`: This file uses :gh_blob:`docker-compose.dev.yml` to
13+
spawn a database (persisted in the local ``.dev/postgres-data`` folder
14+
within the repository) as well as a CMS instance that only runs `bash`,
15+
leaving you with a shell from where you can start cms services and servers.
16+
The DB port and CMS server ports are also automatically forwarded on the
17+
host machine (respectively to ``15432`` for the database, and ``8888-8890``
18+
for CMS).
819

920
Make sure that you have a recent version of Docker installed, as well as Docker
1021
Compose.
@@ -14,45 +25,109 @@ Compose.
1425
Running tests
1526
=============
1627

17-
First you need to build the docker image, then you use it to run the tests.
28+
You can simply run:
29+
30+
.. sourcecode:: bash
31+
32+
./cms-test.sh
33+
34+
Or, you can issue the full command (that is defined in ``cms-test.sh``) which
35+
is similar to:
36+
37+
.. sourcecode:: bash
38+
39+
docker compose -p someprojectname -f docker-compose.test.yml run --build --rm testcms
1840

1941
.. note::
2042

43+
Some versions of docker require to specify ``-p``, some version will fill it
44+
for you based on the current folder's name (which for us would be equivalent
45+
to passing ``-p cms``).
46+
2147
The ``-p`` flag is used as a namespace for the containers that will be
2248
created. When you're running tests on a separate branch, it can be useful to
23-
include the branch name there, to avoid any conflict. (You can also omit the
24-
flag and specify the name via the ``COMPOSE_PROJECT_NAME`` environment
25-
variable.)
49+
include the branch name there, to avoid any conflict. The ``cms-test.sh``
50+
script uses the **name of the current git branch** and passes it to ``-p``.
51+
52+
Note also that if you are not part of the ``docker`` group then you'll need
53+
to run every docker command with ``sudo``, including ``sudo ./cms-test.sh``.
54+
We recommend adding yourself to the ``docker`` group.
55+
56+
What the ``cms-test.sh`` command does is: first build a fresh CMS image when
57+
necessary, and then create (assuming you didn't specify the ``-p`` flag) a
58+
``cms-testdb-1`` container for the database, and a
59+
``cms-testcms-run-<random_string>`` container for CMS.
60+
61+
The database container **will not** be automatically deleted, while the CMS
62+
container will be automatically deleted upon exiting (because of the ``--rm``
63+
flag).
64+
65+
To delete the ``cms-testdb-1`` container after testing you can run:
66+
67+
.. sourcecode:: bash
2668

27-
If you are not part of the ``docker`` group, then you need to run every
28-
docker command with ``sudo``.
69+
docker rm -f cms-testdb-1
2970

30-
To build the image:
71+
Developing CMS
72+
==============
73+
74+
To run a local development instance of CMS, you can simply run:
3175

3276
.. sourcecode:: bash
3377

34-
docker compose -p cms -f docker-compose.test.yml build testcms
78+
./cms-dev.sh
3579

36-
To run the tests:
80+
Or, you can issue the full command (that is defined in ``cms-dev.sh``) which is
81+
similar to:
3782

3883
.. sourcecode:: bash
3984

40-
docker compose -p cms -f docker-compose.test.yml run --rm testcms
85+
docker compose -p someprojectname -f docker-compose.dev.yml run --build --rm --service-ports devcms
86+
87+
The command will build a fresh CMS image when necessary, and drop you into a
88+
bash prompt where the repository is mounted on ``~/cms`` for ease of
89+
development. You can edit the code from the host (i.e. outside the container)
90+
and then reinstall CMS (``python3 setup.py install``) directly from inside the
91+
container, without having to rebuild the image every time.
4192

42-
Another option is to add the ``--build`` flag to the ``run`` command, to perform
43-
a new build before running the tests:
93+
Upon running ``cms-dev.sh`` for the first time, the database will initially be
94+
empty. You need to initialize it (notice that the following commands are
95+
indicated with a ``>>>`` prompt because they are meant to be executed **inside**
96+
the container, from the prompt that you get to after running ``cms-dev.sh``)
97+
like so:
4498

4599
.. sourcecode:: bash
46100

47-
docker compose -p cms -f docker-compose.test.yml run --build --rm testcms
101+
>>> createdb -h devdb -U postgres cmsdb
102+
>>> cmsInitDB
48103

49-
This command will create (assuming you used ``-p cms``) a ``cms-testdb-1``
50-
container for the database which **will not** be automatically deleted, and a
51-
``cms-testcms-run-<random_string>`` container for CMS which will be
52-
automatically deleted (because of the ``--rm`` flag) upon exiting.
104+
Then you probably want to download a test contest and import it, for example
105+
like this:
53106

54-
To delete the ``cms-testdb-1`` container after testing you can run:
107+
.. sourcecode:: bash
108+
109+
>>> git clone https://github.com/cms-dev/con_test.git
110+
>>> cd con_test
111+
>>> cmsImportUser --all
112+
>>> cmsImportContest -i .
113+
114+
If this succeeds, you can then run one of the servers, for example the
115+
ContestWebServer, like so:
55116

56117
.. sourcecode:: bash
57118

58-
docker rm -f cms-testdb-1
119+
>>> cmsContestWebServer
120+
121+
When it prompts you to choose a contest ID, you can simply hit Enter.
122+
123+
When the server is finally running, you can check (from the host machine) that
124+
the server is reachable at http://localhost:8888/
125+
126+
You can also verify that upon exiting the container's bash shell and reentering
127+
it (by running ``cms-dev.sh`` again) you won't need to re-import the contest, as
128+
the database is persisted on disk on the host machine. Even manually destroying
129+
and recreating the database container will retain the same data. If for some
130+
reason you need to reset the database, we recommend using the ``dropdb -h devdb
131+
-U postgres cmsdb`` command inside the container. To remove any trace of the
132+
database data, you can delete the ``.dev/postgres-data`` folder within the git
133+
repository.

0 commit comments

Comments
 (0)