-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
heat_2b.yaml
144 lines (130 loc) · 4.96 KB
/
heat_2b.yaml
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
heat_template_version: 2013-05-23
description: This template deploys a Flasky single instance server with a SQLite database.
parameters:
image:
type: string
label: Image name or ID
description: Image to be used for the server. Please use an Ubuntu based image.
default: trusty-server-cloudimg-amd64
flavor:
type: string
label: Flavor
description: Type of instance (flavor) to be used on the compute instance.
default: m1.small
key:
type: string
label: Key name
description: Name of key-pair to be installed on the compute instance.
default: my_key
private_network:
type: string
label: Private network name or ID
description: Private network to attach server to.
default: private-net
gmail_username:
type: string
label: Gmail account username
description: Username of the Gmail account to use for notifications.
gmail_password:
type: string
label: Gmail account password
description: Password of the Gmail account to use for notifications.
hidden: true
resources:
flask_secret_key:
type: OS::Heat::RandomString
properties:
length: 32
sequence: lettersdigits
flasky_instance:
type: OS::Nova::Server
properties:
image: { get_param: image }
flavor: { get_param: flavor }
key_name: { get_param: key }
networks:
- network: { get_param: private_network }
user_data_format: RAW
user_data:
str_replace:
params:
__gmail_username__: { get_param: gmail_username }
__gmail_password__: { get_param: gmail_password }
__flask_secret_key__: { get_attr: [flask_secret_key, value] }
template: |
#!/bin/bash -ex
# install dependencies
apt-get update
apt-get -y install build-essential python python-dev python-virtualenv nginx supervisor git
# create a flasky user to run the server process
adduser --disabled-password --gecos "" flasky
# clone flasky from github
cd /home/flasky
git clone https://github.com/miguelgrinberg/flasky.git
cd flasky
# Write configuration file
cat >.env <<EOF
FLASK_CONFIG=heroku
SECRET_KEY=__flask_secret_key__
DATABASE_URL=sqlite:////home/flasky/flasky/appdb.sqlite
MAIL_USERNAME=__gmail_username__
MAIL_PASSWORD=__gmail_password__
SSL_DISABLE=1
EOF
# create a virtualenv and install dependencies
virtualenv venv
venv/bin/pip install -r requirements/prod.txt
venv/bin/pip install gunicorn==18.0
# create database
venv/bin/python manage.py deploy
# make the flasky user the owner of the application
chown -R flasky:flasky ./
# configure supervisor to run a private gunicorn web server, and
# to autostart it on boot and when it crashes
# stdout and stderr logs from the server will go to /var/log/flasky
mkdir /var/log/flasky
cat >/etc/supervisor/conf.d/flasky.conf <<EOF
[program:flasky]
command=/home/flasky/flasky/venv/bin/gunicorn -b 127.0.0.1:8000 -w 4 --chdir /home/flasky/flasky --log-file - manage:app
user=flasky
autostart=true
autorestart=true
stderr_logfile=/var/log/flasky/stderr.log
stdout_logfile=/var/log/flasky/stdout.log
EOF
supervisorctl reread
supervisorctl update
# configure nginx as the front-end web server with a reverse proxy
# rule to the gunicorn server
cat >/etc/nginx/sites-available/flasky <<EOF
server {
listen 80;
server_name _;
access_log /var/log/nginx/flasky.access.log;
error_log /var/log/nginx/flasky.error.log;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_redirect off;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
}
location /static {
alias /home/flasky/flasky/static;
}
location /favicon.ico {
alias /home/flasky/flasky/favicon.ico;
}
}
EOF
rm -f /etc/nginx/sites-enabled/default
ln -s /etc/nginx/sites-available/flasky /etc/nginx/sites-enabled/
service nginx restart
outputs:
instance_name:
description: Name of the instance
value: { get_attr: [flasky_instance, name] }
instance_ip:
description: The IP address of the deployed instance
value: { get_attr: [flasky_instance, first_address] }