Odoo Automation Integration
Being an Odoo application, Cetmix Tower benefits from deep integration with Odoo automation. To make the integration process easier, it provides a set of helper functions that can be used in Odoo automated, server or scheduled actions. These functions are located in the special "cetmix.tower" model.
Create a new Server from a Server Template
server_create_from_template(self, template_reference, server_name, **options)
Arguments
Parameter | Type | Description |
---|---|---|
template_reference | Char | Server template reference |
server_name | Char | Name of the new server |
**options
Parameter | Type | Default | Description |
---|---|---|---|
partner | res.partner() record, optional | None | Partner this server belongs to. |
ipv4 | Char, optional | None | IP v4 address. Defaults to None. |
ipv6 | Char, optional | None | IP v6 address. Must be provided if IP v4 is not. Defaults to None. |
ssh_password | Char, optional | None | SSH password. Defaults to None. |
ssh_key | Char, optional | None | SSH private key reference. Defaults to None. |
configuration_variables | Dict, optional | None | Custom configuration variables in following format: {"variable_reference": "variable_value_char"} Eg: {"branch": "prod", "odoo_version": "16.0"} |
Returns
cx.tower.server: The newly created server record.
Example
Automated action that creates a new server from template when a Sales Order is confirmed in Odoo.
# This is a shortcut to the cetmix.tower helper model
cetmix_tower = env["cetmix.tower"]
for quotation in records:
# Check confirmed orders
if quotation.state == "sale":
# Suppose there are special custom fields in 'res.partner' and 'sale.order' models
options = {
"ipv4": quotation.partner_id.ip_v4_address,
"ssh_username": quotation.partner_id.ssh_username or "pepe",
"ssh_password": quotation.partner_id.ssh_password or "frog",
"ssh_auth_mode": "p", # Password authentication
"configuration_variables": {
"odoo_version": "18.0",
"odoo_edition": "ce",
"odoo_workers": "4",
"odoo_cron_threads": "2",
},
}
# Create a new server from the 'demo_template' Server Template
new_server = cetmix_tower.server_create_from_template(
template_reference="demo_template",
server_name=record.name,
**options
)
# Link new server to the quotation
if new_server:
quotation.server_id = new_server.id
Run a Command on a Server
server_run_command(self, server_reference, get_result, **custom_configuration_values)
Arguments
Parameter | Type | Description |
---|---|---|
server_reference | Char | Server reference |
command_reference | Char | Command reference |
get_result | Bool | Get the result of the command. Defaults to True. If False, the result will be saved to the command log when the command is finished. |
**custom_configuration_values
Custom configuration values that will override the values in the server configuration.
Parameter | Description |
---|---|
variable_reference | Variable reference. |
variable_value | Variable value. For variables of type options it will be the value of the selected option. |
Returns
- Dict:
{exit_code: Char, message: Char}
ifget_result
is True. - None: if
get_result
is False. The command log record will be created when the command is finished.
Example
Automated action that runs a command on a server when a Sales Order is confirmed in Odoo.
# Backup Odoo database to S3 storage from Odoo automated action
cetmix_tower = env["cetmix.tower"]
server = "my_cool_server"
command = "backup_odoo_to_s3"
custom_configuration_values = {
"s3_bucket": "my_cool_bucket",
"backup_type": "daily",
}
# Run the command in the background, no need to wait for the result
cetmix_tower.server_run_command(server,command,get_result=False, **custom_configuration_values)
Run a Flight Plan on a Server
server_run_flight_plan(self, server_reference, flight_plan_reference)
Arguments
Parameter | Type | Description |
---|---|---|
server_reference | Char | Server reference |
flight_plan_reference | Char | Flight plan reference |
Returns
- cx.tower.plan.log: flight plan log record or False if error
Example
Automated action that runs a command on a server when a Sales Order is confirmed in Odoo.
# Backup Odoo database to S3 storage
cetmix_tower = env["cetmix.tower"]
server = "my_cool_server"
flight_plan = "update_odoo_modules"
plan_log = cetmix_tower.server_run_flight_plan(server,flight_plan)
if not plan_log:
# Log an error message
server.message_post("Error running flight plan: " + flight_plan)
Set a Variable value for a Server
server_set_variable_value(self, server_reference, variable_reference, value)
Arguments
Parameter | Type | Description |
---|---|---|
server_reference | Char | Server reference |
variable_reference | Char | Variable reference |
value | Char | Variable value |
Returns
Dict: {exit_code: Char, message: Char}
Get a Variable value for a Server
server_get_variable_value(self, server_reference, variable_reference)
Arguments
Parameter | Type | Description |
---|---|---|
server_reference | Char | Server reference |
variable_reference | Char | Variable reference |
Returns
Char: Variable value or None if the variable is not set.
Example
Automated action Odoo workers for all partner servers. If this number is more than the maximum allowed number for this partner it will be set to the maximum allowed number.
# This is a shortcut to the cetmix.tower helper model
cetmix_tower = env["cetmix.tower"]
for partner in records:
if not partner.servers_ids:
continue
for server in partner.servers_ids:
# Get current number of Odoo workers
current_odoo_workers = cetmix_tower.server_get_variable_value(server.reference, "odoo_workers")
# Remember that result is a Char or None
if current_odoo_workers and int(current_odoo_workers) > partner.max_odoo_workers:
# Set the maximum number of Odoo workers
cetmix_tower.server_set_variable_value(
server_reference=server.reference,
variable_reference="odoo_workers",
value=partner.max_odoo_workers
)
Check if SSH connection to the Server is available
server_check_ssh_connection(self, server_reference, attempts=5, wait_time=10, try_command=True, try_file=True)
Arguments
Parameter | Type | Description |
---|---|---|
server_reference | Char | Server reference |
attempts | Int | Number of attempts to try the connection. Defaults to 5. |
wait_time | Int | Wait time in seconds between connection attempts. Defaults to 10 seconds. |
try_command | Bool | Try to execute a command. Defaults to True. |
try_file | Bool | Try file operations. Defaults to True. |
Returns
Dict: {exit_code: Int, message: Char}
Example
Automated action that checks if partner servers are responding.
# This is a shortcut to the cetmix.tower helper model
cetmix_tower = env["cetmix.tower"]
for partner in records:
if not partner.servers_ids:
continue
for server in partner.servers_ids:
result = cetmix_tower.server_check_ssh_connection(server.reference, attempts=3, wait_time=5, try_command=False, try_file=False)
# Post a message if the server is not responding
if result["exit_code"] != 0:
# NB: Odoo automated actions don't support f-strings, so we use the old way
partner.message_post(subject="Server is not responding" + server.name, body=result.get("message", "No error message"))