models.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 django.conf import settings
  6. from libs import ModelMixin, human_datetime
  7. from apps.account.models import User
  8. from apps.config.models import Environment
  9. import subprocess
  10. import json
  11. import os
  12. class App(models.Model, ModelMixin):
  13. name = models.CharField(max_length=50)
  14. key = models.CharField(max_length=50, unique=True)
  15. desc = models.CharField(max_length=255, null=True)
  16. rel_apps = models.TextField(null=True)
  17. rel_services = models.TextField(null=True)
  18. sort_id = models.IntegerField(default=0, db_index=True)
  19. created_at = models.CharField(max_length=20, default=human_datetime)
  20. created_by = models.ForeignKey(User, on_delete=models.PROTECT)
  21. def to_dict(self, *args, **kwargs):
  22. tmp = super().to_dict(*args, **kwargs)
  23. tmp['rel_apps'] = json.loads(self.rel_apps) if self.rel_apps else []
  24. tmp['rel_services'] = json.loads(self.rel_services) if self.rel_services else []
  25. return tmp
  26. def __repr__(self):
  27. return f'<App {self.name!r}>'
  28. class Meta:
  29. db_table = 'apps'
  30. ordering = ('-sort_id',)
  31. class Deploy(models.Model, ModelMixin):
  32. EXTENDS = (
  33. ('1', '常规发布'),
  34. ('2', '自定义发布'),
  35. )
  36. app = models.ForeignKey(App, on_delete=models.PROTECT)
  37. env = models.ForeignKey(Environment, on_delete=models.PROTECT)
  38. host_ids = models.TextField()
  39. extend = models.CharField(max_length=2, choices=EXTENDS)
  40. is_audit = models.BooleanField()
  41. is_parallel = models.BooleanField(default=True)
  42. rst_notify = models.CharField(max_length=255, null=True)
  43. created_at = models.CharField(max_length=20, default=human_datetime)
  44. created_by = models.ForeignKey(User, models.PROTECT, related_name='+')
  45. updated_at = models.CharField(max_length=20, null=True)
  46. updated_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True)
  47. @property
  48. def extend_obj(self):
  49. cls = DeployExtend1 if self.extend == '1' else DeployExtend2
  50. return cls.objects.filter(deploy=self).first()
  51. def to_dict(self, *args, **kwargs):
  52. deploy = super().to_dict(*args, **kwargs)
  53. deploy['app_key'] = self.app_key if hasattr(self, 'app_key') else None
  54. deploy['app_name'] = self.app_name if hasattr(self, 'app_name') else None
  55. deploy['host_ids'] = json.loads(self.host_ids)
  56. deploy['rst_notify'] = json.loads(self.rst_notify)
  57. deploy.update(self.extend_obj.to_dict())
  58. return deploy
  59. def delete(self, using=None, keep_parents=False):
  60. deploy_id = self.id
  61. super().delete(using, keep_parents)
  62. repo_dir = os.path.join(settings.REPOS_DIR, str(deploy_id))
  63. build_dir = os.path.join(settings.BUILD_DIR, f'{deploy_id}_*')
  64. subprocess.Popen(f'rm -rf {repo_dir} {repo_dir + "_*"} {build_dir}', shell=True)
  65. def __repr__(self):
  66. return '<Deploy app_id=%r env_id=%r>' % (self.app_id, self.env_id)
  67. class Meta:
  68. db_table = 'deploys'
  69. ordering = ('-id',)
  70. class DeployExtend1(models.Model, ModelMixin):
  71. deploy = models.OneToOneField(Deploy, primary_key=True, on_delete=models.CASCADE)
  72. git_repo = models.CharField(max_length=255)
  73. dst_dir = models.CharField(max_length=255)
  74. dst_repo = models.CharField(max_length=255)
  75. versions = models.IntegerField()
  76. filter_rule = models.TextField()
  77. hook_pre_server = models.TextField(null=True)
  78. hook_post_server = models.TextField(null=True)
  79. hook_pre_host = models.TextField(null=True)
  80. hook_post_host = models.TextField(null=True)
  81. def to_dict(self, *args, **kwargs):
  82. tmp = super().to_dict(*args, **kwargs)
  83. tmp['filter_rule'] = json.loads(self.filter_rule)
  84. return tmp
  85. def __repr__(self):
  86. return '<DeployExtend1 deploy_id=%r>' % self.deploy_id
  87. class Meta:
  88. db_table = 'deploy_extend1'
  89. class DeployExtend2(models.Model, ModelMixin):
  90. deploy = models.OneToOneField(Deploy, primary_key=True, on_delete=models.CASCADE)
  91. server_actions = models.TextField()
  92. host_actions = models.TextField()
  93. require_upload = models.BooleanField(default=False)
  94. def to_dict(self, *args, **kwargs):
  95. tmp = super().to_dict(*args, **kwargs)
  96. tmp['server_actions'] = json.loads(self.server_actions)
  97. tmp['host_actions'] = json.loads(self.host_actions)
  98. return tmp
  99. def __repr__(self):
  100. return '<DeployExtend2 deploy_id=%r>' % self.deploy_id
  101. class Meta:
  102. db_table = 'deploy_extend2'