RacksDB 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. RacksDB-0.2.0.dist-info/LICENSE +674 -0
  2. RacksDB-0.2.0.dist-info/METADATA +126 -0
  3. RacksDB-0.2.0.dist-info/RECORD +42 -0
  4. RacksDB-0.2.0.dist-info/WHEEL +5 -0
  5. RacksDB-0.2.0.dist-info/entry_points.txt +3 -0
  6. RacksDB-0.2.0.dist-info/top_level.txt +1 -0
  7. racksdb/__init__.py +74 -0
  8. racksdb/bases.py +117 -0
  9. racksdb/drawers/__init__.py +10 -0
  10. racksdb/drawers/base.py +42 -0
  11. racksdb/drawers/infrastructure.py +253 -0
  12. racksdb/drawers/room.py +160 -0
  13. racksdb/dtypes/__init__.py +0 -0
  14. racksdb/dtypes/angle.py +21 -0
  15. racksdb/dtypes/bytes.py +31 -0
  16. racksdb/dtypes/dimension.py +23 -0
  17. racksdb/dtypes/netif_type.py +17 -0
  18. racksdb/dtypes/rack_height.py +17 -0
  19. racksdb/dtypes/rack_width.py +26 -0
  20. racksdb/dtypes/storage_type.py +17 -0
  21. racksdb/errors.py +9 -0
  22. racksdb/exec.py +218 -0
  23. racksdb/generic/__init__.py +0 -0
  24. racksdb/generic/db.py +609 -0
  25. racksdb/generic/definedtype.py +24 -0
  26. racksdb/generic/dumpers/__init__.py +30 -0
  27. racksdb/generic/dumpers/_common.py +30 -0
  28. racksdb/generic/dumpers/console.py +31 -0
  29. racksdb/generic/dumpers/json.py +108 -0
  30. racksdb/generic/dumpers/yaml.py +138 -0
  31. racksdb/generic/errors.py +21 -0
  32. racksdb/generic/openapi.py +199 -0
  33. racksdb/generic/schema.py +367 -0
  34. racksdb/generic/views.py +139 -0
  35. racksdb/tests/__init__.py +0 -0
  36. racksdb/tests/test_schema.py +211 -0
  37. racksdb/tests/test_schema_defined_type_loader.py +23 -0
  38. racksdb/tests/tests_db_load.py +83 -0
  39. racksdb/version.py +11 -0
  40. racksdb/views.py +136 -0
  41. racksdb/web/__init__.py +0 -0
  42. racksdb/web/app.py +195 -0
@@ -0,0 +1,126 @@
1
+ Metadata-Version: 2.1
2
+ Name: RacksDB
3
+ Version: 0.2.0
4
+ Summary: Modelize your datacenters infrastructures in YAML database
5
+ Author-email: Rémi Palancher <remi@rackslab.io>
6
+ License: GPLv3+
7
+ Project-URL: Homepage, https://github.com/rackslab/RacksDB
8
+ Project-URL: Bug Tracker, https://github.com/rackslab/RacksDB/issues
9
+ Keywords: cmdb,inventory,yaml,datacenters,racks,hpc,cluster
10
+ Classifier: Development Status :: 5 - Production/Stable
11
+ Classifier: Environment :: Console
12
+ Classifier: Intended Audience :: System Administrators
13
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
14
+ Classifier: Topic :: Database :: Database Engines/Servers
15
+ Classifier: Topic :: System :: Clustering
16
+ Classifier: Topic :: System :: Systems Administration
17
+ Requires-Python: >=3.6
18
+ Description-Content-Type: text/markdown
19
+ License-File: LICENSE
20
+ Requires-Dist: PyYAML
21
+ Requires-Dist: ClusterShell
22
+ Requires-Dist: pycairo
23
+ Provides-Extra: web
24
+ Requires-Dist: Flask ; extra == 'web'
25
+ Requires-Dist: Flask-Cors ; extra == 'web'
26
+
27
+ <img
28
+ src="docs/modules/overview/images/racksdb_logo.svg"
29
+ alt="RacksDB Overview"
30
+ align="right"
31
+ width="200px"
32
+ style="margin: 20px;">
33
+
34
+ # RacksDB
35
+
36
+ ## Overview
37
+
38
+ **RacksDB** is an _open source_ solution to modelize your datacenters
39
+ infrastructures. It provides a simple database schema to store information about
40
+ the equipments in your datacenters. This database can be used as **reference
41
+ source** for many purposes in IT management.
42
+
43
+ * **Inventory**: Get permanent reference list of equipments charactistics and
44
+ enable conformity testing.
45
+ * **Automation**: Adopt Infrastructure-as-Code by coupling generic configuration
46
+ management logic with infrastructures model.
47
+ * **Continuous deployment**: Define advanced deployment rules based on nodes
48
+ characteristics declared in RacksDB.
49
+ * **Monitoring**: Synchronize monitoring services with equipments database and
50
+ define dynamic dashboard.
51
+ * **Documentation**: Get updated architecture diagrams and define generic
52
+ adaptative procedures.
53
+
54
+ <p align="center">
55
+ <img
56
+ src="docs/modules/overview/images/racksdb_overview.svg"
57
+ alt="RacksDB Overview"
58
+ width="600px"
59
+ style="margin: 0 auto;">
60
+ </p>
61
+
62
+ Generally speaking, **RacksDB** is a specialized
63
+ [CMDB](https://en.wikipedia.org/wiki/Configuration_management_database)
64
+ dedicated to
65
+ [DCIM](https://en.wikipedia.org/wiki/Data_center_management#Data_center_infrastructure_management).
66
+
67
+ Compared to others products, **RacksDB** can be considered lightweight
68
+ alternative to more advanced tools such as [NetBox](https://netbox.dev/) and
69
+ [RackTables](https://www.racktables.org/).
70
+
71
+ The main characteristics of **RacksDB** that makes it unique among other
72
+ solutions are the following:
73
+
74
+ * **YAML based.** The data are stored in plain files using human-readable YAML
75
+ format, easily managed with Git.
76
+ * **Simple and pragmatic.** The database schema is intuitive and practical,
77
+ infrastructures can be described in few minutes.
78
+ * **Tag-based.** Easy filtering of data and equipments based on associated tags.
79
+ * **Decentralized architecture.** RacksDB can be deployed without central
80
+ server, just by replicating few plain YAML files.
81
+ * **Extensibility.** In addition to RacksDB simple format, custom schema
82
+ extensions can be defined to store specific data.
83
+
84
+ RacksDB provides multiple interfaces to request the database with its
85
+ [CLI tool](https://docs.rackslab.io/racksdb/beta/usage/racksdb.html), a
86
+ [Python library](https://docs.rackslab.io/racksdb/beta/usage/api.html) and a
87
+ [REST API](https://docs.rackslab.io/racksdb/beta/usage/rest.html).
88
+
89
+ For more details, read the
90
+ [full description](https://docs.rackslab.io/racksdb/beta/overview/overview.html)
91
+ of RacksDB.
92
+
93
+ ## Status
94
+
95
+ RacksDB is considered stable and ready for production.
96
+
97
+ ## Quickstart
98
+
99
+ To install and start using RacksDB in a few steps, follow the
100
+ [quickstart guide](https://docs.rackslab.io/racksdb/beta/install/quickstart.html)!
101
+
102
+ ## Documentation
103
+
104
+ The [full documentation](https://docs.rackslab.io/racksdb/beta/)
105
+ of RacksDB is available online with complete database definition reference
106
+ documentation, how-tos (extensions, positioning), CLI manpage and library API
107
+ documentation.
108
+
109
+ ## Tests
110
+
111
+ To run the unit tests, launch this command:
112
+
113
+ ```
114
+ $ python3 -m unittest discover -v .
115
+ ```
116
+
117
+ ## Authors
118
+
119
+ RacksDB is developed and maintained by [Rackslab](https://rackslab.io). Please
120
+ [contact us](https://rackslab.io/en/contact/) for any questions or professionnal
121
+ services.
122
+
123
+ ## License
124
+
125
+ RacksDB is distributed under the terms of the GNU General Public License v3.0 or
126
+ later (GPLv3+).
@@ -0,0 +1,42 @@
1
+ racksdb/__init__.py,sha256=OIrBQlt1Pc9hPsPq_9BaqPPkIm6uDsnwlxnQErwhx7s,2306
2
+ racksdb/bases.py,sha256=q8RMkQlXWIJYONgfPXBvg_yfQf6j2QrM5TfwhhGIwRI,3669
3
+ racksdb/errors.py,sha256=RWJNU2OXpwBr1fYZASscEUNd6CjC_TPpv7ErwwrYEBc,157
4
+ racksdb/exec.py,sha256=_U11PjAoDmvZghZIvS0WsV-AuOeLEhHcQbmR8g6UHdE,7381
5
+ racksdb/version.py,sha256=3ULsVAjy2um7pAUXElNbhSLr0Qw7EomCIjY4JAeB74E,219
6
+ racksdb/views.py,sha256=MhTrrorE6LAzFAzNme0wqAp7-wPCkZ0fdv8p7WELkeU,4547
7
+ racksdb/drawers/__init__.py,sha256=NB9X7d6U0C1WMXOtjQ6rRBKTT8o8XEEsMe7wkv7B7d4,244
8
+ racksdb/drawers/base.py,sha256=XSfzS-PU-QRTQ7YaQUMxdQzEfFxKluYGkWDMF8FKbio,1139
9
+ racksdb/drawers/infrastructure.py,sha256=iburFRhm_9apBo883R0DEbUp6_fUICk82AfFlH4_ep8,8140
10
+ racksdb/drawers/room.py,sha256=cdtKkWlGENAALRzY90Dp-c4_YyOj-l8c9kxBEzOYMv0,5338
11
+ racksdb/dtypes/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
+ racksdb/dtypes/angle.py,sha256=ZyC66GZSSVlAhy6KhIx6aJ3dg5Ho5mWZ_7aDFdidudA,550
13
+ racksdb/dtypes/bytes.py,sha256=Yoj1F4fv2vNse4bwXwgykCITgofH0NNyM0wt5AIVCpQ,833
14
+ racksdb/dtypes/dimension.py,sha256=6hXf0FAQzNw1jdBtbMGA965IPS_MZx4rsqe78jR1Ahg,541
15
+ racksdb/dtypes/netif_type.py,sha256=w2YPNA3Y1PUQ3GUHIZRTgn1D0BoOTwJR43NV_kwNHFw,388
16
+ racksdb/dtypes/rack_height.py,sha256=kRm9KKVwhVhUxawKS3aKau5-2GZTwbA_WUSuF9H5wtU,371
17
+ racksdb/dtypes/rack_width.py,sha256=nyQecZSlUtuOwQcP-oaCp0JgDj7wUae2t9zs6bBGMqQ,660
18
+ racksdb/dtypes/storage_type.py,sha256=KvzvS7AxB12UxuhLcETRMF7Hs5O96Zgvr_m1tl1AZvk,384
19
+ racksdb/generic/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
20
+ racksdb/generic/db.py,sha256=aXjJZZnlhEmoPm41-ZUo4c29JYboidc9xkOXb5TRQOI,24456
21
+ racksdb/generic/definedtype.py,sha256=ERdLVndATyuLJ1VFtvyE8G_iwhdk-riDLzEfU9-Oyuo,557
22
+ racksdb/generic/errors.py,sha256=b6_WWpzRqTSSU8LFl652DyLWzs7APilG5zceA0otCR4,285
23
+ racksdb/generic/openapi.py,sha256=HqpnDHdqaptWM4ZDQkVWuOPm8vYUGrh03BCgdINTTcg,7501
24
+ racksdb/generic/schema.py,sha256=-JS_Q-MSTM5Nd17pDfh8mnH1CgtHFI9-SOpIEd0n2qk,12371
25
+ racksdb/generic/views.py,sha256=9f5f2TJmlcj3whuOd48syYEo7uEbIp_SxfgQCVnSsKg,3888
26
+ racksdb/generic/dumpers/__init__.py,sha256=rU6TaK3rHDgTKAdwnlMJ3gzhICR2xaNLUiDP1sALjGk,883
27
+ racksdb/generic/dumpers/_common.py,sha256=kOhRGAECfSQ64que5wFfRxeFUabK3-wjnfEmNPDmkmk,1022
28
+ racksdb/generic/dumpers/console.py,sha256=RwUTfFm2FWGXvoOlRG6IRY-xTPYbn4KuHiZaw-edALE,756
29
+ racksdb/generic/dumpers/json.py,sha256=xNokwck6wOegNoSiCPLRKLtHB9IslJzNvjJOy6HVepQ,4133
30
+ racksdb/generic/dumpers/yaml.py,sha256=RyrxELqC4EP74Pe-FtNiceMdd1rZmXYTb7hrlltKNfc,4991
31
+ racksdb/tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
32
+ racksdb/tests/test_schema.py,sha256=CDwvFCKKcZbnkdgToKZyA0Siwc1okgGG-4eH1W6yGnU,7355
33
+ racksdb/tests/test_schema_defined_type_loader.py,sha256=nzrTk5rZZ5lLH8zsHglfs83CAzBo6DA2ZaSWa3vinDk,843
34
+ racksdb/tests/tests_db_load.py,sha256=lNYtZh820xj7iZiP57mbzP-8gtgBKflH1KzSmh9y0c4,3322
35
+ racksdb/web/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
36
+ racksdb/web/app.py,sha256=TszzHRi61NYIdpxSImFUsNquFx1rQML9NUBS6RhiHN0,5954
37
+ RacksDB-0.2.0.dist-info/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
38
+ RacksDB-0.2.0.dist-info/METADATA,sha256=5AU5hg453GdkDVPqZEblpGmvKfYLk6soox_XoI_B15A,4553
39
+ RacksDB-0.2.0.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
40
+ RacksDB-0.2.0.dist-info/entry_points.txt,sha256=Zy9WcQLArDllnncS54E0-X4VazrVGRwkPrRI8s0yXF8,105
41
+ RacksDB-0.2.0.dist-info/top_level.txt,sha256=Lpa-aw0iJS_OK0Im2V2hktG6iDEhLISXnc9kNf7Q8SY,8
42
+ RacksDB-0.2.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.41.2)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ racksdb = racksdb.exec:RacksDBExec.run
3
+ racksdb-web = racksdb.web.app:RacksDBWebApp.run
@@ -0,0 +1 @@
1
+ racksdb
racksdb/__init__.py ADDED
@@ -0,0 +1,74 @@
1
+ # Copyright (c) 2022-2023 Rackslab
2
+ #
3
+ # This file is part of RacksDB.
4
+ #
5
+ # SPDX-License-Identifier: GPL-3.0-or-later
6
+
7
+ from pathlib import Path
8
+ from typing import Union
9
+
10
+ from .generic.schema import Schema, SchemaFileLoader, SchemaDefinedTypeLoader
11
+ from .generic.db import GenericDB, DBDict, DBList, DBSplittedFilesLoader
12
+ from . import bases
13
+
14
+
15
+ class RacksDB(GenericDB):
16
+
17
+ DEFAULT_DB = "/var/lib/racksdb"
18
+ DEFAULT_SCHEMA = "/usr/share/racksdb/schema.yml"
19
+ DEFAULT_EXT = "/etc/racksdb/extensions.yml"
20
+ PREFIX = "RacksDB"
21
+ DEFINED_TYPES_MODULE = "racksdb.dtypes"
22
+
23
+ def __init__(self, schema, loader):
24
+ super().__init__(self.PREFIX, schema, bases)
25
+ self._loader = loader
26
+
27
+ @property
28
+ def nodes(self):
29
+ result = DBDict()
30
+ for infrastructure in self.infrastructures:
31
+ result.update(infrastructure.nodes)
32
+ return result
33
+
34
+ @property
35
+ def racks(self):
36
+ result = DBList()
37
+ for datacenter in self.datacenters:
38
+ for room in datacenter.rooms:
39
+ for row in room.rows:
40
+ for rack in row.racks:
41
+ result.append(rack)
42
+ return result
43
+
44
+ @classmethod
45
+ def load(
46
+ cls,
47
+ schema: Union[str, Path, None] = None,
48
+ ext: Union[str, Path, None] = None,
49
+ db: Union[str, Path, None] = None,
50
+ ):
51
+ # Unfortunately, default values to arguments cannot be used as they are
52
+ # class attributes and the class is not defined yet at this stage at
53
+ # compilation time. As an alternative, the value None is checked at
54
+ # runtime and replaced by values of class attributes.
55
+
56
+ if schema is None:
57
+ schema = Path(cls.DEFAULT_SCHEMA)
58
+ elif isinstance(schema, str):
59
+ schema = Path(schema)
60
+ if ext is None:
61
+ ext = Path(cls.DEFAULT_EXT)
62
+ elif isinstance(ext, str):
63
+ ext = Path(ext)
64
+ if db is None:
65
+ db = Path(cls.DEFAULT_DB)
66
+ elif isinstance(db, str):
67
+ db = Path(db)
68
+ _schema = Schema(
69
+ SchemaFileLoader(schema, ext),
70
+ SchemaDefinedTypeLoader(cls.DEFINED_TYPES_MODULE),
71
+ )
72
+ _db = cls(_schema, DBSplittedFilesLoader(db))
73
+ super(cls, _db).load(_db._loader)
74
+ return _db
racksdb/bases.py ADDED
@@ -0,0 +1,117 @@
1
+ # Copyright (c) 2022-2023 Rackslab
2
+ #
3
+ # This file is part of RacksDB.
4
+ #
5
+ # SPDX-License-Identifier: GPL-3.0-or-later
6
+
7
+ from .generic.db import DBList, DBDict
8
+
9
+
10
+ class RacksDBDatacenterBase:
11
+ def _filter(self, name=None, tags=None):
12
+ # filter by name
13
+ if name is not None and name != self.name:
14
+ return False
15
+ # filter by tags
16
+ if tags is not None:
17
+ for tag in tags:
18
+ if tag not in self.tags:
19
+ return False
20
+ return True
21
+
22
+
23
+ class RacksDBInfrastructureBase:
24
+ @property
25
+ def nodes(self):
26
+ result = DBDict()
27
+ for part in self.layout:
28
+ # Iterate over the keys of DBDict instead of the DBDict itself to
29
+ # avoid triggering expansion of expandable objects at this stage.
30
+ for key in part.nodes.keys():
31
+ result[key] = part.nodes[key]
32
+ return result
33
+
34
+ def _filter(self, name=None, tags=None):
35
+ # filter by name
36
+ if name is not None and name != self.name:
37
+ return False
38
+ # filter by tags
39
+ if tags is not None:
40
+ for tag in tags:
41
+ if tag not in self.tags:
42
+ return False
43
+ return True
44
+
45
+
46
+ class RacksDBNodeBase:
47
+ @property
48
+ def tags(self):
49
+ result = DBList()
50
+ for tag in getattr(self._parent, "tags", []):
51
+ result.append(tag)
52
+ # RacksDBNodeBase loaded tags are renamed with loaded prefix to avoid
53
+ # conflict with this property.
54
+ for tag in getattr(self, f"{self.LOADED_PREFIX}tags", []):
55
+ result.append(tag)
56
+ return result
57
+
58
+ def _filter(self, infrastructure=None, name=None, tags=None):
59
+ # filter by name
60
+ if name is not None and name != self.name:
61
+ return False
62
+ # filter by infrastructure name
63
+ if infrastructure is not None and infrastructure != self.infrastructure.name:
64
+ return False
65
+ # filter by tags
66
+ if tags is not None:
67
+ for tag in tags:
68
+ if tag not in self.tags:
69
+ return False
70
+ return True
71
+
72
+
73
+ class RacksDBRackBase:
74
+
75
+ COMPUTED_PROPERTIES = ["nodes", "fillrate"]
76
+
77
+ def _filter(self, name=None):
78
+ # filter by name
79
+ if name is not None and name != self.name:
80
+ return False
81
+ return True
82
+
83
+ @property
84
+ def fillrate(self):
85
+ """Return the fill rate of the rack as a float normalized between 0 and 1."""
86
+ occupied = 0.0
87
+ for infrastructure in self._db.infrastructures:
88
+ for part in infrastructure.layout:
89
+ if self.name == part.rack.name:
90
+ for equipment in part.nodes:
91
+ occupied += equipment.type.height * equipment.type.width
92
+ for equipment in part.storage:
93
+ occupied += equipment.type.height * equipment.type.width
94
+ for equipment in part.network:
95
+ occupied += equipment.type.height * equipment.type.width
96
+ return occupied / self.type.slots
97
+
98
+ @property
99
+ def nodes(self):
100
+ result = DBList()
101
+ # add reference to infrastructures nodes
102
+ for infrastructure in self._db.infrastructures:
103
+ for part in infrastructure.layout:
104
+ if self.name == part.rack.name:
105
+ for nodes in part.nodes.values():
106
+ result.append(nodes)
107
+ return result
108
+
109
+
110
+ class RacksDBRacksRowBase:
111
+
112
+ COMPUTED_PROPERTIES = ["nbracks"]
113
+
114
+ @property
115
+ def nbracks(self):
116
+ """Return the number of racks in the row."""
117
+ return len(self.racks)
@@ -0,0 +1,10 @@
1
+ # Copyright (c) 2022-2023 Rackslab
2
+ #
3
+ # This file is part of RacksDB.
4
+ #
5
+ # SPDX-License-Identifier: GPL-3.0-or-later
6
+
7
+ from .room import RoomDrawer
8
+ from .infrastructure import InfrastructureDrawer
9
+
10
+ __all__ = ["RoomDrawer", "InfrastructureDrawer"]
@@ -0,0 +1,42 @@
1
+ # Copyright (c) 2022-2023 Rackslab
2
+ #
3
+ # This file is part of RacksDB.
4
+ #
5
+ # SPDX-License-Identifier: GPL-3.0-or-later
6
+
7
+ import logging
8
+
9
+ import cairo
10
+
11
+ logger = logging.getLogger(__name__)
12
+
13
+
14
+ class ImagePoint:
15
+ def __init__(self, x, y):
16
+ self.x = x
17
+ self.y = y
18
+
19
+
20
+ class Drawer:
21
+ def __init__(self, db, file, output_format):
22
+ self.db = db
23
+ self.file = file
24
+ self.output_format = output_format
25
+ self.surface = None
26
+ self.ctx = None
27
+
28
+ def init_ctx(self, width, height):
29
+ if self.output_format == "png":
30
+ self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
31
+ elif self.output_format == "svg":
32
+ self.surface = cairo.SVGSurface(self.file, width, height)
33
+ elif self.output_format == "pdf":
34
+ self.surface = cairo.PDFSurface(self.file, width, height)
35
+ self.ctx = cairo.Context(self.surface)
36
+
37
+ def write(self):
38
+ if self.output_format == "png":
39
+ self.surface.write_to_png(self.file)
40
+ elif self.output_format in ["svg", "pdf"]:
41
+ self.surface.finish()
42
+ self.surface.flush()
@@ -0,0 +1,253 @@
1
+ # Copyright (c) 2022-2023 Rackslab
2
+ #
3
+ # This file is part of RacksDB.
4
+ #
5
+ # SPDX-License-Identifier: GPL-3.0-or-later
6
+
7
+ import logging
8
+ import math
9
+
10
+ import cairo
11
+
12
+ from .base import Drawer, ImagePoint
13
+ from ..errors import RacksDBError
14
+
15
+ logger = logging.getLogger(__name__)
16
+
17
+
18
+ class InfrastructureDrawer(Drawer):
19
+
20
+ SCALE = 0.40 # 1mm to pixel
21
+ MARGIN_TOP = 30
22
+ ROW_LABEL_OFFSET = 20
23
+ RACK_LABEL_OFFSET = 20
24
+ RACK_OFFSET = 10
25
+ MARGIN_LEFT = 30
26
+ RACK_U_HEIGHT = 44.45 * SCALE
27
+ RACK_PANE_WIDTH = 10
28
+ RACK_SPACING = 3 # space between racks
29
+
30
+ def __init__(self, db, name, file, output_format):
31
+ super().__init__(db, file, output_format)
32
+ self.infrastructure = None
33
+ for infrastructure in self.db.infrastructures:
34
+ if infrastructure.name == name:
35
+ self.infrastructure = infrastructure
36
+ if self.infrastructure is None:
37
+ raise RacksDBError(f"Unable to find infrastructure {name} in database")
38
+ # List of rack rows used by the infrastructure
39
+ self.rack_rows = []
40
+ # List of racks used by the insfrastructure
41
+ self.racks = []
42
+
43
+ def _rack_row_dl(self, row) -> ImagePoint:
44
+ # sum height of all previous rows
45
+ pos_y = self.MARGIN_TOP
46
+ for _row in self.rack_rows:
47
+ if _row is row:
48
+ break
49
+ pos_y += (
50
+ int(_row.height * self.SCALE)
51
+ + self.ROW_LABEL_OFFSET
52
+ + self.RACK_LABEL_OFFSET
53
+ + self.RACK_OFFSET
54
+ )
55
+ pos_y += int(row.height * self.SCALE)
56
+ return ImagePoint(self.MARGIN_LEFT, pos_y)
57
+
58
+ def _rack_dl(self, row, rack) -> ImagePoint:
59
+ dl = self._rack_row_dl(row)
60
+
61
+ # Sum the width of all racks in row before the current rack
62
+ for row_rack in rack.row.racks:
63
+ if row_rack.slot < rack.slot:
64
+ dl.x += int(row_rack.type.width * self.SCALE) + self.RACK_SPACING
65
+ dl.y += self.RACK_LABEL_OFFSET + self.RACK_OFFSET
66
+ return dl
67
+
68
+ def _equipment_tl(self, row, rack, equipment) -> ImagePoint:
69
+ tl = self._rack_dl(row, rack)
70
+
71
+ equipment_height_slot = (
72
+ equipment._first.slot
73
+ + math.floor(
74
+ (equipment.slot - equipment._first.slot) * equipment.type.width
75
+ )
76
+ * equipment.type.height
77
+ )
78
+ equipment_width_slot = (equipment.slot - equipment._first.slot) % (
79
+ 1 / equipment.type.width
80
+ )
81
+
82
+ logger.debug(
83
+ "Equipment %s calculated slots → height: %d width: %d",
84
+ equipment.name,
85
+ equipment_height_slot,
86
+ equipment_width_slot,
87
+ )
88
+
89
+ tl.x += self.RACK_PANE_WIDTH + equipment_width_slot * equipment.type.width * (
90
+ int(equipment.rack.type.width * self.SCALE) - 2 * self.RACK_PANE_WIDTH
91
+ )
92
+ tl.y -= (equipment.type.height + equipment_height_slot) * self.RACK_U_HEIGHT
93
+
94
+ return tl
95
+
96
+ def _draw_rack_equipment(self, row, rack, equipment):
97
+ logger.debug(
98
+ "Drawing equipment %s in rack %s",
99
+ equipment.name,
100
+ rack.name,
101
+ )
102
+
103
+ # top left of equipment
104
+ tl = self._equipment_tl(row, rack, equipment)
105
+
106
+ equipment_width = equipment.type.width * (
107
+ int(equipment.rack.type.width * self.SCALE) - 2 * self.RACK_PANE_WIDTH
108
+ )
109
+ equipment_height = int(equipment.type.height * self.RACK_U_HEIGHT)
110
+
111
+ # draw equipment background
112
+ self.ctx.set_source_rgb(0.6, 0.6, 0.6) # grey
113
+ self.ctx.set_line_width(1)
114
+ self.ctx.rectangle(
115
+ tl.x,
116
+ tl.y,
117
+ equipment_width,
118
+ equipment_height,
119
+ )
120
+ self.ctx.fill()
121
+
122
+ # draw equipment frame
123
+ self.ctx.set_source_rgb(0.2, 0.2, 0.2) # grey
124
+ self.ctx.set_line_width(1)
125
+ self.ctx.rectangle(
126
+ tl.x,
127
+ tl.y,
128
+ equipment_width,
129
+ equipment_height,
130
+ )
131
+ self.ctx.stroke()
132
+
133
+ # Write equipment name, rotate the text if height > width
134
+ if equipment_height > equipment_width:
135
+ self.ctx.move_to(tl.x + 2, tl.y + 2)
136
+ self.ctx.save()
137
+ self.ctx.rotate(math.pi / 2)
138
+ else:
139
+ self.ctx.move_to(tl.x + 2, tl.y + 15)
140
+ self.ctx.show_text(equipment.name)
141
+ if equipment_height > equipment_width:
142
+ self.ctx.restore()
143
+
144
+ def _draw_rack(self, row, rack):
145
+ logger.debug("Drawing rack %s (%s)", rack.name, rack.slot)
146
+
147
+ # top left of rack
148
+ dl = self._rack_dl(row, rack)
149
+
150
+ rack_width = rack.type.width * self.SCALE
151
+ rack_height = rack.type.height * self.SCALE
152
+
153
+ # write rack name
154
+ self.ctx.move_to(dl.x, dl.y - rack_height - self.RACK_OFFSET)
155
+ self.ctx.set_source_rgb(0, 0, 0) # black
156
+ self.ctx.show_text(f"rack {rack.name}")
157
+
158
+ # draw rack frame
159
+ self.ctx.set_source_rgb(0.2, 0.2, 0.2) # grey
160
+ self.ctx.set_line_width(1)
161
+ self.ctx.rectangle(
162
+ dl.x,
163
+ dl.y - rack_height,
164
+ rack_width,
165
+ rack_height,
166
+ )
167
+ self.ctx.stroke()
168
+
169
+ # draw rack panes
170
+ self.ctx.set_source_rgb(0, 0, 0) # black
171
+ self.ctx.rectangle(
172
+ dl.x,
173
+ dl.y - rack_height,
174
+ self.RACK_PANE_WIDTH,
175
+ rack_height,
176
+ )
177
+ self.ctx.rectangle(
178
+ dl.x + rack_width - self.RACK_PANE_WIDTH,
179
+ dl.y - rack_height,
180
+ self.RACK_PANE_WIDTH,
181
+ rack_height,
182
+ )
183
+ self.ctx.fill()
184
+
185
+ # draw equipments in rack
186
+ for part in self.infrastructure.layout:
187
+ if part.rack is rack:
188
+ for equipment in (
189
+ list(part.nodes) + list(part.storage) + list(part.network)
190
+ ):
191
+ self._draw_rack_equipment(row, rack, equipment)
192
+
193
+ def _draw_rack_row(self, row):
194
+
195
+ logger.debug("Drawing row %s", row.name)
196
+
197
+ dl = self._rack_row_dl(row)
198
+
199
+ # write row name
200
+ self.ctx.set_source_rgb(0, 0, 0) # black
201
+ self.ctx.select_font_face(
202
+ "Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD
203
+ )
204
+ self.ctx.set_font_size(14)
205
+ self.ctx.move_to(dl.x, dl.y - int(row.height * self.SCALE))
206
+ self.ctx.show_text(f"row {row.name}")
207
+
208
+ # iterate over the racks to draw racks in row
209
+ for rack in self.racks:
210
+ if rack.row is row:
211
+ self._draw_rack(row, rack)
212
+
213
+ def _draw_infrastructure(self):
214
+ for row in self.rack_rows:
215
+ self._draw_rack_row(row)
216
+
217
+ def draw(self):
218
+
219
+ # Get list of racks and rows used by the infrastructure
220
+ for part in self.infrastructure.layout:
221
+ if part.rack not in self.racks:
222
+ self.racks.append(part.rack)
223
+ if part.rack.row not in self.rack_rows:
224
+ self.rack_rows.append(part.rack.row)
225
+
226
+ # Sum all rows maximum rack height to calculate image height
227
+ total_row_max_heights = 0
228
+ for rack_row in self.rack_rows:
229
+ row_max_height = 0
230
+ for rack in rack_row.racks:
231
+ row_max_height = max(row_max_height, rack.type.height)
232
+ total_row_max_heights += row_max_height
233
+ # add attributes to row
234
+ rack_row.height = row_max_height
235
+
236
+ # Find the maximum rack x to calculate image width
237
+ total_racks_widths = 0
238
+ for rack in self.racks:
239
+ dl = self._rack_dl(rack.row, rack)
240
+ x_tr = dl.x + int(rack.type.width * self.SCALE)
241
+ total_racks_widths = max(total_racks_widths, x_tr)
242
+
243
+ surface_width = total_racks_widths + self.MARGIN_LEFT
244
+ surface_height = (
245
+ 2 * self.MARGIN_TOP
246
+ + len(self.rack_rows)
247
+ * (self.ROW_LABEL_OFFSET + self.RACK_LABEL_OFFSET + self.RACK_OFFSET)
248
+ + int(total_row_max_heights * self.SCALE)
249
+ )
250
+
251
+ self.init_ctx(surface_width, surface_height)
252
+ self._draw_infrastructure()
253
+ self.write()