Skip to content
This repository has been archived by the owner on Aug 17, 2023. It is now read-only.

Commit

Permalink
refs #87 python3.6 support, docker correspond
Browse files Browse the repository at this point in the history
  • Loading branch information
wanshot committed Mar 4, 2019
1 parent 9a049b8 commit ccad49b
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 27 deletions.
13 changes: 0 additions & 13 deletions Dockerfile

This file was deleted.

34 changes: 34 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
DC := docker-compose

MYSQL_EXPOSE_PORT := 33061

help:
@echo "Please use \`make <target>' where <target> is one of"
@echo " build to build docker images."
@echo " deploy to deploy apps (local or dev)"
@echo " mysql to connect with mysql on docker container."
@echo " clean to remove all docker containers, images"
@echo " destroy to remove all docker containers, images, and all volumes"
@echo " down to down all docker containers, images"
@echo " help to show this help messages"

clean:
docker system prune
${DC} down --remove-orphans --rmi all

destroy:
docker system prune
${DC} down --volume --remove-orphans --rmi all

down:
${DC} down

build:
${DC} build

deploy: build
${DC} up -d
sh scripts/start_logging.sh

mysql:
mysql -h127.0.0.1 -P${MYSQL_EXPOSE_PORT} -uroot -p
4 changes: 4 additions & 0 deletions conf/mysql/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM mysql:5.6


COPY ./conf/mysql/my.cnf /etc/mysql/conf.d/
8 changes: 8 additions & 0 deletions conf/mysql/my.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
skip-character-set-client-handshake
init-connect = SET NAMES utf8mb4

[client]
loose-default-character-set = utf8mb4
21 changes: 21 additions & 0 deletions conf/python/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM python:3.6-slim

ENV PYTHONUNBUFFERED 1
RUN mkdir /proj
WORKDIR /proj
COPY /src/constraints.txt /proj/src/
COPY /src/requirements.txt /proj/src/

RUN pip install --upgrade pip\
&& apt-get update\
&& apt-get install -y mysql-client \
default-libmysqlclient-dev \
build-essential \
python3-dev \
&& pip install -r src/requirements.txt\
&& rm -rf ~/.cache/pip \
&& rm -rf /var/lib/apt/lists/*

COPY . /proj/

CMD python3
32 changes: 21 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
version: '2'
version: '3'
services:

bot:
build: .
#environment:
# LANG: ja_JP.UTF-8
build:
context: ./
dockerfile: ./conf/python/Dockerfile
env_file: .env
volumes:
- .:/proj
working_dir: /proj
restart: always
command: "bash -c 'cd src && alembic --config alembic/conf.ini upgrade head && python3 run.py'"
command: /bin/bash /proj/scripts/runserver.sh
depends_on:
- db

db:
image: library/mysql:5.6
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: haro
command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
restart: always
env_file: .env
build:
context: ./
dockerfile: ./conf/mysql/Dockerfile
volumes:
- mysql-data:/var/lib/mysql
ports:
- '${MYSQL_EXPOSE_PORT:-33061}:3306'


volumes:
mysql-data:
driver: local
5 changes: 4 additions & 1 deletion env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ SLACK_ERRORS_TO=#my-channel or username
REDMINE_URL=http://localhost/redmine_url
# REDMINE_URL=https://project.beproud.jp/redmine
HARO_DEBUG=True or False
SQLALCHEMY_URL=sqlite:///haro.sqlite
# SQLALCHEMY_URL=mysql+pymysql://root:example@db/haro?charset=utf8
SQLALCHEMY_ECHO=true or false
#SQLALCHEMY_POOL_SIZE=20
Expand All @@ -20,3 +19,7 @@ SQLALCHEMY_ECHO=true or false
# git_force_checkout=false
# git_sync_local=false
# git_version=master
MYSQL_USER=haro-user
MYSQL_DATABASE=haro
MYSQL_PASSWORD=haro-pass
MYSQL_ROOT_PASSWORD=pass
Empty file added nohup.out
Empty file.
38 changes: 38 additions & 0 deletions scripts/logfilter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from datetime import datetime
import fileinput
import os

here = os.path.dirname(__file__)
LOG_DIR = os.path.abspath(os.path.join(here, '..', 'logs'))
LOG_PREFIX = (
'db',
'bot'
)


def match_prefix(name):
for prefix in LOG_PREFIX:
if name.startswith(prefix):
return True
return False


def main():
for line in fileinput.input():
today = datetime.now().strftime('%Y%m%d')
data = line.split(' ')
if match_prefix(data[0]):
log_name = '{}_{}.log'.format(data[0].strip(), today)
log_file = os.path.join(LOG_DIR, log_name)
row = ' '.join(data[5:])
else:
log_name = 'other_{}.log'.format(today)
log_file = os.path.join(LOG_DIR, log_name)
row = line

with open(log_file, 'a') as fp:
fp.write(row)


if __name__ == '__main__':
main()
3 changes: 3 additions & 0 deletions scripts/runserver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

cd src && alembic --config alembic/conf.ini upgrade head && python3 run.py
13 changes: 13 additions & 0 deletions scripts/start_logging.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

cd "$(dirname "$0")/.."

LOG_PID=`ps -ef | grep "docker-compose logs" | grep -v "grep" | awk '{print $2}'`

if [ -z "$LOG_PID" ];then
echo "Not found logging pid."
else
kill -9 $LOG_PID
fi

nohup sh -c "docker-compose logs --no-color -f | python3 scripts/logfilter.py" &
14 changes: 14 additions & 0 deletions scripts/wait_for_mysql.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

# https://qiita.com/ry0f/items/6e29fa9f689b97058085
set -e

CMD="$@"

until mysqladmin ping -h db --silent; do
echo 'Waiting for mysqld to be connectable...'
sleep 5
done

echo "Command start"
exec $CMD
2 changes: 1 addition & 1 deletion src/constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ python-dateutil==2.6.1
python-editor==1.0.3
requests==2.18.4
six==1.11.0
slackbot==0.5.1
slackbot==0.5.3
slacker==0.9.60
SQLAlchemy==1.2.2
urllib3==1.22
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py35, flake8
envlist = py36, flake8
skipsdist = True

[testenv]
Expand Down

0 comments on commit ccad49b

Please sign in to comment.