如何在heroku上托管知识库(airbnb)?

时间:2017-02-18 22:34:59

标签: heroku flask airbnb

我尝试在heroku上托管knowledge-repo (airbnb)

我发现了几个问题,例如在尝试初始化/app作为知识库时:

我注意到如果我使用knowledge_repo --repo /app init手动将heroku上的目录定义为知识仓库,我至少可以在dyno上使用knowledge_repo --repo <repo_path> runserver命令。如果我没有初始化知识仓库,我只是得到错误的路径错误:

AssertionError: Provided path '/app' is not a valid repository.

因此,将heroku上的目录初始化为知识仓库应用程序,即knowledge_repo --repo ./example_repo init似乎很重要,我试图在setup.py中满足此要求:

import subprocess
# define repo as knowledge repo in BASH
script = """echo yes "y" | knowledge_repo --repo /app init"""
subprocess.call(['sh', '-c', script])

但这不起作用。知识回购没有实现yes方法。现在这似乎是一个难以解决的问题。

完全错误:

2017-02-26T20:59:58.780670+00:00 heroku[web.1]: State changed from crashed to starting
2017-02-26T21:00:09.750240+00:00 heroku[web.1]: Starting process  with command `gunicorn --log-file=- knowledge_app:app\(\) -w 3 -b  0.0.0.0:3572`
2017-02-26T21:00:14.022140+00:00 heroku[web.1]: State changed from starting to up
2017-02-26T21:00:16.240021+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/" host=carwow-knowledge-repo.herokuapp.com request_id=99f7867e-37ce-4417-8a04-53577e13e872 fwd="84.208.51.166" dyno=web.1 connect=5ms service=1158ms status=503 bytes=0
2017-02-26T21:00:27.907629+00:00 heroku[web.1]: Starting process with command `gunicorn --log-file=- knowledge_app:app\(\) -w 3 -b 0.0.0.0:58588`
2017-02-26T21:00:31.561254+00:00 heroku[web.1]: State changed from starting to up
2017-02-26T21:00:31.110520+00:00 app[web.1]: [2017-02-26 21:00:31 +0000] [4] [INFO] Starting gunicorn 19.6.0
2017-02-26T21:00:31.112610+00:00 app[web.1]: [2017-02-26 21:00:31 +0000] [4] [INFO] Listening at: http://0.0.0.0:58588 (4)
2017-02-26T21:00:31.112715+00:00 app[web.1]: [2017-02-26 21:00:31 +0000] [4] [INFO] Using worker: sync
2017-02-26T21:00:31.118097+00:00 app[web.1]: [2017-02-26 21:00:31 +0000] [9] [INFO] Booting worker with pid: 9
2017-02-26T21:00:31.162142+00:00 app[web.1]: [2017-02-26 21:00:31 +0000] [11] [INFO] Booting worker with pid: 11
2017-02-26T21:00:31.230349+00:00 app[web.1]: [2017-02-26 21:00:31 +0000] [13] [INFO] Booting worker with pid: 13
2017-02-26T21:00:35.074917+00:00 app[web.1]: Traceback (most recent call last):
2017-02-26T21:00:35.074937+00:00 app[web.1]:   File "/app/.heroku/python/bin/gunicorn", line 11, in <module>
2017-02-26T21:00:35.074940+00:00 app[web.1]:     sys.exit(run())
2017-02-26T21:00:35.074976+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 74, in run
2017-02-26T21:00:35.075018+00:00 app[web.1]:     WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
2017-02-26T21:00:35.075052+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 192, in run
2017-02-26T21:00:35.075095+00:00 app[web.1]:     super(Application, self).run()
2017-02-26T21:00:35.075118+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 72, in run
2017-02-26T21:00:35.075151+00:00 app[web.1]:     Arbiter(self).run()
2017-02-26T21:00:35.075174+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 218, in run
2017-02-26T21:00:35.075233+00:00 app[web.1]:     self.halt(reason=inst.reason, exit_status=inst.exit_status)
2017-02-26T21:00:35.075236+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 331, in halt
2017-02-26T21:00:35.075309+00:00 app[web.1]:     self.stop()
2017-02-26T21:00:35.075331+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 381, in stop
2017-02-26T21:00:35.075429+00:00 app[web.1]:     time.sleep(0.1)
2017-02-26T21:00:35.075509+00:00 app[web.1]:     self.reap_workers()
2017-02-26T21:00:35.075444+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 231, in handle_chld
2017-02-26T21:00:35.075531+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 506, in reap_workers
2017-02-26T21:00:35.075628+00:00 app[web.1]:     raise HaltServer(reason, self.WORKER_BOOT_ERROR)
2017-02-26T21:00:35.075688+00:00 app[web.1]: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
2017-02-26T21:00:35.147852+00:00 app[web.1]: Repository already exists. Do you want to convert it to a knowledge data repository? Note that this will override any existing `README.md` and `.knowledge_repo_config.py` files, and replace any submodule at `.resources`. (y/n) WARNING:knowledge_repo.repositories.gitrepository:Not updating existing repository. Aborting!

