Skip to content

Kodkollektivet/unitime-api

Repository files navigation

Unitime API 2.0

https://travis-ci.org/Kodkollektivet/unitime-api.svg?branch=celery-docker

This is the new Unitime 2.0 branch.

It is still under development but it’s getting stable.

Try it live

Course:

curl -X POST --header 'Content-Type: application/json' --data '{"course": "2DV50E"}' \
  https://unitime.jherrlin.se/api/course/ | python -mjson.tool

Lectures:

curl -X POST --header 'Content-Type: application/json' --data '{"course": "2DV50E"}' \
  https://unitime.jherrlin.se/api/lectures/ | python -mjson.tool

All courses in a list:

curl -X GET --header 'Content-Type: application/json' \
  https://unitime.jherrlin.se/api/courses/ | python -mjson.tool

Local development environment

By default this project is running Docker and docker-compose.

To start the development environment, enter the project directory and run the following:

docker-compose up

Unitime endpoints and usage

POST http://localhost:8000/api/course/
User-Agent: Emacs Restclient
Content-Type: application/json

{
    "course": "2DV50E"
}
{
  "code": "2DV50E",
  "name": "Degree Project at Bachelor Level",
  "speed": "50%",
  "points": "15 hp",
  "syllabus": "http://api.kursinfo.lnu.se/GenerateDocument.ashx?templatetype=coursesyllabus&code=2DV50E&documenttype=pdf&lang=en"
}
POST http://localhost:8000/api/lectures/
User-Agent: Emacs Restclient
Content-Type: application/json

{
    "course": "2DV50E"
}
[
  {
    "start_datetime": "2018-01-30T10:15:00",
    "end_datetime": "2018-01-30T12:00:00",
    "teacher": "Jesper Andersson",
    "info": "Seminar",
    "description": "",
    "room": {
      "name": "D1167",
      "floor": null,
      "lat": null,
      "lon": null
    }
  },
  ...
  {
    "start_datetime": "2018-05-29T10:15:00",
    "end_datetime": "2018-05-29T12:00:00",
    "teacher": "Jesper Andersson",
    "info": "Seminar",
    "description": "",
    "room": {
      "name": "D1167",
      "floor": 1,
      "lat": 1.203,
      "lon": 0.34530
    }
  }
]
GET http://localhost:8000/api/courses/
User-Agent: Emacs Restclient
Content-Type: application/json
[
  {
    "code": "5SP01E",
    "name": "Master's Thesis in Spanish",
    "speed": "50%",
    "points": "30 hp",
    "syllabus": "http://api.kursinfo.lnu.se/GenerateDocument.ashx?templatetype=coursesyllabus&code=5SP01E&documenttype=pdf&lang=en"
  },
  ...
  {
    "code": "2SA606",
    "name": "Vulnerability, conditions and interventions on individual, group and society level",
    "speed": "100%",
    "points": "15 hp",
    "syllabus": "http://api.kursinfo.lnu.se/GenerateDocument.ashx?templatetype=coursesyllabus&code=2SA606&documenttype=pdf&lang=en"
  }
]

Set up a new production server

Local environment

To be able to use the application or deploy it you need to have Docker and docker-compose. This instrictions are only for GNU/Linux systems. If you use something else I am sure there are ways to translate the instruction to you OS.

Start a new server

Start a new Debian (yes it must be Debian) server somewhere. I mostly use digitalocean. Log in to the server first via SSH as it will ask you to set a new password for the root user.

Clone this project

to your local machine.

Generate SSH key

Navigate to the secrets folder within the project directory. Take a look at the files and what they contains. Generate a new SSH key. Move the public key to ansible users filder. This key will be used for deploy. If it asks you to overwrite the existing file, choose y. Dont enter a passphrase.

cd <PROJECTDIR>/secrets
ssh-keygen -t ed25519 -f unitime-deploy-user -C unitime-deploy-user
cp unitime-deploy-user.pub ../ansible/roles/users/files/authorized_keys

Update inventory

In secrets/inventory you shall now update some secrets.

Change the ansible_ssh_host variable to your servers IP.

Change the ansible_ssh_pass variable to your server root password account.

In secrets/production.json change:

unix->password to a new random password

postgres->password to a new random password

Configure the server

To configure the server run:

docker build -t unians:0.1 -f Dockerfile-ansible .
docker run -it -v $PWD:/app --rm -w /app/ansible unians:0.1 ansible-playbook -e @../secrets/production.json main.yml

If all things went fine (no red output).

Set up unitime application

This will apply the code from the project and start the services.

docker build -t unifab:0.1 -f Dockerfile-fabric .
docker run -it -v $PWD:/app --rm -w /app unifab:0.1 fab bootstrap

Update the application

To update the application use the following to update it by specifying a branch.

docker build -t unifab:0.1 -f Dockerfile-fabric .
docker run -it -v $PWD:/app --rm -w /app unifab:0.1 fab deploy:branch=celery-docker

Rollback when deploy fails

If a deploy fails you can do a rollback, this will rollback to the codebase that was before the deploy. This wont handle database rollbacks!

docker build -t unifab:0.1 -f Dockerfile-fabric .
docker run -it -v $PWD:/app --rm -w /app unifab:0.1 fab rollback

Try to access the API

Now, try to access the API with a course

curl -i -X GET --header 'Content-Type: application/json' --data '{"course": "2DV50E"}' http://46.101.162.201/api/course/

If you are using Emacs orgmode restclient and reading this documentation from orgmode. Here is a src block for you:

GET http://46.101.162.201/api/course/
User-Agent: Emacs Restclient
Content-Type: application/json

{
    "course": "2DV50E"
}

Manual SSH with deploy user.

This will log you in to the server with the deploy user.

cd <PROJECT>
ssh -i secrets/unitime-deploy-user deploy@46.101.162.201

Manual SSH with root user.

This will log you in to the server with the root user. You find the password in secrets/inventory. The default is: SERVERROOTPASSWORD

ssh root@46.101.162.201

TODOs:

  • [ ] Server NTP time, Ansible
  • [ ] SSL/TLS, Ansible
  • [ ] Unattended updates, Ansible
  • [ ] iptables, Ansible