简体   繁体   English

部署后 Google App Engine 运行命令

[英]Google App Engine run command after deploy

Is there a way to run a list of linux commands on the docker after deployement finish automatically, like lifecycle (valable for Kube.netes ) on the yaml file?有没有办法在部署自动完成后在 docker 上运行 linux 命令列表,例如 yaml 文件上的生命周期(对 Kube.netes 有价值)?

I do not want to have to ssh to the instance and run my command.我不想必须 ssh 到实例并运行我的命令。 I need to install ssh-client and some time vim and other.我需要安装ssh-client和一些时间vim等。

App Engine is Serverless solution. App Engine 是无服务器解决方案。 In product description you may find:在产品描述中,您可能会发现:

Fully managed完全托管

A fully managed environment lets you focus on code while App Engine manages infrastructure concerns.完全托管的环境让您可以专注于代码,而 App Engine 可以管理基础架构问题。

This means that, if you choose App Engine you don't have to care about server.这意味着,如果您选择 App Engine,则不必关心服务器。 So as design, it's for those who do not want to have any access to server, but focus on code leaving all server maintenance to GCP.因此,作为设计,它是为那些不想访问服务器,而是专注于代码而将所有服务器维护留给 GCP 的人准备的。 I think the main feature is automatic scaling of the app.我认为主要功能是应用程序的自动缩放。

We do not know what do you intend to do, you can review the app.yaml reference to find all features available.我们不知道您打算做什么,您可以查看app.yaml 参考以查找所有可用功能。 The configuration is different depending of the language environment you want to use.根据您要使用的语言环境,配置会有所不同。

If you want to have access to environment you should use Kube.netes solutions or even Compute engine.如果您想访问环境,您应该使用 Kube.netes 解决方案甚至计算引擎。

I hope it will help somehow!我希望它会有所帮助!

For those who are looking for a solution to this problem.对于那些正在寻找解决此问题的人。
App Engine with runtime: python or other default source in the app.yaml, there won't be too much customization.具有运行时的 App Engine:python或 app.yaml 中的其他默认源,不会有太多自定义。
To be able to create your own build you have to use runtime: custom and add Dockerfile file in same directory (root).为了能够创建自己的构建,您必须使用runtime: custom并在同一目录(root)中添加Dockerfile文件。
This is what it looks like:这是它的样子:

app.yaml : Only the first line change. 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 : 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

Another simple workaround would be to create a separate URL handler that will run your shell script.另一个简单的解决方法是创建一个单独的 URL 处理程序来运行您的 shell 脚本。 For example /migrate .例如/migrate And after deployment, you may use curl to trigger that URL.部署后,您可以使用 curl 来触发 URL。

Please note that in such case anybody, who would try to find some secret URLs of your backend may find it and trigger it as many times, as they want.请注意,在这种情况下,任何试图找到您后端的秘密 URL 的人都可能会找到它并根据需要多次触发它。 So if you need to ensure only trusted people can trigger it - you should either:因此,如果您需要确保只有受信任的人才能触发它 - 您应该:

  • come up with more secret URL than just /migrate想出比/migrate更多的秘密 URL
  • check permissions inside this view (but in such case it will be more difficult to call it via curl, cause you'll also need to pass some auth data)检查此视图内的权限(但在这种情况下,通过 curl 调用它会更加困难,因为您还需要传递一些身份验证数据)

Example basic view (using python + django rest framework):示例基本视图(使用 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.

相关问题 将 Meteor 部署到 Google App Engine 2017 - Deploy Meteor to Google App Engine 2017 在 Google App Engine 中部署 Spring boot gradle 应用 - Deploy Spring boot gradle app in Google App Engine 如何使用 nuxtjs 配置 vuetify 以部署在 Google App Engine 上 - How can I config vuetify with nuxtjs to deploy on Google App Engine 如何将 Helidon 应用程序部署到 Google Cloud App Engine? - How to deploy a Helidon application to Google Cloud App Engine? 谷歌云应用引擎部署的最新版本是否有 URL? - Is there a URL for the latest version of a google cloud app engine deploy? 何时使用 Google App Engine Flex 与 Google Cloud Run - When to use Google App Engine Flex vs Google Cloud Run 谷歌应用引擎部署我所有的反应文件甚至来源 - Google app-engine deploy all my react file even sources 无法使用 web3 库将 Flask 应用程序部署到 Google App Engine - Can't deploy Flask application to Google App Engine with web3 library 错误:在谷歌应用引擎上部署 node.js 后找不到模块“/workspace/server.js” - Error: Cannot find module '/workspace/server.js' upon node js deploy on google app engine 仅将构建的 React 应用程序部署到应用程序引擎 - Deploy only build react app to app engine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM