Blame view

README.md 9.03 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
## Installation

To complete the installation we will need to perform the following steps:
93e13002   Miguel Barão   updates README.md.
7

a96cd2c7   Miguel Barão   - added logging s...
8
1. install python3.7, pip and npm
39126690   Miguel Barão   removes npm and n...
9
10
11
12
1. download aprendizations from the repository
1. install javascript libraries (with npm)
1. install aprendizations (with pip)
1. generate SSL certificates
f540e673   Miguel Barão   - added README.md
13
1. configure the firewall (optional)
5d859a41   Miguel Barão   update documentation
14
15

To use the software we need to:
39126690   Miguel Barão   removes npm and n...
16

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

43785de6   Miguel Barão   - added documenta...
21
Each of these steps is explained below.
39126690   Miguel Barão   removes npm and n...
22

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

Python can be installed either from the system package management or compiled from sources.
c54ee176   Miguel Barão   update javascript...
26
27

#### Installing from the system package manager
39126690   Miguel Barão   removes npm and n...
28
29

```sh
1e8f0810   Miguel Barão   update setup.py a...
30
sudo apt install python3.7 npm               # Linux (Ubuntu)
93e13002   Miguel Barão   updates README.md.
31
32
sudo pkg install python37 py37-sqlite3 npm   # FreeBSD
sudo port install python37 npm6              # MacOS
43785de6   Miguel Barão   - added documenta...
33
```
59213008   Miguel Barão   - fixed user path...
34

443a1eea   Miguel Barão   Update to latest ...
35
#### Installing from source
a96cd2c7   Miguel Barão   - added logging s...
36
     
43785de6   Miguel Barão   - added documenta...
37
Make sure that the build tools and libraries are installed:
39126690   Miguel Barão   removes npm and n...
38
39

```sh
1e8f0810   Miguel Barão   update setup.py a...
40
# Ubuntu:
43785de6   Miguel Barão   - added documenta...
41
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
f540e673   Miguel Barão   - added README.md
42
```
443a1eea   Miguel Barão   Update to latest ...
43
44

