DotCloud

备注

This cookbook recipe is obsolete because DotCloud has been acquired by Docker. 拜托 submit a pull request 更新这个配方。

DotCloud 为所有WSGi框架提供支持。下面是 Pyramid 应用程序的快速入门指南。您也可以阅读 DotCloud Python documentation 完整的概述。

步骤0:安装Dotcloud

Install DotCloud's CLI 跑步:

$ pip install dotcloud

步骤1:添加Dotcloud所需的文件

dotcloud希望python应用程序在项目的根目录中有一些文件。首先,你需要一个点 requirements.txt 用于指示Dotcloud为应用程序安装哪些python库依赖项的文件。其次,你需要一个 dotcloud.yaml 通知dotcloud您的应用程序(至少)有一个python服务的文件。您可能还需要其他服务,例如MongoDB数据库或PostgreSQL数据库等—这些都是在yaml中指定的。

最后,您需要一个名为 wsgi.py 这就是DotcloudUwsgi服务器配置要查找的内容。这个wsgi.py脚本需要为 Pyramid 应用程序创建一个wsgi可调用程序,该程序必须存在于一个名为“application”的全局应用程序中。

您需要将requirements.txt、dotcloud.yml和wsgi.py文件添加到应用程序的根目录中。以下是一些基本Pyramid应用程序的示例:

requirements.txt

cherrypy
Pyramid==1.3
# Add any other dependencies that should be installed as well

dotcloud.yml

www:
    type: python
db:
    type: postgresql

进一步了解 DotCloud buildfile .

wsgi.py

 1# Your WSGI callable should be named “application”, be located in a
 2# "wsgi.py" file, itself located at the top directory of the service.
 3#
 4# For example, to load the app from your "production.ini" file in the same
 5# directory:
 6import os.path
 7from pyramid.scripts.pserve import cherrypy_server_runner
 8from pyramid.paster import get_app
 9
10application = get_app(os.path.join(os.path.dirname(__file__), 'production.ini'))
11
12if __name__ == "__main__":
13    cherrypy_server_runner(application, host="0.0.0.0")

步骤2:配置数据库

如果您在dotcloud.yml中指定了数据库服务,那么连接信息将在json文件中提供给您的服务,该文件位于/home/dotcloud/environment.json。例如,以下代码将读取environment.json文件,并将PostgreSQL URL添加到 Pyramid 应用程序的设置中:

1import json
2
3# if dotcloud, read PostgreSQL URL from environment.json
4db_uri = settings['postgresql.url']
5DOTCLOUD_ENV_FILE = "/home/dotcloud/environment.json"
6if os.path.exists(DOTCLOUD_ENV_FILE):
7    with open(DOTCLOUD_ENV_FILE) as f:
8        env = json.load(f)
9        db_uri = env["DOTCLOUD_DATA_POSTGRESQL_URL"]

第3步:部署应用程序

现在您可以部署应用程序了。如果您使用的是Mercurial或Git,请记住提交您的更改,然后在应用程序的顶部目录中运行以下命令:

$ dotcloud create your_app_name
$ dotcloud push your_app_name

At the end of the push, you'll see the URL(s) for your new app. 玩得高兴!