Skip to content

Commit fbc1a68

Browse files
authored
Merge pull request #13 from ModusCreateOrg/feature/wsgi
feature/wsgi
2 parents 246ad1f + 1f1d678 commit fbc1a68

File tree

12 files changed

+170
-18
lines changed

12 files changed

+170
-18
lines changed

Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
FROM centos:latest AS infra-demo
3+
4+
# setup rpm repos, install base packages and create virtual env in a single step
5+
RUN yum install -y https://centos7.iuscommunity.org/ius-release.rpm \
6+
&& yum update -y \
7+
&& yum install -y \
8+
python36u python36u-libs python36u-devel \
9+
python36u-pip uwsgi-plugin-python36u uwsgi \
10+
gcc make glibc-devel kernel-headers \
11+
pcre pcre-devel pcre2 pcre2-devel \
12+
postgresql-devel \
13+
&& yum clean all \
14+
&& mkdir /app \
15+
&& python3.6 -m venv --copies --clear /app/venv
16+
17+
# Copy in your requirements file
18+
ADD src/requirements.txt /app/requirements.txt
19+
20+
# setup python packages
21+
RUN /app/venv/bin/pip install -U pip \
22+
&& /bin/sh -c "/app/venv/bin/pip install --no-cache-dir -r /app/requirements.txt"

ansible/roles/app-AfterInstall/tasks/main.yml

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050

5151
- name: Set up SELinux rules for Amazon EC2
5252
shell: |
53-
semanage fcontext -a -t httpd_sys_content_t "{{ app_dir }}(/.*)?"
53+
setsebool -P httpd_can_network_connect 1
54+
setsebool -P httpd_can_network_relay 1
55+
semanage fcontext -a -t httpd_sys_content_t "{{ app_dir }}(/.*)?"
5456
restorecon -R "{{ app_dir }}"
5557
when: ec2
5658

@@ -65,9 +67,38 @@
6567
dest: /etc/nginx/conf.d/app.conf
6668
mode: 0640
6769

68-
6970
- name: Install python dependencies for app
7071
pip:
7172
requirements: /app/src/requirements.txt
7273
virtualenv: /app/venv
7374

75+
- name: nginx owns /app/socket
76+
file:
77+
path: /app/socket
78+
state: directory
79+
owner: nginx
80+
group: nginx
81+
82+
- name: Emperor systemd config
83+
template:
84+
src: emperor.service.j2
85+
dest: /etc/systemd/system/emperor.service
86+
owner: root
87+
group: root
88+
mode: 0640
89+
90+
- name: emperor.ini
91+
template:
92+
src: emperor.ini.j2
93+
dest: /app/emperor.ini
94+
owner: root
95+
group: root
96+
mode: 0644
97+
98+
- name: uwsgi app config infra-demo.ini
99+
template:
100+
src: infra-demo.ini.j2
101+
dest: /app/src/infra-demo.ini
102+
owner: root
103+
group: root
104+
mode: 0644

ansible/roles/app-AfterInstall/templates/app.conf.j2

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ server {
77
index index.html index.htm;
88
}
99

10+
location /api/spin {
11+
uwsgi_pass 127.0.0.1:8008;
12+
include /etc/nginx/uwsgi_params;
13+
uwsgi_param UWSGI_SCRIPT /app/src/wsgi.py;
14+
# following config allow us to map /api/spin to /spin on uwsgi:
15+
uwsgi_param SCRIPT_NAME /api; # set SCRIPT_NAME to match subpath
16+
uwsgi_modifier1 30; # strips SCRIPT_NAME from PATH_INFO
17+
}
18+
1019
# redirect server error pages to the static page /50x.html
1120
#
1221
error_page 500 502 503 504 /50x.html;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[uwsgi]
2+
venv = /app/venv
3+
emperor = /app/src
4+
uid = nginx
5+
gid = nginx
6+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[Unit]
2+
Description=uWSGI Emperor
3+
After=syslog.target
4+
5+
[Service]
6+
ExecStart=/app/venv/bin/uwsgi --ini /app/emperor.ini
7+
# Requires systemd version 211 or newer
8+
RuntimeDirectory=uwsgi
9+
Restart=always
10+
KillSignal=SIGQUIT
11+
Type=notify
12+
StandardError=syslog
13+
NotifyAccess=all
14+
15+
[Install]
16+
WantedBy=multi-user.target
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[uwsgi]
2+
venv = /app/venv
3+
wsgi-file = /app/src/wsgi.py
4+
chdir = /app/src
5+
master = 1
6+
workers = 2
7+
threads = 8
8+
lazy-apps = 1
9+
wsgi-env-behaviour = holy
10+
enable-threads = 1
11+
http-auto-chunked = 1
12+
http-keepalive = 1
13+
uwsgi-socket = 127.0.0.1:8008
14+

ansible/roles/app-StartServer/tasks/main.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
---
22
# tasks file for prepare-web-content
33

4+
- name: Start and enable emperor, as it should start off disabled
5+
service:
6+
name: emperor
7+
enabled: yes
8+
state: restarted
9+
410
- name: Start and enable nginx, as it should start off disabled
511
service:
612
name: nginx

