osism 0.20250312.0__py3-none-any.whl → 0.20250326.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.
@@ -1,137 +0,0 @@
1
- # SPDX-License-Identifier: Apache-2.0
2
-
3
- from datetime import datetime
4
- import ipaddress
5
- import os
6
-
7
- from loguru import logger
8
- import git
9
- import gitdb
10
- import jinja2
11
- from pottery import Redlock
12
-
13
- from osism.utils import first
14
- from osism import utils
15
-
16
-
17
- def vlans_as_string(untagged_vlan, tagged_vlans):
18
- vlans = [str(x.vid) for x in tagged_vlans]
19
- if untagged_vlan:
20
- vlans = vlans + [str(untagged_vlan.vid)]
21
- return ",".join(sorted(vlans))
22
-
23
-
24
- def for_device(name, template=None):
25
- device = utils.nb.dcim.devices.get(name=name)
26
-
27
- if (
28
- "device_type" not in device.custom_fields
29
- or device.custom_fields["device_type"] != "switch"
30
- ):
31
- return
32
-
33
- if "Managed by OSISM" not in [str(x) for x in device.tags]:
34
- return
35
-
36
- logger.info(f"Generate configuration for device {device.name}")
37
-
38
- if not template:
39
- template = f"{device.name}.cfg.j2"
40
-
41
- if not os.path.isfile(f"/netbox/templates/{template}"):
42
- template = f"{device.device_type.manufacturer.name}.cfg.j2"
43
-
44
- if not os.path.isfile(f"/netbox/templates/{template}"):
45
- template = "default.cfg.j2"
46
-
47
- vlans = utils.nb.ipam.vlans.filter(available_on_device=device.id)
48
- interfaces = utils.nb.dcim.interfaces.filter(device=device)
49
-
50
- interfaces_ethernet = []
51
- interfaces_port_channels = []
52
- interfaces_virtual = []
53
-
54
- for interface in interfaces:
55
- if str(interface.type) == "Link Aggregation Group (LAG)":
56
- interfaces_port_channels.append(interface)
57
- elif str(interface.type) == "Virtual":
58
- interfaces_virtual.append(interface)
59
- else:
60
- interfaces_ethernet.append(interface)
61
-
62
- # Port-Channel10 + Vlan4094 are always used as MLAG
63
- try:
64
- mlag = utils.nb.dcim.interfaces.get(device=device, name="Port-Channel10")
65
- mlag_vlan = utils.nb.dcim.interfaces.get(device=device, name="Vlan4094")
66
- mlag_address = utils.nb.ipam.ip_addresses.get(
67
- device=device, interface="Vlan4094"
68
- )
69
- except: # noqa
70
- mlag = None
71
- mlag_vlan = None
72
- mlag_address = None
73
-
74
- # NOTE: only work with /30
75
- try:
76
- x = list(ipaddress.ip_interface(mlag_address.address).network.hosts())
77
- x.remove(ipaddress.ip_interface(mlag_address.address).ip)
78
- mlag_peer_address = x[0]
79
- except: # noqa
80
- mlag_peer_address = None
81
-
82
- try:
83
- mlag_domain_id = device.name.split("-")[1]
84
- except: # noqa
85
- mlag_domain_id = None
86
-
87
- repo = git.Repo.init(path="/state")
88
- repo.config_writer().set_value("user", "name", "Netbox Generator").release()
89
- repo.config_writer().set_value(
90
- "user", "email", "netbox-generator@reconciler.local"
91
- ).release()
92
-
93
- data = {
94
- "hostname": device.name,
95
- "interfaces_ethernet": interfaces_ethernet,
96
- "interfaces_virtual": interfaces_virtual,
97
- "interfaces_port_channels": interfaces_port_channels,
98
- "vlans": vlans,
99
- "device": name,
100
- "nb": utils.nb,
101
- "first": first,
102
- "vlans_as_string": vlans_as_string,
103
- "mlag": mlag,
104
- "mlag_vlan": mlag_vlan,
105
- "mlag_peer_address": mlag_peer_address,
106
- "mlag_domain_id": mlag_domain_id,
107
- "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
108
- "device_type": device.device_type.model,
109
- "device_manufacturer": device.device_type.manufacturer.name,
110
- }
111
-
112
- loader = jinja2.FileSystemLoader(searchpath="/netbox/templates/")
113
- environment = jinja2.Environment(loader=loader)
114
- template = environment.get_template(template)
115
- result = template.render(data)
116
-
117
- logger.info(f"Writing generated configuration to /state/{device.name}.cfg.j2")
118
- with open(f"/state/{device.name}.cfg.j2", "w+") as fp:
119
- fp.write(os.linesep.join([s for s in result.splitlines() if s]))
120
-
121
- # Allow only one change per time
122
- lock = Redlock(key="lock_repository", masters={utils.redis})
123
- lock.acquire()
124
-
125
- repo.git.add(f"/state/{device.name}.cfg.j2")
126
-
127
- try:
128
- if len(repo.index.diff("HEAD")) > 0:
129
- logger.info(f"Committing changes in /state/{device.name}.cfg.j2")
130
- repo.git.commit(message=f"Update {device.name}")
131
-
132
- # Ref 'HEAD' did not resolve to an object
133
- except gitdb.exc.BadName:
134
- logger.info("Initial commit")
135
- repo.git.commit(message="Initial commit")
136
-
137
- lock.release()
@@ -1 +0,0 @@
1
- janhorstmann <horstmann@osism.tech>
@@ -1 +0,0 @@
1
- {"git_version": "314593e", "is_release": false}