Environment Setup

This chapter assumes that you do not have PDM, vagrant and virtual box installed on your machine.

Development Tools

  • sqlite3

    Required to run the django database shell pdm run manage.py dbshell

     sudo apt install sqlite3
    
  • graphviz

    Graphviz is required to generate a graph from the models. This can be done my calling make render-model

     sudo apt install graphviz # binaries sudo apt install graphviz-dev # Headers to build the django-extensions
    

Installing PDM

follow the instructions provided on the PDM main documentation

Installing Vagrant && Virtual Box

While it is possible to install both via the command line, the easiest is to install both vagrant and virtual box via the software manager provided with your linux distribution.

Vagrant Provisioning

Setting up vagrant the first time

To initialize the vagrant file the first time, you run the following command and then fill out the vagrant file (below)

 vagrant init ubuntu/bionic64
A `Vagrantfile` has been placed in this directory. You are now                                                                                                                                                                   ─╯
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
╭─ ~/github/lbrack/profile-rest-api ···················   profile-rest-api Py ─╮
╰─                                                                             ─╯

Vagrant File

Below is the vagrant file found in the root directory. This file is used to provision the vagrant boxes. Because the project uses PDM and PDM relies on Python 3.7 or higher, we have to install Python (at the time of writing, 3.8 was available).

 1# -*- mode: ruby -*-
 2# vi: set ft=ruby :
 3
 4Vagrant.configure("2") do |config|
 5  # The most common configuration options are documented and commented below.
 6  # For a complete reference, please see the online documentation at
 7  # https://docs.vagrantup.com.
 8
 9  # Vagrant Box
10  # boxes at https://vagrantcloud.com/search.
11  config.vm.box = "ubuntu/bionic64"
12  config.vm.box_version = "~> 20200304.0.0"
13  config.vm.network "forwarded_port", guest: 8000, host: 8000, host_ip: "127.0.0.1"
14
15  # Provisioning Directives
16  config.vm.provision "shell", inline: <<-SHELL
17    systemctl disable apt-daily.service
18    systemctl disable apt-daily.timer
19    export DEBIAN_FRONTEND=noninteractive
20    sudo apt-get update
21    touch /home/vagrant/.bash_aliases
22    if ! grep -q PYTHON_38_ADDED /home/vagrant/.bash_aliases; then
23       sudo apt-get install -y python3.8
24       sudo apt-get install -y python3-venv
25       sudo apt-get install -y python3.8-venv
26       sudo apt-get install -y zip
27       echo "# PYTHON_38_ADDED" >> /home/vagrant/.bash_aliases
28       echo "alias python='python3.8'" >> /home/vagrant/.bash_aliases
29       echo "# PYTHON_38_ADDED END" >> /home/vagrant/.bash_aliases
30    fi
31    if ! grep -q PDM_INSTALLED /home/vagrant/.bashrc; then
32     runuser -l vagrant -c 'curl -sSL https://pdm.fming.dev/install-pdm.py | python3.8 -'
33     echo "# PDM_INSTALLED" >> /home/vagrant/.bashrc
34     echo 'export PATH=/home/vagrant/.local/bin:$PATH'  >> /home/vagrant/.bashrc
35     echo 'source /vagrant/.aliases' >> /home/vagrant/.bashrc
36     echo 'cd /vagrant' >> /home/vagrant/.bashrc
37     echo "# PDM_INSTALLED END" >> /home/vagrant/.bashrc
38    fi
39    runuser -l vagrant -c 'export PATH=/home/vagrant/.local/bin:$PATH;cd /vagrant;pdm venv remove --yes vagrant;pdm venv create --force --name vagrant 3.8;pdm use --venv vagrant'
40    runuser -l vagrant -c 'export PATH=/home/vagrant/.local/bin:$PATH;cd /vagrant;pdm install --prod'
41  SHELL
42end

On line 13 forward the port from the host OS to the guest one so the app can be reached from the dev machine.

on line 22, we install python 3.8 and alias python with that version thus overriding the default python executable.

On line 31 we install PDM (as vagrant and not root), set the path and execute some useful aliases to start the server.

On line 39, we create a virtual environment (called vagrant) using PDM and use it by default in this project. This environment is created in another location so it doesn’t interfere with the .venv dev environment at the root of the project. Finally, we set the environment as the default on that vagrant box.

Finally, on line 40, we run PDM install to install all the dependencies so we can run our server.

$ vagrant destroy -gf # Destroy the vagrant boc
$ vagrant up # start the vagrant box
$ vagtant up --provision # start the box and run the shell command on line 16
$ vagrant ssh # ssh to the vagrant box.