models.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug
  2. # Copyright: (c) <spug.dev@gmail.com>
  3. # Released under the AGPL-3.0 License.
  4. from django.db import models
  5. from libs import ModelMixin, human_datetime
  6. from apps.account.models import User
  7. from apps.setting.utils import AppSetting
  8. from libs.ssh import SSH
  9. import json
  10. class Host(models.Model, ModelMixin):
  11. name = models.CharField(max_length=100)
  12. hostname = models.CharField(max_length=50)
  13. port = models.IntegerField(null=True)
  14. username = models.CharField(max_length=50)
  15. pkey = models.TextField(null=True)
  16. desc = models.CharField(max_length=255, null=True)
  17. is_verified = models.BooleanField(default=False)
  18. created_at = models.CharField(max_length=20, default=human_datetime)
  19. created_by = models.ForeignKey(User, models.PROTECT, related_name='+')
  20. @property
  21. def private_key(self):
  22. return self.pkey or AppSetting.get('private_key')
  23. def get_ssh(self, pkey=None, default_env=None):
  24. pkey = pkey or self.private_key
  25. return SSH(self.hostname, self.port, self.username, pkey, default_env=default_env)
  26. def to_view(self):
  27. tmp = self.to_dict()
  28. if hasattr(self, 'hostextend'):
  29. tmp.update(self.hostextend.to_view())
  30. tmp['group_ids'] = []
  31. return tmp
  32. def __repr__(self):
  33. return '<Host %r>' % self.name
  34. class Meta:
  35. db_table = 'hosts'
  36. ordering = ('-id',)
  37. class HostExtend(models.Model, ModelMixin):
  38. INSTANCE_CHARGE_TYPES = (
  39. ('PrePaid', '包年包月'),
  40. ('PostPaid', '按量计费'),
  41. ('Other', '其他')
  42. )
  43. INTERNET_CHARGE_TYPES = (
  44. ('PayByTraffic', '按流量计费'),
  45. ('PayByBandwidth', '按带宽计费'),
  46. ('Other', '其他')
  47. )
  48. host = models.OneToOneField(Host, on_delete=models.CASCADE)
  49. instance_id = models.CharField(max_length=64, null=True)
  50. zone_id = models.CharField(max_length=30, null=True)
  51. cpu = models.IntegerField()
  52. memory = models.FloatField()
  53. disk = models.CharField(max_length=255, default='[]')
  54. os_name = models.CharField(max_length=50)
  55. os_type = models.CharField(max_length=20)
  56. private_ip_address = models.CharField(max_length=255)
  57. public_ip_address = models.CharField(max_length=255)
  58. instance_charge_type = models.CharField(max_length=20, choices=INSTANCE_CHARGE_TYPES)
  59. internet_charge_type = models.CharField(max_length=20, choices=INTERNET_CHARGE_TYPES)
  60. created_time = models.CharField(max_length=20, null=True)
  61. expired_time = models.CharField(max_length=20, null=True)
  62. updated_at = models.CharField(max_length=20, default=human_datetime)
  63. def to_view(self):
  64. tmp = self.to_dict(excludes=('id',))
  65. tmp['disk'] = json.loads(self.disk)
  66. tmp['private_ip_address'] = json.loads(self.private_ip_address)
  67. tmp['public_ip_address'] = json.loads(self.public_ip_address)
  68. tmp['instance_charge_type_alias'] = self.get_instance_charge_type_display()
  69. tmp['internet_charge_type_alisa'] = self.get_internet_charge_type_display()
  70. return tmp
  71. class Meta:
  72. db_table = 'host_extend'
  73. class Group(models.Model, ModelMixin):
  74. name = models.CharField(max_length=20)
  75. parent_id = models.IntegerField(default=0)
  76. sort_id = models.IntegerField(default=0)
  77. hosts = models.ManyToManyField(Host, related_name='groups')
  78. def to_view(self, with_hosts=False):
  79. response = dict(key=self.id, value=self.id, title=self.name, children=[])
  80. if with_hosts:
  81. def make_item(x):
  82. return dict(title=x.name, key=f'{self.id}_{x.id}', id=x.id, isLeaf=True)
  83. response['children'] = [make_item(x) for x in self.hosts.all()]
  84. return response
  85. class Meta:
  86. db_table = 'host_groups'
  87. ordering = ('-sort_id',)