Blame view

README.md 8.16 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
Latest review: 2022-04-04
5d859a41   Miguel Barão   update documentation
4
5
6

## Installation

93e13002   Miguel Barão   updates README.md.
7
To complete the installation we will need to perform the following steps:
a96cd2c7   Miguel Barão   - added logging s...
8

39126690   Miguel Barão   removes npm and n...
9
10
11
12
1. install python3 and pip
2. install aprendizations
3. generate SSL certificates
4. configure the firewall (optional)
f540e673   Miguel Barão   - added README.md
13

5d859a41   Miguel Barão   update documentation
14
15
To use the software we need to:

39126690   Miguel Barão   removes npm and n...
16
1. initialize a database
443a1eea   Miguel Barão   Update to latest ...
17
2. go to the demo directory (or an existing course)
39126690   Miguel Barão   removes npm and n...
18
3. run `aprendizations courses.yaml`
5d859a41   Miguel Barão   update documentation
19
20

Each of these steps is explained below.
43785de6   Miguel Barão   - added documenta...
21

39126690   Miguel Barão   removes npm and n...
22
### Install python3 with sqlite3 support
43785de6   Miguel Barão   - added documenta...
23

39126690   Miguel Barão   removes npm and n...
24
25
Minimum supported version is python3.9. The installed versions depend on the
operating system default.
c54ee176   Miguel Barão   update javascript...
26
27

```sh
39126690   Miguel Barão   removes npm and n...
28
29
sudo pkg install python3 py39-sqlite3   # FreeBSD
sudo apt install python3                # Linux (Ubuntu)
1e8f0810   Miguel Barão   update setup.py a...
30
sudo port install python311             # MacOS
93e13002   Miguel Barão   updates README.md.
31
32
```

43785de6   Miguel Barão   - added documenta...
33
### Install pip
59213008   Miguel Barão   - fixed user path...
34

443a1eea   Miguel Barão   Update to latest ...
35
Install `pip` from the system package manager:
a96cd2c7   Miguel Barão   - added logging s...
36

43785de6   Miguel Barão   - added documenta...
37
```sh
39126690   Miguel Barão   removes npm and n...
38
39
sudo pkg install py39-pip          # FreeBSD
sudo apt install python3-pip       # Linux (Ubuntu)
1e8f0810   Miguel Barão   update setup.py a...
40
sudo port install py311-pip        # MacOS
43785de6   Miguel Barão   - added documenta...
41
```
f540e673   Miguel Barão   - added README.md
42

443a1eea   Miguel Barão   Update to latest ...
43
44
In the end you should be able to run `python3 -m pip --version` and `python3 -c
"import sqlite3"` without errors.
39126690   Miguel Barão   removes npm and n...
45

5d859a41   Miguel Barão   update documentation
46
Packages should **not** be installed system-wide. Either install them to a
1e8f0810   Miguel Barão   update setup.py a...
47
48
49
50
51
python virtual environment or in the user area.

To install in the user area use `python3 -m pip install --user some_package` or
edit the configuration file `~/.config/pip/pip.conf` (FreeBSD, Linux) or
`~/Library/Application Support/pip/pip.conf` (MacOS) and add the lines
5dae401a   Miguel Barão   update README.md
52

f540e673   Miguel Barão   - added README.md
53
```ini
43785de6   Miguel Barão   - added documenta...
54
55
56
57
[global]
user = yes
```

f540e673   Miguel Barão   - added README.md
58
### Install aprendizations
39126690   Miguel Barão   removes npm and n...
59

f540e673   Miguel Barão   - added README.md
60
```sh
43785de6   Miguel Barão   - added documenta...
61
python3 -m pip install git+https://git.xdi.uevora.pt/mjsb/aprendizations.git
39126690   Miguel Barão   removes npm and n...
62
```
43785de6   Miguel Barão   - added documenta...
63

f540e673   Miguel Barão   - added README.md
64
Python packages are usually installed in:
c3eba869   Miguel Barão   update README.md
65

a96cd2c7   Miguel Barão   - added logging s...
66
* `~/.local/lib/python3.9/site-packages/` in Linux/FreeBSD.
443a1eea   Miguel Barão   Update to latest ...
67
* `~/Library/python/3.11/lib/python/site-packages/` in MacOS.
1e8f0810   Miguel Barão   update setup.py a...
68

