Blog Post

...

Odoo module upgrade on multiple databases

This post provides the script that solves the problem of automated module upgrade on multiple databases in Odoo 10.

If you've worked with Odoo you might know that in order to install some new functionality, you need to install (or update) the appropriate module, i.e. initiate the upgrade process on a database. By default Odoo can deal with this smoothly, just by going to Modules List (having Admin privileges) and pressing "Upgrade" button. The issue here is that this upgrade process takes place on the current database only. And what if there are multiple databases exist, then you have to login to each DB entity via Odoo and navigate to Module list and press that Upgrade button. These steps to be performed manually and if there are, let's say, more than 3 DBs then it may be really annoying to get through each upgrade. There are many inquires on the internet to solve that problem, e.g. here and here, but the solution there is outdated, so in this post you’ll find a workable script for modern Odoo 10 version.

To automate the module upgrade process - a Python script is written that can be invoked on Lynux only (because it involves the usage of pexpect library, which is available on Linux only), no Windows, sorry.

Firstly we get the list of available databases in Postgres, then we iterate through each of them to perform an upgrade.

Second important step is to form odoo-bin execution command. Odoo-bin is an entry point to start/upgrade Odoo instance and in this way it allows to initiate the update of some particular module on a database using predefined parameters (e.g. --database, --update).

Third step is to use pexpect lib to invoke the command on odoo-bin and keep track of Odoo log to catch the message "Modules loaded.", which means that Odoo has finished upgrade and we can switch to the other DB.

The final script looks so:

#!/usr/bin/env python

from datetime import date
import subprocess, xmlrpclib, sys, os, psycopg2, pexpect

# Database info
db_user = 'odoo'
db_passwd = ''
db_host = 'localhost'
db_port = 5432
db_name = 'postgres'
oerp_conf = '/opt/odoo/odoo.conf'
oerp_bin = "/opt/odoo/odoo-bin"
module_to_upgrade = "module_to_upgrade"

today = date.today()
log_date = today.strftime('%Y%m%d')
log_name = "update-{}.log".format(log_date)
base_dir = os.getcwd()

with open(log_name, 'a+') as log_file:

  # Get list of databases
  db = psycopg2.connect(user=       db_user, 
                        password=   db_passwd,
                        host=       db_host,
                        port=       db_port,
                        database=   db_name)
  cr = db.cursor()
  cr.execute("select datname from pg_database where datdba=(select usesysid from pg_user where usename='{}') order by datname".format(db_user))
  dblist = [str(name) for (name,) in cr.fetchall()]

  for database in dblist:
    # Wait for server to upgrade database, then kill it
    print "Upgrading database {}...".format(database)
    
    dbupdate_cmd = "{} shell -c {} --database={} --update={}".format(oerp_bin, oerp_conf, database, module_to_upgrade) 
    output = pexpect.spawn(dbupdate_cmd)
    try:
      # to catch this string loading.py was altered (line 461)
      output.expect('.*Modules loaded.', timeout=600)

    except pexpect.ExceptionPexpect as e:
      print "Timeout reached while upgrading {}. Try manually upgrading the database with the command '{}'.".format(database, dbupdate_cmd)
    output.kill(0)
    status = "Upgraded database {}.".format(database)
    print status
    log_file.write(status + "\n")

This script is also provides logging to console and external file, so you could always check the upgrade progress and results.

In this post the multiple database upgrade script for Odoo 10 is introduced. We use it pretty often and I hope you’ll like it as we do.

Comments (0)

Tags: odoo


0 comments

Leave a Comment