From c59f16ff09af54e6c5b0465ba2a8711075569a2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Grand-Guillaume?= Date: Wed, 12 Oct 2011 15:54:04 +0200 Subject: [PATCH] [ADD] server evironment file + sample (lp:c2c-addons/6.1 rev 11) --- server_environment/__init__.py | 21 ++ server_environment/__openerp__.py | 74 +++++++ server_environment/serv_config.py | 184 ++++++++++++++++++ server_environment/serv_config.xml | 19 ++ server_environment/system_info.py | 62 ++++++ server_environment_files/__init__.py | 20 ++ server_environment_files/__openerp__.py | 37 ++++ server_environment_files/default/base.conf | 10 + server_environment_files/default/mytopic.conf | 15 ++ server_environment_files/dev/base.conf | 8 + server_environment_files/dev/mytopic.conf | 9 + server_environment_files/preprod/mytopic.conf | 7 + server_environment_files/prod/base.conf | 8 + server_environment_files/prod/mytopic.conf | 14 ++ 14 files changed, 488 insertions(+) create mode 100644 server_environment/__init__.py create mode 100644 server_environment/__openerp__.py create mode 100644 server_environment/serv_config.py create mode 100644 server_environment/serv_config.xml create mode 100644 server_environment/system_info.py create mode 100644 server_environment_files/__init__.py create mode 100644 server_environment_files/__openerp__.py create mode 100644 server_environment_files/default/base.conf create mode 100644 server_environment_files/default/mytopic.conf create mode 100644 server_environment_files/dev/base.conf create mode 100644 server_environment_files/dev/mytopic.conf create mode 100644 server_environment_files/preprod/mytopic.conf create mode 100644 server_environment_files/prod/base.conf create mode 100644 server_environment_files/prod/mytopic.conf diff --git a/server_environment/__init__.py b/server_environment/__init__.py new file mode 100644 index 000000000..765b02bda --- /dev/null +++ b/server_environment/__init__.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Adapted by Nicolas Bessi. Copyright Camptocamp SA +# Based on Florent Xicluna original code. Copyright Wingo SA +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +############################################################################## +from .serv_config import serv_config, setboolean diff --git a/server_environment/__openerp__.py b/server_environment/__openerp__.py new file mode 100644 index 000000000..b919f1761 --- /dev/null +++ b/server_environment/__openerp__.py @@ -0,0 +1,74 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Adapted by Nicolas Bessi. Copyright Camptocamp SA +# Based on Florent Xicluna original code. Copyright Wingo SA +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +############################################################################## + +{ + "name": "server configuration environment files", + "version": "1.0", + "depends": ["base", "server_environment_files"], + "author": "Camptocamp", + "description": """This module provides a classical configuration by environnement file pattern into OpenERP. +Based on code written by WinGo and Camptocamp. + +This module allows you to use the classical environment file pattern by reading +a directive call running_env in config file or openerpc. + +[options] +running_env=dev / prod / etc. + +We intended to add a server command line but there is no correct way to do it. + +This method allows you to have your settings into a module instead of using config file that might be mixed with openerprc or altered. +It is an alternative way to config the base config file. +All your configurations will be read_only and accessible under the admin menu. +If you are not in the 'dev' environment you will not be able to see the values containing 'passw' in key. + +At the current time, the module does not allow to set low level attributes such as database server, etc. . + +The first goal of the module is to ensure that OpenERP will never mess up the external system. +Once installed, profile is mandatory. We do not want to launch an instance in the dev environment on a production server. + + + +The configuration files are to put in the module server_environment_files ; they are using the config parser module syntax. +Look at the module to get some sexamples. +The default configuration are to put in the default folder. All config defined in other environment will be overwritten or added to default one. +Then, you can add a folder by used environment with the name of the environment +If your attibutes contain passw it will only be shown in dev environment. + +Usage samples: + +from server_environment import serv_config +for key, value in serv_config.items('external_service.ftp'): + print (key, value) + + +serv_config.get('external_service.ftp', 'tls') + """, + "website": "http://www.camptocamp.com", + "category": "Tools", + "init_xml": [], + "demo_xml": [], + "update_xml": [ + 'serv_config.xml', + ], + "installable": True, + "active": False, +} diff --git a/server_environment/serv_config.py b/server_environment/serv_config.py new file mode 100644 index 000000000..dc0224dd3 --- /dev/null +++ b/server_environment/serv_config.py @@ -0,0 +1,184 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Adapted by Nicolas Bessi. Copyright Camptocamp SA +# Based on Florent Xicluna original code. Copyright Wingo SA +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +############################################################################## + +import os +import ConfigParser + +from lxml import etree + +from osv import osv, fields +from tools.config import config as system_base_config + +from .system_info import get_server_environment + +import server_environment_files +_dir = os.path.dirname(server_environment_files.__file__) + +# Same dict as RawConfigParser._boolean_states +_boolean_states = {'1': True, 'yes': True, 'true': True, 'on': True, + '0': False, 'no': False, 'false': False, 'off': False} + +if not system_base_config.get('running_env', False): + raise Exception( + ("The parameter 'running_env' has not be set neither in base config file option -c or in openerprc.\n" + "We strongly recommand you not to use the rc file but instead use an explicite config file with this content : \n" + "[options] \nrunning_env = dev") + ) + +ck_path = os.path.join(_dir, system_base_config['running_env']) + +if not os.path.exists(ck_path) : + raise Exception( + "Provided server environment does not exists please add a folder %s"%s(ck_path) + ) + +def setboolean(obj, attr, _bool=_boolean_states): + """Replace the attribute with a boolean.""" + res = _bool[getattr(obj, attr).lower()] + setattr(obj, attr, res) + return res + + +# Borrowed from MarkupSafe +def _escape(s): + """Convert the characters &<>'" in string s to HTML-safe sequences.""" + return (str(s).replace('&', '&') + .replace('>', '>') + .replace('<', '<') + .replace("'", ''') + .replace('"', '"')) + + +def _listconf(env_path): + """List configuration files in a folder.""" + files = [os.path.join(env_path, name) + for name in sorted(os.listdir(env_path)) + if name.endswith('.conf')] + return files + + +def _load_config(): + """Load the configuration and return a ConfigParser instance.""" + default = os.path.join(_dir, 'default') + running_env = os.path.join(_dir, system_base_config['running_env']) + if os.path.isdir(default): + conf_files = _listconf(default) + _listconf(running_env) + else: + conf_files = _listconf(running_env) + + config_p = ConfigParser.SafeConfigParser() + # options are case-sensitive + config_p.optionxform = str + try: + config_p.read(conf_files) + except Exception, e: + raise Exception('Cannot read config files "%s": %s' % (conf_files, e)) + + return config_p + +serv_config = _load_config() + + +class _Defaults(dict): + __slots__ = () + + def __setitem__(self, key, value): + func = lambda *a: str(value) + return dict.__setitem__(self, key, func) + + +class ServerConfiguration(osv.osv_memory): + """Display server configuration.""" + _name = 'server.config' + _columns = {} + _defaults = _Defaults() + + def __init__(self, pool, cr): + res = super(ServerConfiguration, self).__init__(pool, cr) + self.running_env = system_base_config['running_env'] + # Only show passwords in development + self.show_passwords = self.running_env in ('dev',) + self._build_osv() + return res + + def _group(self, items, prefix): + """Return an XML chunk which represents a group of fields.""" + names = [] + for k, v in items: + key = '%s\\%s' % (prefix, k) + # Mask passwords + if 'passw' in k and not self.show_passwords: + v = '**********' + # for the GTK display, we need to replace '_' with '__'. + # XXX: remove this hack when we switch to the web client. + k = k.replace('_', '__') + self._columns[key] = fields.char(k, size=1024) + self._defaults[key] = v + names.append(key) + + return ('' + + ''.join(['' % + _escape(name) for name in names]) + + '') + + def _build_osv(self): + """Build the view for the current configuration.""" + arch = ('' + '
' + '') + + # OpenERP server configuration + rcfile = system_base_config.rcfile + items = sorted(system_base_config.options.items()) + arch += '' + arch += '' % _escape(rcfile) + arch += self._group(items, prefix='openerp') + arch += '' + + arch += '' + for section in sorted(serv_config.sections()): + items = sorted(serv_config.items(section)) + arch += '' % _escape(section) + arch += self._group(items, prefix=section) + arch += '' + + # System information + arch += '' + arch += '' + arch += self._group(get_server_environment(), prefix='system') + arch += '' + + arch += '
' + self._arch = etree.fromstring(arch) + + def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False): + """Overwrite the default method to render the custom view.""" + res = super(ServerConfiguration, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar) + if view_type == 'form': + arch_node = self._arch + xarch, xfields = self._view_look_dom_arch(cr, uid, arch_node, view_id, context=context) + res['arch'] = xarch + res['fields'] = xfields + return res + + # TODO: button action_reload + +ServerConfiguration() diff --git a/server_environment/serv_config.xml b/server_environment/serv_config.xml new file mode 100644 index 000000000..7227b67b3 --- /dev/null +++ b/server_environment/serv_config.xml @@ -0,0 +1,19 @@ + + + + + + + + + + diff --git a/server_environment/system_info.py b/server_environment/system_info.py new file mode 100644 index 000000000..e93226372 --- /dev/null +++ b/server_environment/system_info.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Adapted by Nicolas Bessi. Copyright Camptocamp SA +# Based on Florent Xicluna original code. Copyright Wingo SA +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +############################################################################## + +import locale +import os +import platform +import subprocess + +import release +from tools.config import config + + +def _get_output(cmd): + bindir = config['root_path'] + p = subprocess.Popen(cmd, shell=True, cwd=bindir, stdout=subprocess.PIPE) + return p.communicate()[0].rstrip() + + +def get_server_environment(): + # inspired by server/bin/service/web_services.py + try: + rev_id = _get_output('bzr revision-info') + except Exception, e: + rev_id = 'Exception: %s' % (e,) + + os_lang = '.'.join([x for x in locale.getdefaultlocale() if x]) + if not os_lang: + os_lang = 'NOT SET' + if os.name == 'posix' and platform.system() == 'Linux': + lsbinfo = _get_output('lsb_release -a') + else: + lsbinfo = 'not lsb compliant' + return ( + ('platform', platform.platform()), + ('os.name', os.name), + ('lsb_release', lsbinfo), + ('release', platform.release()), + ('version', platform.version()), + ('architecture', platform.architecture()[0]), + ('locale', os_lang), + ('python', platform.python_version()), + ('openerp', release.version), + ('revision', rev_id), + ) diff --git a/server_environment_files/__init__.py b/server_environment_files/__init__.py new file mode 100644 index 000000000..31fea2953 --- /dev/null +++ b/server_environment_files/__init__.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Author Nicolas Bessi. Copyright Camptocamp SA +# Author Florent Xicluna. Copyright Wingo SA +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +############################################################################## \ No newline at end of file diff --git a/server_environment_files/__openerp__.py b/server_environment_files/__openerp__.py new file mode 100644 index 000000000..58a7fdc0c --- /dev/null +++ b/server_environment_files/__openerp__.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Adapted by Nicolas Bessi. Copyright Camptocamp SA +# Based on Florent Xicluna original code. Copyright Wingo SA +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +############################################################################## + +{ + "name": "server configuration environment files repository module", + "version": "1.0", + "depends": ["base"], + "author": "Camptocamp", + "description": """This module provides a file store for classical configuration by environnement file pattern into OpenERP provided by server_environment. + It is ment to be used by server_environment module. Please look at this module for more info and doc. + """, + "website": "http://www.camptocamp.com", + "category": "Tools", + "init_xml": [], + "demo_xml": [], + "update_xml": [], + "installable": True, + "active": False, +} diff --git a/server_environment_files/default/base.conf b/server_environment_files/default/base.conf new file mode 100644 index 000000000..bf92248f9 --- /dev/null +++ b/server_environment_files/default/base.conf @@ -0,0 +1,10 @@ +[misc] +completed_state = 13 +smtp_server = xxx.xxx.ch + +[custom_ged] +ged_folder = /my_mounting_point/ged/ + +[wkhtml2pdf] +lib_path = /xxx/xxx/lib/wkhtmltopdf-linux-i386-0-9-9 + diff --git a/server_environment_files/default/mytopic.conf b/server_environment_files/default/mytopic.conf new file mode 100644 index 000000000..c896c902d --- /dev/null +++ b/server_environment_files/default/mytopic.conf @@ -0,0 +1,15 @@ +[external_service] +CONSTA = D01 +CONSTB = 1 +CONSTC = -23 + + +[external_service.ftp] +server = 127.0.0.1 +in_path = /in/ +out_path= /out/ +user = toto +password = my_dev_password +tls = 0 +port = 8074 + diff --git a/server_environment_files/dev/base.conf b/server_environment_files/dev/base.conf new file mode 100644 index 000000000..72f754176 --- /dev/null +++ b/server_environment_files/dev/base.conf @@ -0,0 +1,8 @@ +[misc] +smtp_server = dev.xxx.xxx.ch + +[custom_ged] +ged_folder = /tmp/ged/ + +[wkhtml2pdf] +lib_path = /myHome/lib/wkhtmltopdf-linux-i386-0-9-9 diff --git a/server_environment_files/dev/mytopic.conf b/server_environment_files/dev/mytopic.conf new file mode 100644 index 000000000..00cfb03a3 --- /dev/null +++ b/server_environment_files/dev/mytopic.conf @@ -0,0 +1,9 @@ +[external_service] +CONSTC = -25 + + +[external_service.ftp] +user = toto +password = toto + + diff --git a/server_environment_files/preprod/mytopic.conf b/server_environment_files/preprod/mytopic.conf new file mode 100644 index 000000000..94a0c21b5 --- /dev/null +++ b/server_environment_files/preprod/mytopic.conf @@ -0,0 +1,7 @@ +[external_service] +CONSTC = -25 + + +[external_service.ftp] +user = toto +password = preprod_toto diff --git a/server_environment_files/prod/base.conf b/server_environment_files/prod/base.conf new file mode 100644 index 000000000..08d04b26e --- /dev/null +++ b/server_environment_files/prod/base.conf @@ -0,0 +1,8 @@ +[misc] +smtp_server = prod.xxx.xxx.ch + +[custom_ged] +ged_folder = /my_mounting_point/ged/ + +[wkhtml2pdf] +lib_path = /xxx/xxx/lib/wkhtmltopdf-linux-i386-0-9-9 \ No newline at end of file diff --git a/server_environment_files/prod/mytopic.conf b/server_environment_files/prod/mytopic.conf new file mode 100644 index 000000000..71fb81d84 --- /dev/null +++ b/server_environment_files/prod/mytopic.conf @@ -0,0 +1,14 @@ +[external_service] +CONSTA = D01 +CONSTB = 1 +CONSTC = -23 + + +[external_service.ftp] +server = my_prod_server +in_path = /in/ +out_path= /out/ +user = prod_user +password = my_prod_password +tls = 0 +port = 21 \ No newline at end of file