1 个答案:

答案 0 :(得分:1)

我认为问题出在PORT上,请参阅heroku docs:

  

Web进程必须侦听$ PORT上的HTTP流量,该流量由设置   Heroku的。 Dockerfile中的EXPOSE不受尊重,但可以用于   本地测试。仅支持HTTP请求。

我通过停靠应用程序解决了这个问题,文件内容如下:

setup.py的内容

from setuptools import setup, find_packages
from pip.req import parse_requirements
install_reqs = parse_requirements("requirements.txt", session=False)

reqs = [str(ir.req) for ir in install_reqs]
setup(name='name_of_your_knowledge_repo',
         packages=find_packages(),
         install_reqs=reqs)

docker-python-setup.sh的内容

#!/bin/sh

export PATH=/heroku/python/bin:$PATH
export PYTHONUNBUFFERED=true
export PYTHONHOME=/heroku/python
export LIBRARY_PATH=/heroku/vendor/lib:/heroku/python/lib:$LIBRARY_PATH
export LD_LIBRARY_PATH=/heroku/vendor/lib:/heroku/python/lib:$LD_LIBRARY_PATH
export LANG=${LANG:-en_US.UTF-8}
export PYTHONHASHSEED=${PYTHONHASHSEED:-random}
export PYTHONPATH=${PYTHONPATH:-/app/};

exec $*

Dockerfile的内容

###
### This is mostly taken from github.com/heroku/docker-python, but the Docker Hub version of that
### now only supports Python 3, so we have an adapted copy here.
### The main change here is moving our code from /app/user to /app, and moving the python install
### from /app/.heroku to /heroku.
###

# Inherit from Heroku's stack
FROM heroku/cedar:14

# Which version of Python?
ENV PYTHON_VERSION python-2.7.11

# Add Python binaries to path.
ENV PATH /heroku/python/bin/:$PATH

# Create some needed directories
RUN mkdir -p /heroku/python /app/.profile.d
WORKDIR /app

# Install Python
RUN curl -s https://lang-python.s3.amazonaws.com/cedar-14/runtimes/$PYTHON_VERSION.tar.gz | tar zx -C /heroku/python

# Install Pip & Setuptools
RUN curl -s https://bootstrap.pypa.io/get-pip.py | /heroku/python/bin/python

# Export the Python environment variables in /heroku/python.sh
COPY docker-python-setup.sh /heroku/python.sh

ADD requirements.txt /app/
RUN /heroku/python/bin/pip install -r requirements.txt
ADD . /app

# Make locally-built containers run as not-root
# RUN useradd -m notroot
# USER notroot

ENTRYPOINT ["/heroku/python.sh"]
CMD /heroku/python/bin/knowledge_repo --repo /app deploy --port $PORT

knowledge_app.py的内容

from knowledge_repo import KnowledgeRepository

import subprocess
# define repo as knowledge repo in BASH
script = """echo yes "y" | knowledge_repo --repo /app init"""
subprocess.call(['sh', '-c', script])

app = KnowledgeRepository.for_uri('/app/.').get_app(config='server_config.py')

要部署,我使用heroku指南进行python app部署,并更新你需要做这样的事情:

通过docker

在heroku上部署
  1. 安装Docker
  2. cd到您当地的knowledge_repo目录:cd ~/repo/knowlegde_repo
  3. docker build .
  4. 安装插件:heroku-container-registry add in
  5. heroku container:push web