|
| 1 | +# How to add a new programming language to Codeocean |
| 2 | +## Overview |
| 3 | +Adding a new programming language to Codeocean is done in three steps: |
| 4 | +1. Create a Docker image that contains the necessary tools to compile, run, etc. |
| 5 | +2. Create a Ruby file under lib/ that specifies the name of the testing framework, parses the output of the testing tools, and returns the number of tests, number of failed tests, and associated error messages. |
| 6 | +3. Create a new execution environment for the programming language in Codeoceans graphical interface. |
| 7 | + |
| 8 | +### 1. Create the Docker image |
| 9 | +The following Dockerfile is used as a starting point: |
| 10 | +```dockerfile |
| 11 | +FROM openhpi/docker_exec_phusion |
| 12 | +LABEL description='' |
| 13 | +LABEL run_command='' |
| 14 | +LABEL test_command='' |
| 15 | + |
| 16 | +# insert installation routines, environment variable adjustment, etc. here |
| 17 | + |
| 18 | +# switch user |
| 19 | +USER user |
| 20 | +``` |
| 21 | + |
| 22 | +Each execution environment must be derived from `openhpi/docker_exec_phusion`. This image is based on `phusion/baseimage:master`, a Docker-optimized version of Ubuntu that provides, among other things, the [install_clean](https://github.com/phusion/baseimage-docker#overview) command for installing Ubuntu packages. |
| 23 | +At the end, the user needs to be switched so that he does not get elevated privileges in his container. |
| 24 | + |
| 25 | +Specify the run and test command to be used for the programming language. You can use the parameter `%{filename}` inside the command string. When a user clicks on *run* or *test* for a certain file in Codeoceans graphical interface, `%{filename}` will be replaced with the name of this file. Note that the run and test command are not automatically imported into Codeocean when you add a new Execution Environment. |
| 26 | + |
| 27 | +Add the installation routines and customizations needed for the programming language. |
| 28 | + |
| 29 | +Build with the following command: |
| 30 | +`docker build --no-cache -t openhpi/co_execenv_<language> .` |
| 31 | +where `<language>` is the corresponding programming language. |
| 32 | + |
| 33 | +Select the image in Codeoceans graphical interface when creating a new Execution Environment. |
| 34 | + |
| 35 | +### 2. Create the Adapter |
| 36 | +Create a file named my_adapter.rb and add the following content: |
| 37 | +```ruby |
| 38 | +# frozen_string_literal: true |
| 39 | + |
| 40 | +class MyAdapter < TestingFrameworkAdapter |
| 41 | + def self.framework_name |
| 42 | + # insert here what you want to call your test framework |
| 43 | + end |
| 44 | + |
| 45 | + def parse_output(output) |
| 46 | + # insert code here that parses the output generated by the testing tools |
| 47 | + end |
| 48 | +end |
| 49 | +``` |
| 50 | + |
| 51 | +Change the name of the file and class to match your programming language and testing tools. |
| 52 | +This class is a subclass of *`TestingFrameworkAdapter`*. Implement the methods *`self.framework_name`* and *`parse_output(output)`*: |
| 53 | +*`self.framework_name`* shall return the name of the testing framework as a string. |
| 54 | +*`parse_output`* gets a [hash object](https://ruby-doc.org/core-3.1.2/Hash.html) *`output`* with the following keys and data types of values: |
| 55 | +- `file_role`: String |
| 56 | +- `waiting_for_container_time`: Float |
| 57 | +- `stdout`: String |
| 58 | +- `stderr`: String |
| 59 | +- `messages`: Array of hash objects with the keys `:cmd, :stream, :log, :timestamp`. |
| 60 | +- `exit_code`: Integer |
| 61 | +- `container_execution_time`: Float |
| 62 | +- `status`: Symbol |
| 63 | + |
| 64 | +*`parse_output`* shall return a [hash object](https://ruby-doc.org/core-3.1.2/Hash.html) with the following keys: |
| 65 | +- `count`: Integer ( the number of executed tests) |
| 66 | +- `failed`: Integer ( the number of failed tests) |
| 67 | +- `error_messages`: Array of strings (the error messages of the failed tests.) |
| 68 | + |
| 69 | +### 3. Create a new Execution Environment |
| 70 | +1. In Codeoceans graphical interface select `Administration` > `Execution Environments` and click on `Add Execution Environment`. |
| 71 | +2. Give it a name and default file type. |
| 72 | +3. Select the Docker image you built above. |
| 73 | +4. Copy and paste the run and test command from the Dockerfile into the corresponding fields. |
| 74 | +5. Select the testing framework you defined above in *`self.framework_name`*. |
| 75 | +6. It might be necessary to set the `Prewarming Pool Size` to > 0. |
| 76 | +7. Make further adjustments if necessary and then click on `Create Execution Environment`. |
0 commit comments