Blame view

README.md 8.68 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
To complete the installation we will need to perform the following steps:
5d859a41   Miguel Barão   update documentation
4
5
6

1. install python3.7, pip and npm
1. download aprendizations from the repository
93e13002   Miguel Barão   updates README.md.
7
1. install javascript libraries (with npm)
a96cd2c7   Miguel Barão   - added logging s...
8
1. install aprendizations (with pip)
39126690   Miguel Barão   removes npm and n...
9
10
11
12
1. initialize database
1. generate SSL certificates
1. configure the firewall (optional)
1. try running `aprendizations demo.yaml`
f540e673   Miguel Barão   - added README.md
13

5d859a41   Miguel Barão   update documentation
14
15
These steps are explained next in detail.

39126690   Miguel Barão   removes npm and n...
16
### Install python3.7 with sqlite3 support and npm
443a1eea   Miguel Barão   Update to latest ...
17

39126690   Miguel Barão   removes npm and n...
18
Python can be installed either from the system package management or compiled from sources.
5d859a41   Miguel Barão   update documentation
19
20

#### Installing from the system package manager
43785de6   Miguel Barão   - added documenta...
21

39126690   Miguel Barão   removes npm and n...
22
```sh
43785de6   Miguel Barão   - added documenta...
23
sudo apt install python3.7 npm               # Linux (Ubuntu)
39126690   Miguel Barão   removes npm and n...
24
25
sudo pkg install python37 py37-sqlite3 npm   # FreeBSD
sudo port install python37 npm6              # MacOS
c54ee176   Miguel Barão   update javascript...
26
27
```

39126690   Miguel Barão   removes npm and n...
28
29
#### Installing from source
     
1e8f0810   Miguel Barão   update setup.py a...
30
Make sure that the build tools and libraries are installed:
93e13002   Miguel Barão   updates README.md.
31
32

```sh
43785de6   Miguel Barão   - added documenta...
33
# Ubuntu:
59213008   Miguel Barão   - fixed user path...
34
sudo apt install build-essential libssl-dev zlib1g-dev libncurses5-dev libncursesw5-dev libreadline-dev libsqlite3-dev libgdbm-dev libdb5.3-dev libbz2-dev libexpat1-dev liblzma-dev tk-dev libffi-dev
443a1eea   Miguel Barão   Update to latest ...
35
```
a96cd2c7   Miguel Barão   - added logging s...
36

