Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cie_infra/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-

from . import controllers
from . import models
43 changes: 43 additions & 0 deletions cie_infra/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
{
'name': "Coop IT Easy Infrastructure",

'summary': """
this module allows to visualize our servers throught an inventary. """,

'description': """
The purpose of this module is to visualize all the information
contains on our server. It means the datacenter, the location,
the different instance on it and finally the different module.
""",

'author': "CoopItEasy",
'website': "http://coopiteasy.be",
"license": "AGPL-3",

# Categories can be used to filter modules in modules listing
# Check https://github.com/odoo/odoo/blob/12.0/odoo/addons/base/data/ir_module_category_data.xml
# for the full list
#'category': 'Uncategorized',
#'version': '12.0.1.0.0',

# any module necessary for this one to work correctly
'depends': ['base'],

# always loaded
'data': [
'security/cie_infra_security.xml',
'security/ir.model.access.csv',
'views/views.xml',
'views/cie_infra_server_views.xml',
'views/cie_infra_instance_view.xml',
'views/cie_infra_database_view.xml',
'views/cie_infra_module_view.xml',
'views/cie_infra_datacenter_view.xml',
'views/server_list_template.xml'
],
# only loaded in demonstration mode
#'demo': [
# 'demo/demo.xml',
#],
}
3 changes: 3 additions & 0 deletions cie_infra/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-

from . import controllers
19 changes: 19 additions & 0 deletions cie_infra/controllers/controllers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from odoo import http

class CieInfra(http.Controller):
#@http.route('/cie_infra/index', auth='public')
#def index(self, **kw):
# return "Hello, world"

@http.route('/cie_infra/server/', auth='public')
def list_server(self, **kw):
server=http.request.env['infra.server']
servers=server.search([])
return http.request.render('cie_infra.server_list_template', {
'servers': servers
})
# @http.route('/cie_infra/cie_infra/objects/<model("cie_infra.cie_infra"):obj>/', auth='public')
# def object(self, obj, **kw):
# return http.request.render('cie_infra.object', {
# 'object': obj
# })
37 changes: 37 additions & 0 deletions cie_infra/demo/demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0"?>
<odoo noupdate="1">
<data>

<record id="server_localhost:0101" model="cie_infra.server">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'ai un petit doute sur l'utilisation des : dans l'id d'un record. Je mettrais plutôt un -, sauf si tu as trouver d'autres exemple qui font ça. :)

<field name="name">localhost:0101</field>
<field name="ipv4">192.168.1.1</field>
<field name="datacenter_info">OVH plus grand base de serveur d'EUROPE</field>
<field name="ovh_address"> [email protected]</field>
<field name="cie_address">[email protected]</field>
<field name="note">pas de pb depuis 6mois</field>
</record>

<record id="location_france" model="infra.location">
<field name="name">France</field>
<field name="country_id">208</field>
</record>

<record id="datacenter_OVH1" model="cie_infra.datacenter">
<field name="name">OVH Compiegne</field>
<field name="location_id"
eval="[(4, ref('location_france'))]"/>
</record>
<record id="instance1" model="cie_infra.instance">
<field name="name">OdooV1</field>
<field name="URL">CoopItEasy/odooV1</field>
<field name="note">Instance utilisé par coopvelib et coopvrap</field>
<field name="server_id"
eval="[(4,ref('server_localhost:0101')]"/>
</record>
<!-- <record id="object4" model="cie_infra.cie_infra"> -->
<!-- <field name="name">Object 4</field> -->
<!-- <field name="value">40</field> -->
<!-- </record> -->
<!-- -->
</data>
</odoo>
8 changes: 8 additions & 0 deletions cie_infra/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from . import infra_server
from . import infra_datacenter
from . import infra_location
from . import infra_instance
from . import infra_instance_database
from . import infra_instance_module_info
from . import infra_instance_module
from . import infra_server_shortname
9 changes: 9 additions & 0 deletions cie_infra/models/infra_datacenter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from odoo import models, fields


class Datacenter(models.Model):
_name = 'infra.datacenter'
_description = 'datacenter'
name = fields.Char('Datacenter', required=True)
location_id = fields.Many2one('infra.location', string='location')
server_ids = fields.One2many('infra.server', 'datacenter_id', string='Server')
81 changes: 81 additions & 0 deletions cie_infra/models/infra_instance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from odoo import models
from odoo import fields
from odoo import api
from odoo import _
import requests
import json
from werkzeug.exceptions import BadRequest, InternalServerError, NotFound
from odoo.exceptions import AccessDenied, Warning as UserError