a96cd2c7   Miguel Barão   - added logging s...
69
When aprendizations is installed with pip, all the dependencies are also
443a1eea   Miguel Barão   Update to latest ...
70
installed.
39126690   Miguel Barão   removes npm and n...
71

f540e673   Miguel Barão   - added README.md
72
At this point, the commands `aprendizations` and `initdb-aprendizations` are
39126690   Miguel Barão   removes npm and n...
73
74
installed in

5dae401a   Miguel Barão   update README.md
75
76
```sh
~/.local/bin                          # Linux/FreeBSD
93e13002   Miguel Barão   updates README.md.
77
~/Library/Python/3.11/bin             # MacOS
1e8f0810   Miguel Barão   update setup.py a...
78
```
5dae401a   Miguel Barão   update README.md
79
80

and can be run from the terminal:
93e13002   Miguel Barão   updates README.md.
81

b4ceb7fc   Miguel Barão   minor change in l...
82
```sh
93e13002   Miguel Barão   updates README.md.
83
84
85
aprendizations --version
aprendizations --help
initdb-aprendizations --help
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
### SSL Certificates
59213008   Miguel Barão   - fixed user path...
89
90

We need certificates for https. Certificates can be self-signed or validated by
443a1eea   Miguel Barão   Update to latest ...
91
92
a trusted authority.

c288f8c6   Miguel Barao   - updated documen...
93
Self-signed can be used locally for development and testing, but browsers will
443a1eea   Miguel Barão   Update to latest ...
94
95
96
complain. LetsEncrypt issues trusted and free certificates, but the server must
have a registered publicly accessible domain name.

c288f8c6   Miguel Barao   - updated documen...
97
#### Generating selfsigned certificates
93e13002   Miguel Barão   updates README.md.
98

f540e673   Miguel Barão   - added README.md
99
Generate a selfsigned certificate and place it in `~/.local/share/certs`.
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
openssl req -x509 -newkey rsa:4096 -keyout privkey.pem -out cert.pem -days 365 -nodes
```

59213008   Miguel Barão   - fixed user path...
105
#### LetsEncrypt certificates
93e13002   Miguel Barão   updates README.md.
106
107
108

Install the certbot from LetsEncrypt:

bbc1c506   Miguel Barão   - documentation u...
109
```sh
43785de6   Miguel Barão   - added documenta...
110
sudo pkg install py39-certbot       # FreeBSD
443a1eea   Miguel Barão   Update to latest ...
111
sudo apt install certbot            # Ubuntu
5d859a41   Miguel Barão   update documentation
112
```
43785de6   Miguel Barão   - added documenta...
113

bbc1c506   Miguel Barão   - documentation u...
114
To generate or renew the certificates, ports 80 and 443 must be accessible.
443a1eea   Miguel Barão   Update to latest ...
115
116
**The firewall and webserver have to be stopped**.

bbc1c506   Miguel Barão   - documentation u...
117
```sh
43785de6   Miguel Barão   - added documenta...
118
sudo certbot certonly --standalone -d www.example.com   # first time
93e13002   Miguel Barão   updates README.md.
119
120
sudo certbot renew                                      # renew
```
43785de6   Miguel Barão   - added documenta...
121

bbc1c506   Miguel Barão   - documentation u...
122
Certificates are saved under
443a1eea   Miguel Barão   Update to latest ...
123
124
125
`/usr/local/etc/letsencrypt/live/www.example.com/`. Copy them to
`~/.local/share/certs` and change permissions to be readable:

bbc1c506   Miguel Barão   - documentation u...
126
```sh
43785de6   Miguel Barão   - added documenta...
127
cd ~/.local/share/certs
443a1eea   Miguel Barão   Update to latest ...
128
sudo cp /usr/local/etc/letsencrypt/live/www.example.com/cert.pem .
43785de6   Miguel Barão   - added documenta...
129
130
131
132
sudo cp /usr/local/etc/letsencrypt/live/www.example.com/privkey.pem .
chmod 400 cert.pem privkey.pem
```

bbc1c506   Miguel Barão   - documentation u...
133
## Configuration
5d859a41   Miguel Barão   update documentation
134
135
136
137

### Database

User data is maintained in a sqlite3 database which has to be created manually
443a1eea   Miguel Barão   Update to latest ...
138
139
using the `initdb-aprendizations` command.  The database file should be located
in the same directory as the main YAML configuration file (`courses.yaml`).
39126690   Miguel Barão   removes npm and n...
140

5d859a41   Miguel Barão   update documentation
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
For example, to run the included demo do:

```sh
cd demo                               # contains a small example
initdb-aprendizations                 # show or initialize database
initdb-aprendizations --admin         # add admin user
initdb-aprendizations inscricoes.csv  # add students from CSV
initdb-aprendizations --add 1184 "Aladino da Silva"   # add new user
initdb-aprendizations --update 1184 --pw alibaba      # update password
initdb-aprendizations --help          # for available options
```

The default password is equal to the user name, if left undefined.

### Running the demo
93e13002   Miguel Barão   updates README.md.
156

c55940af   Miguel Barão   - minor change in...
157
The application includes a small example in `demo/courses.yaml` that can be
39126690   Miguel Barão   removes npm and n...
158
used for initial testing. Run it with
443a1eea   Miguel Barão   Update to latest ...
159

f540e673   Miguel Barão   - added README.md
160
```sh
43785de6   Miguel Barão   - added documenta...
161
cd demo
3ec49458   Miguel Barão   fix README.md and...
162
aprendizations courses.yaml
39126690   Miguel Barão   removes npm and n...
163
```
43785de6   Miguel Barão   - added documenta...
164

59213008   Miguel Barão   - fixed user path...
165
Open the browser at [https://127.0.0.1:8443](https://127.0.0.1:8443).
443a1eea   Miguel Barão   Update to latest ...
166
167
If everything looks good, check at the correct address
`https://www.example.com:8443`.
93e13002   Miguel Barão   updates README.md.
168
The option `--debug` provides more verbose logging and might be useful during
443a1eea   Miguel Barão   Update to latest ...
169
170
testing.

c288f8c6   Miguel Barao   - updated documen...
171
### Firewall configuration
59213008   Miguel Barão   - fixed user path...
172
173

Ports 80 and 443 are only usable by root. For security reasons the server runs
443a1eea   Miguel Barão   Update to latest ...
174
175
176
as an unprivileged user on port 8443 for https.  To access the server in the
default https port (443), port forwarding must be configured in the firewall.

59213008   Miguel Barão   - fixed user path...
177
178
179
180
181
#### FreeBSD and pf

Edit `/etc/pf.conf`:

```sh
43785de6   Miguel Barão   - added documenta...
182
ext_if="em0"                # change em0 to the correct network interface
93e13002   Miguel Barão   updates README.md.
183
rdr on $ext_if proto tcp from any to any port 80 -> 127.0.0.1 port 8080
43785de6   Miguel Barão   - added documenta...
184
185
186
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...
187
Virtualbox with guest additions uses `ext_if="vtnet0"`.
5d859a41   Miguel Barão   update documentation
188

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

59213008   Miguel Barão   - fixed user path...
191
```sh
43785de6   Miguel Barão   - added documenta...
192
193
194
195
pf_enable="YES"
pf_flags=""
pf_rules="/etc/pf.conf"

59213008   Miguel Barão   - fixed user path...
196
# optional logging:
43785de6   Miguel Barão   - added documenta...
197
198
199
200
201
pflog_enable="YES"
pflog_flags=""
pflog_logfile="/var/log/pflog"
```

59213008   Miguel Barão   - fixed user path...
202
Reboot or `sudo service pf start`.
ea4ff4d2   Miguel Barão   - radio: added su...
203

59213008   Miguel Barão   - fixed user path...
204
Example configuration files are in the `freebsd` directory.
39126690   Miguel Barão   removes npm and n...
205
206

### Testing the system
93e13002   Miguel Barão   updates README.md.
207
208
209
210

Make sure the following steps have been done:

* install python3 and pip
39126690   Miguel Barão   removes npm and n...
211
212
* install aprendizations using pip
* initialized database with at least 1 user
443a1eea   Miguel Barão   Update to latest ...
213
214
215
* generate and copy certificates to the appropriate place
* (optional) configure the firewall and port forwarding
* run `aprendizations courses.yaml`
39126690   Miguel Barão   removes npm and n...
216

a203d3cc   Miguel Barão   - new http server...
217
## Keeping aprendizations updated
5d859a41   Miguel Barão   update documentation
218
219
220
221
222

To update aprendizations to the latest version do:

```sh
pip install -U git+https://git.xdi.uevora.pt/mjsb/aprendizations.git
39126690   Miguel Barão   removes npm and n...
223
```
5d859a41   Miguel Barão   update documentation
224
225

## Troubleshooting
59213008   Miguel Barão   - fixed user path...
226
227

To help with troubleshooting, use the option `--debug` when running the server.
443a1eea   Miguel Barão   Update to latest ...
228
229
This will increase logs in the terminal and will present the python exception
errors in the browser.
5dae401a   Miguel Barão   update README.md
230

43785de6   Miguel Barão   - added documenta...
231
Logging levels can be adjusted in `~/.config/aprendizations/logger.yaml` and
443a1eea   Miguel Barão   Update to latest ...
232
`~/.config/aprendizations/logger-debug.yaml`.
5dae401a   Miguel Barão   update README.md
233

43785de6   Miguel Barão   - added documenta...
234
If these files do not yet exist, there are examples in `aprendizations/config`
443a1eea   Miguel Barão   Update to latest ...
235
236
that can be copied to `~/.config/aprendizations`.

43785de6   Miguel Barão   - added documenta...
237
### UnicodeEncodeError
443a1eea   Miguel Barão   Update to latest ...
238

59213008   Miguel Barão   - fixed user path...
239
The server should not generate this error, but when using external scripts to
443a1eea   Miguel Barão   Update to latest ...
240
241
242
generate questions or to correct, these scripts can print unicode strings to
stdout. If the terminal does not support unicode, python will generate an
exception.
5dae401a   Miguel Barão   update README.md
243

59213008   Miguel Barão   - fixed user path...
244
* FreeBSD fix: edit `~/.login_conf` to use UTF-8, for example:
443a1eea   Miguel Barão   Update to latest ...
245

59213008   Miguel Barão   - fixed user path...
246
```sh
43785de6   Miguel Barão   - added documenta...
247
248
249
250
251
me:\
    :charset=UTF-8:\
    :lang=en_US.UTF-8:
```

f75a344f   Miguel Barão   - README.md updat...
252
* Debian fix: check `locale`...
443a1eea   Miguel Barão   Update to latest ...
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267

### The application runs but questions do not show up

Some operating systems have an option to disable animations to try to avoid
motion sickness in some people. Browsers will check this option with the OS and
prevent animate.css library to work. Since questions have several animations,
these will will not work and nothing is shown on the page.

To fix this issue you need to allow animations in the Operating System:

* On windows 10, go to System Preferences, search for "Show animations in
  windows" and turn it **ON**.
* On MacOS or iOS search for reduced motion and switch it **OFF**
  (Preferences -> Acessibility -> Display -> Reduce motion).

db04e658   Miguel Barão   - added option to...
268
## FAQ
43785de6   Miguel Barão   - added documenta...
269

f75a344f   Miguel Barão   - README.md updat...
270
Common database manipulations:
93e13002   Miguel Barão   updates README.md.
271

f75a344f   Miguel Barão   - README.md updat...
272
```sh
43785de6   Miguel Barão   - added documenta...
273
initdb-aprendizations -u 12345 --pw alibaba          # reset student password
d187aad4   Miguel Barão   - adds courses
274
275
initdb-aprendizations -a 007 "James Bond" --pw mi6   # add new student "007"
```
43785de6   Miguel Barão   - added documenta...
276

f75a344f   Miguel Barão   - README.md updat...
277
Some common database queries:
d187aad4   Miguel Barão   - adds courses
278

f75a344f   Miguel Barão   - README.md updat...
279
```sh
43785de6   Miguel Barão   - added documenta...
280
# Which students did at least one topic?
93e13002   Miguel Barão   updates README.md.
281
282
sqlite3 students.db "select distinct student_id from studenttopic"

f75a344f   Miguel Barão   - README.md updat...
283
# How many topics has each student done?
93e13002   Miguel Barão   updates README.md.
284
sqlite3 students.db "select student_id, count(topic_id) from studenttopic \
443a1eea   Miguel Barão   Update to latest ...
285
286
  group by student_id order by count(topic_id) desc"

f75a344f   Miguel Barão   - README.md updat...
287
# Which questions have more wrong answers?
93e13002   Miguel Barão   updates README.md.
288
sqlite3 students.db "select count(ref), ref from answers where grade<1.0 \
443a1eea   Miguel Barão   Update to latest ...
289
290
  group by ref order by count(ref) desc"
```
93e13002   Miguel Barão   updates README.md.