system_instance.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # -*- coding: utf-8 -*-
  2. from openerp import models, fields, api
  3. from openerp.tools import config
  4. from openerp.exceptions import Warning
  5. # from random import randint
  6. # from jinja2 import Environment, PackageLoader
  7. # from subprocess import check_output
  8. # from ..utils import docker_api
  9. # import os
  10. import unicodedata
  11. import stringcase
  12. # import time
  13. # print(config['docker_sock'])
  14. class SystemInstance(models.Model):
  15. _name = 'system.instance'
  16. CONTAINER_STATUS = [
  17. ('draft', 'Por activar'),
  18. ('activated', 'Activado'),
  19. ('disapproved', 'No aprobado'),
  20. ('suspended', 'Suspendido'),
  21. ('destroyed', 'Eliminado')
  22. ]
  23. name = fields.Char(string='Nombre', size=50)
  24. normalized_name = fields.Char(string='Nombre normalizado', compute='_normalize_name', size=50)
  25. logo = fields.Binary(string='Logo')
  26. internal_ip = fields.Char(string='IP interno', size=15)
  27. internal_port = fields.Integer(string='Puerto interno')
  28. external_ip = fields.Char(string='IP externo', size=15)
  29. external_port = fields.Integer(string='Puerto externo')
  30. expose_ip = fields.Boolean(string='Exponer IP', default=True)
  31. state = fields.Selection(string='Estado', selection=CONTAINER_STATUS, default='draft')
  32. demo = fields.Boolean(string='Es un demo?', default=False)
  33. domain = fields.Char(string='Dominio', size=100)
  34. running = fields.Boolean(string='Está online?', default=False)
  35. payment_plan_id = fields.Many2one(string='Plan de pago', comodel_name='payment.plan', required=True)
  36. @api.one
  37. @api.onchange('name')
  38. def _onchange_name(self):
  39. if self.name:
  40. self.name = self.name.title()
  41. @api.one
  42. @api.depends('name')
  43. def _normalize_name(self):
  44. name = None
  45. try:
  46. name = unicodedata.normalize('NFKD', self.name)
  47. name = name.encode('ASCII', 'ignore')
  48. except TypeError:
  49. pass
  50. if not name:
  51. return
  52. name = stringcase.trimcase(name)
  53. name = stringcase.lowercase(name)
  54. self.normalized_name = stringcase.snakecase(name)
  55. @api.one
  56. def action_activate(self):
  57. if self.state not in ('draft', 'suspended'):
  58. raise Warning('No se puede activar un sistema ya activo')
  59. self.state = 'activated'
  60. @api.one
  61. def action_disapprove(self):
  62. if self.state != 'draft':
  63. raise Warning('No se puede desaprobar un sistema ya activo')
  64. self.state = 'disapproved'
  65. @api.one
  66. def action_suspend(self):
  67. if self.state != 'activated':
  68. raise Warning('No se puede suspender un sistema no activo')
  69. self.state = 'suspended'
  70. self.running = False
  71. @api.one
  72. def copy(self):
  73. raise Warning('Atención', 'No se puede duplicar una instancia. Por favor, cree uno nuevo')
  74. @api.one
  75. def action_destroy(self):
  76. if self.state == 'destroyed':
  77. raise Warning('No se puede eliminar un sistema ya eliminado')
  78. self.state = 'destroyed'
  79. self.running = False
  80. @api.one
  81. def action_start(self):
  82. if self.running:
  83. raise Warning('Atención', 'No se puede arrancar una instancia que ya está arrancada')
  84. self.running = True
  85. @api.one
  86. def action_restart(self):
  87. if not self.running:
  88. raise Warning('Atención', 'No se puede parar y arrancar una instancia que ya está parada')
  89. self.running = True
  90. # def check_existence(self):
  91. # root_path = config['odoo_root_path'] or None
  92. # if not root_path:
  93. # raise Warning('No se puede encontrar la configuración')
  94. # system_path = os.path.join(root_path, self.normalized_name)
  95. # if system_path:
  96. # raise Warning('Ya existe un sistema con este nombre')
  97. # return True
  98. # def check_docker_port(self, port):
  99. # if port == 0:
  100. # return False
  101. # if port in docker_api.get_all_external_ports():
  102. # return False
  103. # return True
  104. # def take_randomized_port(self):
  105. # port_range = config['odoo_ports_range']
  106. # port_range = map(lambda x: int(x.strip()), port_range.split(','))
  107. # port = 0
  108. # while not self.check_docker_port(port):
  109. # port = randint(port_range[0], port_range[1])
  110. # time.sleep(0.3)
  111. # self.external_port = port
  112. # def create_odoo_folders(self):
  113. # root_path = config('odoo_root_path')
  114. # defaults_path = config('odoo_defaults_path')
  115. # for p in defaults_path:
  116. # full_path = os.path.join(root_path, self.normalized_name, p.strip())
  117. # os.makedirs(full_path)
  118. # time.sleep(0.3)
  119. # return True
  120. # def create_odoo_config(self):
  121. # config_path = os.path.join(config('odoo_root_path'), self.normalized_name, 'config')
  122. # if not os.path.exists(config_path):
  123. # return False
  124. # env = Environment(loader=PackageLoader('resources'))
  125. # template = env.get_template('openerp-server.j2')
  126. # rendered = template.stream({
  127. # 'admin_password': config('odoo_default_password'),
  128. # 'db_host': config('odoo_db_host'),
  129. # 'db_name': self.normalized_name,
  130. # 'db_user': config('db_user'),
  131. # 'db_password': config('db_password')
  132. # })
  133. # rendered.dump(os.path.join(config_path, 'openerp-server.conf'))
  134. # return True
  135. # def create_odoo_db(self):
  136. # cmd = 'createdb -U %s %s' % (config('db_user'), self.normalized_name)
  137. # result = docker_api.run_command(config('odoo_db_container'), cmd)
  138. # return result
  139. # def copy_odoo_db_seed(self):
  140. # backup_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'resources', 'odoo.tar')
  141. # return docker_api.copy_in(config('odoo_db_container'), '/tmp', backup_path)
  142. # def restore_odoo_db(self):
  143. # cmd = 'psql -U %s -d %s -f %s' % (config('db_user'), self.normalized_name, '/tmp/odoo.sql')
  144. # return docker_api.run_command(config('odoo_db_container'), cmd)
  145. # def remove_odoo_db_seed(self):
  146. # cmd = 'rm -f %s' % '/tmp/odoo.sql'
  147. # return docker_api.run_command(config('odoo_db_container'), cmd)
  148. # def create_odoo_container(self):
  149. # result = docker_api.run_container(
  150. # image=config('odoo_image'),
  151. # name=self.normalized_name,
  152. # ports={'8069/tcp': self.external_port},
  153. # volumes=None,
  154. # network=config('odoo_network'),
  155. # memory_limit='150M',
  156. # memory_swap_limit='150M'
  157. # )
  158. # if not result:
  159. # raise Warning('No se pudo crear el contenedor')
  160. # return True
  161. # def apply_odoo_folders_permission(self):
  162. # full_path = os.path.join(config('odoo_root_path'), self.normalized_name)
  163. # check_output(['chmod', '-Rf', '777'], full_path)
  164. # return True
  165. # def take_internal_ip(self):
  166. # self.internal_ip = docker_api.get_internal_ip(self.normalized_name)