summaryrefslogtreecommitdiff
path: root/docs/README.md
blob: 07e5c4625d7cd434dae8d73f1a618484467e751e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Bartender: a wsgi server

This impliments a wsgi server that is called by a github webhook. When called it validates the request, schedules an at job, and returns a status. The at job pulls the "docs" off of github, builds the site, and moves it into place.

## The Stack

```mermaid
graph LR
    B -- http(s)://127.0.0.1:8000 <--> C[nginx] -- http(s)://bartender/whisky/STYLE <--> I([Internet])
    A[flask] -- wsgi <--> B[gunicorn];
```

The implimentation

```mermaid
graph LR 
   A[bartender app] --> C[AT]
   A -- 200 ok / 400 bad id / 404  --> N[NGinX]
   N <-- bartender.digithink.com --> I([Internet])
   G[(github)] -- pull --> D
   C --> D[makesite.sh]
   D -- mkdocks build --> O[(site)] -->N
   N -- whiskey/STYLE --> A
```

## The source code and the results

- [https://github.com/suspect-devices/bartender](https://github.com/suspect-devices/bartender)
  The source code for the bartender service is in the same repository as the content it serves
- [https://www.digithink.com](https://www.digithink.com) is the one of the target websites.
- [https://www.3dangst.com](https://www.3dangst.com) is another one of the target website.
- [https://bartender.digithink.com/](https://bartender.digithink.com/) contains this and the other documents for the service.

### Installing the service

```sh
apt install nginx
apt install certbot python3-certbot-nginx
apt install python3-flask
apt install python3-gunicorn
apt install at

echo www-data |tee /etc/at.allow
nano /etc/systemd/system/whiskey.service
[Unit]
Description=Gunicorn instance to serve whiskey
After=network.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/bartender/repo/whiskey
#Environment="PATH=/home/sammy/myproject/myprojectenv/bin"
#     --bind unix:/run/whiskey.sock  \

ExecStart=/usr/bin/gunicorn \
     --workers 3 \
     --bind 127.0.0.1:5000 \
     --reload \
     --access-logfile /var/www/bartender/repo/whiskey/logs/gunicorn_access.log \
     --error-logfile /var/www/bartender/repo/whiskey/logs/gunicorn_error.log \
     -m 007 wsgi:app
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target
^X
systemctl enable whiskey
systemctl start whiskey
```

### nginx.conf

```sh
# lots of hard coded foo here
server {
    server_name bartender.digithink.com;

    listen 198.202.31.232:443 ssl;
    server_name bartender.digithink.com;
    ssl_certificate /etc/letsencrypt/live/bartender.digithink.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/bartender.digithink.com/privkey.pem; # managed by Certbot

    root /var/www/bartender/site;
    index index.html;
    location /whiskey {
        include proxy_params;
        proxy_pass http://bartender/whiskey;
    }

    error_page 404 /404.html;
    location  /404.html {
          internal;
    #     root  /var/www/digithink/site;
    }
    location /lacuenta { # FIX THIS
        root /var/www/bartender/whiskey/logs;
    }
}

server {
    if ($host = bartender.digithink.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 198.202.31.232:80;
    server_name bartender.digithink.com;
    return 404; # managed by Certbot
}
```

### the WSGI app

The actual app will grow into something with better feedback more general use (ie to make different static web sites)

#### drink.py

Minimum Viable Product 

```python
from flask import Flask
from markupsafe import escape
import subprocess

app = Flask(__name__)

@app.route("/whiskey/<style>",methods = ['POST'])
def whiskey(style):
    # break this out by style.
    subprocess.call(['at', 'now', '-f', '/var/www/digithink/site/makesite.sh'])
    return f"One Whiskey, {escape(style)}!"
```

The actual [drink.py](https://github.com/suspect-devices/bartender/blob/main/whiskey/drink.py) is slightly more developed.

#### wsgi.py, Turning the above into a WSGI app

```python
from drink import app

if __name__ == "__main__":
    app.run()
```

## Converting the git content to a static html site.

### mkdocs plus the extensions

Ubuntu (... ok, debian really...) fracked up the packaging for mkdocs and mkdocs-material. I wound up removing the packages and pip3 installing most of it with --break-system-packages.

```sh
apt remove mkdocs*
apt remove markdown
apt remove python3-markdown
apt install python3-regex
apt install libvips-dev
apt install python3-pip
pip3 install mkdocs-material --break-system-packages
pip3 install mkdocs-with-pdf -U git+https://github.com/domWalters/mkdocs-to-pdf.git@release-v0.9.4 --break-system-packages
```

#### pull dependencies based on the current mkdocs install and mkdocs.yml

```sh
cd /var/www/digithink/&& git pull 
mkdocs-get-deps > requirements.txt
pip3 install $(mkdocs-get-deps) --break-system-packages
mkdocs build && chown -R www-data:www-data site/
```

## linkdump

- <https://github.com/codingforentrepreneurs/Pi-Awesome/blob/main/how-tos/Create%20a%20Minimal%20Web%20Application%20with%20Nginx%2C%20Python%2C%20Flask%20%26%20Raspberry%20Pi.md>
- <https://www.stackovercloud.com/2020/05/27/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-20-04/>
- <https://www.digitalocean.com/community/tutorials/how-to-set-up-uwsgi-and-nginx-to-serve-python-apps-on-ubuntu-14-04>
- <https://stackoverflow.com/questions/10748108/nginx-uwsgi-unavailable-modifier-requested-0#11055729>
- https://github.com/mkdocs/get-deps