Skip to content

Commit

Permalink
Merge pull request #84 from dduk-ddak/issue_80
Browse files Browse the repository at this point in the history
Issue #80
  • Loading branch information
minhoryang authored Mar 19, 2017
2 parents 0898cba + 7a01754 commit 02b36ac
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 108 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,6 @@ collected_static/
# For hide SECRET_KEY
secret.json
pw.txt
nginx/local_nginx.conf
.prepared

50 changes: 13 additions & 37 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,46 +1,22 @@
default: start
OS := $(shell uname)

sudo:
sudo -v
default: start
.PHONY: start stop uninstall

start: prepare sudo
sudo service redis-server start
python3 manage.py runworker &
daphne -b 0.0.0.0 -p 8001 coding_night_live.asgi:channel_layer &
sudo service nginx start # FIXME

db.sqlite3:
python3 manage.py migrate
include Makefile.deps
include Makefile.prepare

pw.txt: db.sqlite3
python3 manage.py createsuperuserauto

collected_static/:
yes yes | python3 manage.py collectstatic
start: \.prepared deps-start
python3 manage.py runworker &
daphne -b 0.0.0.0 -p 8001 coding_night_live.asgi:channel_layer &

secret.json: db.sqlite3
python3 manage.py autodeploy

prepare: deps-install db.sqlite3 pw.txt collected_static/ secret.json
stop: deps-stop
-killall -9 daphne
-killall -9 python3
-killall -9 python # FIXME: daphne at MAC OS


OS := $(shell uname)
deps-install:
ifeq ($(OS),Linux)
sudo apt-get install redis-server
sudo apt-get install nginx
else
echo 'ACITON REQUIRED) Need to install redis and nginx before this.'
endif

stop: sudo
-sudo service nginx stop
-sudo killall -9 daphne # FIXME
-sudo killall -9 python3 # FIXME
-sudo service redis-server stop

clean:
-rm secret.json db.sqlite3
-rm -r collected_static

uninstall: stop clean
uninstall: stop clean deps-uninstall
77 changes: 77 additions & 0 deletions Makefile.deps
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
.PHONY: sudo deps-install deps-uninstall deps-start deps-stop prepare-nginx


sudo:
sudo -v


deps-install: sudo
ifeq ($(OS),Linux)
sudo apt-get install redis-server
sudo apt-get install nginx
else ifeq ($(OS),Darwin)
brew list redis > /dev/null || brew install redis --build-from-source # FIXME: Homebrew/homebrew-core#11134
brew list nginx > /dev/null || brew install nginx
else
echo 'ACITON REQUIRED) Need to install redis and nginx before this.'
endif


deps-uninstall: sudo
ifeq ($(OS),Linux)
# TODO
echo 'ACITON REQUIRED) Need to uninstall redis and nginx after this.'
else ifeq ($(OS),Darwin)
-brew list redis > /dev/null && brew uninstall redis
-brew list nginx > /dev/null && brew uninstall nginx
else
echo 'ACITON REQUIRED) Need to uninstall redis and nginx after this.'
endif


deps-start: sudo
ifeq ($(OS),Linux)
sudo service redis-server start
else ifeq ($(OS),Darwin)
brew services run redis
else
sudo redis-server &
endif
ifeq ($(OS),Linux)
sudo service nginx start
else
sudo nginx &
endif


deps-stop: sudo
ifeq ($(OS),Linux)
sudo service nginx stop
else ifeq ($(OS),Darwin)
-sudo killall -9 'nginx: master process nginx'
-sudo killall -9 'nginx: worker process'
-sudo killall -9 nginx
else
-sudo killall -9 'nginx: master process nginx'
-sudo killall -9 'nginx: worker process'
-sudo killall -9 nginx
endif
ifeq ($(OS),Linux)
-sudo service redis-server stop
else ifeq ($(OS),Darwin)
-brew services stop redis
else
-sudo killall -9 redis-server
endif


prepare-nginx: sudo nginx/local_nginx.conf
ifeq ($(OS),Linux)
sudo rm -f /etc/nginx/sites-enabled/local_nginx.conf
sudo ln -s `pwd`/nginx/local_nginx.conf /etc/nginx/sites-enabled/
else ifeq ($(OS),Darwin)
rm -f /usr/local/etc/nginx/servers/local_nginx.conf
ln -s `pwd`/nginx/local_nginx.conf /usr/local/etc/nginx/servers/
else
# FIXME ln -s `pwd`/nginx/local_nginx.conf /usr/local/etc/nginx/servers/
endif
36 changes: 36 additions & 0 deletions Makefile.prepare
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.PHONY: prepare clean

db.sqlite3:
python3 manage.py migrate

pw.txt: db.sqlite3
python3 manage.py createsuperuserauto

collected_static/:
yes yes | python3 manage.py collectstatic

secret.json: db.sqlite3
python3 manage.py autodeploy

nginx/local_nginx.conf: secret.json
python3 manage.py nginxconfgenerator > nginx/local_nginx.conf

prepare \.prepared: \
deps-install\
db.sqlite3\
pw.txt\
collected_static/\
secret.json\
nginx/local_nginx.conf\
prepare-nginx\

touch .prepared

clean:
-rm \
secret.json\
db.sqlite3\
pw.txt\
nginx/local_nginx.conf\
.prepared\
-rm -r collected_static
40 changes: 36 additions & 4 deletions coding_night_live/management/commands/autodeploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,43 @@

