[英]Google App Engine run command after deploy
有没有办法在部署自动完成后在 docker 上运行 linux 命令列表,例如 yaml 文件上的生命周期(对 Kube.netes 有价值)?
我不想必须 ssh 到实例并运行我的命令。 我需要安装ssh-client
和一些时间vim
等。
App Engine 是无服务器解决方案。 在产品描述中,您可能会发现:
完全托管
完全托管的环境让您可以专注于代码,而 App Engine 可以管理基础架构问题。
这意味着,如果您选择 App Engine,则不必关心服务器。 因此,作为设计,它是为那些不想访问服务器,而是专注于代码而将所有服务器维护留给 GCP 的人准备的。 我认为主要功能是应用程序的自动缩放。
我们不知道您打算做什么,您可以查看app.yaml 参考以查找所有可用功能。 根据您要使用的语言环境,配置会有所不同。
如果您想访问环境,您应该使用 Kube.netes 解决方案甚至计算引擎。
我希望它会有所帮助!
对于那些正在寻找解决此问题的人。
具有运行时的 App Engine:python或 app.yaml 中的其他默认源,不会有太多自定义。
为了能够创建自己的构建,您必须使用runtime: custom并在同一目录(root)中添加Dockerfile文件。
这是它的样子:
app.yaml :只有第一行发生变化。
runtime: custom
# the PROJECT-DIRECTORY is the one with settings.py and wsgi.py
entrypoint: gunicorn -b :$PORT mysite.wsgi # specific to a GUnicorn HTTP server deployment
env: flex # for Google Cloud Flexible App Engine
# any environment variables you want to pass to your application.
# accessible through os.environ['VARIABLE_NAME']
env_variables:
# the secret key used for the Django app (from PROJECT-DIRECTORY/settings.py)
SECRET_KEY: 'lkfjop8Z8rXWbrtdVCwZ2fMWTDTCuETbvhaw3lhwqiepwsfghfhlrgldf'
DEBUG: 'False' # always False for deployment
DB_HOST: '/cloudsql/app-example:us-central1:example-postgres'
DB_PORT: '5432' # PostgreSQL port
DB_NAME: 'example-db'
DB_USER: 'mysusername' # either 'postgres' (default) or one you created on the PostgreSQL instance page
DB_PASSWORD: 'sgvdsgbgjhrhytriuuyokkuuywrtwerwednHUQ'
STATIC_URL: 'https://storage.googleapis.com/example/static/' # this is the url that you sync static files to
automatic_scaling:
min_num_instances: 1
max_num_instances: 2
handlers:
- url: /static
static_dir: static
beta_settings:
cloud_sql_instances: app-example:us-central1:example-postgres
runtime_config:
python_version: 3 # enter your Python version BASE ONLY here. Enter 2 for 2.7.9 or 3 for 3.6.4
Dockerfile :
FROM gcr.io/google-appengine/python
# Create a virtualenv for dependencies. This isolates these packages from
# system-level packages.
# Use -p python3 or -p python3.7 to select python version. Default is version 2.
RUN virtualenv /env -p python3
# Setting these environment variables are the same as running
# source /env/bin/activate.
ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH
# Install custom linux packages (python-qt4 for open-cv)
RUN apt-get update -y && apt-get install vim python-qt4 ssh-client git -y
# Add the application source code and install all dependencies into the virtualenv
ADD . /app
RUN pip install -r /app/requirements.txt
# add my ssh key for github
RUN mv /app/.ssh / && \
chmod 600 /.ssh/*
# Run a WSGI server to serve the application.
EXPOSE 8080
# gunicorn must be declared as a dependency in requirements.txt.
WORKDIR /app
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 Blacks.wsgi:application --timeout 0 --preload && \
python3 manage.py makemigrations && \
python3 manage.py migrate
另一个简单的解决方法是创建一个单独的 URL 处理程序来运行您的 shell 脚本。 例如/migrate
。 部署后,您可以使用 curl 来触发 URL。
请注意,在这种情况下,任何试图找到您后端的秘密 URL 的人都可能会找到它并根据需要多次触发它。 因此,如果您需要确保只有受信任的人才能触发它 - 您应该:
/migrate
更多的秘密 URL示例基本视图(使用 python + django rest 框架):
from io import StringIO
from django.core.management import call_command
from rest_framework.renderers import StaticHTMLRenderer
from rest_framework.response import Response
from rest_framework.views import APIView
class MigrateView(APIView):
permission_classes = ()
renderer_classes = [StaticHTMLRenderer]
def post(self, request, *args, **kwargs):
out = StringIO()
# --no-color because ANSI symbols (used for colors)
# render incorrectly in browser/curl
call_command('migrate', '--no-color', stdout=out)
return Response(out.getvalue())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.