Download python from [http://www.python.org]() and
39126690   Miguel Barão   removes npm and n...
45

5d859a41   Miguel Barão   update documentation
46
```sh
1e8f0810   Miguel Barão   update setup.py a...
47
48
49
50
51
tar xvfJ Python-3.7.tar.xz
cd Python-3.7
./configure --prefix=$HOME/.local --enable-optimizations
make && make install
```
5dae401a   Miguel Barão   update README.md
52

f540e673   Miguel Barão   - added README.md
53
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`.
43785de6   Miguel Barão   - added documenta...
54
55
56
57

### 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 directly available in the terminal. 
f540e673   Miguel Barão   - added README.md
58
To install `pip` from the system package manager:
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
sudo apt install python3.7-pip     # Ubuntu 19.04+
39126690   Miguel Barão   removes npm and n...
62
sudo pkg py37-pip                  # FreeBSD
43785de6   Miguel Barão   - added documenta...
63
sudo port install py37-pip         # MacOS
f540e673   Miguel Barão   - added README.md
64
```
c3eba869   Miguel Barão   update README.md
65

a96cd2c7   Miguel Barão   - added logging s...
66
otherwise run:
443a1eea   Miguel Barão   Update to latest ...
67

1e8f0810   Miguel Barão   update setup.py a...
68
```sh
a96cd2c7   Miguel Barão   - added logging s...
69
python3.7 -m pip install pip        # install in user area
443a1eea   Miguel Barão   Update to latest ...
70
```
39126690   Miguel Barão   removes npm and n...
71

f540e673   Miguel Barão   - added README.md
72
The latter will install `pip` in your user account under `~/.local/bin`.
39126690   Miguel Barão   removes npm and n...
73
74
In the end you should be able to run `pip --version` and 
`python3 -c "import sqlite3"` without errors.
5dae401a   Miguel Barão   update README.md
75
76
Sometimes the `pip` command is named `pip3`, 
`pip3.7` or `pip-3.7`.
93e13002   Miguel Barão   updates README.md.
77

1e8f0810   Miguel Barão   update setup.py a...
78
Edit the configuration file `~/.config/pip/pip.conf` (FreeBSD, Linux) or 
5dae401a   Miguel Barão   update README.md
79
80
`Library/Application Support/pip/pip.conf` (MacOS) and add the lines

93e13002   Miguel Barão   updates README.md.
81
```ini
b4ceb7fc   Miguel Barão   minor change in l...
82
[global]
93e13002   Miguel Barão   updates README.md.
83
84
85
user = yes
```

39126690   Miguel Barão   removes npm and n...
86
This will set pip to install modules in the user area (recommended).
93e13002   Miguel Barão   updates README.md.
87

f540e673   Miguel Barão   - added README.md
88
### Download and install aprendizations:
59213008   Miguel Barão   - fixed user path...
89
90

```sh
443a1eea   Miguel Barão   Update to latest ...
91
92
git clone https://git.xdi.uevora.pt/mjsb/aprendizations.git
cd aprendizations
c288f8c6   Miguel Barao   - updated documen...
93
npm install             # install javascript libraries
443a1eea   Miguel Barão   Update to latest ...
94
95
96
pip install .           # install aprendizations and dependencies
```

c288f8c6   Miguel Barao   - updated documen...
97
Javascript libraries are installed in `aprendizations/node_modules` and are linked from `aprendizations/aprendizations/static`.
93e13002   Miguel Barão   updates README.md.
98

f540e673   Miguel Barão   - added README.md
99
Python packages are usually installed in:
b4ceb7fc   Miguel Barão   minor change in l...
100

59213008   Miguel Barão   - fixed user path...
101
- `~/.local/lib/python3.7/site-packages/` in Linux/FreeBSD.
43785de6   Miguel Barão   - added documenta...
102
103
104
- `~/Library/python/3.7/lib/python/site-packages/` in MacOS.

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.
59213008   Miguel Barão   - fixed user path...
105

93e13002   Miguel Barão   updates README.md.
106
107
108
At this point `aprendizations` is installed in

```sh
bbc1c506   Miguel Barão   - documentation u...
109
~/.local/bin                          # Linux/FreeBSD
43785de6   Miguel Barão   - added documenta...
110
~/Library/Python/3.7/bin              # MacOS
443a1eea   Miguel Barão   Update to latest ...
111
```
5d859a41   Miguel Barão   update documentation
112

43785de6   Miguel Barão   - added documenta...
113
and can be run from the terminal:
bbc1c506   Miguel Barão   - documentation u...
114

443a1eea   Miguel Barão   Update to latest ...
115
116
```sh
aprendizations --version
bbc1c506   Miguel Barão   - documentation u...
117
aprendizations --help
43785de6   Miguel Barão   - added documenta...
118
```
93e13002   Miguel Barão   updates README.md.
119
120

### SSL Certificates
43785de6   Miguel Barão   - added documenta...
121

bbc1c506   Miguel Barão   - documentation u...
122
We need certificates for https. Certificates can be self-signed or validated by a trusted authority.
443a1eea   Miguel Barão   Update to latest ...
123
124
125

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.
bbc1c506   Miguel Barão   - documentation u...
126

43785de6   Miguel Barão   - added documenta...
127
#### Generating selfsigned certificates
443a1eea   Miguel Barão   Update to latest ...
128

43785de6   Miguel Barão   - added documenta...
129
130
131
132
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
bbc1c506   Miguel Barão   - documentation u...
133
```
5d859a41   Miguel Barão   update documentation
134
135
136
137

#### LetsEncrypt certificates

Install the certbot from LetsEncrypt:
443a1eea   Miguel Barão   Update to latest ...
138
139

```sh
39126690   Miguel Barão   removes npm and n...
140
sudo pkg install py36-certbot       # FreeBSD
5d859a41   Miguel Barão   update documentation
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
sudo apt install certbot            # Ubuntu
```

To generate or renew the certificates, ports 80 and 443 have to be accessible. The firewall and webserver have to be stopped. 

```sh
sudo certbot certonly --standalone -d www.example.com   # first time
sudo certbot renew                                      # renew
```

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 .
93e13002   Miguel Barão   updates README.md.
156
chmod 400 cert.pem privkey.pem
c55940af   Miguel Barão   - minor change in...
157
```
39126690   Miguel Barão   removes npm and n...
158

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

f540e673   Miguel Barão   - added README.md
160
## Configuration
43785de6   Miguel Barão   - added documenta...
161

3ec49458   Miguel Barão   fix README.md and...
162
### Database
39126690   Miguel Barão   removes npm and n...
163

43785de6   Miguel Barão   - added documenta...
164
User data is maintained in a sqlite3 database which has to be created manually using the `initdb-aprendizations` command.
59213008   Miguel Barão   - fixed user path...
165
The database file should be located in the same directory as the main 
443a1eea   Miguel Barão   Update to latest ...
166
167
YAML configuration file.

93e13002   Miguel Barão   updates README.md.
168
For example, to run the included demo do:
443a1eea   Miguel Barão   Update to latest ...
169
170

```sh
c288f8c6   Miguel Barao   - updated documen...
171
cd demo                               # contains a small example
59213008   Miguel Barão   - fixed user path...
172
173
initdb-aprendizations                 # show or initialize database
initdb-aprendizations --admin         # add admin user
443a1eea   Miguel Barão   Update to latest ...
174
175
176
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
59213008   Miguel Barão   - fixed user path...
177
178
179
180
181
initdb-aprendizations --help          # for available options
```

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

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

93e13002   Miguel Barão   updates README.md.
183
### Running the demo
43785de6   Miguel Barão   - added documenta...
184
185
186

The application includes a small example in `demo/demo.yaml` that can be used for initial testing. Run it with

59213008   Miguel Barão   - fixed user path...
187
```sh
5d859a41   Miguel Barão   update documentation
188
cd demo
59213008   Miguel Barão   - fixed user path...
189
aprendizations demo.yaml
43785de6   Miguel Barão   - added documenta...
190
```
59213008   Miguel Barão   - fixed user path...
191

43785de6   Miguel Barão   - added documenta...
192
193
194
195
Open the browser at [https://127.0.0.1:8443](https://127.0.0.1:8443). 
If everything looks good, check at the correct address 
`https://www.example.com:8443`.
The option `--debug` provides more verbose logging and might 
59213008   Miguel Barão   - fixed user path...
196
be useful during testing.
43785de6   Miguel Barão   - added documenta...
197
198
199
200
201

### Firewall configuration

Ports 80 and 443 are only usable by root. For security reasons the server runs as an unprivileged user on port 8443 for https. 
To access the server in the default https port (443), port forwarding can be configured in the firewall. 
59213008   Miguel Barão   - fixed user path...
202

ea4ff4d2   Miguel Barão   - radio: added su...
203
#### FreeBSD and pf
59213008   Miguel Barão   - fixed user path...
204

39126690   Miguel Barão   removes npm and n...
205
206
Edit `/etc/pf.conf`:

93e13002   Miguel Barão   updates README.md.
207
208
209
210
```sh
ext_if="em0"                # change em0 to 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
39126690   Miguel Barão   removes npm and n...
211
212
```

443a1eea   Miguel Barão   Update to latest ...
213
214
215
Virtualbox with guest additions uses `ext_if="vtnet0"`.

Edit `/etc/rc.conf`
39126690   Miguel Barão   removes npm and n...
216

a203d3cc   Miguel Barão   - new http server...
217
```sh
5d859a41   Miguel Barão   update documentation
218
219
220
221
222
pf_enable="YES"
pf_flags=""
pf_rules="/etc/pf.conf"

# optional logging:
39126690   Miguel Barão   removes npm and n...
223
pflog_enable="YES"
5d859a41   Miguel Barão   update documentation
224
225
pflog_flags=""
pflog_logfile="/var/log/pflog"
59213008   Miguel Barão   - fixed user path...
226
227
```

443a1eea   Miguel Barão   Update to latest ...
228
229
Reboot or `sudo service pf start`.

5dae401a   Miguel Barão   update README.md
230
### Testing the system
43785de6   Miguel Barão   - added documenta...
231

443a1eea   Miguel Barão   Update to latest ...
232
Make sure the following steps have been done:
5dae401a   Miguel Barão   update README.md
233

43785de6   Miguel Barão   - added documenta...
234
- installed python3.7, pip and npm
443a1eea   Miguel Barão   Update to latest ...
235
236
- git-cloned the aprendizations from the main repository
- installed javascript libraries with npm
43785de6   Miguel Barão   - added documenta...
237
- installed aprendizations with pip
443a1eea   Miguel Barão   Update to latest ...
238
- initialized database with at least 1 user
59213008   Miguel Barão   - fixed user path...
239
- generate and copy certificates to the appropriate place
443a1eea   Miguel Barão   Update to latest ...
240
241
242
- (optional) configure the firewall to do port forwarding
- run `aprendizations demo.yaml --check`

5dae401a   Miguel Barão   update README.md
243
## Keeping aprendizations updated
59213008   Miguel Barão   - fixed user path...
244

443a1eea   Miguel Barão   Update to latest ...
245
To update aprendizations to the latest version do:
59213008   Miguel Barão   - fixed user path...
246

43785de6   Miguel Barão   - added documenta...
247
248
249
250
251
```sh
cd aprendizations
git pull                # get latest version
npm update              # update javascript libraries
pip install -U .        # updates installed version to latest
f75a344f   Miguel Barão   - README.md updat...
252
```
443a1eea   Miguel Barão   Update to latest ...
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267

## Troubleshooting

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 
`~/.config/aprendizations/logger-debug.yaml`.

If these files do not yet exist, there are examples in `aprendizations/config` that can be copied to `~/.config/aprendizations`.

#### UnicodeEncodeError

The server should not generate this error, but when using external scripts to 
db04e658   Miguel Barão   - added option to...
268
generate questions or to correct, these scripts can print unicode strings to 
43785de6   Miguel Barão   - added documenta...
269
stdout. If the terminal does not support unicode, python will generate this 
f75a344f   Miguel Barão   - README.md updat...
270
exception.
93e13002   Miguel Barão   updates README.md.
271

f75a344f   Miguel Barão   - README.md updat...
272
- FreeBSD fix: edit `~/.login_conf` to use UTF-8, for example:
43785de6   Miguel Barão   - added documenta...
273

d187aad4   Miguel Barão   - adds courses
274
275
```sh
me:\
43785de6   Miguel Barão   - added documenta...
276
    :charset=UTF-8:\
f75a344f   Miguel Barão   - README.md updat...
277
    :lang=en_US.UTF-8:
d187aad4   Miguel Barão   - adds courses
278
```
f75a344f   Miguel Barão   - README.md updat...
279

43785de6   Miguel Barão   - added documenta...
280
- Debian fix: check `locale`...
93e13002   Miguel Barão   updates README.md.
281
282

## FAQ
f75a344f   Miguel Barão   - README.md updat...
283

93e13002   Miguel Barão   updates README.md.
284
Common database manipulations:
443a1eea   Miguel Barão   Update to latest ...
285
286

```sh
f75a344f   Miguel Barão   - README.md updat...
287
initdb-aprendizations -u 12345 --pw alibaba     # reset student password
93e13002   Miguel Barão   updates README.md.
288
initdb-aprendizations -a 12345 --pw alibaba     # add new student
443a1eea   Miguel Barão   Update to latest ...
289
290
```

93e13002   Miguel Barão   updates README.md.
291
Common database queries: