容器

概述

每个构建版本都隔离在其自己的容器(Linux 命名空间容器)中。

基础是一个Ubuntu系统,其中安装了Odoo所有必需的依赖项以及常见的有用包。

If your project requires additional Python dependencies, or more recent releases, you can define a requirements.txt file in the root of your branches listing them. The platform will take care to install these dependencies in your containers. The pip requirements specifiers documentation can help you write a requirements.txt file. To have a concrete example, check out the requirements.txt file of Odoo.

子模块的 :file:requirements.txt 文件也被考虑在内。该平台在包含Odoo模块的每个文件夹中查找:file:`requirements.txt`文件:不是在模块文件夹本身,而是在其父文件夹中。

目录结构

由于容器是基于Ubuntu的,因此它们的目录结构遵循linux文件系统层次结构标准。“Ubuntu 的文件系统树概述<https://help.ubuntu.com/community/LinuxFilesystemTreeOverview#Main_directories>”_ 解释了主目录。

以下是 Odoo.sh 相关目录:

.
├── home
│    └── odoo
│         ├── src
│         │    ├── odoo                Odoo Community source code
│         │    │    └── odoo-bin       Odoo server executable
│         │    ├── enterprise          Odoo Enterprise source code
│         │    ├── themes              Odoo Themes source code
│         │    └── user                Your repository branch source code
│         ├── data
│         │    ├── filestore           database attachments, as well as the files of binary fields
│         │    └── sessions            visitors and users sessions
│         └── logs
│              ├── install.log         Database installation logs
│              ├── odoo.log            Running server logs
│              ├── update.log          Database updates logs
│              └── pip.log             Python packages installation logs
└── usr
     ├── lib
     │    ├── python2.7
     │         └── dist-packages       Python 2.7 standard libraries
     │    ├── python3
     │         └── dist-packages       Python 3 standard libraries
     │    └── python3.5
     │         └── dist-packages       Python 3.5 standard libraries
     ├── local
     │    └── lib
     │         ├── python2.7
     │         │    └── dist-packages  Python 2.7 third-party libraries
     │         └── python3.5
     │              └── dist-packages  Python 3.5 third-party libraries
     └── usr
          └── bin
               ├── python2.7           Python 2.7 executable
               └── python3.5           Python 3.5 executable

Python 2.7 和 3.5 都安装在容器中。然而:

  • 如果您的项目配置为使用Odoo 10.0,则Odoo服务器将与Python 2.7一起运行。

  • 如果您的项目配置为使用Odoo 11.0或更高版本,则Odoo服务器将与Python 3.5一起运行。

数据库外壳

使用 shell 访问容器时,可以使用 psql 访问数据库。

odoo@odoo-addons-master-1.odoo.sh:~$ psql
psql (9.5.2, server 9.5.11)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

odoo-addons-master-1=>

Be careful ! Use transactions (BEGIN…COMMIT/ROLLBACK) for every sql statements leading to changes (UPDATE, DELETE, ALTER, …), especially for your production database.

交易机制是您在发生错误时的安全网。您只需回滚更改即可将数据库还原到其以前的状态。

例如,您可能会忘记设置 WHERE 条件。

odoo-addons-master-1=> BEGIN;
BEGIN
odoo-addons-master-1=> UPDATE res_users SET password = '***';
UPDATE 457
odoo-addons-master-1=> ROLLBACK;
ROLLBACK

在这种情况下,您可以回滚以还原您刚刚错误地执行的不需要的更改,并重写语句:

odoo-addons-master-1=> BEGIN;
BEGIN
odoo-addons-master-1=> UPDATE res_users SET password = '***' WHERE id = 1;
UPDATE 1
odoo-addons-master-1=> COMMIT;
COMMIT

但是,不要忘记在完成事务后提交或回滚事务。打开的事务可能会锁定表中的记录,并且正在运行的数据库可能会等待它们被释放。它可能导致服务器无限期挂起。

此外,如果可能,请先使用临时数据库来测试语句。它为您提供了额外的安全网。

运行 Odoo 服务器

您可以从容器 shell 启动 Odoo 服务器实例。您将无法使用浏览器从外部世界访问它,但您可以例如:

  • 使用 Odoo shell,

$  odoo-bin shell
>>> partner = env['res.partner'].search([('email', '=', 'asusteK@yourcompany.example.com')], limit=1)
>>> partner.name
'ASUSTeK'
>>> partner.name = 'Odoo'
>>> env['res.partner'].search([('email', '=', 'asusteK@yourcompany.example.com')], limit=1).name
'Odoo'
  • 装载新的模块

$  odoo-bin -i sale --without-demo=all --stop-after-init
  • 更新模块,

$  odoo-bin -u sale --stop-after-init
  • 运行模块的测试,

$  odoo-bin -i sale --test-enable --log-level=test --stop-after-init

在上面的命令中,参数为:

  • “–without-demo=all”阻止加载所有模块的演示数据

  • “—在初始化后停止”将在服务器实例完成您请求的操作后立即将其关闭。

还有更多选项,详见:doc:CLI 文档</developer/reference/cli>

您可以在日志(~/logs/odoo.log)中找到 Odoo.sh 用于运行服务器的插件路径。查找“odoo: 插件路径”:

2018-02-19 10:51:39,267 4 INFO ? odoo: Odoo version saas-15.2
2018-02-19 10:51:39,268 4 INFO ? odoo: Using configuration file at /home/odoo/.config/odoo/odoo.conf
2018-02-19 10:51:39,268 4 INFO ? odoo: addons paths: ['/home/odoo/data/addons/saas-15.2', '/home/odoo/src/user', '/home/odoo/src/enterprise', '/home/odoo/src/themes', '/home/odoo/src/odoo/addons', '/home/odoo/src/odoo/odoo/addons']

Be careful, especially with your production database. Operations that you perform running this Odoo server instance are not isolated: Changes will be effective in the database. Always, make your tests in your staging databases.

在 Odoo.sh 中调试

调试 Odoo.sh 生成与另一个 Python 应用没有什么不同。本文仅介绍 Odoo.sh 平台的特性和限制,并假定您已经知道如何使用调试器。

注解

如果您还不知道如何调试Python应用程序,则可以在Internet上轻松找到多个入门课程。

您可以使用“pdb”,“pudb”或“ipdb”在 Odoo.sh 调试代码。由于服务器在 shell 外部运行,因此您无法直接从 Odoo 实例后端启动调试器,因为调试器需要一个 shell 才能运行。

然后编辑要触发调试器的代码并添加以下内容:

import sys
if sys.__stdin__.isatty():
    import pdb; pdb.set_trace()

条件:code:`sys.__stdin__.isatty()`是一个黑客,可以检测你是否从shell运行Odoo。

保存文件,然后运行 Odoo Shell:

$ odoo-bin shell

最后,*通过*Odoo Shell,您可以触发要调试的代码/函数/方法。

显示在 Odoo.sh shell 中运行 ``pdb`` 的控制台截图。