This repository has been archived by the owner on Aug 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs #87 python3.6 support, docker correspond
- Loading branch information
Showing
17 changed files
with
201 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
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 bot" | ||
@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 " test to excute all tests." | ||
@echo " migrations to generate alembic migration files." | ||
@echo " migrate to apply alembic migration." | ||
@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 | ||
${DC} exec bot sh scripts/wait_for_mysql.sh honcho start migrate | ||
${DC} restart bot | ||
|
||
test: | ||
${DC} exec bot tox -e py36 | ||
|
||
migrations: | ||
${DC} exec bot honcho start makemigrations | ||
|
||
migrate: | ||
${DC} exec bot honcho start migrate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ Haro is [slackbot](https://github.com/lins05/slackbot "lins05/slackbot: A chat b | |
|
||
## 事前準備 | ||
|
||
- Macのローカルで開発する場合、Docker、ansibleを構築する必要なし | ||
- 開発環境には、Dockerを利用します | ||
- 起動方法まで参照することで、ローカル環境が構築できる | ||
|
||
### APIトークンの取得 | ||
|
@@ -16,42 +16,23 @@ Haro is [slackbot](https://github.com/lins05/slackbot "lins05/slackbot: A chat b | |
- 「Save Integration」ボタンをクリックして保存する | ||
- API Token(``xoxb-XXXXXXX-XXXXXXX``)をこのあと使用するので、コピーしておく | ||
|
||
### Requirements | ||
|
||
- Python 3.5.2 or later. | ||
## 開発方法 | ||
|
||
```bash | ||
$ python3 -m venv env | ||
$ git clone [email protected]:beproud/beproudbot.git | ||
$ cd beproudbot | ||
$ source /path/env/bin/activate | ||
(env)$ cp env.sample .env | ||
(env)$ vi .env # API Token 等を記入する | ||
(env)$ export SLACK_API_TOKEN=xoxb-XXXX # API Tokenがターミナルの環境変数で.envに反映されない場合 | ||
(env)$ pip install -r src/requirements.txt | ||
``` | ||
- 開発に必要な操作はMakefileを参照 | ||
|
||
## 起動方法 | ||
|
||
```bash | ||
$ source /path/env/bin/activate | ||
# configには環境変数を指定します | ||
(env)$ export $(cat .env |grep -v '#') | ||
(env)$ cd src && python run.py | ||
``` | ||
|
||
### Docker | ||
|
||
```bash | ||
# MySQL を使用する場合先に立ち上げておく | ||
# docker-compose up -d db | ||
# bot の起動 | ||
$ docker-compose build bot | ||
$ docker-compose run -d bot | ||
# コンテナにはいる | ||
$ docker-compose run --rm bot bash | ||
# 終了 | ||
# docker-compose down | ||
Makefile | ||
$ make | ||
Please use `make <target>' where <target> is one of | ||
build to build docker images. | ||
deploy to deploy apps (local or dev) | ||
test to excute all tests. | ||
flake8 to excute flake8 to python codes. | ||
clean to remove all docker containers, images | ||
destroy to remove all docker containers, images, and all volumes | ||
down to down all docker containers, images | ||
help to show this help messages | ||
``` | ||
|
||
## DB操作 | ||
|
@@ -81,7 +62,6 @@ alembic を使用します | |
honchoは .env を自動的に読み込み、スクリプトを開始することができます。 | ||
|
||
```bash | ||
(env)$ pip install honcho | ||
# honcho start bot | ||
# honcho start migrate | ||
# honcho start makemigrations | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
FROM mysql:5.7 | ||
|
||
|
||
COPY ./conf/mysql/my.cnf /etc/mysql/conf.d/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[mysqld] | ||
character-set-server = utf8 | ||
collation-server = utf8_general_ci | ||
init-connect = SET NAMES utf8 | ||
innodb-file-format = barracuda | ||
innodb_file_format_max = barracuda | ||
innodb_file_per_table = 1 | ||
innodb_large_prefix = 1 | ||
|
||
[client] | ||
loose-default-character-set = utf8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
|
||
cd src && python3 run.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" & |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,6 @@ geopy | |
kml2geojson | ||
|
||
pillow | ||
|
||
honcho | ||
tox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters