Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create python packages requirements.txt file from template #153

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,28 +227,46 @@ This tag helps you install or update Odoo modules without performing a full setu
ansible-playbook playbook.yml --tags "only-modules"
```

Community Roles
---------------
Odoo modules
------------

#### Deploy
To use community roles, you need to deploy this modules in the server. This role manage the modules deployment with `pip`.
To use Odoo modules (from OCA or custom modules), you need to deploy modules packages to the server. This role manages the modules deployment with `pip`.
In your inventory you can define up to two variables in order to add python packages to the target `requirements.txt` that will be generated.

You can define a `requirements.txt` file to manage the modules and ensure the version installed:
Define the `odoo_role_odoo_community_packages` variable to install packages not maintained by you. For example when you're deploying an Odoo instance that just requires OCA modules:

```yml
# inventory/group_vars/all.yml
odoo_role_odoo_community_packages:
- odoo11-addon-contract==11.0.2.1.0
- odoo11-addon-contract-sale-invoicing==11.0.1.0.0
- odoo11-addon-contract-variable-qty-timesheet==11.0.1.0.0
- odoo11-addon-contract-variable-quantity==11.0.1.2.1
```
# requirements.txt
odoo11-addon-contract==11.0.2.1.0
odoo11-addon-contract-sale-invoicing==11.0.1.0.0
odoo11-addon-contract-variable-qty-timesheet==11.0.1.0.0
odoo11-addon-contract-variable-quantity==11.0.1.2.1

In some case you want to deploy different versions of the same module to different hosts.
A typical case is when you have developed a custom module and need to deploy a packaged version of it to production but in local development environment you need to install it es editable.
Copy link
Contributor

@oyale oyale Jul 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A typical case is when you have developed a custom module and need to deploy a packaged version of it to production but in local development environment you need to install it es editable.
A typical case is when you have developed a custom module and need to deploy a packaged version of it to production but in local development environment you need to install it as editable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need the "as editable" part since in that use case you'll need to install the package with the -e option, it's just an example, a possible use case.

In this case you can define the `odoo_role_odoo_project_packages` variable:
```yml
# inventory/host_vars/production.yml
odoo_role_odoo_project_packages:
- odoo14-my-custom-module==14.0.0.1.0
```

```yml
# inventory/host_vars/development.yml
odoo_role_odoo_project_packages:
- '-e file:///opt/odoo_modules/setup/my_custom_module'
```

> The default the `requirements.txt` file path is `"{{ inventory_dir }}/../files/requirements.txt"`.
For backward compatibility, the Ansible template will look first for a `files/requirements.txt` file in your inventory and use it as the source of the template.
If you have a `files/requirements.txt` file defined in your inventory, the two variables just described will not take any effect.
oyale marked this conversation as resolved.
Show resolved Hide resolved

# Install
Once the modules are in the server, you need to install them in the database.
#### Install
Once the modules packages are installed in the server, you need to install them in the Odoo database.

Define a `odoo_role_odoo_community_modules` var with the list of the modules names you want to install.
Define a `odoo_role_odoo_community_modules` variable with the list of the modules names you want to install.

```yml
# inventory/group_vars/all.yml
Expand Down
10 changes: 7 additions & 3 deletions tasks/community-modules.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
---
- name: Copy requirements.txt
copy:
src: "{{ odoo_role_community_modules_requirements_path }}"
- name: Create requirements.txt from template
ansible.builtin.template:
src: "{{ lookup('ansible.builtin.first_found', template_sources) }}"
dest: "{{ odoo_role_odoo_modules_path }}/requirements.txt"
owner: "{{ odoo_role_odoo_user }}"
group: "{{ odoo_role_odoo_group }}"
mode: 0644
vars:
template_sources:
- "{{ inventory_dir }}/../files/requirements.txt"
- templates/requirements.txt.j2
tags: ['community-modules', 'only-modules']

- name: Deploy community roles with pip
Expand Down
23 changes: 22 additions & 1 deletion tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,32 @@
state: link
when: odoo_role_odoo_version < "12.0" and ansible_distribution == "Ubuntu" and not ansible_distribution_version >= "18.04"


- name: Check if Odoo is already installed in virtualenv
become: true
become_user: "{{ odoo_role_odoo_user }}"
ansible.builtin.shell: >
{{ odoo_role_odoo_python_path }} -m pip show odoo
register: odoo_package_check
failed_when: odoo_package_is_installed.rc not in [0,1]

- name: Set boolean for Odoo package installation
set_fact:
odoo_package_not_installed: "{{ odoo_package_check.rc == 1 }}"

- name: Determine if Odoo needs to be installed or reinstalled
set_fact:
odoo_install_required: >-
(odoo_role_desired_tar_download.changed) or
(odoo_role_desired_git_download.changed) or
(odoo_package_not_installed) or
(odoo_role_force_install | default(false))

- name: Install Odoo
become: true
become_user: "{{ odoo_role_odoo_user }}"
shell: "cd {{ odoo_role_odoo_path }} && {{ odoo_role_odoo_python_path }} setup.py install"
when: odoo_role_desired_tar_download.changed or odoo_role_desired_git_download.changed
when: odoo_install_required

- name: Populate community db modules
set_fact:
Expand Down
9 changes: 9 additions & 0 deletions templates/requirements.txt.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# {{ ansible_managed }}

{% for package in odoo_role_odoo_community_packages|default([]) %}
{{ package }}
{% endfor %}

{% for package in odoo_role_odoo_project_packages|default([]) %}
{{ package }}
{% endfor %}