-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_ubuntu_vm.sh
68 lines (56 loc) · 1.67 KB
/
setup_ubuntu_vm.sh
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
apt update
apt install -y nginx ssl-cert python3-flask gunicorn
echo "Create nginx default site"
cat <<EOF > /etc/nginx/sites-enabled/default
server {
listen 80 default_server;
listen 443 ssl default_server;
include snippets/snakeoil.conf;
root /var/www/html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
proxy_ssl_server_name on;
proxy_ssl_name \$host;
proxy_pass http://localhost:8000;
proxy_set_header Host \$host;
proxy_set_header sni \$ssl_server_name;
proxy_set_header x-forwarded-for \$proxy_add_x_forwarded_for;
#try_files \$uri \$uri/ =404;
}
}
EOF
echo "Create /svc and the restapp.py"
mkdir /svc
cat <<EOF > /svc/restapp.py
import os
import json
import argparse
from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST', 'PUT', 'DELETE'])
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def index(path=""):
hdrs = dict(request.headers)
hdrs['Method'] = request.method
hdrs['Client'] = request.remote_addr
return json.dumps(hdrs, indent=4, sort_keys=True) + "\n"
EOF
num_workers=$(cat /proc/cpuinfo | grep processor | wc -l)
echo "Create restapp systemd service"
cat <<EOF > /etc/systemd/system/restapp.service
[Unit]
Description=Run restapp that returns back all the headers
After=network.target
[Service]
User=ubuntu
Group=ubuntu
ExecStart=/usr/bin/gunicorn --chdir /svc -w $num_workers restapp:app
[Install]
WantedBy=multi-user.target
EOF
echo "Restart services"
systemctl daemon-reload
systemctl restart nginx
systemctl start restapp