diff --git a/.gitignore b/.gitignore index 773d442..8f6a93c 100644 --- a/.gitignore +++ b/.gitignore @@ -101,4 +101,6 @@ collected_static/ # For hide SECRET_KEY secret.json pw.txt +nginx/local_nginx.conf +.prepared diff --git a/Makefile b/Makefile index 8c1d1ee..5914923 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/Makefile.deps b/Makefile.deps new file mode 100644 index 0000000..151d3cb --- /dev/null +++ b/Makefile.deps @@ -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 \ No newline at end of file diff --git a/Makefile.prepare b/Makefile.prepare new file mode 100644 index 0000000..8d0c756 --- /dev/null +++ b/Makefile.prepare @@ -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 diff --git a/coding_night_live/management/commands/autodeploy.py b/coding_night_live/management/commands/autodeploy.py index bff234f..984bc25 100644 --- a/coding_night_live/management/commands/autodeploy.py +++ b/coding_night_live/management/commands/autodeploy.py @@ -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') @@ -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 @@ -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() diff --git a/coding_night_live/management/commands/createsuperuserauto.py b/coding_night_live/management/commands/createsuperuserauto.py index 205574f..8d56984 100644 --- a/coding_night_live/management/commands/createsuperuserauto.py +++ b/coding_night_live/management/commands/createsuperuserauto.py @@ -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( diff --git a/coding_night_live/management/commands/nginxconfgenerator.py b/coding_night_live/management/commands/nginxconfgenerator.py new file mode 100644 index 0000000..4d246c1 --- /dev/null +++ b/coding_night_live/management/commands/nginxconfgenerator.py @@ -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(''' +# /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)) diff --git a/deploy_helper.py b/deploy_helper.py index 2c8d874..8a52589 100644 --- a/deploy_helper.py +++ b/deploy_helper.py @@ -6,7 +6,7 @@ platform = sys.platform if platform in ('win32', 'win64'): print('Error: Cannot run in Windows..') - exit(0) + exit(-1) cmd = 'python3' @@ -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') diff --git a/nginx/local_nginx.conf b/nginx/local_nginx.conf deleted file mode 100644 index ae1a6ea..0000000 --- a/nginx/local_nginx.conf +++ /dev/null @@ -1,34 +0,0 @@ -# /etc/nginx/ may not your nginx installation path. please check your installation path before. - -# You need to run the script below! -# sudo ln -s {{ your_path }}/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 coding-night-live.japaneast.cloudapp.azure.com; - # ex1) server_name localhost; - # ex2) server_name coding-night-live.cloudapp.net; - charset utf-8; - client_max_body_size 20M; - - location /static/ { - alias /home/punk/coding-night-live/collected_static/; # Please edit this line! - # ex) alias /home/punk/Documents/coding-night-live/collected_static/; - } - - location / { - #localhost deploy -> proxy_pass http:/localhost:8000; - # 80 deploy -> proxy_pass http://0.0.0.0:8000; - 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; - } -} diff --git a/requirements.txt b/requirements.txt index 00f5bbb..0062c5e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,4 +15,4 @@ asgi_redis pyhaikunator django-redis raven -Twisted==16.0.0 # Issue #54. Not worked with Twisted==17.0.0 +Twisted