from django.core.management.base import BaseCommand
from django.contrib.sites.models import Site
from django.db.utils import OperationalError
from django.utils.crypto import get_random_string

from allauth.socialaccount.models import SocialApp


class AutoDeployException(Exception):
pass


class AutoDeploySecretNotFound(AutoDeployException):
pass


class AutoDeployDatabaseNotPrepared(AutoDeployException):
@staticmethod
def checker(suspiciousFunc):
def _runtime_checker(*args, **kwargs):
try:
return suspiciousFunc(*args, **kwargs)
except OperationalError:
raise AutoDeployDatabaseNotPrepared('ACTION REQUIRED) RUN `python manage.py migrate` FIRST!')
return _runtime_checker


def loadSecret():
secret = {}
try:
with open('secret.json', 'r') as f: # FIXME: PATH
secret.update(json.loads(f.read()))
except FileNotFoundError:
raise AutoDeploySecretNotFound('ACITON REQUIRED) RUN `python manage.py autodeploy` FIRST!')
return secret


class Command(BaseCommand):
requires_migrations_checks = True

def open_secret(self):
print('* Please write your OAuth Client ID')
Expand All @@ -29,10 +60,11 @@ def open_secret(self):

with open('secret.json', 'w') as f:
json.dump(result, f)
with open('secret.json', 'r') as f:
secret = json.loads(f.read())
self.social_app_setting(secret['DOMAIN'], secret['CLIENT_ID'], secret['SECRET'])

secret = loadSecret()
self.social_app_setting(secret['DOMAIN'], secret['CLIENT_ID'], secret['SECRET'])

@AutoDeployDatabaseNotPrepared.checker
def social_app_setting(self, domain, client_id, secret):
default_site_1 = Site.objects.get(id=1)
default_site_1.domain = domain
Expand All @@ -51,6 +83,6 @@ def social_app_setting(self, domain, client_id, secret):
new_social_app.save()
new_social_app.sites.add(default_site_1)
new_social_app.save()

def handle(self, *args, **options):
self.open_secret()
5 changes: 2 additions & 3 deletions coding_night_live/management/commands/createsuperuserauto.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ class Command(BaseCommand):
def handle(self, *args, **options):
password = get_random_string()

pwfile = open('pw.txt', 'w')
pwfile.write(password)
pwfile.close()
with open('pw.txt', 'w') as pwfile:
pwfile.write(password)

print('Generated root password : %s' % (password,))
admin = User.objects.create_superuser(
Expand Down
57 changes: 57 additions & 0 deletions coding_night_live/management/commands/nginxconfgenerator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from django.core.management.base import BaseCommand
from django.conf import settings
from django.template import Template, Context

from .autodeploy import loadSecret


NGINX_CONF_TEMPLATE = Template('''
# <NOTICE> /etc/nginx/ may not your nginx installation path. please check your installation path before.
# You need to run the script below!
# sudo ln -s coding-night-live/coding-night-live_nginx.conf /etc/nginx/sites-enabled/
# ex) sudo ln -s /home/punk/coding-night-live/collected_static /etc/nginx/sites-enabled/
server {
listen 80;
server_name {{ SERVER_NAME }};
charset utf-8;
client_max_body_size 20M;
location /static/ {
alias {{ BASE_DIR }}/collected_static/;
}
location / {
proxy_pass http://0.0.0.0:8001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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;
proxy_set_header X-Forwarded-Host $server_name;
}
}
''')


def _dontBelieve(conf, key, _defaultValue):
believableValue = conf.get(key, None)
if believableValue:
return believableValue
return _defaultValue


class Command(BaseCommand):
def handle(self, *args, **options):
secret = loadSecret()
variables = Context({
'BASE_DIR': settings.BASE_DIR,
'SERVER_NAME': _dontBelieve(secret, 'DOMAIN', 'localhost'),
'PROXY_PASS': None,
'PORT': None,
})
self.stdout.write(NGINX_CONF_TEMPLATE.render(variables))
31 changes: 2 additions & 29 deletions deploy_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
platform = sys.platform
if platform in ('win32', 'win64'):
print('Error: Cannot run in Windows..')
exit(0)
exit(-1)

cmd = 'python3'

Expand All @@ -25,31 +25,4 @@
import pip
pip.main(['install', '-r', 'requirements.txt'])

# DB Migration
os.system('%s manage.py migrate' % cmd)

# Admin user setting
os.system('%s manage.py createsuperuserauto' % cmd)

# Install redis-server / nginx
if platform == 'linux':
os.system('sudo apt-get install redis-server')
os.system('sudo apt-get install nginx')

# Find nginx location
# nginx = subprocess.checkoutput('sudo find / -name nginx.conf', shell=True)

# Server Deploy
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
os.system('sudo rm -rf /etc/nginx/sites-enabled/local_nginx.conf')
os.system('sudo ln -s %s/nginx/local_nginx.conf /etc/nginx/sites-enabled/' % BASE_DIR)

# OAuth setting
os.system('%s manage.py collectstatic' % cmd)
os.system('%s manage.py autodeploy' % cmd)

# Server run
os.system('redis-server &')
os.system('%s manage.py runworker &' % cmd)
os.system('daphne -b 0.0.0.0 -p 8001 coding_night_live.asgi:channel_layer &')
os.system('sudo service nginx start')
os.system('make')
34 changes: 0 additions & 34 deletions nginx/local_nginx.conf

This file was deleted.

Loading

0 comments on commit 02b36ac

Please sign in to comment.