1
0
mirror of https://github.com/avatao-content/test-tutorial-framework synced 2024-11-14 17:37:18 +00:00
test-tutorial-framework/README.md

257 lines
12 KiB
Markdown
Raw Normal View History

2018-03-30 20:14:11 +00:00
# tutorial-framework test
This is an example playground project built via TFW.
It is a good starting point to build your own challenges from and will host automated tests in the future.
2018-10-22 13:44:49 +00:00
It also gives home to several useful scripts in the `hack` folder to speed up development.
2018-03-30 20:14:11 +00:00
## Getting started
TFW consists of 3 repositories:
2018-04-21 09:30:34 +00:00
- [baseimage-tutorial-framework](https://github.com/avatao-content/baseimage-tutorial-framework) Docker baseimage
- [frontend-tutorial-framework](https://github.com/avatao-content/frontend-tutorial-framework) Angular frontend
- [test-tutorial-framework](https://github.com/avatao-content/test-tutorial-framework) (this repo)
2018-03-30 20:14:11 +00:00
2018-10-22 13:44:49 +00:00
See the documentation of each in their `README.md` files. To publish your challenge on the Avatao platform please check out the details on [docs.avatao-challenge.com](https://docs.avatao-challenge.com/publish-challenge).
2018-03-30 20:14:11 +00:00
Note that before getting started with TFW you should already know how to create simpler Avatao challenges.
To learn about Avatao challenge creation in general please consult the [challenge-toolbox](https://github.com/avatao-content/challenge-toolbox) repository.
2018-04-25 15:44:12 +00:00
To learn the stuff you need to know about TFW in order to get started you should consult the [baseimage-tutorial-framework](https://github.com/avatao-content/baseimage-tutorial-framework) repo first.
2018-03-30 20:14:11 +00:00
Getting started with creating challenges using the framework *setting up a development environment, building, running and such* is documented here.
2018-03-30 20:14:11 +00:00
## Setting up a development environment
2018-03-31 16:23:11 +00:00
2018-04-27 09:55:52 +00:00
Dependencies:
- bash
- git
- Docker
- yarn
- Angular CLI
- GNU coreutils
- GNU findutils
- GNU sed
- GNU grep
2018-04-27 09:55:52 +00:00
2018-06-08 10:00:30 +00:00
Just copy and paste the following command in a terminal:
`bash -c "$(curl -fsSL https://git.io/vxBfj)"`
Please do not hesitate to contact us with error logs included should this command fail to complete.
Note that your SSH public key must be added to your GitHub user for this to work, or you mush select HTTPS remotes when prompted.
2018-03-31 16:23:11 +00:00
2018-04-25 15:44:12 +00:00
This will set up a dev environment based on [test-tutorial-framework](https://github.com/avatao-content/test-tutorial-framework) just for you:
- it builds the latest release of the framework Docker baseimage locally
- it pins `solvable/Dockerfile` to use the this image
- it includes the latest frontend in `solvable/frontend` with dependencies installed
2018-03-31 16:23:11 +00:00
2018-06-08 10:00:30 +00:00
By default your IDE will fail to autocomplete code and will complain about missing dependencies.
To fix this you should install the `tfw` pip package in your dev virtualenv:
- `pip install git+ssh://git@github.com/avatao-content/baseimage-tutorial-framework.git` (SSH)
- `pip install git+https://github.com/avatao-content/baseimage-tutorial-framework.git` (HTTPS)
## Building & running
### Automated
Our magical bash script `hack/tfw.sh` can handle everything for you. Just run it without any arguments to see usage information.
It is advisable to run the frontend locally while developing to avoid really looooong build times. The `hack/tfw.sh` script handles this for you automagically.
2018-05-08 10:49:31 +00:00
### Challenge-toolbox
You can also use our [toolbox](https://github.com/avatao-content/challenge-toolbox) to build and run TFW based challenges, just like the regular ones.
Note that this will always create a production build with the frontend included.
### Doing it manually
2018-04-25 15:44:12 +00:00
In case you **must** *really* do it then you can build & run manually.
Note that this is relatively painful and you should use the `hack/tfw.sh` script when possible.
Building without frontend execute from project root:
`docker build -t test-tutorial-framework -f solvable/Dockerfile --build-arg BUILD_CONTEXT=solvable --build-arg NOFRONTEND=1 .`
This will create a Docker image without the frontend, which you can run locally. For procudtion builds exclude the argument `--build-arg NOFRONTEND=1` to include a frontend instance.
2018-04-25 15:44:12 +00:00
Execute the following command to run the image:
`docker run --rm -p 8888:8888 -e AVATAO_SECRET=secret test-tutorial-framework`
2018-04-23 15:32:21 +00:00
In case of a build without frontend (built with `--build-arg NOFRONTEND=1` included) you will need to run `yarn start` from the `solvable/frontend` directory as well.
This will serve the frontend locally on `http://localhost:4200` and take care of proxying.
If you've created a production build (without `--build-arg NOFRONTEND=1`) you don't have to run the frontend locally and you can access the challenge on `http://localhost:8888`.
### Building the TFW baseimage without test-tutorial-framework
You might need to build our baseimage separately in case you've cloned an existing challenge depending on a specific version.
To do this simply issue `BASEIMAGE_ONLY=version bash -c "$(curl -fsSL https://git.io/vxBfj)"`, where `version` is a tag or commit of the [baseimage-tutorial-framework](https://github.com/avatao-content/baseimage-tutorial-framework) repository.
## Getting our hands dirty
The repository of a tutorial-framework based challenge is quite similar to a regular challenge.
The project root should look something like this:
2018-07-10 15:42:08 +00:00
```
your_repo
├── solvable
│ └── [TFW based Docker image]
├── controller
│ └── [solution checking]
├── metadata
│ └── [challenge descriptions, writeups, etc.]
└── config.yml
```
2018-04-17 16:04:59 +00:00
The only notable difference is that the `solvable` Docker image is a child of our baseimage: `solvable/Dockerfile` begins with `FROM eu.gcr.io/avatao-challengestore/tutorial-framework`.
From now on we are going to focus on the `solvable` image.
### Basics of a TFW based challenge
Let us take a closer look on `solvable`:
2018-07-10 15:42:08 +00:00
```
solvable
├── Dockerfile
├── nginx webserver configurations
├── supervisor process manager (init replacement)
├── frontend clone of the frontend-tutorial-framework repo with dependencies installed
2018-04-17 16:04:59 +00:00
└── src example source code
```
2018-04-25 15:44:12 +00:00
Note that our baseimage *requires* the `nginx`, `supervisor` and `frontend` folders to be in these **exact** locations and to be used as described below.
This is a contract your image **must** comply.
2018-04-17 15:39:57 +00:00
2018-04-25 15:44:12 +00:00
The `src` directory contains a simple example of using TFW
2018-04-17 15:39:57 +00:00
### nginx
All TFW based challenges expose a single port defined in the `TFW_PUBLIC_PORT` envvar which is set to `8888` by default.
This means that in order to listen on more than a single port we must use a reverse proxy.
2018-04-17 15:39:57 +00:00
Any `.conf` files in `solvable/nginx/` will be automatically included in the nginx configuration.
2018-04-25 15:44:12 +00:00
In case you want to serve a website or service you must proxy it through `TFW_PUBLIC_PORT`.
2018-04-17 15:39:57 +00:00
This is really easy: just create a config file in `solvable/nginx/` similar to this one:
2018-07-10 15:42:08 +00:00
```
location /yoururl {
proxy_pass http://127.0.0.1:3333;
}
```
After this you can access the service running on port `3333` at `http://localhost:8888/yoururl`
It is very important to understand that from now on your application must behave well behind a reverse proxy.
What this means is all `href`s must point the proxied paths (e.g. links should refer to `/yoururl/register` instead of `/register`) on your HTML pages.
2018-04-17 15:39:57 +00:00
You can learn about configuring nginx in [this](https://www.digitalocean.com/community/tutorials/understanding-the-nginx-configuration-file-structure-and-configuration-contexts) handy little tutorial.
### supervisor
2018-04-17 16:04:59 +00:00
In most Docker conainers there is a single process running (it gets `PID 1`).
2018-04-25 15:44:12 +00:00
When working with TFW you can run as many processes as you want to by using supervisord.
2018-04-17 16:04:59 +00:00
Any `.conf` files in the `solvable/supervisor/` directory will be included in the supervisor configuration.
2018-04-25 15:44:12 +00:00
The programs, you define this way, are easy to manage (starting/stopping/restarting) using the `supervisorctl` command line tool or our built-in event handler.
2018-04-17 16:04:59 +00:00
You can even configure your processes to start with the container by including `autostart=true` in your configuration file.
2018-04-17 15:39:57 +00:00
To run your own webservice for instance you need to create a config file in `solvable/supervisor/` similar to this one:
2018-07-10 15:42:08 +00:00
```
[program:yourprogram]
user=user
directory=/home/user/example/
command=python3 server.py
autostart=true
```
2018-04-17 16:04:59 +00:00
This starts the `/home/user/example/server.py` script using `python3` after your container entered the running state (because of `autostart=true`, supervisor does not start programs by default).
2018-04-17 15:39:57 +00:00
You can learn more about configuring supervisor [here](http://supervisord.org/configuration.html).
### frontend
This is a clone of the `frontend-tutorial-framework` repository with dependencies installed in `solvable/frontend/node_modules`.
2018-04-17 16:04:59 +00:00
You can modify it to fit your needs, but this requires some Angular knowledge (not much at all).
2018-04-25 15:44:12 +00:00
If all you want to do is starting a simple web application and to send some messages you can mostly skip the Angluar knowledge bit.
Refer to the example in this repo.
2018-04-17 16:04:59 +00:00
### src
2018-07-30 06:56:40 +00:00
This folder contains the source code of our pre-written event handlers and example FSMs.
2018-04-17 16:04:59 +00:00
Note that this is not a part of the framework by any means, these are just simple examples.
2018-04-17 15:39:57 +00:00
2018-07-10 15:42:08 +00:00
```
2018-04-17 15:39:57 +00:00
solvable/src
├── event_handler_main.py event handlers implemented in python
2018-07-10 15:42:08 +00:00
├── test_fsm.py example FSM in python
└── test_fsm.yml example FSM in yaml
2018-04-17 15:39:57 +00:00
```
`event_handler_main.py` contains example usage of our pre-defined event handlers written in Python3.
As you can see they run in a separate process (set up in `solvable/supervisor/event_handler_main.conf`).
2018-04-17 15:39:57 +00:00
These event handlers could be implemented in any language that has ZMQ bindings.
2018-04-25 15:44:12 +00:00
Note that you don't have to use all our event handlers.
Should you want to avoid using a feature, you can just delete the appropriate event handler from `event_handler_main.py`.
2018-07-10 15:42:08 +00:00
`test_fsm.yml` and `test_fsm.py` are the implementations of the same FSM in YAML and Python to provide you examples of creating your own machine.
2018-04-25 15:44:12 +00:00
It is genarally a good idea to separate these files from the rest of the stuff in `solvable`, so it is a good practice to create an `src` directory.
2018-07-10 15:53:45 +00:00
### FSM
A good state machine is the backbone of a good TFW challenge.
There are two ways to define a state machine:
- Using a YAML configuration file
2018-07-27 11:59:34 +00:00
- Implementing it in Python
2018-07-10 15:53:45 +00:00
2018-07-11 08:17:19 +00:00
The first option allows you to handle FSM callbacks and custom logic in any programming language (not just Python) and is generally really easy to work with (you can execute arbitrary shell commands on events).
2018-07-10 15:53:45 +00:00
You should choose this method unless you have good reason not to.
This involves creating your YAML file (see `test_fsm.yml` for an example) and parsing it using our `YamlFSM` class (see `event_handler_main.py` for an example).
The second option allows you to implement your FSM in Python, using the transitions library.
To do this just subclass our `FSMBase` class or use our `LinearFSM` class for simple machines (see `test_fsm.py` for an example).
In your FSM you can define callbacks for states and transitions.
State callbacks:
- `on_enter`
- `on_exit`
Transition callbacks:
- `before`
- `after`
2018-07-11 08:17:19 +00:00
In your YAML file you can use these in the state and transition objects as keys, then add a shell command to run as a value (again, see `test_fsm.yml` for examples).
It is also possible to add preconditions to transitions.
This is done by adding a `predicates` key with a list of shell commands to run.
If you do this, the transition will only succeed if the return code of all predicates was `0` (as per unix convention for success).
2018-07-27 11:59:34 +00:00
Our `YamlFSM` implementation also supports jinja2 templates inside the `YAML` config file (examples in `test_fsm.yml`).
2018-04-17 15:39:57 +00:00
## Baby steps
2018-04-25 15:44:12 +00:00
When creating your own challenge the process should be the following:
1. Use our install script to bootstrap your dev environment
2. Create an FSM that describes your challenge
2018-07-10 15:42:08 +00:00
- An example is in `solvable/src/test_fsm.yml`
- The same FSM in python is in `solvable/src/test_fsm.py`
2018-04-25 15:44:12 +00:00
3. Create a `TFWServer` instance and set it up to run:
- Create a server app: `solvable/src/tfw_server.py`
- Set it up to run: `solvable/supervisor/tfw_server.conf`
4. Create event handlers connecting to the `TFWServer` handling events you want to process:
- Create an event handler server: `solvable/src/event_handler_main.py`
- Set it up to run: `solvable/supervisor/event_handler_main.conf`
2018-04-25 15:44:12 +00:00
5. Modify the frontend in `solvable/frontend` to fit your challenge
2018-04-17 15:39:57 +00:00
- This usually involves using our pre-made components
- And perhaps doing some of your own stuff, like:
2018-04-17 16:04:59 +00:00
- Sending messages then handling these in event handlers written in step 4
2018-04-17 15:39:57 +00:00
- Sending triggers to step the FSM
2018-04-18 08:49:56 +00:00
- Including images of cats http://thecatapi.com