Skip to content

Commit

Permalink
Add utils folder
Browse files Browse the repository at this point in the history
with instructions on how to setup a
dev environment
  • Loading branch information
mlebreuil committed Jun 22, 2024
1 parent 90ecf7a commit 51ec844
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 69 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ readme = "README.md"
requires-python = ">=3.10"
dependencies = [
'python-dateutil',
'drf_yasg',
]
classifiers = [
"Programming Language :: Python :: 3",
Expand Down
118 changes: 63 additions & 55 deletions utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:

Expand All @@ -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.
Expand Down Expand Up @@ -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:

Expand Down
13 changes: 8 additions & 5 deletions utils/database_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
26 changes: 18 additions & 8 deletions netbox-configuration.py → utils/netbox-configuration-final.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'


#########################
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion utils/netbox-configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'


#########################
Expand Down

0 comments on commit 51ec844

Please sign in to comment.