【问题标题】:How to host knowledge-repo (airbnb) on heroku?如何在heroku上托管知识库(airbnb)?
【发布时间】:2017-07-08 08:51:04
【问题描述】:

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

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

我注意到,如果我使用knowledge_repo --repo /app init 手动将heroku 上的目录定义为知识库,我至少可以在测功机上使用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!

【问题讨论】:

  • 这可以部署为普通的 Flask 应用程序。 Heroku 有相关文档。
  • 你发现了吗?我遇到了同样的问题。除了我什至无法让 setup.py 工作。
  • 我最终通过 docker 让它工作了。将 setup.py 和 docker.sh 的内容作为对本帖的回复发布。我还没有时间向 airbnb 发送 Pull 请求。

标签: flask heroku knowledge-repo


【解决方案1】:

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

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

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

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 应用程序部署,要更新您需要执行以下操作:

通过docker部署在heroku上

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-05
    • 2018-05-22
    • 2014-12-20
    • 2023-04-02
    • 2018-07-20
    • 2019-01-08
    • 1970-01-01
    • 2013-02-28
    相关资源
    最近更新 更多