bin/common.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function clean_root_owned_docker_files {
4747
BASE_DIR="$(pwd)"
4848
if is_ec2; then
4949
docker run -i \
50-
--mount type=bind,source="${BASE_DIR}"/terraform,target="${TF_DIR}" \
50+
--mount type=bind,source="${BASE_DIR}",target="${TF_DIR}" \
5151
-w "${TF_DIR}" \
5252
--entrypoint /bin/sh \
5353
busybox \
@@ -66,12 +66,12 @@ function get_docker_packer {
6666
PACKER_AWS_VPC_ID="$(curl --silent http://169.254.169.254/latest/meta-data/network/interfaces/macs/"$INTERFACE"/vpc-id)"
6767
fi
6868

69-
echo "docker run -i
70-
${USE_TTY}
69+
echo "docker run -i
70+
${USE_TTY}
7171
--env-file $TMPFILE
72-
-e PACKER_AWS_SUBNET_ID=$PACKER_AWS_SUBNET_ID
73-
-e PACKER_AWS_VPC_ID=$PACKER_AWS_VPC_ID
74-
--mount type=bind,source=${BASE_DIR},target=/app
72+
-e PACKER_AWS_SUBNET_ID=$PACKER_AWS_SUBNET_ID
73+
-e PACKER_AWS_VPC_ID=$PACKER_AWS_VPC_ID
74+
--mount type=bind,source=${BASE_DIR},target=/app
7575
hashicorp/packer:light"
7676
}
7777

codedeploy/bin/ApplicationStop.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ ${DEBUG:-false} && set -vx
1717
# and http://wiki.bash-hackers.org/scripting/debuggingtips
1818
export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
1919

20+
systemctl stop emperor
21+
systemctl disable emperor
2022
systemctl stop nginx
2123
systemctl disable nginx

codedeploy/bin/build.sh

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Set bash unofficial strict mode http://redsymbol.net/articles/unofficial-bash-strict-mode/
44
set -euo pipefail
5-
5+
66
# Set DEBUG to true for enhanced debugging: run prefixed with "DEBUG=true"
77
${DEBUG:-false} && set -vx
88
# Credit to https://stackoverflow.com/a/17805088
@@ -16,10 +16,14 @@ BUILD_DIR="$BASE_DIR/build"
1616
ANSIBLE_DIR="$BASE_DIR/../ansible"
1717
APPLICTION_DIR="$BASE_DIR/../application"
1818
SRC_DIR="$BASE_DIR/../src"
19+
VENV_DIR="$BASE_DIR/../venv"
20+
DOCKER_DIR="$BASE_DIR/.."
1921

2022
GIT_REV="$(git rev-parse --short HEAD)"
2123
BUILD_NUMBER=${BUILD_NUMBER:-0}
2224
ARCHIVE="codedeploy-$BUILD_NUMBER-$GIT_REV.zip"
25+
CONTAINERNAME=infra-demo
26+
2327
echo "GIT_REV=$GIT_REV"
2428
echo "BUILD_NUMBER=$BUILD_NUMBER"
2529
echo "ARCHIVE=$ARCHIVE"
@@ -31,24 +35,42 @@ BUCKET="codedeploy-$AWS_ACCOUNT_ID"
3135
if [[ -d "$BUILD_DIR" ]]; then
3236
rm -rf "$BUILD_DIR"
3337
fi
34-
mkdir -p "$BUILD_DIR"
38+
mkdir -p "$BUILD_DIR/socket"
39+
40+
echo Build docker container $CONTAINERNAME
41+
docker build -f=Dockerfile -t "$CONTAINERNAME" "$DOCKER_DIR"
42+
43+
echo Create python virtual environment
44+
docker run --rm -v "$DOCKER_DIR:/src" "$CONTAINERNAME" /bin/bash -c \
45+
"mkdir -p /src/venv ; \
46+
cp -fa /app/venv/* /src/venv"
3547

3648
SOURCES="$BASE_DIR/bin
3749
$ANSIBLE_DIR
3850
$APPLICTION_DIR
3951
$SRC_DIR
4052
$BASE_DIR/appspec.yml
41-
$BASE_DIR/bin"
53+
$BASE_DIR/bin
54+
$VENV_DIR"
4255
for src in $SOURCES; do
4356
cp -a "$src" "$BUILD_DIR"
4457
done
4558

46-
cd "$BUILD_DIR"
47-
zip -r "$ARCHIVE" \
48-
appspec.yml \
49-
bin \
50-
ansible \
51-
application \
52-
src \
59+
(
60+
cd "$BUILD_DIR"
61+
zip -r "$ARCHIVE" \
62+
appspec.yml \
63+
bin \
64+
ansible \
65+
application \
66+
src \
67+
venv \
68+
socket
69+
)
70+
71+
echo Remove docker generated files
72+
docker run --rm -v "$DOCKER_DIR:/src" "$CONTAINERNAME" /bin/bash -c \
73+
"rm -rf /src/venv"
5374

75+
cd "$BUILD_DIR"
5476
aws s3 cp "$ARCHIVE" "s3://$BUCKET/$ARCHIVE"

src/startit.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/sh
2+
3+
# current directory needs where this script is located
4+
cd "$(dirname "$0")" || exit
5+
6+
UWSGI_VIRTUALENV=/app/venv \
7+
UWSGI_WSGI_FILE=/app/src/wsgi.py \
8+
UWSGI_MASTER=1 \
9+
UWSGI_WORKERS=2 \
10+
UWSGI_THREADS=8 \
11+
UWSGI_UID=nobody \
12+
UWSGI_GID=nobody \
13+
UWSGI_LAZY_APPS=1 \
14+
UWSGI_WSGI_ENV_BEHAVIOR=holy \
15+
/app/venv/bin/uwsgi \
16+
--enable-threads \
17+
--http-auto-chunked \
18+
--http-keepalive \
19+
--socket=/app/socket/uwsgi.sock &
20+

src/stopit.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
3+
pkill --signal INT -f uwsgi || true
4+

0 commit comments

Comments
 (0)