谷歌应用引擎灵活的数据存储和 Pyramid

可以运行 Pyramid 应用于 Google App Engine . 本教程是“与环境无关的”,这意味着这里的命令应该在Linux、macOS或Windows上运行。本教程还假设您已经安装并创建了 Pyramid application, and that you have a Google App Engine account.

安装程序

首先,我们需要在appengine中设置一些东西。如果您的项目或任何其他GCP服务不需要访问数据存储,可以跳过凭据部分。

资格证书

导航到appengine的IAM和Admin部分,单击左侧边栏中的serviceaccounts,然后创建一个 Service Account .

一旦创建了服务帐户,您将获得 .json 密钥文件。这将用于允许金字塔应用程序与GCP服务通信。将此文件移动到金字塔项目。这里的最佳实践是确保此文件列在 .gitignore 这样它就不会与其他代码一起签入。

现在我们有了一个服务帐户,我们需要给它几个角色。点击 IAMIAM And Admin . 找到您刚刚创建的服务帐户,然后单击 Edit 按钮。把这个账户给我 云数据存储用户 读/写访问的角色。对于只读访问,请将其 云数据存储查看器 .

项目文件

创建包含以下内容的文件。

  1. requirements.txt

    1Pyramid
    2waitress
    3pyramid_debugtoolbar
    4pyramid_chameleon
    5google-cloud-ndb
    

    如果不使用数据存储,则可以排除 google-cloud-ndb .

  2. dockerfile

     1FROM gcr.io/google-appengine/python
     2# Create a virtualenv for dependencies. This isolates these packages from
     3# system-level packages.
     4# Use -p python3 or -p python3.7 to select python version. Default is version 2.
     5RUN virtualenv /env -p python3
     6
     7# Setting these environment variables are the same as running
     8# source /env/bin/activate.
     9ENV VIRTUAL_ENV /env
    10ENV PATH /env/bin:$PATH
    11ENV PYTHONUNBUFFERED 0
    12
    13# Copy the application's requirements.txt and run pip to install all
    14# dependencies into the virtualenv.
    15ADD requirements.txt /app/requirements.txt
    16ADD my-gcp-key.json /app/my-gcp-key.json
    17ENV GOOGLE_APPLICATION_CREDENTIALS /app/my-gcp-key.json
    18RUN pip install -r /app/requirements.txt
    19
    20# Add the application source code.
    21ADD . /app
    22
    23# Run a WSGI server to serve the application. waitress must be declared as
    24# a dependency in requirements.txt.
    25RUN pip install -e .
    26
    27CMD pserve production.ini
    

    替换 my-gcp-key.json 文件名,其中包含创建服务帐户时提供的JSON文件。

  3. datastore_tween.py

     1from my_project import datastore_client
     2
     3
     4class datastore_tween_factory(object):
     5    def __init__(self, handler, registry):
     6        self.handler = handler
     7        self.registry = registry
     8
     9    def __call__(self, request):
    10
    11        with datastore_client.context():
    12            response = self.handler(request)
    13
    14        return response
    
  4. app.yaml

     1runtime: custom
     2env: flex
     3service: default
     4runtime_config:
     5  python_version: 3.7
     6
     7manual_scaling:
     8  instances: 1
     9resources:
    10  cpu: 1
    11  memory_gb: 0.5
    12  disk_size_gb: 10
    

    For more details about app.yamlapp.yaml Reference .

  5. __init__.py

    该文件应该已经存在于项目的根级别,因为它应该是由金字塔的 cookiecutters . 在 main 方法的 config 语境:

    config.add_tween('my_project.datastore_tween.datastore_tween_factory')
    

    这允许您在每个请求中与数据存储进行通信。

  6. production.ini

    你的 Pyramid 应用程序应该已经包含两个 development.ini 和A production.ini . 为了让AppEngine与您的应用程序通信,它需要监听端口8080。假设您使用的是服务生WSGI服务器,请修改 listen 变量 server:main 块。

    listen = *:8080
    

现在,假设您在代码中定义了与数据存储“kind”相关的以下模型:

 1from google.cloud import ndb
 2
 3
 4class Accounts(ndb.Model):
 5
 6    email = ndb.StringProperty()
 7    password = ndb.StringProperty()
 8
 9    def __init__(self, **kwds):
10        super(Accounts, self).__init__(**kwds)

然后可以在任何处理程序/端点中查询此模型,如下所示:

Accounts.query().filter(Accounts.email == user_email).get()

局部运行

与AppEngine的标准环境不同,我们以一种非常典型的方式运行金字塔。您可以使用 dockerfile 我们之前创建了 pserve development.ini ,也可以在Docker容器中运行 dockerfile 这种灵活性将被使用。不需要做任何改变。这对于调试Flexible下可能遇到的任何问题非常有用,而无需部署到它。

部署

使用googlecloudsdk,部署非常简单。

$ gcloud app deploy app.yaml --version my-version --project my-gcp-project

替换 my-version 使用某种标识符,这样您就知道部署了什么代码。这几乎可以是任何东西。

替换 my-gcp-project 应用程序引擎应用程序的ID。

你的 Pyramid 应用程序现在已经在世界上运行了!您可以通过导航到您的域名、通过“<applicationid>.appspot.com”访问它,或者如果您在默认值之外指定了一个版本,那么它将是“<version dot applicationid>.appspot.com”。