43785de6   Miguel Barão   - added documenta...
37
Download python from [http://www.python.org]() and
39126690   Miguel Barão   removes npm and n...
38
39

```sh
1e8f0810   Miguel Barão   update setup.py a...
40
tar xvfJ Python-3.7.tar.xz
43785de6   Miguel Barão   - added documenta...
41
cd Python-3.7
f540e673   Miguel Barão   - added README.md
42
./configure --prefix=$HOME/.local --enable-optimizations
443a1eea   Miguel Barão   Update to latest ...
43
44
make && make install
```
39126690   Miguel Barão   removes npm and n...
45

5d859a41   Miguel Barão   update documentation
46
This will install python locally under `~/.local/bin`. Make sure to add it to your `PATH` in `~/.profile`. If `~/bin` is already in the path, you may just make a symbolic link `ln -s ~/.local/bin ~/bin`.
1e8f0810   Miguel Barão   update setup.py a...
47
48
49
50
51

### Install pip

Python usually includes pip which is accessible through `python -m pip install something`, but it's also convenient to have the `pip` command installed. 
If the `pip` command is not yet installed, run one of these:
5dae401a   Miguel Barão   update README.md
52

f540e673   Miguel Barão   - added README.md
53
```sh
43785de6   Miguel Barão   - added documenta...
54
55
56
57
sudo apt install python3.7-pip     # Ubuntu 19.04+
python3.7 -m pip install pip       # Ubuntu 18.04
sudo pkg py37-pip                  # FreeBSD
sudo port install py37-pip         # MacOS
f540e673   Miguel Barão   - added README.md
58
```
39126690   Miguel Barão   removes npm and n...
59

f540e673   Miguel Barão   - added README.md
60
The latter will install `pip` in your user account under `~/.local/bin`.
43785de6   Miguel Barão   - added documenta...
61
In the end you should be able to run `pip --version` and 
39126690   Miguel Barão   removes npm and n...
62
`python3 -c "import sqlite3"` without errors.
43785de6   Miguel Barão   - added documenta...
63
Sometimes the `pip` command is named `pip3`, 
f540e673   Miguel Barão   - added README.md
64
`pip3.7` or `pip-3.7`.
c3eba869   Miguel Barão   update README.md
65

a96cd2c7   Miguel Barão   - added logging s...
66
Edit the configuration file `~/.config/pip/pip.conf` (FreeBSD, Linux) or 
443a1eea   Miguel Barão   Update to latest ...
67
`Library/Application Support/pip/pip.conf` (MacOS) and add the lines
1e8f0810   Miguel Barão   update setup.py a...
68

a96cd2c7   Miguel Barão   - added logging s...
69
```ini
443a1eea   Miguel Barão   Update to latest ...
70
[global]
39126690   Miguel Barão   removes npm and n...
71
user = yes
f540e673   Miguel Barão   - added README.md
72
```
39126690   Miguel Barão   removes npm and n...
73
74

This will set pip to install modules in the user area (recommended).
5dae401a   Miguel Barão   update README.md
75
76

### Install aprendizations and dependencies:
93e13002   Miguel Barão   updates README.md.
77

1e8f0810   Miguel Barão   update setup.py a...
78
```sh
5dae401a   Miguel Barão   update README.md
79
80
git clone https://git.xdi.uevora.pt/mjsb/aprendizations.git
cd aprendizations
93e13002   Miguel Barão   updates README.md.
81
npm install             # install javascript libraries
b4ceb7fc   Miguel Barão   minor change in l...
82
pip install .           # install aprendizations and dependencies
93e13002   Miguel Barão   updates README.md.
83
84
85
```

Javascript libraries are initially installed in `aprendizations/node_modules` directory.
39126690   Miguel Barão   removes npm and n...
86
These libraries already have symbolic links from `aprendizations/aprendizations/static`.
93e13002   Miguel Barão   updates README.md.
87

f540e673   Miguel Barão   - added README.md
88
Python packages are usually installed in:
59213008   Miguel Barão   - fixed user path...
89
90

- `~/.local/lib/python3.7/site-packages/` in Linux/FreeBSD.
443a1eea   Miguel Barão   Update to latest ...
91
92
- `~/Library/python/3.7/lib/python/site-packages/` in MacOS.

c288f8c6   Miguel Barao   - updated documen...
93
When aprendizations is installed with pip, all the dependencies are also installed. The javascript libraries previously installed with npm are copied to the above directory and the cloned repository is no longer needed.
443a1eea   Miguel Barão   Update to latest ...
94
95
96

At this point, aprendizations is installed in

c288f8c6   Miguel Barao   - updated documen...
97
```sh
93e13002   Miguel Barão   updates README.md.
98
~/.local/bin                          # Linux/FreeBSD
f540e673   Miguel Barão   - added README.md
99
~/Library/Python/3.7/bin              # MacOS
b4ceb7fc   Miguel Barão   minor change in l...
100
```
59213008   Miguel Barão   - fixed user path...
101

43785de6   Miguel Barão   - added documenta...
102
103
104
and can be run from the terminal:

```sh
59213008   Miguel Barão   - fixed user path...
105
aprendizations --version
93e13002   Miguel Barão   updates README.md.
106
107
108
aprendizations --help
```

bbc1c506   Miguel Barão   - documentation u...
109
## Configuration
43785de6   Miguel Barão   - added documenta...
110

443a1eea   Miguel Barão   Update to latest ...
111
### Database
5d859a41   Miguel Barão   update documentation
112

43785de6   Miguel Barão   - added documenta...
113
User data is maintained in a sqlite3 database which has to be created manually using the command `initdb-aprendizations`.
bbc1c506   Miguel Barão   - documentation u...
114
The database file should be located in the same directory as the main 
443a1eea   Miguel Barão   Update to latest ...
115
116
YAML configuration file.

bbc1c506   Miguel Barão   - documentation u...
117
For example, to run the included demo do:
43785de6   Miguel Barão   - added documenta...
118

93e13002   Miguel Barão   updates README.md.
119
120
```sh
cd demo                               # contains a small example
43785de6   Miguel Barão   - added documenta...
121
initdb-aprendizations                 # show or initialize database
bbc1c506   Miguel Barão   - documentation u...
122
initdb-aprendizations --admin         # add admin user
443a1eea   Miguel Barão   Update to latest ...
123
124
125
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
bbc1c506   Miguel Barão   - documentation u...
126
initdb-aprendizations --help          # for available options
43785de6   Miguel Barão   - added documenta...
127
```
443a1eea   Miguel Barão   Update to latest ...
128

43785de6   Miguel Barão   - added documenta...
129
130
131
132
The default password is equal to the user name, if left undefined.


### SSL Certificates
bbc1c506   Miguel Barão   - documentation u...
133

5d859a41   Miguel Barão   update documentation
134
135
136
137
We need certificates for https. Certificates can be self-signed or validated by a trusted authority.

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

#### Generating selfsigned certificates
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
Generate a selfsigned certificate and place it in `~/.local/share/certs`.

```sh
openssl req -x509 -newkey rsa:4096 -keyout privkey.pem -out cert.pem -days 365 -nodes
```

#### LetsEncrypt certificates

Install the certbot from LetsEncrypt:

```sh
sudo pkg install py36-certbot       # FreeBSD
sudo apt install certbot            # Linux Ubuntu
```

93e13002   Miguel Barão   updates README.md.
156
To generate or renew the certificates, ports 80 and 443 have to be accessible. The firewall and webserver have to be stopped. 
c55940af   Miguel Barão   - minor change in...
157

39126690   Miguel Barão   removes npm and n...
158
```sh
443a1eea   Miguel Barão   Update to latest ...
159
sudo certbot certonly --standalone -d www.example.com   # first time
f540e673   Miguel Barão   - added README.md
160
sudo certbot renew                                      # renew
43785de6   Miguel Barão   - added documenta...
161
```
3ec49458   Miguel Barão   fix README.md and...
162

39126690   Miguel Barão   removes npm and n...
163
Certificates are saved under `/usr/local/etc/letsencrypt/live/www.example.com/`. Copy them to `~/.local/share/certs` and change permissions to be readable:
43785de6   Miguel Barão   - added documenta...
164

59213008   Miguel Barão   - fixed user path...
165
```sh
443a1eea   Miguel Barão   Update to latest ...
166
167
sudo cp /usr/local/etc/letsencrypt/live/www.example.com/cert.pem .
sudo cp /usr/local/etc/letsencrypt/live/www.example.com/privkey.pem .
93e13002   Miguel Barão   updates README.md.
168
chmod 400 cert.pem privkey.pem
443a1eea   Miguel Barão   Update to latest ...
169
170
```

c288f8c6   Miguel Barao   - updated documen...
171
### Running the demo
59213008   Miguel Barão   - fixed user path...
172
173

The application includes a small example in `demo/demo.yaml` that can be used for initial testing. Run it with
443a1eea   Miguel Barão   Update to latest ...
174
175
176

```sh
cd demo
59213008   Miguel Barão   - fixed user path...
177
178
179
180
181
aprendizations demo.yaml
```

Open the browser at [https://127.0.0.1:8443](https://127.0.0.1:8443). 
If it everything looks good, check at the correct address 
43785de6   Miguel Barão   - added documenta...
182
`https://www.example.com:8443`.
93e13002   Miguel Barão   updates README.md.
183
The option `--debug` provides more verbose logging and might 
43785de6   Miguel Barão   - added documenta...
184
185
186
be useful during testing.

### Firewall configuration
59213008   Miguel Barão   - fixed user path...
187

5d859a41   Miguel Barão   update documentation
188
Ports 80 and 443 are only usable by root. For security reasons the server runs as an unprivileged user on port 8443 for https. 
59213008   Miguel Barão   - fixed user path...
189
To access the server in the default https port (443), port forwarding can be configured in the firewall. 
43785de6   Miguel Barão   - added documenta...
190

59213008   Miguel Barão   - fixed user path...
191
#### FreeBSD and pf
43785de6   Miguel Barão   - added documenta...
192
193
194
195

Edit `/etc/pf.conf`:

```sh
59213008   Miguel Barão   - fixed user path...
196
ext_if="em0"                # change em0 to the correct network interface
43785de6   Miguel Barão   - added documenta...
197
198
199
200
201
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
```

Under virtualbox with guest additions use `ext_if="vtnet0"`.
59213008   Miguel Barão   - fixed user path...
202

ea4ff4d2   Miguel Barão   - radio: added su...
203
Edit `/etc/rc.conf`
59213008   Miguel Barão   - fixed user path...
204

39126690   Miguel Barão   removes npm and n...
205
206
```sh
pf_enable="YES"
93e13002   Miguel Barão   updates README.md.
207
208
209
210
pf_flags=""
pf_rules="/etc/pf.conf"

# optional logging:
39126690   Miguel Barão   removes npm and n...
211
212
pflog_enable="YES"
pflog_flags=""
443a1eea   Miguel Barão   Update to latest ...
213
214
215
pflog_logfile="/var/log/pflog"
```

39126690   Miguel Barão   removes npm and n...
216
Reboot or `sudo service pf start`.
a203d3cc   Miguel Barão   - new http server...
217

5d859a41   Miguel Barão   update documentation
218
219
220
221
222
### Testing the system

Make sure the following steps have been done:

- installed python3.7, pip and npm
39126690   Miguel Barão   removes npm and n...
223
- git-cloned the aprendizations from the main repository
5d859a41   Miguel Barão   update documentation
224
225
- installed javascript libraries with npm
- installed aprendizations with pip
59213008   Miguel Barão   - fixed user path...
226
227
- initialized database with at least 1 user
- generate and copy certificates to the appropriate place
443a1eea   Miguel Barão   Update to latest ...
228
229
- (optional) configure the firewall to do port forwarding
- run `aprendizations demo.yaml --check`
5dae401a   Miguel Barão   update README.md
230

43785de6   Miguel Barão   - added documenta...
231
## Troubleshooting
443a1eea   Miguel Barão   Update to latest ...
232

5dae401a   Miguel Barão   update README.md
233
To help with troubleshooting, use the option `--debug` when running the server. 
43785de6   Miguel Barão   - added documenta...
234
This will increase logs in the terminal and will present the python exception 
443a1eea   Miguel Barão   Update to latest ...
235
236
errors in the browser.

43785de6   Miguel Barão   - added documenta...
237
Logging levels can be adjusted in `~/.config/aprendizations/logger.yaml` and 
443a1eea   Miguel Barão   Update to latest ...
238
`~/.config/aprendizations/logger-debug.yaml`.
59213008   Miguel Barão   - fixed user path...
239

443a1eea   Miguel Barão   Update to latest ...
240
241
242
If these files do not yet exist, there are examples in `aprendizations/config` that can be copied to `~/.config/aprendizations`.

#### UnicodeEncodeError
5dae401a   Miguel Barão   update README.md
243

59213008   Miguel Barão   - fixed user path...
244
The server should not generate this error, but when using external scripts to 
443a1eea   Miguel Barão   Update to latest ...
245
generate questions or to correct, these scripts can print unicode strings to 
59213008   Miguel Barão   - fixed user path...
246
stdout. If the terminal does not support unicode, python will generate this 
43785de6   Miguel Barão   - added documenta...
247
248
249
250
251
exception.

- FreeBSD fix: edit `~/.login_conf` to use UTF-8, for example:

```sh
f75a344f   Miguel Barão   - README.md updat...
252
me:\
443a1eea   Miguel Barão   Update to latest ...
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
    :charset=UTF-8:\
    :lang=en_US.UTF-8:
```

- Debian fix: check `locale`...

## FAQ

Common database manipulations:

```sh
initdb-aprendizations -u 12345 --pw alibaba     # reset student password
initdb-aprendizations -a 12345 --pw alibaba     # add new student
```

db04e658   Miguel Barão   - added option to...
268
Common database queries:
43785de6   Miguel Barão   - added documenta...
269

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

d187aad4   Miguel Barão   - adds courses
274
275
# How many topics has each student done?
sqlite3 students.db "select student_id, count(topic_id) from studenttopic group by student_id order by count(topic_id) desc"
43785de6   Miguel Barão   - added documenta...
276

f75a344f   Miguel Barão   - README.md updat...
277
# Which questions have more wrong answers?
d187aad4   Miguel Barão   - adds courses
278
sqlite3 students.db "select count(ref), ref from answers where grade<1.0 group by ref order by count(ref) desc"
f75a344f   Miguel Barão   - README.md updat...
279
```
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.