Blame view

README.md 7.54 KB
43785de6   Miguel Barão   - added documenta...
1
# Getting Started
a96cd2c7   Miguel Barão   - added logging s...
2

1e8f0810   Miguel Barão   update setup.py a...
3

5d859a41   Miguel Barão   update documentation
4
5
6
## Requirements

This application requires python3.7+ and a few additional python packages.
93e13002   Miguel Barão   updates README.md.
7
It also uses npm (Node package management) to install javascript libraries.
a96cd2c7   Miguel Barão   - added logging s...
8

39126690   Miguel Barão   removes npm and n...
9
10
11
12
### Install python3.7 with sqlite3 support and npm

This can be done using the system package management, downloaded from [http://www.python.org](), or compiled from sources.

f540e673   Miguel Barão   - added README.md
13
- Installing from the system package manager:
5d859a41   Miguel Barão   update documentation
14
15

```sh
39126690   Miguel Barão   removes npm and n...
16
sudo port install python37 npm5              # MacOS
443a1eea   Miguel Barão   Update to latest ...
17
sudo pkg install python37 py37-sqlite3 npm   # FreeBSD
39126690   Miguel Barão   removes npm and n...
18
sudo apt install python3.7 npm               # Linux
5d859a41   Miguel Barão   update documentation
19
20
```

43785de6   Miguel Barão   - added documenta...
21
- Installing from source:
39126690   Miguel Barão   removes npm and n...
22
     
43785de6   Miguel Barão   - added documenta...
23
Download from [http://www.python.org]() and
39126690   Miguel Barão   removes npm and n...
24
25

```sh
c54ee176   Miguel Barão   update javascript...
26
27
unxz Python-3.7.tar.xz
tar xvf Python-3.7.tar
39126690   Miguel Barão   removes npm and n...
28
29
cd Python-3.7
./configure --prefix=$HOME/.local/bin
1e8f0810   Miguel Barão   update setup.py a...
30
make && make install
93e13002   Miguel Barão   updates README.md.
31
32
```

43785de6   Miguel Barão   - added documenta...
33
This will install python locally under `~/.local/bin`. Make sure to add it to your `PATH` (edit `~/.profile` in MacOS or FreeBSD).
59213008   Miguel Barão   - fixed user path...
34

443a1eea   Miguel Barão   Update to latest ...
35

a96cd2c7   Miguel Barão   - added logging s...
36
### Install pip
43785de6   Miguel Barão   - added documenta...
37

39126690   Miguel Barão   removes npm and n...
38
39
If the `pip` command is not yet installed, run one of these:

1e8f0810   Miguel Barão   update setup.py a...
40
```sh
43785de6   Miguel Barão   - added documenta...
41
sudo apt install python3.7-pip     # Ubuntu
f540e673   Miguel Barão   - added README.md
42
sudo pkg py37-pip                  # FreeBSD
443a1eea   Miguel Barão   Update to latest ...
43
44
sudo port install py37-pip         # MacOS
python3.7 -m ensurepip --user      # otherwise
39126690   Miguel Barão   removes npm and n...
45
```
5d859a41   Miguel Barão   update documentation
46

1e8f0810   Miguel Barão   update setup.py a...
47
48
49
50
51
The latter will install `pip` in your user account under `~/.local/bin`.
In the end you should be able to run `pip3 --version` and 
`python3 -c "import sqlite3"` without errors (sometimes `pip3` is `pip`, 
`pip3.7` or `pip-3.7`).

5dae401a   Miguel Barão   update README.md
52
If you want to always install python modules on the user account (recommended), 
f540e673   Miguel Barão   - added README.md
53
edit the pip configuration file `~/.config/pip/pip.conf` (FreeBSD, Linux) or 
43785de6   Miguel Barão   - added documenta...
54
55
56
57
`Library/Application Support/pip/pip.conf` (MacOS) and add the lines

```ini
[global]
f540e673   Miguel Barão   - added README.md
58
user = yes
39126690   Miguel Barão   removes npm and n...
59
```
f540e673   Miguel Barão   - added README.md
60

43785de6   Miguel Barão   - added documenta...
61
### Install python packages and javascript libraries:
39126690   Miguel Barão   removes npm and n...
62

43785de6   Miguel Barão   - added documenta...
63
Replace USER by your bitbucket username:
f540e673   Miguel Barão   - added README.md
64

c3eba869   Miguel Barão   update README.md
65
```sh
a96cd2c7   Miguel Barão   - added logging s...
66
cd somewhere
443a1eea   Miguel Barão   Update to latest ...
67
git clone https://git.xdi.uevora.pt/mjsb/aprendizations.git
1e8f0810   Miguel Barão   update setup.py a...
68
cd aprendizations
a96cd2c7   Miguel Barão   - added logging s...
69
npm install             # install javascript libraries
443a1eea   Miguel Barão   Update to latest ...
70
pip install .           # install aprendizations and dependencies
39126690   Miguel Barão   removes npm and n...
71
```
f540e673   Miguel Barão   - added README.md
72

39126690   Miguel Barão   removes npm and n...
73
74
Python packages are usually installed in:

5dae401a   Miguel Barão   update README.md
75
76
- `~/.local/lib/python3.7/site-packages/` in Linux/FreeBSD.
- `~/Library/python/3.7/lib/python/site-packages/` in MacOS.
93e13002   Miguel Barão   updates README.md.
77

1e8f0810   Miguel Barão   update setup.py a...
78
Javascript libraries are installed in `aprendizations/node_modules` directory.
5dae401a   Miguel Barão   update README.md
79
80
This libraries have symbolic links from `aprendizations/aprendizations/static`.

93e13002   Miguel Barão   updates README.md.
81
At this point aprendizations is installed in
b4ceb7fc   Miguel Barão   minor change in l...
82

93e13002   Miguel Barão   updates README.md.
83
84
85
```sh
~/Library/Python/3.7/bin              # MacOS
~/.local/bin                          # FreeBSD/Linux
39126690   Miguel Barão   removes npm and n...
86
```
93e13002   Miguel Barão   updates README.md.
87

f540e673   Miguel Barão   - added README.md
88
Make sure this directory is in your `$PATH`.
59213008   Miguel Barão   - fixed user path...
89
90

The server can be run with the command `aprendizations` from the terminal.
443a1eea   Miguel Barão   Update to latest ...
91
92

## Configuration
c288f8c6   Miguel Barao   - updated documen...
93

443a1eea   Miguel Barão   Update to latest ...
94
95
96
### Database

The user data is maintained in a sqlite3 database file. We first need to create 
c288f8c6   Miguel Barao   - updated documen...
97
the database.
93e13002   Miguel Barão   updates README.md.
98
At the moment, the database should be located in the same directory as the main 
f540e673   Miguel Barão   - added README.md
99
configuration file (see below). As an example, do
b4ceb7fc   Miguel Barão   minor change in l...
100

59213008   Miguel Barão   - fixed user path...
101
```sh
43785de6   Miguel Barão   - added documenta...
102
103
104
cd demo                               # contains a small example
initdb-aprendizations                 # show or initialize database
initdb-aprendizations --admin         # add admin user
59213008   Miguel Barão   - fixed user path...
105
initdb-aprendizations inscricoes.csv  # add students from CSV
93e13002   Miguel Barão   updates README.md.
106
107
108
initdb-aprendizations --add 1184 "Aladino da Silva"   # add user
initdb-aprendizations --update 1184 --pw alibaba      # update password
initdb-aprendizations --help          # for the available options
bbc1c506   Miguel Barão   - documentation u...
109
```
43785de6   Miguel Barão   - added documenta...
110

443a1eea   Miguel Barão   Update to latest ...
111
The default password is equal to the user name used to login.
5d859a41   Miguel Barão   update documentation
112

43785de6   Miguel Barão   - added documenta...
113

bbc1c506   Miguel Barão   - documentation u...
114
### SSL Certificates
443a1eea   Miguel Barão   Update to latest ...
115
116

We need certificates for https. Certificates can be self-signed or validated by 
bbc1c506   Miguel Barão   - documentation u...
117
a trusted authority.
43785de6   Miguel Barão   - added documenta...
118

93e13002   Miguel Barão   updates README.md.
119
120
Self-signed can be used locally for development and testing, but browsers will 
complain. LetsEncrypt issues trusted and free certificates, but the server must 
43785de6   Miguel Barão   - added documenta...
121
have a registered publicly accessible domain name.
bbc1c506   Miguel Barão   - documentation u...
122

443a1eea   Miguel Barão   Update to latest ...
123
124
125
#### Selfsigned

Generate a selfsigned certificate and place it in `~/.local/share/certs`.
bbc1c506   Miguel Barão   - documentation u...
126

43785de6   Miguel Barão   - added documenta...
127
```sh
443a1eea   Miguel Barão   Update to latest ...
128
openssl req -x509 -newkey rsa:4096 -keyout privkey.pem -out cert.pem -days 365 -nodes
43785de6   Miguel Barão   - added documenta...
129
130
131
132
```

#### LetsEncrypt

bbc1c506   Miguel Barão   - documentation u...
133
```sh
5d859a41   Miguel Barão   update documentation
134
135
136
137
sudo pkg install py27-certbot       # FreeBSD
```

Shutdown the firewall and any web server that might be running. Then run the script to generate the certificate:
443a1eea   Miguel Barão   Update to latest ...
138
139

```sh
39126690   Miguel Barão   removes npm and n...
140
sudo service pf stop                            # disable pf firewall (FreeBSD)
5d859a41   Miguel Barão   update documentation
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
sudo certbot certonly --standalone -d www.example.com
sudo service pf start                           # enable pf firewall
```

Certificates are saved under `/usr/local/etc/letsencrypt/live/www.example.com/`. Copy them to `~/.local/share/certs` and change permissions to be readable:

```sh
sudo cp /usr/local/etc/letsencrypt/live/www.example.com/cert.pem .
sudo cp /usr/local/etc/letsencrypt/live/www.example.com/privkey.pem .
chmod 400 cert.pem privkey.pem
```

Renews can be done as follows:

```sh
93e13002   Miguel Barão   updates README.md.
156
sudo service pf stop   # shutdown firewall
c55940af   Miguel Barão   - minor change in...
157
sudo certbot renew
39126690   Miguel Barão   removes npm and n...
158
sudo service pf start  # start firewall
443a1eea   Miguel Barão   Update to latest ...
159
```
f540e673   Miguel Barão   - added README.md
160

43785de6   Miguel Barão   - added documenta...
161
and then copy the `cert.pem` and `privkey.pem` files to `~/.local/share/certs` directory. Change permissions and ownership as appropriate.
3ec49458   Miguel Barão   fix README.md and...
162

39126690   Miguel Barão   removes npm and n...
163

43785de6   Miguel Barão   - added documenta...
164
### Testing
59213008   Miguel Barão   - fixed user path...
165

443a1eea   Miguel Barão   Update to latest ...
166
167
The application includes a small example in `demo/demo.yaml`. Run it with

93e13002   Miguel Barão   updates README.md.
168
```sh
443a1eea   Miguel Barão   Update to latest ...
169
170
cd demo
aprendizations demo.yaml
c288f8c6   Miguel Barao   - updated documen...
171
```
59213008   Miguel Barão   - fixed user path...
172
173

and open a browser at [https://127.0.0.1:8443](https://127.0.0.1:8443). 
443a1eea   Miguel Barão   Update to latest ...
174
175
176
If it everything looks good, 
check at the correct address `https://www.example.com` (requires port forward 
in the firewall). The option `--debug` provides more verbose logging and might 
59213008   Miguel Barão   - fixed user path...
177
178
179
180
181
be useful during testing. The option `--check` generates all the questions once
before running the server to check for any obvious syntax error.


### Firewall configuration
43785de6   Miguel Barão   - added documenta...
182

93e13002   Miguel Barão   updates README.md.
183
Ports 80 and 443 are only usable by root. For security reasons it is better to 
43785de6   Miguel Barão   - added documenta...
184
185
186
run the server as an unprivileged user on higher ports like 8080 for http and 
8443 for https. For this, we can configure port forwarding in the firewall to 
redirect incoming tcp traffic from 80 to 8080 and 443 to 8443.
59213008   Miguel Barão   - fixed user path...
187

5d859a41   Miguel Barão   update documentation
188
#### FreeBSD and pf
59213008   Miguel Barão   - fixed user path...
189

43785de6   Miguel Barão   - added documenta...
190
Edit `/etc/pf.conf`:
59213008   Miguel Barão   - fixed user path...
191

43785de6   Miguel Barão   - added documenta...
192
193
194
195
```sh
ext_if="em0"                # this should be the correct network interface
rdr on $ext_if proto tcp from any to any port 80 -> 127.0.0.1 port 8080
rdr on $ext_if proto tcp from any to any port 443 -> 127.0.0.1 port 8443
59213008   Miguel Barão   - fixed user path...
196
```
43785de6   Miguel Barão   - added documenta...
197
198
199
200
201

Under virtualbox with guest additions use `ext_if="vtnet0"`.

Edit `/etc/rc.conf`

59213008   Miguel Barão   - fixed user path...
202
```sh
ea4ff4d2   Miguel Barão   - radio: added su...
203
pf_enable="YES"
59213008   Miguel Barão   - fixed user path...
204
pf_flags=""
39126690   Miguel Barão   removes npm and n...
205
206
pf_rules="/etc/pf.conf"

93e13002   Miguel Barão   updates README.md.
207
208
209
210
# optional logging:
pflog_enable="YES"
pflog_flags=""
pflog_logfile="/var/log/pflog"
39126690   Miguel Barão   removes npm and n...
211
212
```

443a1eea   Miguel Barão   Update to latest ...
213
214
215
Reboot or `sudo service pf start`.


39126690   Miguel Barão   removes npm and n...
216
## Troubleshooting
a203d3cc   Miguel Barão   - new http server...
217

5d859a41   Miguel Barão   update documentation
218
219
220
221
222
To help with troubleshooting, use the option `--debug` when running the server. 
This will increase logs in the terminal and will present the python exception 
errors in the browser.

Logging levels can be adjusted in `~/.config/aprendizations/logger.yaml` and 
39126690   Miguel Barão   removes npm and n...
223
`~/.config/aprendizations/logger-debug.yaml`.
5d859a41   Miguel Barão   update documentation
224
225

If these files do not yet exist, there are examples in `aprendizations/config` that can be copied to `~/.config/aprendizations`.
59213008   Miguel Barão   - fixed user path...
226
227

#### UnicodeEncodeError
443a1eea   Miguel Barão   Update to latest ...
228
229

The server should not generate this error, but when using external scripts to 
5dae401a   Miguel Barão   update README.md
230
generate questions or to correct, these scripts can print unicode strings to 
43785de6   Miguel Barão   - added documenta...
231
stdout. If the terminal does not support unicode, python will generate this 
443a1eea   Miguel Barão   Update to latest ...
232
exception.
5dae401a   Miguel Barão   update README.md
233

43785de6   Miguel Barão   - added documenta...
234
- FreeBSD fix: edit `~/.login_conf` to use UTF-8, for example:
443a1eea   Miguel Barão   Update to latest ...
235
236

```sh
43785de6   Miguel Barão   - added documenta...
237
me:\
443a1eea   Miguel Barão   Update to latest ...
238
    :charset=UTF-8:\
59213008   Miguel Barão   - fixed user path...
239
    :lang=en_US.UTF-8:
443a1eea   Miguel Barão   Update to latest ...
240
241
242
```

- Debian fix: check `locale`...
5dae401a   Miguel Barão   update README.md
243

59213008   Miguel Barão   - fixed user path...
244
## FAQ
443a1eea   Miguel Barão   Update to latest ...
245

59213008   Miguel Barão   - fixed user path...
246
- Which students did at least one topic?
43785de6   Miguel Barão   - added documenta...
247
248
249
250
251

```sh
sqlite3 students.db "select distinct student_id from studenttopic"
```

f75a344f   Miguel Barão   - README.md updat...
252
- How many topics has each student done?
443a1eea   Miguel Barão   Update to latest ...
253
254
255
256
257
258
259
260
261
262

```sh
sqlite3 students.db "select student_id, count(topic_id) from studenttopic group by student_id order by count(topic_id) desc"
```

- Which questions have more wrong answers?

```sh
sqlite3 students.db "select count(ref), ref from answers where grade<1.0 group by ref order by count(ref) desc"
```
db04e658   Miguel Barão   - added option to...

43785de6   Miguel Barão   - added documenta...

f75a344f   Miguel Barão   - README.md updat...

93e13002   Miguel Barão   updates README.md.

f75a344f   Miguel Barão   - README.md updat...

43785de6   Miguel Barão   - added documenta...

d187aad4   Miguel Barão   - adds courses

43785de6   Miguel Barão   - added documenta...

f75a344f   Miguel Barão   - README.md updat...

d187aad4   Miguel Barão   - adds courses

f75a344f   Miguel Barão   - README.md updat...

43785de6   Miguel Barão   - added documenta...

93e13002   Miguel Barão   updates README.md.

f75a344f   Miguel Barão   - README.md updat...

93e13002   Miguel Barão   updates README.md.

443a1eea   Miguel Barão   Update to latest ...

f75a344f   Miguel Barão   - README.md updat...

93e13002   Miguel Barão   updates README.md.

443a1eea   Miguel Barão   Update to latest ...

93e13002   Miguel Barão   updates README.md.