Port server environment to version 8.0

This commit is contained in:
Nicolas Bessi 2014-09-09 14:11:45 +02:00 committed by Marcos Oitaben
parent 9ba8b7b59c
commit 4e77ccb509
5 changed files with 143 additions and 23 deletions

View File

@ -21,7 +21,7 @@
{
"name": "server configuration environment files",
"version": "1.0",
"version": "1.1",
"depends": ["base", "server_environment_files"],
"author": "Camptocamp",
"description": """\

View File

@ -22,8 +22,9 @@
import os
import ConfigParser
from lxml import etree
from itertools import chain
from openerp.osv import fields, orm
from openerp import models, fields
from openerp.tools.config import config as system_base_config
from .system_info import get_server_environment
@ -53,8 +54,10 @@ if not os.path.exists(ck_path):
)
def setboolean(obj, attr, _bool=_boolean_states):
def setboolean(obj, attr, _bool=None):
"""Replace the attribute with a boolean."""
if _bool is None:
_bool = dict(_boolean_states)
res = _bool[getattr(obj, attr).lower()]
setattr(obj, attr, res)
return res
@ -109,13 +112,17 @@ class _Defaults(dict):
return dict.__setitem__(self, key, func)
class ServerConfiguration(orm.TransientModel):
class ServerConfiguration(models.TransientModel):
"""Display server configuration."""
_name = 'server.config'
_columns = {}
_conf_defaults = _Defaults()
def __init__(self, pool, cr):
"""Add columns to model dynamically
and init some properties
"""
self._add_columns()
super(ServerConfiguration, self).__init__(pool, cr)
self.running_env = system_base_config['running_env']
# Only show passwords in development
@ -123,18 +130,55 @@ class ServerConfiguration(orm.TransientModel):
self._arch = None
self._build_osv()
def _group(self, items, prefix):
def _format_key(self, section, key):
return '%s | %s' % (section, key)
def _add_columns(self):
"""Add columns to model dynamically"""
cols = chain(
self._get_base_cols().items(),
self._get_env_cols().items(),
self._get_system_cols().items()
)
for col, value in cols:
col_name = col.replace('.', '_')
setattr(ServerConfiguration,
col_name,
fields.Char(string=col, readonly=True))
self._conf_defaults[col_name] = value
def _get_base_cols(self):
""" Compute base fields"""
res = {}
for col, item in system_base_config.options.items():
key = self._format_key('openerp', col)
res[key] = item
return res
def _get_env_cols(self, sections=None):
""" Compute base fields"""
res = {}
sections = sections if sections else serv_config.sections()
for section in sections:
for col, item in serv_config.items(section):
key = self._format_key(section, col)
res[key] = item
return res
def _get_system_cols(self):
""" Compute system fields"""
res = {}
for col, item in get_server_environment():
key = self._format_key('system', col)
res[key] = item
return res
def _group(self, items):
"""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 = '**********'
self._columns[key] = fields.char(k, size=1024)
self._conf_defaults[key] = v
names.append(key)
for key in sorted(items):
names.append(key.replace('.', '_'))
return ('<group col="2" colspan="4">' +
''.join(['<field name="%s" readonly="1"/>' %
_escape(name) for name in names]) +
@ -148,23 +192,23 @@ class ServerConfiguration(orm.TransientModel):
# OpenERP server configuration
rcfile = system_base_config.rcfile
items = sorted(system_base_config.options.items())
items = self._get_base_cols()
arch += '<page string="OpenERP">'
arch += '<separator string="%s" colspan="4"/>' % _escape(rcfile)
arch += self._group(items, prefix='openerp')
arch += self._group(items)
arch += '<separator colspan="4"/></page>'
arch += '<page string="Environment based configurations">'
for section in sorted(serv_config.sections()):
items = sorted(serv_config.items(section))
items = self._get_env_cols(sections=[section])
arch += '<separator string="[%s]" colspan="4"/>' % _escape(section)
arch += self._group(items, prefix=section)
arch += self._group(items)
arch += '<separator colspan="4"/></page>'
# System information
arch += '<page string="System">'
arch += '<separator string="Server Environment" colspan="4"/>'
arch += self._group(get_server_environment(), prefix='system')
arch += self._group(self._get_system_cols())
arch += '<separator colspan="4"/></page>'
arch += '</notebook></form>'
@ -191,5 +235,8 @@ class ServerConfiguration(orm.TransientModel):
def default_get(self, cr, uid, fields_list, context=None):
res = {}
for key in self._conf_defaults:
res[key] = self._conf_defaults[key]()
if 'passw' in key and not self.show_passwords:
res[key] = '**********'
else:
res[key] = self._conf_defaults[key]()
return res

View File

@ -37,9 +37,12 @@ def _get_output(cmd):
def get_server_environment():
# inspired by server/bin/service/web_services.py
try:
rev_id = _get_output('bzr revision-info')
except Exception as e:
rev_id = 'Exception: %s' % (e,)
rev_id = 'git:%s' % _get_output('git rev-parse HEAD')
except Exception:
try:
rev_id = 'bzr: %s' % _get_output('bzr revision-info')
except Exception:
rev_id = 'Can not retrive revison from git or bzr'
os_lang = '.'.join([x for x in locale.getdefaultlocale() if x])
if not os_lang:

View File

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Nicolas Bessi
# Copyright 2014 Camptocamp SA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from . import test_server_environment
checks = [test_server_environment]

View File

@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Nicolas Bessi
# Copyright 2014 Camptocamp SA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp.tests import common
from openerp.addons.server_environment import serv_config
class TestEnv(common.TransactionCase):
def test_view(self):
model = self.env['server.config']
view = model.fields_view_get()
self.assertTrue(view)
def test_default(self):
model = self.env['server.config']
rec = model.create({})
defaults = rec.default_get([])
self.assertTrue(defaults)
self.assertIsInstance(defaults, dict)
pass_checked = False
for default in defaults:
if 'passw' in default:
self.assertNotEqual(defaults[default],
'**********')
pass_checked = True
self.assertTrue(pass_checked)
def test_value_retrival(self):
val = serv_config.get('external_service.ftp', 'user')
self.assertEqual(val, 'toto')