| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright 2014 Sébastien Alix |
| 3 |
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl) |
| 4 |
"""Provide the :class:`DB` class to manage the server databases.""" |
| 5 |
import base64 |
| 6 |
import io |
| 7 |
import sys |
| 8 |
|
| 9 |
from odoorpc import error |
| 10 |
from odoorpc.tools import v |
| 11 |
|
| 12 |
# Python 2 |
| 13 |
if sys.version_info[0] < 3: |
| 14 |
|
| 15 |
def encode2bytes(data): |
| 16 |
return data |
| 17 |
|
| 18 |
|
| 19 |
# Python >= 3 |
| 20 |
else: |
| 21 |
|
| 22 |
def encode2bytes(data): |
| 23 |
return bytes(data, 'ascii') |
| 24 |
|
| 25 |
|
| 26 |
class DB(object): |
| 27 |
"""The `DB` class represents the database management service. |
| 28 |
It provides functionalities such as list, create, drop, dump |
| 29 |
and restore databases. |
| 30 |
|
| 31 |
.. note:: |
| 32 |
This service have to be used through the :attr:`odoorpc.ODOO.db` |
| 33 |
property. |
| 34 |
|
| 35 |
>>> import odoorpc |
| 36 |
>>> odoo = odoorpc.ODOO('localhost') # doctest: +SKIP |
| 37 |
>>> odoo.db |
| 38 |
<odoorpc.db.DB object at 0x...> |
| 39 |
|
| 40 |
""" |
| 41 |
|
| 42 |
def __init__(self, odoo): |
| 43 |
self._odoo = odoo |
| 44 |
|
| 45 |
def dump(self, password, db, format_='zip'): |
| 46 |
"""Backup the `db` database. Returns the dump as a binary ZIP file |
| 47 |
containing the SQL dump file alongside the filestore directory (if any). |
| 48 |
|
| 49 |
>>> dump = odoo.db.dump('super_admin_passwd', 'prod') # doctest: +SKIP |
| 50 |
|
| 51 |
.. doctest:: |
| 52 |
:hide: |
| 53 |
|
| 54 |
>>> dump = odoo.db.dump(SUPER_PWD, DB) |
| 55 |
|
| 56 |
If you get a timeout error, increase this one before performing the |
| 57 |
request: |
| 58 |
|
| 59 |
>>> timeout_backup = odoo.config['timeout'] |
| 60 |
>>> odoo.config['timeout'] = 600 # Timeout set to 10 minutes |
| 61 |
>>> dump = odoo.db.dump('super_admin_passwd', 'prod') # doctest: +SKIP |
| 62 |
>>> odoo.config['timeout'] = timeout_backup |
| 63 |
|
| 64 |
Write it on the file system: |
| 65 |
|
| 66 |
.. doctest:: |
| 67 |
:options: +SKIP |
| 68 |
|
| 69 |
>>> with open('dump.zip', 'wb') as dump_zip: |
| 70 |
... dump_zip.write(dump.read()) |
| 71 |
... |
| 72 |
|
| 73 |
.. doctest:: |
| 74 |
:hide: |
| 75 |
|
| 76 |
>>> with open('dump.zip', 'wb') as dump_zip: |
| 77 |
... fileno = dump_zip.write(dump.read()) # Python 3 |
| 78 |
... |
| 79 |
|
| 80 |
You can manipulate the file with the `zipfile` module for instance: |
| 81 |
|
| 82 |
.. doctest:: |
| 83 |
:options: +SKIP |
| 84 |
|
| 85 |
>>> import zipfile |
| 86 |
>>> zipfile.ZipFile('dump.zip').namelist() |
| 87 |
['dump.sql', |
| 88 |
'filestore/ef/ef2c882a36dbe90fc1e7e28d816ad1ac1464cfbb', |
| 89 |
'filestore/dc/dcf00aacce882bbfd117c0277e514f829b4c5bf0', |
| 90 |
...] |
| 91 |
|
| 92 |
.. doctest:: |
| 93 |
:hide: |
| 94 |
|
| 95 |
>>> import zipfile |
| 96 |
>>> zipfile.ZipFile('dump.zip').namelist() # doctest: +NORMALIZE_WHITESPACE |
| 97 |
['dump.sql'...'filestore/...'...] |
| 98 |
|
| 99 |
The super administrator password is required to perform this method. |
| 100 |
|
| 101 |
*Python 2:* |
| 102 |
|
| 103 |
:return: `io.BytesIO` |
| 104 |
:raise: :class:`odoorpc.error.RPCError` (access denied / wrong database) |
| 105 |
:raise: `urllib2.URLError` (connection error) |
| 106 |
|
| 107 |
*Python 3:* |
| 108 |
|
| 109 |
:return: `io.BytesIO` |
| 110 |
:raise: :class:`odoorpc.error.RPCError` (access denied / wrong database) |
| 111 |
:raise: `urllib.error.URLError` (connection error) |
| 112 |
""" |
| 113 |
args = [password, db] |
| 114 |
if v(self._odoo.version)[0] >= 9: |
| 115 |
args.append(format_) |
| 116 |
data = self._odoo.json( |
| 117 |
'/jsonrpc', {'service': 'db', 'method': 'dump', 'args': args} |
| 118 |
) |
| 119 |
# Encode to bytes forced to be compatible with Python 3.2 |
| 120 |
# (its 'base64.standard_b64decode()' function only accepts bytes) |
| 121 |
result = encode2bytes(data['result']) |
| 122 |
content = base64.standard_b64decode(result) |
| 123 |
return io.BytesIO(content) |
| 124 |
|
| 125 |
def change_password(self, password, new_password): |
| 126 |
"""Change the administrator password by `new_password`. |
| 127 |
|
| 128 |
>>> odoo.db.change_password('super_admin_passwd', 'new_admin_passwd') # doctest: +SKIP |
| 129 |
|
| 130 |
.. doctest: |
| 131 |
:hide: |
| 132 |
|
| 133 |
>>> odoo.db.change_password(SUPER_PWD, 'new_admin_passwd') |
| 134 |
>>> odoo.db.change_password('new_admin_passwd', SUPER_PWD) |
| 135 |
|
| 136 |
The super administrator password is required to perform this method. |
| 137 |
|
| 138 |
*Python 2:* |
| 139 |
|
| 140 |
:raise: :class:`odoorpc.error.RPCError` (access denied) |
| 141 |
:raise: `urllib2.URLError` (connection error) |
| 142 |
|
| 143 |
*Python 3:* |
| 144 |
|
| 145 |
:raise: :class:`odoorpc.error.RPCError` (access denied) |
| 146 |
:raise: `urllib.error.URLError` (connection error) |
| 147 |
""" |
| 148 |
self._odoo.json( |
| 149 |
'/jsonrpc', |
| 150 |
{ |
| 151 |
'service': 'db', |
| 152 |
'method': 'change_admin_password', |
| 153 |
'args': [password, new_password], |
| 154 |
}, |
| 155 |
) |
| 156 |
|
| 157 |
def create( |
| 158 |
self, password, db, demo=False, lang='en_US', admin_password='admin' |
| 159 |
): |
| 160 |
"""Request the server to create a new database named `db` |
| 161 |
which will have `admin_password` as administrator password and |
| 162 |
localized with the `lang` parameter. |
| 163 |
You have to set the flag `demo` to `True` in order to insert |
| 164 |
demonstration data. |
| 165 |
|
| 166 |
>>> odoo.db.create('super_admin_passwd', 'prod', False, 'fr_FR', 'my_admin_passwd') # doctest: +SKIP |
| 167 |
|
| 168 |
If you get a timeout error, increase this one before performing the |
| 169 |
request: |
| 170 |
|
| 171 |
>>> timeout_backup = odoo.config['timeout'] |
| 172 |
>>> odoo.config['timeout'] = 600 # Timeout set to 10 minutes |
| 173 |
>>> odoo.db.create('super_admin_passwd', 'prod', False, 'fr_FR', 'my_admin_passwd') # doctest: +SKIP |
| 174 |
>>> odoo.config['timeout'] = timeout_backup |
| 175 |
|
| 176 |
The super administrator password is required to perform this method. |
| 177 |
|
| 178 |
*Python 2:* |
| 179 |
|
| 180 |
:raise: :class:`odoorpc.error.RPCError` (access denied) |
| 181 |
:raise: `urllib2.URLError` (connection error) |
| 182 |
|
| 183 |
*Python 3:* |
| 184 |
|
| 185 |
:raise: :class:`odoorpc.error.RPCError` (access denied) |
| 186 |
:raise: `urllib.error.URLError` (connection error) |
| 187 |
""" |
| 188 |
self._odoo.json( |
| 189 |
'/jsonrpc', |
| 190 |
{ |
| 191 |
'service': 'db', |
| 192 |
'method': 'create_database', |
| 193 |
'args': [password, db, demo, lang, admin_password], |
| 194 |
}, |
| 195 |
) |
| 196 |
|
| 197 |
def drop(self, password, db): |
| 198 |
"""Drop the `db` database. Returns `True` if the database was removed, |
| 199 |
`False` otherwise (database did not exist): |
| 200 |
|
| 201 |
>>> odoo.db.drop('super_admin_passwd', 'test') # doctest: +SKIP |
| 202 |
True |
| 203 |
|
| 204 |
The super administrator password is required to perform this method. |
| 205 |
|
| 206 |
*Python 2:* |
| 207 |
|
| 208 |
:return: `True` or `False` |
| 209 |
:raise: :class:`odoorpc.error.RPCError` (access denied) |
| 210 |
:raise: `urllib2.URLError` (connection error) |
| 211 |
|
| 212 |
*Python 3:* |
| 213 |
|
| 214 |
:return: `True` or `False` |
| 215 |
:raise: :class:`odoorpc.error.RPCError` (access denied) |
| 216 |
:raise: `urllib.error.URLError` (connection error) |
| 217 |
""" |
| 218 |
if self._odoo._env and self._odoo._env.db == db: |
| 219 |
# Remove the existing session to avoid HTTP session error |
| 220 |
self._odoo.logout() |
| 221 |
data = self._odoo.json( |
| 222 |
'/jsonrpc', |
| 223 |
{'service': 'db', 'method': 'drop', 'args': [password, db]}, |
| 224 |
) |
| 225 |
return data['result'] |
| 226 |
|
| 227 |
def duplicate(self, password, db, new_db): |
| 228 |
"""Duplicate `db' as `new_db`. |
| 229 |
|
| 230 |
>>> odoo.db.duplicate('super_admin_passwd', 'prod', 'test') # doctest: +SKIP |
| 231 |
|
| 232 |
The super administrator password is required to perform this method. |
| 233 |
|
| 234 |
*Python 2:* |
| 235 |
|
| 236 |
:raise: :class:`odoorpc.error.RPCError` (access denied / wrong database) |
| 237 |
:raise: `urllib2.URLError` (connection error) |
| 238 |
|
| 239 |
*Python 3:* |
| 240 |
|
| 241 |
:raise: :class:`odoorpc.error.RPCError` (access denied / wrong database) |
| 242 |
:raise: `urllib.error.URLError` (connection error) |
| 243 |
""" |
| 244 |
self._odoo.json( |
| 245 |
'/jsonrpc', |
| 246 |
{ |
| 247 |
'service': 'db', |
| 248 |
'method': 'duplicate_database', |
| 249 |
'args': [password, db, new_db], |
| 250 |
}, |
| 251 |
) |
| 252 |
|
| 253 |
def list(self): |
| 254 |
"""Return the list of the databases: |
| 255 |
|
| 256 |
>>> odoo.db.list() # doctest: +SKIP |
| 257 |
['prod', 'test'] |
| 258 |
|
| 259 |
*Python 2:* |
| 260 |
|
| 261 |
:return: `list` of database names |
| 262 |
:raise: `urllib2.URLError` (connection error) |
| 263 |
|
| 264 |
*Python 3:* |
| 265 |
|
| 266 |
:return: `list` of database names |
| 267 |
:raise: `urllib.error.URLError` (connection error) |
| 268 |
""" |
| 269 |
data = self._odoo.json( |
| 270 |
'/jsonrpc', {'service': 'db', 'method': 'list', 'args': []} |
| 271 |
) |
| 272 |
return data.get('result', []) |
| 273 |
|
| 274 |
def restore(self, password, db, dump, copy=False): |
| 275 |
"""Restore the `dump` database into the new `db` database. |
| 276 |
The `dump` file object can be obtained with the |
| 277 |
:func:`dump <DB.dump>` method. |
| 278 |
If `copy` is set to `True`, the restored database will have a new UUID. |
| 279 |
|
| 280 |
>>> odoo.db.restore('super_admin_passwd', 'test', dump_file) # doctest: +SKIP |
| 281 |
|
| 282 |
If you get a timeout error, increase this one before performing the |
| 283 |
request: |
| 284 |
|
| 285 |
>>> timeout_backup = odoo.config['timeout'] |
| 286 |
>>> odoo.config['timeout'] = 7200 # Timeout set to 2 hours |
| 287 |
>>> odoo.db.restore('super_admin_passwd', 'test', dump_file) # doctest: +SKIP |
| 288 |
>>> odoo.config['timeout'] = timeout_backup |
| 289 |
|
| 290 |
The super administrator password is required to perform this method. |
| 291 |
|
| 292 |
*Python 2:* |
| 293 |
|
| 294 |
:raise: :class:`odoorpc.error.RPCError` |
| 295 |
(access denied / database already exists) |
| 296 |
:raise: :class:`odoorpc.error.InternalError` (dump file closed) |
| 297 |
:raise: `urllib2.URLError` (connection error) |
| 298 |
|
| 299 |
*Python 3:* |
| 300 |
|
| 301 |
:raise: :class:`odoorpc.error.RPCError` |
| 302 |
(access denied / database already exists) |
| 303 |
:raise: :class:`odoorpc.error.InternalError` (dump file closed) |
| 304 |
:raise: `urllib.error.URLError` (connection error) |
| 305 |
""" |
| 306 |
if dump.closed: |
| 307 |
raise error.InternalError("Dump file closed") |
| 308 |
b64_data = base64.standard_b64encode(dump.read()).decode() |
| 309 |
self._odoo.json( |
| 310 |
'/jsonrpc', |
| 311 |
{ |
| 312 |
'service': 'db', |
| 313 |
'method': 'restore', |
| 314 |
'args': [password, db, b64_data, copy], |
| 315 |
}, |
| 316 |
) |