Fuel UI
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

release.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2013 Mirantis, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. from sqlalchemy import Column
  16. from sqlalchemy import Enum
  17. from sqlalchemy import Integer
  18. from sqlalchemy import String
  19. from sqlalchemy import Unicode
  20. from sqlalchemy import UniqueConstraint
  21. from sqlalchemy.dialects import postgresql as psql
  22. from sqlalchemy.orm import relationship
  23. from nailgun import consts
  24. from nailgun.db.sqlalchemy.models.base import Base
  25. from nailgun.db.sqlalchemy.models.fields import JSON
  26. from nailgun.db.sqlalchemy.models.mutable import MutableDict
  27. from nailgun.db.sqlalchemy.models.mutable import MutableList
  28. from nailgun.utils import get_environment_version
  29. class Release(Base):
  30. __tablename__ = 'releases'
  31. __table_args__ = (
  32. UniqueConstraint('name', 'version'),
  33. )
  34. id = Column(Integer, primary_key=True)
  35. name = Column(Unicode(100), nullable=False)
  36. version = Column(String(30), nullable=False)
  37. description = Column(Unicode)
  38. operating_system = Column(String(50), nullable=False)
  39. state = Column(
  40. Enum(
  41. *consts.RELEASE_STATES,
  42. name='release_state'
  43. ),
  44. nullable=False,
  45. default=consts.RELEASE_STATES.unavailable
  46. )
  47. networks_metadata = Column(MutableDict.as_mutable(JSON), default={})
  48. attributes_metadata = Column(MutableDict.as_mutable(JSON), default={})
  49. volumes_metadata = Column(MutableDict.as_mutable(JSON), default={})
  50. modes_metadata = Column(MutableDict.as_mutable(JSON), default={})
  51. roles_metadata = Column(MutableDict.as_mutable(JSON), default={})
  52. tags_metadata = Column(
  53. MutableDict.as_mutable(JSON), server_default='{}', nullable=False)
  54. network_roles_metadata = Column(
  55. MutableList.as_mutable(JSON), default=[], server_default='[]')
  56. vmware_attributes_metadata = Column(
  57. MutableDict.as_mutable(JSON), default={})
  58. components_metadata = Column(
  59. MutableList.as_mutable(JSON), default=[], server_default='[]')
  60. required_component_types = Column(MutableList.as_mutable(JSON), default=[],
  61. server_default='[]', nullable=False)
  62. modes = Column(MutableList.as_mutable(JSON), default=[])
  63. clusters = relationship(
  64. "Cluster",
  65. primaryjoin="Release.id==Cluster.release_id",
  66. backref="release",
  67. cascade="all,delete")
  68. extensions = Column(psql.ARRAY(String(consts.EXTENSION_NAME_MAX_SIZE)),
  69. default=[], nullable=False, server_default='{}')
  70. node_attributes = Column(MutableDict.as_mutable(JSON), default={},
  71. server_default='{}', nullable=False)
  72. nic_attributes = Column(MutableDict.as_mutable(JSON), default={},
  73. server_default='{}', nullable=False)
  74. bond_attributes = Column(MutableDict.as_mutable(JSON), default={},
  75. server_default='{}', nullable=False)
  76. # TODO(enchantner): get rid of properties
  77. @property
  78. def openstack_version(self):
  79. return self.version.split('-')[0]
  80. @property
  81. def environment_version(self):
  82. """Returns environment version based on release version.
  83. """
  84. return get_environment_version(self.version)
  85. @property
  86. def os_weight(self):
  87. try:
  88. weight = consts.RELEASE_OS[::-1].index(self.operating_system)
  89. except ValueError:
  90. weight = -1
  91. return weight
  92. def __cmp__(self, other):
  93. """Allows to compare two releases
  94. :other: an instance of nailgun.db.sqlalchemy.models.release.Release
  95. """
  96. if self.environment_version < other.environment_version:
  97. return -1
  98. if self.environment_version > other.environment_version:
  99. return 1
  100. if self.openstack_version < other.openstack_version:
  101. return -1
  102. if self.openstack_version > other.openstack_version:
  103. return 1
  104. if self.os_weight == other.os_weight == -1:
  105. if self.operating_system > other.operating_system:
  106. return -1
  107. if self.operating_system < other.operating_system:
  108. return 1
  109. else:
  110. if self.os_weight < other.os_weight:
  111. return -1
  112. if self.os_weight > other.os_weight:
  113. return 1
  114. return 0