From 396c4b5d10b37d9afc402ea63b004d9df418d1b2 Mon Sep 17 00:00:00 2001 From: Solvik Date: Sun, 4 Aug 2019 00:00:22 +0200 Subject: [PATCH] Project cleanup (#6) * add requirements.txt * Create LICENSE * add test directory * code cleanup * flake8 fixes and code cleanup * setup.py and tox.ini * more fixes --- LICENSE | 201 ++++++++++++++++++++++++++++++++++++++ dev-requirements.txt | 8 ++ netbox_agent/__init__.py | 2 +- netbox_agent/cli.py | 36 +++++++ netbox_agent/dell/dell.py | 11 +-- netbox_agent/dmidecode.py | 26 ++--- netbox_agent/hp/hp.py | 5 +- netbox_agent/main.py | 25 ----- netbox_agent/server.py | 33 ++++--- requirements.txt | 1 + setup.py | 27 +++++ tests/__init__.py | 0 tox.ini | 69 +++++++++++++ 13 files changed, 385 insertions(+), 59 deletions(-) create mode 100644 LICENSE create mode 100644 dev-requirements.txt create mode 100644 netbox_agent/cli.py delete mode 100644 netbox_agent/main.py create mode 100644 requirements.txt create mode 100644 setup.py create mode 100644 tests/__init__.py create mode 100644 tox.ini diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/dev-requirements.txt b/dev-requirements.txt new file mode 100644 index 0000000..502ab4c --- /dev/null +++ b/dev-requirements.txt @@ -0,0 +1,8 @@ +pytest +pytest-cov +mypy +flake8 >= 3.3.0 +pep8-naming >= 0.4.1 +flake8-quotes >= 0.8.1 +flake8-import-order >= 0.9.0 +Sphinx diff --git a/netbox_agent/__init__.py b/netbox_agent/__init__.py index ca5fd98..e8fa341 100644 --- a/netbox_agent/__init__.py +++ b/netbox_agent/__init__.py @@ -1,4 +1,4 @@ -from pkg_resources import get_distribution, DistributionNotFound +from pkg_resources import DistributionNotFound, get_distribution try: __version__ = get_distribution(__name__).version diff --git a/netbox_agent/cli.py b/netbox_agent/cli.py new file mode 100644 index 0000000..c0498bf --- /dev/null +++ b/netbox_agent/cli.py @@ -0,0 +1,36 @@ +import argparse + +from netbox_agent.dell.dell import DellHost +import netbox_agent.dmidecode as dmidecode +from netbox_agent.hp.hp import HPHost + +MANUFACTURERS = { + 'Dell Inc.': DellHost, + 'HP': HPHost, + 'HPE': HPHost, + } + + +def run(args): + manufacturer = dmidecode.get_by_type('Chassis')[0].get('Manufacturer') + server = MANUFACTURERS[manufacturer](dmidecode) + if args.debug: + server.print_debug() + if args.register: + server.netbox_create() + return True + + +def main(): + parser = argparse.ArgumentParser(description='Netbox agent command line') + parser.add_argument('--register', action='store_true', + help='Register server in Netbox') + parser.add_argument('--debug', action='store_true', + help='Print debug informations') + + args = parser.parse_args() + return run(args) + + +if __name__ == '__main__': + main() diff --git a/netbox_agent/dell/dell.py b/netbox_agent/dell/dell.py index 87422d3..748832b 100644 --- a/netbox_agent/dell/dell.py +++ b/netbox_agent/dell/dell.py @@ -1,18 +1,16 @@ -import socket -from pprint import pprint - from netbox_agent.server import ServerBase + class DellHost(ServerBase): def is_blade(self): return self.get_product_name().startswith('PowerEdge M') def get_blade_slot(self): - ''' + """ Return blade slot - dmidecode output is: + dmidecode output is: ` Location In Chassis: Slot 03` - ''' + """ if self.is_blade(): return int(self.dmi.get_by_type('Baseboard')[0].get('Location In Chassis').split()[1]) return None @@ -26,4 +24,3 @@ class DellHost(ServerBase): if self.is_blade(): return self.dmi.get_by_type('chassis')[0]['Serial Number'] return self.get_service_tag() - diff --git a/netbox_agent/dmidecode.py b/netbox_agent/dmidecode.py index b7d0613..9ddc483 100644 --- a/netbox_agent/dmidecode.py +++ b/netbox_agent/dmidecode.py @@ -2,10 +2,10 @@ import re as _re import subprocess as _subprocess -_handle_re = _re.compile("^Handle\\s+(.+),\\s+DMI\\s+type\\s+(\\d+),\\s+(\\d+)\\s+bytes$") -_in_block_re = _re.compile("^\\t\\t(.+)$") -_record_re = _re.compile("\\t(.+):\\s+(.+)$") -_record2_re = _re.compile("\\t(.+):$") +_handle_re = _re.compile('^Handle\\s+(.+),\\s+DMI\\s+type\\s+(\\d+),\\s+(\\d+)\\s+bytes$') +_in_block_re = _re.compile('^\\t\\t(.+)$') +_record_re = _re.compile('\\t(.+):\\s+(.+)$') +_record2_re = _re.compile('\\t(.+):$') _type2str = { 0: 'BIOS', @@ -129,7 +129,7 @@ def get_by_type(type_id): def _execute_cmd(): - return _subprocess.check_output("dmidecode", stderr=_subprocess.PIPE) + return _subprocess.check_output('dmidecode', stderr=_subprocess.PIPE) def _parse(buffer): @@ -153,21 +153,21 @@ def _parse(buffer): dmi_handle = handle_data[0] output_data[dmi_handle] = {} - output_data[dmi_handle]["DMIType"] = int(handle_data[1]) - output_data[dmi_handle]["DMISize"] = int(handle_data[2]) + output_data[dmi_handle]['DMIType'] = int(handle_data[1]) + output_data[dmi_handle]['DMISize'] = int(handle_data[2]) # Okay, we know 2nd line == name - output_data[dmi_handle]["DMIName"] = record_element[1] + output_data[dmi_handle]['DMIName'] = record_element[1] - in_block_elemet = "" - in_block_list = "" + in_block_elemet = '' + in_block_list = '' # Loop over the rest of the record, gathering values for i in range(2, len(record_element), 1): if i >= len(record_element): break # Check whether we are inside a \t\t block - if in_block_elemet != "": + if in_block_elemet != '': in_block_data = _in_block_re.findall(record_element[1]) @@ -175,14 +175,14 @@ def _parse(buffer): if not in_block_list: in_block_list = in_block_data[0][0] else: - in_block_list = in_block_list + "\t\t" + in_block_data[0][1] + in_block_list = in_block_list + '\t\t' + in_block_data[0][1] output_data[dmi_handle][in_block_elemet] = in_block_list continue else: # We are out of the \t\t block; reset it again, and let # the parsing continue - in_block_elemet = "" + in_block_elemet = '' record_data = _record_re.findall(record_element[i]) diff --git a/netbox_agent/hp/hp.py b/netbox_agent/hp/hp.py index d420d71..82746b5 100644 --- a/netbox_agent/hp/hp.py +++ b/netbox_agent/hp/hp.py @@ -1,5 +1,6 @@ from netbox_agent.server import ServerBase + class HPHost(ServerBase): def __init__(self, *args, **kwargs): super(HPHost, self).__init__(*args, **kwargs) @@ -10,11 +11,11 @@ class HPHost(ServerBase): return self.get_product_name().startswith('ProLiant BL') def _find_rack_locator(self): - ''' + """ Depending on the server, the type of the `HP ProLiant System/Rack Locator` can change. So we need to find it every time - ''' + """ # FIXME: make a dmidecode function get_by_dminame() ? if self.is_blade(): for key, value in self.dmi.parse().items(): diff --git a/netbox_agent/main.py b/netbox_agent/main.py deleted file mode 100644 index 2f130bb..0000000 --- a/netbox_agent/main.py +++ /dev/null @@ -1,25 +0,0 @@ -import sys -from pprint import pprint -import netbox_agent.dmidecode as dmidecode -from netbox_agent.dell.dell import DellHost -from netbox_agent.hp.hp import HPHost - -MANUFACTURERS = { - 'Dell Inc.': DellHost, - 'HP': HPHost, - 'HPE': HPHost, - } - -def main(): - manufacturer = dmidecode.get_by_type('Chassis')[0].get('Manufacturer') - print(manufacturer) - server = MANUFACTURERS[manufacturer](dmidecode) - print(server.get_product_name()) - print(server.get_chassis()) - print(server.get_service_tag()) - print(server.get_chassis_service_tag()) - server.netbox_create() -# print(server.get_network_cards()) - -if __name__ == '__main__': - main() diff --git a/netbox_agent/server.py b/netbox_agent/server.py index a5f9ff7..3677ca1 100644 --- a/netbox_agent/server.py +++ b/netbox_agent/server.py @@ -1,13 +1,15 @@ -import re import os +import re import socket -import netbox_agent.dmidecode as dmidecode + from netbox_agent.config import netbox_instance as nb +import netbox_agent.dmidecode as dmidecode # Regex to match base interface name # Doesn't match vlan interfaces and other loopback etc INTERFACE_REGEX = re.compile('^(eth[0-9]+|ens[0-9]+|enp[0-9]+s[0-9]f[0-9])$') + class ServerBase(): def __init__(self, dmi=None): if dmi: @@ -20,15 +22,15 @@ class ServerBase(): self.network_cards = [] def get_product_name(self): - ''' + """ Return the Chassis Name from dmidecode info - ''' + """ return self.system[0]['Product Name'] def get_service_tag(self): - ''' + """ Return the Service Tag from dmidecode info - ''' + """ return self.system[0]['Serial Number'] def is_blade(self): @@ -59,7 +61,7 @@ class ServerBase(): nic = { 'name': interface, 'mac': open('/sys/class/net/{}/address'.format(interface), 'r').read().strip(), - 'ip': None, #FIXME + 'ip': None, # FIXME } nics.append(nic) return nics @@ -74,7 +76,7 @@ class ServerBase(): name='Server Chassis', ) datacenter = nb.dcim.sites.get( - name='DC3' # FIXME: datacenter support + name='DC3', # FIXME: datacenter support ) new_chassis = nb.dcim.devices.create( name=''.format(), @@ -93,7 +95,7 @@ class ServerBase(): model=self.get_product_name(), ) datacenter = nb.dcim.sites.get( - name='DC3' # FIXME: datacenter support + name='DC3', # FIXME: datacenter support ) new_blade = nb.dcim.devices.create( name='{}'.format(socket.gethostname()), @@ -115,7 +117,7 @@ class ServerBase(): if not device_type: raise Exception('Chassis "{}" doesn\'t exist'.format(self.get_chassis())) datacenter = nb.dcim.sites.get( - name='DC3' # FIXME: datacenter support + name='DC3' # FIXME: datacenter support ) new_server = nb.dcim.devices.create( name='{}'.format(socket.gethostname()), @@ -126,7 +128,6 @@ class ServerBase(): ) return new_server - def netbox_create(self): if self.is_blade(): # let's find the blade @@ -155,3 +156,13 @@ class ServerBase(): server = nb.dcim.devices.get(serial=self.get_service_tag()) if not server: self._netbox_create_server() + + def print_debug(self): + # FIXME: do something more generic by looping on every get_* methods + print('Datacenter:', self.get_datacenter()) + print('Netbox Datacenter:', self.get_netbox_datacenter()) + print('Is blade:', self.is_blade()) + print('Product Name:', self.get_product_name()) + print('Chassis:', self.get_chassis()) + print('Chassis service tag:', self.get_chassis_service_tag()) + print('Service tag:', self.get_service_tag()) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0e602b2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pynetbox >= 4.0 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..4a69fa5 --- /dev/null +++ b/setup.py @@ -0,0 +1,27 @@ +from setuptools import setup, find_packages + +setup( + name='netbox_agent', + version='0.1', + description='NetBox agent for server', + long_description=open('README.md', encoding="utf-8").read(), + url='https://github.com/solvik/netbox_agent', + author='Solvik Blum', + author_email='solvik@solvik.fr', + license='Apache2', + include_package_data=True, + use_scm_version=True, + packages=find_packages(), + install_requires=find_packages(), + zip_safe=False, + keywords=['netbox'], + classifiers=[ + 'Intended Audience :: Developers', + 'Development Status :: 5 - Production/Stable', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.6', + ], + entry_points={ + 'console_scripts': ['netbox_agent=netbox_agent.cli:main'], + } +) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..0ce66df --- /dev/null +++ b/tox.ini @@ -0,0 +1,69 @@ +[tox] +# These are the default environments that will be run +# when ``tox`` is run without arguments. +envlist = + py35 + py36 + py37 + coverage + mypy + pep8 + docs +skip_missing_interpreters = true + +[flake8] +# Use the more relaxed max line length permitted in PEP8. +max-line-length = 99 + +# Enforce the Google Python style for grouping and sorting imports: +# https://github.com/google/styleguide/blob/gh-pages/pyguide.md#313-imports-formatting +import-order-style = google + +# Inform flake8-import-order plugin that `fact` should be treated as a local package name. +application-import-names = netbox_agent + +# [testenv] +# setenv = +# COVERAGE_FILE = .coverage.{envname} +# deps = +# -r{toxinidir}/requirements.txt +# -r{toxinidir}/dev-requirements.txt +# commands = +# # Use -bb to enable BytesWarnings as error to catch str/bytes misuse. +# # Use -Werror to treat warnings as errors. +# # Must ignore a DeprecationWarnings triggered in pytest until the following are fixed: +# # https://github.com/pytest-dev/pytest/issues/1403 +# # https://github.com/pytest-dev/pytest/issues/4507 +# # Must ignore DeprecationWarnings in virtualenv site.py only if supporting Travis CI +# # Python 3.5 environments that bundle an older version of virtualenv. +# python -bb \ +# -Werror -Wignore:::_pytest.assertion.rewrite -Wignore:::_pytest.tmpdir -Wignore:::site \ +# -m pytest --cov="{envsitepackagesdir}/fact" + +# [testenv:coverage] +# skip_install = true +# depends = py35,py36,py37 +# # Set blank setenv to overwrite setenv from [testenv] (inherited). +# setenv = +# deps = +# -r{toxinidir}/dev-requirements.txt +# commands = +# coverage combine +# coverage html + +# [testenv:mypy] +# skip_install = true +# setenv = +# deps = +# -r{toxinidir}/requirements.txt +# -r{toxinidir}/dev-requirements.txt +# commands = +# mypy src + +[testenv:pep8] +skip_install = true +setenv = +deps = + -r{toxinidir}/dev-requirements.txt +commands = + flake8 netbox_agent tests