class Instance(models.Model):
_name='infra.instance'
_description='instance'
name=fields.Char('name', required=True)
url=fields.Char('URL')
note=fields.Text('text')
server_id=fields.Many2one('infra.server', string='server')
database_ids=fields.One2many('infra.instance.database','instance_id', string="database")

@api.multi
def http_get(self, url, params, headers):
self.ensure_one()
if url.startswith("/"):
url = self.url + url
return requests.get(url,params,headers)

def _process_response(self, response):
if response.status_code == 200:
content = response.content.decode("utf-8")
return json.loads(content)
elif response.status_code == 400:
content = response.content.decode("utf-8")
raise BadRequest("%s" % content)
elif response.status_code == 403:
raise AccessDenied(
_("You are not allowed to access this resource")
)
elif response.status_code == 404:
raise NotFound(
_("Resource not found %s on server" % response.status_code)
)
else: # 500 et al.
content = response.content.decode("utf-8")
raise InternalServerError(_("%s" % content))

@api.multi
def http_get_content(self, url, params=None, headers=None):
self.ensure_one()
response = self.http_get(url, params=params, headers=headers)
return self._process_response(response)


def cron_get_database(self):
"""
thanks to http request, the cron will recover daily
the different database based on a odoo's instance
"""

route="/web/databases"
instances = self.env['infra.instance'].search([])
for instance in instances:
databases = instance.http_get_content(route)
for db in databases["databases"]:
if not instance.is_db_already_exist(db) :
self.env["infra.instance.database"].create({
"name": db,
"instance_id": instance.id,
})
instance.write({
"database_ids": db,
})
Comment on lines +64 to +71
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Il manque un traitement else dans le cas ou la DB existe déjà. Si elle existe déjà, il faudrait s'assuré qu'elle soit bien liée à la bonne instance (par ex: une base de donnée qui a été bougée d'une instance à une autre).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

est ce possible qu'on supprime une db d'une instance ? Si oui il faut aussi que je gere le cas ou une db a été supprimer et ainsi supprimer le lien entre cette instance et la db

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a checker


def is_db_already_exist(self, new_db):
databases=self.env["infra.instance.database"].search([])
for old_db in databases:
if new_db == old_db.name :
return 1
return 0
Comment on lines +75 to +78
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tu peux simplement écrire :

return new_db in databases

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

je ne comprends pas trop cette manière d'écrire, pour moi ce n'est pas l'équivalent de ce que j'ai écrit plus haut.




89 changes: 89 additions & 0 deletions cie_infra/models/infra_instance_database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from odoo import models
from odoo import fields
from odoo import api
from odoo import _
import requests
import json
from werkzeug.exceptions import BadRequest, InternalServerError, NotFound
from odoo.exceptions import AccessDenied, Warning as UserError

class Database(models.Model):
_name = 'infra.instance.database'
_description = 'database'
name=fields.Char('name', required=True)
instance_id=fields.Many2one('infra.instance', string='instance')
installed_module_ids=fields.One2many(
comodel_name='infra.instance.module.info',
inverse_name='database_id',
string='installed_module')

@api.multi
def http_get(self, url, params, headers):
self.ensure_one()
if url.startswith("/"):
url = self.url + url
return requests.get(url,params,headers)

def _process_response(self, response):
if response.status_code == 200:
content = response.content.decode("utf-8")
return json.loads(content)
elif response.status_code == 400:
content = response.content.decode("utf-8")
raise BadRequest("%s" % content)
elif response.status_code == 403:
raise AccessDenied(
_("You are not allowed to access this resource")
)
elif response.status_code == 404:
raise NotFound(
_("Resource not found %s on server" % response.status_code)
)
else: # 500 et al.
content = response.content.decode("utf-8")
raise InternalServerError(_("%s" % content))
Comment on lines +27 to +44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On pourrait réfléchir pour n'avoir ce code que dans un seul modèle, ou dans un fichier python à part. Car pas besoin de répéter le même code dans chaque modèle.


@api.multi
def http_get_content(self, url, params=None, headers=None):
self.ensure_one()
response = self.http_get(url, params=params, headers=headers)
return self._process_response(response)


def cron_get_module(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Il faudra traiter dans cette fonction le cas ou le infra.instance.module existe déjà, mais n'est pas encore lié à cette base de données par un infra.instance.module.info.

"""
thanks to http request, the cron will recover daily
the different database based on a odoo's instance
"""

databases = self.env['infra.instance.database'].search([])
for database in databases:
route = "/server-info/databases/"+database.name+"/modules"
modules = database.http_get_content(route)
for module in modules:
if not self.is_module_already_installed(module):
new_module = self.env['infra.instance.module'].create({
"name": module["name"],
})
module_info=self.env["infra.instance.module.info"].create({
"installed_version": module["published_version"],
})
new_module.write({
"module_info_ids": module_info.id,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attention, ici module_info_ids contient plusieurs module.info. Donc comme tu fais là, tu supprimes tous les modules_info, et tu en lie uniquement un.

Aussi, tu dois passer l'objet directement et pas juste l'id de l'objet. :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d'apres la documentation odoo, je trouve cette options pour la fonction write :
(0, 0, values) adds a new record created from the provided value dict.
''' updated_module=[0,0,{"module_info_ids" : module_info}]
new_module.write(
updated_module
)'''

est ce la solution ou je dois override la function write pour lui donner le bon comportement?

})
module_info.write({
"module_id":new_module.id,
})
database.write({
"installed_module_ids": module_info.id,
})
return

def is_module_already_installed(self,module):
modules = self.env["infra.instance.module"].search([])
for old_module in modules:
if module["name"] == old_module.name:
return 1
return 0
Comment on lines +83 to +87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem que pour l'aurtre fonction similaire.

return module in modules



8 changes: 8 additions & 0 deletions cie_infra/models/infra_instance_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from odoo import fields, models

class Module(models.Model):
_name='infra.instance.module'
_description='module'
name=fields.Char('name',required=True)
module_info_ids=fields.One2many('infra.instance.module.info','module_id',string='module_info')

11 changes: 11 additions & 0 deletions cie_infra/models/infra_instance_module_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from odoo import fields , models

class ModuleInfo(models.Model):
_name='infra.instance.module.info'
_description='module.info'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We used to leave a empty line between _xxx attribute and the others. :)

installed_version=fields.Char('installed_version',required=True)
available_version=fields.Char('available_version')
database_id=fields.Many2one('infra.instance.database',string='database')
module_id=fields.Many2one('infra.instance.module',string='module')


8 changes: 8 additions & 0 deletions cie_infra/models/infra_location.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from odoo import fields, models

class Location(models.Model):
_name='infra.location'
_description='location'
name=fields.Char('name', required=True)
country_id=fields.Many2one('res.country', string='country')
datacenter_ids=fields.One2many('infra.datacenter','location_id', string='datacenter')
54 changes: 54 additions & 0 deletions cie_infra/models/infra_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from odoo import fields, models, api
from odoo.exceptions import Warning


class Server(models.Model):
_name = 'infra.server'
_description = 'server'
name = fields.Char('name', required=True)
ipv4 = fields.Char('ipv4')
ipv6 = fields.Char('ipv6')
datacenter_info = fields.Char('Datacenter info')
ovh_address = fields.Char('OVH\'s adress', required=True)
cie_address = fields.Char('cie adress', required=True)
note = fields.Text('Note')
datacenter_id = fields.Many2one('infra.datacenter', string='Datacenter')
shortname_ids = fields.One2many('infra.server.shortname', 'server_id', string='shortname')
instance_ids = fields.One2many('infra.instance', 'server_id', string='instance')

@api.multi
def _check_ipv4(self, IP):
def isIPv4(s):
try:
return str(int(s)) == s and 0 <= int(s) <= 255
except:
return False

if IP.count(".") == 3 and all(isIPv4(i) for i in IP.split(".")):
return True
return False
Comment on lines +27 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Écrit simplement :

return IP.count(".") == 3 and all(isIPv4(i) for i in IP.split("."))


@api.multi
def _check_ipv6(self, IP):
def isIPv6(s):
if len(s) > 4:
return False
try:
return int(s, 16) >= 0 and s[0] != '-'
except:
return False

if IP.count(":") == 7 and all(isIPv6(i) for i in IP.split(":")):
return True
return False

@api.multi
def button_check_ipv(self):
for server in self:
if not server.ipv4 and not server.ipv6:
raise Warning('Please provide an ipv4 or ipv6 for %s' % server.name)
if server.ipv6 and not server._check_ipv6(self.ipv6):
raise Warning('%s is an invalid ipv6 address' % server.ipv6)
if server.ipv4 and not server._check_ipv4(self.ipv4):
raise Warning('%s is an invalid ipv4 address' % server.ipv4)
return True
8 changes: 8 additions & 0 deletions cie_infra/models/infra_server_shortname.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from odoo import fields, models

class ServerShortName(models.Model):
_name='infra.server.shortname'
_description='server.shortname'
name=fields.Char('name')
server_id=fields.Many2one('infra.server',string='server')

Loading