Skip to content

Instantly share code, notes, and snippets.

@repodevs
Created June 10, 2018 09:23
Show Gist options
  • Save repodevs/539a768a1ddfeb3bf5996f99e84c25c2 to your computer and use it in GitHub Desktop.
Save repodevs/539a768a1ddfeb3bf5996f99e84c25c2 to your computer and use it in GitHub Desktop.
Odoo CLI Helpers
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""Install, Uninstall, Upgrade a module"""
import xmlrpclib
import argparse
import getpass
import time
parser = argparse.ArgumentParser()
# Connection args
parser.add_argument('-d', '--database', help="Use DATABASE as the database name",
action='store', metavar='DATABASE', default=getpass.getuser())
parser.add_argument('-u', '--user', help="Use USER as the database user name",
action='store', metavar='USER', default='admin')
parser.add_argument('-w', '--password',
help="Use PASSWORD as the database password.",
action='store', metavar='PASSWORD', default='admin')
parser.add_argument('-s', '--url',
help="Point to the web services hosted at URL",
action='store', metavar='URL',
default='http://localhost:8069')
parser.add_argument('-c', '--config',
help="Config file to connect odoo server",
action='store', metavar='CONFIG')
parser.add_argument('action', help="Action name: install, uninstall or upgrade",
action='store', choices=['install', 'uninstall', 'upgrade'],
metavar='ACTION')
parser.add_argument('module', help="Uninstall the module MODULE",
action='store', metavar='MODULE')
xargs = vars(parser.parse_args())
## Set Variable
URL = xargs.get('url')
DB = xargs.get('database')
USER = xargs.get('user')
PASSWORD = xargs.get('password')
ACTION = xargs['action']
MODULE = xargs['module']
ODOO_ACTION = 'button_immediate_{}'.format(ACTION)
# Log in
ws_common = xmlrpclib.ServerProxy(URL + '/xmlrpc/common')
try:
print("[+] Try Login to {}.".format(URL))
uid = ws_common.login(DB, USER, PASSWORD)
print("[+] Logged in to {}.".format(URL))
except Exception as e:
raise Exception("[!] Failed Login to {} | {}".format(URL, e))
# Get the object proxy
ws_object = xmlrpclib.ServerProxy(URL + '/xmlrpc/object')
print("[+] Connected to the object service.")
# Find the parent location by name
res_ids = ws_object.execute(
DB, uid, PASSWORD,
'ir.module.module', 'search', [('name', '=', MODULE)])
if len(res_ids) != 1:
raise Exception("[!] No module found for {}".format(MODULE))
# Run an action for the module
print("[+] {} {} module in 5 seconds".format(ACTION.title(), MODULE))
time.sleep(5)
# Execute action that cannot be cancel anymore
print("[+] Executing command for {}".format(ACTION))
ws_object.execute(
DB, uid, PASSWORD,
'ir.module.module', ODOO_ACTION, res_ids)
print("[+] All action done.")
## used ./odoo-cli.py -s http://0.0.0.0:7769 -d odoo_database -u admin -w admin upgrade pos_notes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment