Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #80 #84

Merged
merged 40 commits into from
Mar 19, 2017
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
08852d4
createsuperuserauto command creates pw.txt file.
punkyoon Mar 8, 2017
eb3516b
modified .gitignore
punkyoon Mar 8, 2017
247b7a8
create python script for auto deploy
punkyoon Mar 8, 2017
aee790f
added a autodeploy command
punkyoon Mar 8, 2017
95568e5
developing autodeploy command...
punkyoon Mar 8, 2017
aa5ac62
developting autodeploy command ... ; now can set oauth provider
punkyoon Mar 8, 2017
d4bf728
added a script file
punkyoon Mar 8, 2017
cc5afce
developing script ..
punkyoon Mar 8, 2017
f1e9236
developing script...
punkyoon Mar 8, 2017
67a5152
developing script...
punkyoon Mar 8, 2017
2a8403c
developing script.. but there's some error.. I will fix it soon
punkyoon Mar 10, 2017
baa8bfd
fixed a bug
punkyoon Mar 11, 2017
d7f0252
can deploy with auto.py
punkyoon Mar 11, 2017
fa7cdee
modified a .gitignore
punkyoon Mar 11, 2017
7ab9f8d
remove collected_static
punkyoon Mar 11, 2017
ba0d8ad
developed deploy&undeploy helper
punkyoon Mar 11, 2017
c4a6bd9
fixed flake8
punkyoon Mar 11, 2017
90ebf7e
change path
punkyoon Mar 11, 2017
a135e31
[Issue #80] Now autodeploy.py got OAuth Initializer
minhoryang Mar 12, 2017
be68a4c
[WIP] Makefile for deploy/undeploy_helper.py
minhoryang Mar 12, 2017
eb1f4b3
Merge pull request #86 from minhoryang/issue_80
punkyoon Mar 12, 2017
ca1be49
Merge branch 'master' of https://github.com/punkyoon/coding-night-liv…
punkyoon Mar 13, 2017
4b2eaa5
feedback
punkyoon Mar 13, 2017
93f9497
flake8 fix
punkyoon Mar 13, 2017
2642deb
modified travis command
punkyoon Mar 13, 2017
5a3963a
modified travis cmd
punkyoon Mar 13, 2017
9355534
[Issue #80] `python manage.py nginxconfgenerator > nginx/local_nginx.…
minhoryang Mar 13, 2017
9c6cb68
구질구질한 Makefile
AilisObrian Mar 13, 2017
b713d03
Merge remote-tracking branch 'minhoryang/issue_80' into issue_80
AilisObrian Mar 15, 2017
0ae5cf3
Minor Feedbacks Apply
AilisObrian Mar 15, 2017
fa8940e
Merge pull request #87 from minhoryang/issue_80
punkyoon Mar 16, 2017
79ec867
[Issue #84][BugFix] NGINX required sudoer for 80 port
AilisObrian Mar 18, 2017
ae66ab6
[Issue #84][BugFix] Latest Redis from Homebrew Kegs(cached) won't sta…
AilisObrian Mar 18, 2017
5d9632f
[Issue #84] Purge makefile for deps
AilisObrian Mar 18, 2017
48226e1
[Issue #84][BugFix][Related 79ec867] NGINX required sudoer rights for…
AilisObrian Mar 18, 2017
a34f399
[Issue #84][BugFix][Related 48226e1] `sudo brew services` won't worke…
AilisObrian Mar 18, 2017
c173f85
[Issue #84][Readability Improvement] Divided Makefile
AilisObrian Mar 18, 2017
1ca9ed8
Merge pull request #92 from minhoryang/issue_80
minhoryang Mar 18, 2017
c04bcb3
Tiny typo fix at requirements.txt
AilisObrian Mar 18, 2017
7a01754
Daphne-Twisted Bug Resolved, so remove the version requirement statem…
AilisObrian Mar 19, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,11 @@ ENV/
*.rdb
*.swp

collected_static/

# For hide SECRET_KEY
secret.json
pw.txt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@punkyoon @minhoryang secret.json과 pw.txt가 쪼개져있는게 약간 미묘해요. secret.json 안에 계정 비밀번호를 적어놔도 좋지 않을까 싶어요.

Copy link
Member

@minhoryang minhoryang Mar 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

지금 secret.json이 DOMAIN도 가지고있어서 사실 애매모호해요.ㅎㅎ 이번 리뷰과정중에서도 이걸 짚고 넘어갈 수 있구, 아니면 다음에 해도 될 것 같아요.

nginx/local_nginx.conf
.prepared

2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ install:
- "pip install flake8"

script:
- "python secret_key_gen.py"
- "python manage.py migrate"
- "python manage.py createsuperuserauto"
- "flake8 --count --ignore=E501,E302,W293,F841 --exclude ./docs,./manage_room/diff_match_patch,./manage_room/migrations,./manage_chat/migrations,./manage.py"
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
OS := $(shell uname)

default: start
.PHONY: start stop uninstall


include Makefile.deps
include Makefile.prepare


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


stop: deps-stop
-killall -9 daphne
-killall -9 python3
-killall -9 python # FIXME: daphne at MAC OS


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
88 changes: 88 additions & 0 deletions coding_night_live/management/commands/autodeploy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import json

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')
client_id = input('>')
print('** Please write your OAuth Secret')
secret = input('>')
print('*** Please write your Server Domain (ex. example.com)')
domain = input('>')

chars = 'qwertyuiopasdfghjklzxcvbnm0987654321!@#$%^&*(-_=+)'
SECRET_KEY = get_random_string(50, chars)

result = {}

result['CLIENT_ID'] = str(client_id)
result['SECRET'] = str(secret)
result['DOMAIN'] = str(domain)
result['SECRET_KEY'] = SECRET_KEY

with open('secret.json', 'w') as f:
json.dump(result, f)

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

@AutoDeployDatabaseNotPrepared.checker
def social_app_setting(self, domain, client_id, secret):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 함수가 실패했는데도, secret.json을 저장해도 될까요?

default_site_1 = Site.objects.get(id=1)
default_site_1.domain = domain
default_site_1.name = domain
default_site_1.save()

new_social_app = SocialApp(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 함수를 두번 호출하면, id=1인 SocialApp을 두번 생성하려 할겁니다.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 함수를 두번 부를 일이 없을거라고 생각하지만,
undeploy_helper.py의 역할이 무엇이냐에 따라서, 다를 수도 있습니다.

Copy link
Member

@juice500ml juice500ml Mar 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그렇게할까요? @punkyoon @juice500ml

id=1,
provider='google',
name=domain,
client_id=client_id,
secret=secret,
key='',
)

new_social_app.save()
new_social_app.sites.add(default_site_1)
new_social_app.save()

def handle(self, *args, **options):
self.open_secret()
4 changes: 4 additions & 0 deletions coding_night_live/management/commands/createsuperuserauto.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class Command(BaseCommand):

def handle(self, *args, **options):
password = get_random_string()

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

print('Generated root password : %s' % (password,))
admin = User.objects.create_superuser(
email='root@localhost',
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))
11 changes: 6 additions & 5 deletions coding_night_live/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
secret_file = os.path.join(BASE_DIR, 'secret.json')

with open(secret_file, 'r') as f:
secret = json.loads(f.read())

try:
secret_file = os.path.join(BASE_DIR, 'secret.json')
with open(secret_file, 'r') as f:
secret = json.loads(f.read())
except FileNotFoundError:
secret = {'SECRET_KEY': True}

def get_secret(setting, secret=secret):
try:
Expand Down
28 changes: 28 additions & 0 deletions deploy_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
import sys


# Check OS
platform = sys.platform
if platform in ('win32', 'win64'):
print('Error: Cannot run in Windows..')
exit(-1)

cmd = 'python3'

# Check Python Version (3 or 2)
if sys.version_info[0] == 2:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

무슨 이유로 이 파일은 python3만 지원하는건가요?

Copy link
Member Author

@punkyoon punkyoon Mar 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@minhoryang django 2.0부터는 python2.7을 아예 지원하지 않습니다. 추후 django 버전 업을 대비하여 2버전에서는 아예 실행하지 못하도록 막아두었습니다.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@punkyoon 오! 장고도 드디어! 알겠습니다 :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@punkyoon Django 2.0으로의 migration(?)이 바람직할려나 모르겠네요! 1.1x를 그대로 갖고 가는게 나을지도 모르고요

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

django 2.0에서의 channels 지원 여부에 따라 좀 갈릴 것 같네요. 일단 py2에서도 하게 하는게 맞는 것 같아요!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@juice500ml django-channels는 py3.5+를 지원한다는데, django 2.0이 channels를 지원할 계획인지 여부 말하시는거죠?
https://github.com/django/channels/blob/13472369ebf18d2925302aa1a407405b67a3e7c5/tox.ini

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@minhoryang 넵넵 그쵸 사실 django-channels가 django 1.10에 들어오기로 했었는데 그게 무산됐거든요! django 2버전에서는 어떻게 될지 아직 미정 상태인거같아요

print('Error: Cannot run in Python 2.x..')
exit(-1)

# Install python packages
try:
import pip
except ImportError:
print("Installing pip...")
if platform == 'linux':
os.system('sudo apt-get install python3-pip')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pip를 설치만 하고, import를 진행하지 않습니다. Line 51에서 pip를 찾지 못할 예정입니다.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved

import pip
pip.main(['install', '-r', 'requirements.txt'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation이 이상해요!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@juice500ml 음?! 어떤점이 이상할까요?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

플랫폼이 linux가 아니면 pip package가 import가 안되는데요?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@juice500ml ping?


os.system('make')
34 changes: 0 additions & 34 deletions local_nginx.conf

This file was deleted.

Loading