cdn-django/install.sh

raw link view readme
1 STORAGE_NAME=$1
2 STORAGE_KEY=$2
3 DNS_NAME=$3
4 CDN_ROOT=$4.azureedge.net
5
6 yum install epel-release -y
7
8 yum install policycoreutils-python -y
9 semanage fcontext -at httpd_sys_rw_content_t "/srv/cdndjango(/.*)?"
10 semanage fcontext -at httpd_sys_content_t "/srv/common(/.*)?"
11
12 yum install firewalld -y
13 systemctl start firewalld
14 systemctl enable firewalld
15 firewall-cmd --permanent --zone=public --add-interface=eth0
16 firewall-cmd --permanent --add-service=http --add-service=https --zone=public
17 firewall-cmd --reload
18
19 # Just for testing...
20
21 mkdir /srv/common
22 cat > /srv/common/robots.txt << EOF
23 User-agent: ia_archiver
24 Disallow: /
25 EOF
26
27 # UWSGI
28
29 yum install uwsgi uwsgi-plugin-python3 -y
30
31 cat > /etc/uwsgi.d/cdndjango.ini << EOF
32 [uwsgi]
33 project = engine
34 base = /srv/cdndjango/site
35 virtualenv = /srv/cdndjango
36
37 chdir = %(base)
38 home = %(virtualenv)
39 module = %(project).core.wsgi:application
40
41 master = true
42 processes = 5
43
44 socket = %(base)/%(project).sock
45 chmod-socket = 660
46 uid = uwsgi
47 gid = uwsgi
48 vacuum = true
49
50 plugins = python3
51 EOF
52
53 chown -R nobody:uwsgi /etc/uwsgi.d
54 chmod -R g+s /etc/uwsgi.d
55
56 # App
57
58 yum install python34 python-pip -y 2> /dev/null
59 pip install --upgrade pip
60 pip install --upgrade virtualenv
61 cd /srv
62 virtualenv -p python3 cdndjango
63 chmod -R 2750 /srv/cdndjango
64 setfacl -m d:o:--- /srv/cdndjango
65 cd /srv/cdndjango
66 source bin/activate
67 mkdir site
68 cd site
69
70 mkdir -p engine/core
71 mkdir templates
72
73 cat > ./requirements.txt << EOF
74 Django==1.10.1
75 EOF
76
77 pip install -r requirements.txt
78
79 cat > ./engine/core/views.py << EOF
80 from django.shortcuts import render
81 from engine import settings as engine_settings
82
83 def process(request):
84 render_data = {
85 'title': 'random demo',
86 'cdn': engine_settings.CDN_ROOT.replace('https:', '')
87 }
88
89 return render(request, ['index.html', 'index.htm'], render_data)
90
91 EOF
92
93 cat > ./engine/core/__init__.py << EOF
94 EOF
95
96 cat > ./engine/core/wsgi.py << EOF
97 import os
98 from django.core.wsgi import get_wsgi_application
99 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "engine.settings")
100 application = get_wsgi_application()
101 EOF
102
103 cat > ./engine/core/urls.py << EOF
104 from django.conf.urls import url
105 import sys
106
107 from . import views
108
109 class CoreUrl:
110 def init(self):
111 pass
112
113 def setup(self):
114 self.urlpatterns = [
115 url(r'^$', views.process),
116 ]
117
118 return self.urlpatterns
119
120 urlpatterns = CoreUrl().setup()
121 EOF
122
123 cat > ./engine/__init__.py << EOF
124 EOF
125
126 cat > ./engine/settings.py << EOF
127 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
128 import os, sys
129
130 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
131
132 SECRET_KEY = 'whatever8786ubhj8giyhbjhuiybhuhu08h9ui38u084hubj'
133
134 DEBUG = True
135
136 ALLOWED_HOSTS = ['127.0.0.1', 'localhost', 'does.nothing.in.debug.mode']
137
138 INSTALLED_APPS = (
139 'engine',
140 'engine.core',
141 'django.contrib.auth',
142 'django.contrib.contenttypes',
143 'django.contrib.messages',
144 )
145
146 MIDDLEWARE_CLASSES = (
147 'django.middleware.common.CommonMiddleware',
148 )
149
150 ROOT_URLCONF = 'engine.core.urls'
151
152 ROOT_PATH = os.path.split(os.path.abspath(__file__))[0]
153
154 TEMPLATES = [
155 {
156 'BACKEND': 'django.template.backends.django.DjangoTemplates',
157 'DIRS': [
158 os.path.join(BASE_DIR, 'templates')
159 ],
160 'OPTIONS': {
161 'context_processors': [
162 'django.template.context_processors.debug',
163 'django.template.context_processors.request',
164 'django.contrib.auth.context_processors.auth',
165 'django.contrib.messages.context_processors.messages',
166 ],
167 'loaders': [
168 ('django.template.loaders.cached.Loader', [
169 'django.template.loaders.filesystem.Loader',
170 'django.template.loaders.app_directories.Loader',
171 ]),
172 ],
173 },
174 },
175 ]
176
177 PROJECT_DIR = os.path.dirname(__file__)
178
179 STATICFILES_DIRS = (
180 os.path.join(PROJECT_DIR, 'static'),
181 )
182
183 LANGUAGE_CODE = 'en-us'
184
185 TIME_ZONE = 'UTC'
186
187 USE_I18N = True
188
189 USE_L10N = True
190
191 USE_TZ = True
192
193 DOMAIN = 'https://does.not.matter'
194
195 # the web doesn't have folders; this isn't 1994
196 APPEND_SLASH = False
197
198 application_root = '/'
199
200 CDN_ROOT = '$CDN_ROOT'
201
202 EOF
203
204 cat > ./manage.py << EOF
205 #!/usr/bin/env python2
206 import os
207 import sys
208
209 if __name__ == "__main__":
210 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "engine.settings")
211
212 from django.core.management import execute_from_command_line
213
214 execute_from_command_line(sys.argv)
215
216 EOF
217
218 cat > ./runserver.sh << EOF
219 python manage.py runserver 127.0.0.1:8081 --settings=engine.settings
220 EOF
221 chmod +x ./runserver.sh
222
223 cat > ./templates/index.html << EOF
224
225
226
227 {{ title }}
228
229
230
231
232

