2017年1月5日木曜日

Heroku で Python3 で Bottle アプリ

母艦の環境

  • Ubuntu 16.04.1 LTS (xenial)

パッケージをインストール

パッケージをインストール

$ sudo apt install virtualenv
$ sudo apt install git
$ sudo apt install curl
$ sudo apt install w3m
$ sudo apt install software-properties-common

Heroku のリポジトリを追加

$ sudo add-apt-repository "deb https://cli-assets.heroku.com/branches/stable/apt ./"
$ curl -L https://cli-assets.heroku.com/apt/release.key | sudo apt-key add -
$ sudo apt update

Heroku パッケージをインストール

$ sudo apt install heroku

Heroku にアプリを作成

Heroku にログイン

$ heroku login
  • 事前に登録したメールアドレスとパスワードを入力

アプリを作成

$ heroku create --app testapp-20170104
Creating ? testapp-20170104... done
https://testapp-20170104.herokuapp.com/ | https://git.heroku.com/testapp-20170104.git

Heroku にデプロイするアプリのファイルを準備

アプリケーション用の virtualenv を作成してアクティベート

$ virtualenv --python python3 testenv
$ source testenv/bin/activate

以下、virtualenv 環境下で実施します。

Python3 パッケージインストール

$ pip3 install gunicorn
$ pip3 install bottle

リポジトリをクローン

$ git clone https://git.heroku.com/testapp-20170104.git
Cloning into 'testapp-20170104'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.

$ ls -la
total 16
drwxrwxr-x 4 ubuntu ubuntu 4096 Jan  4 17:26 .
drwxrwxr-x 5 ubuntu ubuntu 4096 Jan  4 17:10 ..
drwxrwxr-x 3 ubuntu ubuntu 4096 Jan  4 17:26 testapp-20170104
drwxrwxr-x 5 ubuntu ubuntu 4096 Jan  4 17:25 testenv

クローンした git リポジトリに移動

$ cd testapp-20170104/

Bottle アプリを用意

$ cat testapp.py
import sys
from bottle import route, default_app

@route('/')
def index():
  sHtml = '''
<html><body><pre>
=== sys.version ===
{}
===================
</pre></body></html>
'''.format(sys.version)

  return sHtml

appfunc = default_app()

Procfile を用意

$ cat Procfile
web: gunicorn testapp:appfunc --log-file -

requirements.txt 作成

$ pip3 freeze | egrep -v '^pkg-resources==' > requirements.txt

$ cat requirements.txt
bottle==0.12.11
gunicorn==19.6.0

ローカルでアプリをテスト

ローカルでアプリを起動

$ heroku local web
[WARN] No ENV file found
3:27:48 PM web.1 |  [2017-01-04 15:27:48 +0900] [6740] [INFO] Starting gunicorn 19.6.0
3:27:48 PM web.1 |  [2017-01-04 15:27:48 +0900] [6740] [INFO] Listening at: http://0.0.0.0:5000 (6740)
3:27:48 PM web.1 |  [2017-01-04 15:27:48 +0900] [6740] [INFO] Using worker: sync
3:27:48 PM web.1 |  [2017-01-04 15:27:48 +0900] [6774] [INFO] Booting worker with pid: 6774

起動メッセージから 5000番ポートであることがわかるので、別のターミナルを開き w3m で接続してみる (virtualenv 環境でなくても良いです)

$ w3m -dump http://localhost:5000/
=== sys.version ===
3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609]
===================
  • ローカルで起動した Heroku アプリが python-3.5.2 で実行されました。

Ctrl-C でアプリを終了

virtualenv 環境を終了

$ deactivate

Heroku にアプリをデプロイ

クローンしたリポジトリのディレクトリに移動

ローカルのアプリは Python-3.5.2 で実行されましたが、 https://devcenter.heroku.com/articles/python-runtimes を読むと Heroku での Python3 実行環境は python-3.6.0 とのことです。

runtime.txt で python-3.6.0 を指定

$ cat runtime.txt
python-3.6.0

ディレクトリは以下のようになっています。

$ ls -la
total 32
drwxrwxr-x 4 ubuntu ubuntu 4096 Jan  4 18:06 .
drwxrwxr-x 4 ubuntu ubuntu 4096 Jan  4 17:26 ..
drwxrwxr-x 8 ubuntu ubuntu 4096 Jan  4 17:59 .git
-rw-rw-r-- 1 ubuntu ubuntu   43 Jan  4 17:29 Procfile
drwxrwxr-x 2 ubuntu ubuntu 4096 Jan  4 17:40 __pycache__
-rw-rw-r-- 1 ubuntu ubuntu   33 Jan  4 17:58 requirements.txt
-rw-rw-r-- 1 ubuntu ubuntu   13 Jan  4 18:06 runtime.txt
-rwxr--r-- 1 ubuntu ubuntu  238 Jan  4 17:39 testapp.py

git アカウント情報登録

$ git config --global user.email "testuser@localdomain"
$ git config --global user.name "testuser"

git リポジトリにファイルを追加してコミット

$ git add Procfile
$ git add requirements.txt
$ git add runtime.txt
$ git add testapp.py
$ git commit -m 'initial commit'

デプロイ

$ git push origin master

w3m で確認

$ w3m -dump https://testapp-20170104.herokuapp.com/
=== sys.version ===
3.6.0 (default, Dec 24 2016, 04:08:05)
[GCC 4.8.4]
===================
  • Python-3.6.0 で実行されました。


0 件のコメント:

コメントを投稿