Coverage for adhoc-cicd-odoo-odoo / odoo / cli / server.py: 70%

74 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-09 18:15 +0000

1# Part of Odoo. See LICENSE file for full copyright and licensing details. 

2 

3""" 

4OpenERP - Server 

5OpenERP is an ERP+CRM program for small and medium businesses. 

6 

7The whole source code is distributed under the terms of the 

8GNU Public Licence. 

9 

10(c) 2003-TODAY, Fabien Pinckaers - OpenERP SA 

11""" 

12 

13import atexit 

14import logging 

15import os 

16import sys 

17 

18from psycopg2.errors import InsufficientPrivilege 

19 

20from odoo.release import author as __author__ # noqa: F401 

21from odoo.release import version as __version__ # noqa: F401 

22from odoo.service import server 

23from odoo.tools import config 

24 

25from . import Command 

26 

27# Also use the `odoo` logger for the main script. 

28_logger = logging.getLogger('odoo') 

29 

30 

31def check_root_user(): 

32 """Warn if the process's user is 'root' (on POSIX system).""" 

33 if os.name == 'posix' and os.getuid() == 0: 33 ↛ 34line 33 didn't jump to line 34 because the condition on line 33 was never true

34 sys.stderr.write("Running as user 'root' is a security risk.\n") 

35 

36 

37def check_postgres_user(): 

38 """ Exit if the configured database user is 'postgres'. 

39 

40 This function assumes the configuration has been initialized. 

41 """ 

42 if (config['db_user'] or os.environ.get('PGUSER')) == 'postgres': 42 ↛ 43line 42 didn't jump to line 43 because the condition on line 42 was never true

43 sys.stderr.write("Using the database user 'postgres' is a security risk, aborting.") 

44 sys.exit(1) 

45 

46def report_configuration(): 

47 """ Log the server version and some configuration values. 

48 

49 This function assumes the configuration has been initialized. 

50 """ 

51 import odoo.addons # noqa: PLC0415 

52 import odoo.release # noqa: PLC0415 

53 _logger.info("Odoo version %s", odoo.release.version) 

54 if os.path.isfile(config['config']): 54 ↛ 56line 54 didn't jump to line 56 because the condition on line 54 was always true

55 _logger.info("Using configuration file at %s", config['config']) 

56 _logger.info('addons paths: %s', odoo.addons.__path__) 

57 if config.get('upgrade_path'): 57 ↛ 58line 57 didn't jump to line 58 because the condition on line 57 was never true

58 _logger.info('upgrade path: %s', config['upgrade_path']) 

59 if config.get('pre_upgrade_scripts'): 59 ↛ 60line 59 didn't jump to line 60 because the condition on line 59 was never true

60 _logger.info('extra upgrade scripts: %s', config['pre_upgrade_scripts']) 

61 host = config['db_host'] or os.environ.get('PGHOST', 'default') 

62 port = config['db_port'] or os.environ.get('PGPORT', 'default') 

63 user = config['db_user'] or os.environ.get('PGUSER', 'default') 

64 _logger.info('database: %s@%s:%s', user, host, port) 

65 replica_host = config['db_replica_host'] 

66 replica_port = config['db_replica_port'] 

67 if replica_host or replica_port or 'replica' in config['dev_mode']: 67 ↛ 68line 67 didn't jump to line 68 because the condition on line 67 was never true

68 _logger.info('replica database: %s@%s:%s', user, replica_host or 'default', replica_port or 'default') 

69 if sys.version_info[:2] > odoo.release.MAX_PY_VERSION: 69 ↛ 70line 69 didn't jump to line 70 because the condition on line 69 was never true

70 _logger.warning("Python %s is not officially supported, please use Python %s instead", 

71 '.'.join(map(str, sys.version_info[:2])), 

72 '.'.join(map(str, odoo.release.MAX_PY_VERSION)) 

73 ) 

74 

75def rm_pid_file(main_pid): 

76 if config['pidfile'] and main_pid == os.getpid(): 

77 try: 

78 os.unlink(config['pidfile']) 

79 except OSError: 

80 pass 

81 

82def setup_pid_file(): 

83 """ Create a file with the process id written in it. 

84 

85 This function assumes the configuration has been initialized. 

86 """ 

87 import odoo # for evented # noqa: PLC0415 

88 if not odoo.evented and config['pidfile']: 88 ↛ 89line 88 didn't jump to line 89 because the condition on line 88 was never true

89 pid = os.getpid() 

90 with open(config['pidfile'], 'w') as fd: 

91 fd.write(str(pid)) 

92 atexit.register(rm_pid_file, pid) 

93 

94 

95def main(args): 

96 check_root_user() 

97 config.parse_config(args, setup_logging=True) 

98 check_postgres_user() 

99 report_configuration() 

100 

101 for db_name in config['db_name']: 

102 from odoo.service import db # noqa: PLC0415 

103 try: 

104 db._create_empty_database(db_name) 

105 config['init']['base'] = True 

106 except InsufficientPrivilege as err: 

107 # We use an INFO loglevel on purpose in order to avoid 

108 # reporting unnecessary warnings on build environment 

109 # using restricted database access. 

110 _logger.info("Could not determine if database %s exists, " 

111 "skipping auto-creation: %s", db_name, err) 

112 except db.DatabaseExists: 

113 pass 

114 

115 stop = config["stop_after_init"] 

116 

117 setup_pid_file() 

118 rc = server.start(preload=config['db_name'], stop=stop) 

119 sys.exit(rc) 

120 

121 

122class Server(Command): 

123 """Start the odoo server (default command)""" 

124 

125 def run(self, args): 

126 config.parser.prog = self.prog 

127 main(args)