{{ title }}

233
234
235
236

Here is my content. The CDN is up and running when this is blue.

237
238
239
240
241 EOF
242
243 deactivate
244
245 chown -R nobody:uwsgi /srv/cdndjango
246 restorecon -R /srv
247
248 #Nginx
249
250 cat > /etc/yum.repos.d/nginx.repo << EOF
251 [nginx]
252 name=nginx repo
253 baseurl=http://nginx.org/packages/mainline/centos/\$releasever/\$basearch/
254 gpgcheck=0
255 enabled=1
256 EOF
257
258 yum install -y nginx
259
260 export PUBLIC_IP=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
261
262 mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.disabled
263 cat > /etc/nginx/conf.d/cdndjango.conf << EOF
264 server {
265 listen $PUBLIC_IP:80;
266
267 location /robots.txt {
268 alias /srv/common/robots.txt;
269 }
270
271 location / {
272 include uwsgi_params;
273 uwsgi_pass unix:/srv/cdndjango/site/engine.sock;
274
275 proxy_redirect off;
276 proxy_set_header Host \$host;
277 proxy_set_header X-Real-IP \$remote_addr;
278 proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
279 proxy_set_header X-Forwarded-Host \$server_name;
280 }
281 }
282 EOF
283
284 usermod nginx -aG uwsgi
285
286 systemctl start uwsgi
287 systemctl enable uwsgi
288
289 systemctl start nginx
290 systemctl enable nginx
291
292 cat > /tmp/site.css <<\EOF
293 p > span > span {
294 color: #00f;
295 }
296 EOF
297
298 yum install openssl-devel python-devel gcc -y
299 cd /srv
300 # azure storage still requires legacy python (aka python2)
301 virtualenv assetuploader
302 cd /srv/assetuploader
303 source bin/activate
304 pip install azure-storage
305 mkdir app
306 cd app
307 cat > upload.py << EOF
308 from azure.common import AzureMissingResourceHttpError
309 from azure.storage.blob import BlockBlobService, ContentSettings, PublicAccess
310 from azure.storage import CorsRule
311
312 blob_service = BlockBlobService(account_name='$STORAGE_NAME', account_key='$STORAGE_KEY')
313
314 content_settings = ContentSettings()
315 content_settings.content_type = 'text/css'
316
317 blob_service.create_container('css', public_access=PublicAccess.Blob)
318 with open('/tmp/site.css', 'r') as f:
319 blob_service.create_blob_from_bytes('css', 'site.css', f.read(), content_settings=content_settings)
320
321 blob_service.set_blob_service_properties(cors=[CorsRule(
322 allowed_origins=['$DNS_NAME'],
323 allowed_methods=['GET'],
324 allowed_headers = ['*'],
325 exposed_headers = ['*'],
326 max_age_in_seconds = 1800
327 )])
328 EOF
329 chmod 0600 upload.py
330
331 python upload.py
332 deactivate
333