From 51ec8444524495fdf1151b1827616442e81a6fa4 Mon Sep 17 00:00:00 2001 From: Marc Lebreuil Date: Sat, 22 Jun 2024 15:57:12 +0000 Subject: [PATCH] Add utils folder with instructions on how to setup a dev environment --- pyproject.toml | 1 + utils/README.md | 118 ++++++++++-------- utils/database_init.py | 13 +- .../netbox-configuration-final.py | 26 ++-- utils/netbox-configuration.py | 2 +- 5 files changed, 91 insertions(+), 69 deletions(-) rename netbox-configuration.py => utils/netbox-configuration-final.py (94%) diff --git a/pyproject.toml b/pyproject.toml index f6f9bef..a3c256b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,7 @@ readme = "README.md" requires-python = ">=3.10" dependencies = [ 'python-dateutil', + 'drf_yasg', ] classifiers = [ "Programming Language :: Python :: 3", diff --git a/utils/README.md b/utils/README.md index 2349d51..55b3452 100644 --- a/utils/README.md +++ b/utils/README.md @@ -2,24 +2,15 @@ ## Configure the dev environment ### create the dev container -Copy the .devcontainer -File Save workspace as +Create a netbox folder +clone your fork of the netbox-contract repository +Copy the utils/.devcontainer folder to your root directory -ctrl + shift + P -Dev Container: Rebuild container and reopen in container - -## Install Netbox - -Install netbox following the "Option B: Clone the Git Repository": -[netbox installtion doc](https://github.com/netbox-community/netbox/blob/develop/docs/installation/3-netbox.md) +Save the workspace definition file to the root of the netbox folder +File > Save workspace as -### Clone Netbox - -```bash -mkdir netbox -cd netbox -git clone -b master --depth 1 https://github.com/netbox-community/netbox.git . -``` +Press F1 +Dev Container: Rebuild container and reopen in container ### initialisze the database @@ -33,22 +24,35 @@ pip install psycopg2 Run the initialization script ```bash -cd .. -python3 database_init.py +python3 netbox-contract/utils/database_init.py +``` + +## Install Netbox + +[netbox installtion doc](https://netboxlabs.com/docs/netbox/en/stable/installation/3-netbox/) + +### Clone the netbox repository + +```bash +mkdir netbox +cd netbox +git clone -b master --depth 1 https://github.com/netbox-community/netbox.git . ``` +You do not need to create the Betbox system user + ### generate secret key ``` python3 netbox/netbox/generate_secret_key.py ``` -update the netbox netbox-configuration.py with this secret key +update the netbox-contract/utils/netbox-configuration.py with this secret key ### update netbox configuration ``` -cp netbox-configuration.py netbox/netbox/netbox/configuration.py +sudo cp netbox-contract/utils/netbox-configuration.py netbox/netbox/netbox/configuration.py ``` ### Run the Upgrade Script @@ -77,9 +81,41 @@ python3 netbox/netbox/manage.py createsuperuser python3 netbox/netbox/manage.py runserver ``` -### Upgrade Netbox +### Install the plugin + +For development, install the plugin from the local file system: + + ```bash +python3 -m pip install -e netbox-contract +``` + +Update netbox configuration to configure the plugin: + +```bash +sudo cp netbox-contract/utils/netbox-configuration-final.py netbox/netbox/netbox/configuration.py +``` +run database migrations: + +```bash +python3 netbox/netbox/manage.py migrate +``` + +install pre-commit: + +```bash +cd netbox-contract +python -m pip install pre-commit +pre-commit install +``` + +Test the installation + +```bash +cd .. +python3 netbox/netbox/manage.py runserver +``` -Upgrade Netbox: +## Upgrade Netbox ```bash cd netbox @@ -101,7 +137,7 @@ python -m pip install pre-commit pre-commit install ``` -### Backup and restore the db +## Backup and restore the db ```bash pg_dump --username netbox --password --host db netbox > netbox.sql @@ -113,37 +149,9 @@ Restore: psql --host=db --username=postgres --password -c 'drop database netbox' psql --host=db --username=postgres --password -c 'create database netbox' psql --host=db --username=postgres --password netbox < netbox.sql -``` - -## contracts plugin - -### Development environment - -Convention to adhere to: -Netbox [Style Guide](https://docs.netbox.dev/en/stable/development/style-guide/) -Django [Coding style](https://docs.djangoproject.com/en/4.2/internals/contributing/writing-code/coding-style/) - -For this: -All files will be formated using the [black](https://black.readthedocs.io/en/stable/) auto-formatter. -Confiiguration is stored in pyproject.toml - -[isort](https://github.com/PyCQA/isort#readme) is used to automate import sorting. - -Linting and PEP8 style enforcement will be done with [Flake8](https://flake8.pycqa.org/en/latest/) which is a wrapper arround: -- PyFlakes -- pycodestyle -- Ned Batchelder’s McCabe script -Configuration is maintained in the .flake8 file (no support for pyproject.toml) - -The pre-commit Python framework is used to simplify the managment of pre-commit hooks: -Config is stored in .pre-commit-config.yaml - -```bash -python -m pip install pre-commit -pre-commit install -``` +``` -### Model changes +## Model changes Create the migrations: @@ -160,7 +168,7 @@ source netbox/venv/bin/activate python3 netbox/netbox/manage.py migrate ``` -### Generating the distribution archives +## Generating the distribution archives Note: This part is now automated with Github Actions. Each time a release is created, a new package is created. @@ -197,7 +205,7 @@ python3 -m pip install --upgrade twine python3 -m twine upload dist/* ``` -### install the pluggin +## install the pluggin For development install from local file system with the "editable" option: diff --git a/utils/database_init.py b/utils/database_init.py index 8ab852e..e76b1d9 100644 --- a/utils/database_init.py +++ b/utils/database_init.py @@ -11,13 +11,16 @@ # Open a cursor to perform database operations cur = conn.cursor() -# Execute a query -cur.execute('DROP DATABASE netbox;') -cur.execute('DROP USER netbox;') +# The below 2 lines are required if the database already exists +# cur.execute('DROP DATABASE netbox;') +# cur.execute('DROP USER netbox;') + cur.execute('CREATE DATABASE netbox;') cur.execute("CREATE USER netbox WITH PASSWORD 'J5brHrAXFLQSif0K';") -cur.execute('GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;') -cur.execute('ALTER USER netbox CREATEDB;') +cur.execute('ALTER DATABASE netbox OWNER TO netbox;') + +# required on postgres v15 or later +cur.execute('GRANT CREATE ON SCHEMA public TO netbox;') # Make the changes to the database persistent conn.commit() diff --git a/netbox-configuration.py b/utils/netbox-configuration-final.py similarity index 94% rename from netbox-configuration.py rename to utils/netbox-configuration-final.py index 6d783ba..4ef29c3 100644 --- a/netbox-configuration.py +++ b/utils/netbox-configuration-final.py @@ -57,7 +57,7 @@ # For optimal security, SECRET_KEY should be at least 50 characters in length and contain a mix of letters, numbers, and # symbols. NetBox will not run without this defined. For more information, see # https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-SECRET_KEY -SECRET_KEY = 'mNiCp)$_q^XKAPmaOARwu_ytW)Q$5ukOsHH#LqbOUQPeJu&fO!' +SECRET_KEY = 'z(DvJ7Q0)8k)ch9xaQZueQxyfs&&gZ38-oe7jQh&0^KlPU^Yqc' ######################### @@ -169,16 +169,26 @@ METRICS_ENABLED = False # Enable installed plugins. Add the name of each plugin to the list. -PLUGINS = [] +PLUGINS = [ + 'netbox_contract', +] # Plugins configuration settings. These settings are used by various plugins that the user may have installed. # Each key in the dictionary is the name of an installed plugin and its value is a dictionary of settings. -# PLUGINS_CONFIG = { -# 'my_plugin': { -# 'foo': 'bar', -# 'buzz': 'bazz' -# } -# } +PLUGINS_CONFIG = { + 'netbox_contract': { + 'top_level_menu': True, + 'default_accounting_dimensions': { + 'account': '', + 'project': '', + 'cost center': '', + }, + 'mandatory_contract_fields': ['accounting_dimensions'], + 'hidden_contract_fields': [], + 'mandatory_invoice_fields': ['accounting_dimensions'], + 'hidden_invoice_fields': [], + } +} # Remote authentication support REMOTE_AUTH_ENABLED = False diff --git a/utils/netbox-configuration.py b/utils/netbox-configuration.py index 6d783ba..794a1c3 100644 --- a/utils/netbox-configuration.py +++ b/utils/netbox-configuration.py @@ -57,7 +57,7 @@ # For optimal security, SECRET_KEY should be at least 50 characters in length and contain a mix of letters, numbers, and # symbols. NetBox will not run without this defined. For more information, see # https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-SECRET_KEY -SECRET_KEY = 'mNiCp)$_q^XKAPmaOARwu_ytW)Q$5ukOsHH#LqbOUQPeJu&fO!' +SECRET_KEY = 'z(DvJ7Q0)8k)ch9xaQZueQxyfs&&gZ38-oe7jQh&0^KlPU^Yqc' #########################