Skip to content

Commit

Permalink
Merge pull request #13 from k-304/support-netbox-on-psql-cluster
Browse files Browse the repository at this point in the history
Add support for standby NetBox on read only PSQL database
  • Loading branch information
k-304 authored Oct 11, 2023
2 parents 77158ec + 8c70e10 commit ce5148d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
20 changes: 18 additions & 2 deletions ansible/roles/netbox/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ netbox__domain: '{{ ansible_domain }}'

# ]]]
# ]]]
# Primary/Standby configuration [[[
# -----------------------------

# .. envvar:: netbox__primary [[[
#
# Boolean to define if the instance of NetBox will be primary or not.
# True: This instance will become primary and needs read and write
# database access.
# False: This instance will become standby,
# "netbox__config_maintenance_mode" will be set to True and
# "netbox__config_session_file_path" will be populated.
netbox__primary: True

# ]]]
# ]]]

# APT packages [[[
# ----------------

Expand Down Expand Up @@ -660,7 +676,7 @@ netbox__config_base_path: ''
#
# Enable or disable maintenance mode banner.
# This overwrites potentially existing `dynamic configuration settings <https://netbox.readthedocs.io/en/stable/configuration/dynamic-settings/>`__.
netbox__config_maintenance_mode: False
netbox__config_maintenance_mode: '{{ not netbox__primary | bool }}'

# ]]]
# .. envvar:: netbox__config_napalm_username [[[
Expand Down Expand Up @@ -833,7 +849,7 @@ netbox__config_metrics_enabled: False
# can be useful for enabling authentication on a standby instance with
# read-only database access.) Note that the user as which NetBox runs must have
# read and write permissions to this path.
netbox__config_session_file_path: False
netbox__config_session_file_path: '{{ "" if netbox__primary | bool else netbox__data + "/sessions" }}'

# ]]]
# .. envvar:: netbox__config_media_root [[[
Expand Down
57 changes: 55 additions & 2 deletions ansible/roles/netbox/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,61 @@
PATH: '{{ netbox__virtualenv_env_path }}'
become: True
become_user: '{{ netbox__user }}'
when: netbox__register_checkout is changed
when: (netbox__register_checkout is changed and
netbox__primary|bool)
register: netbox__register_migration

- name: Generate static content
## Since we do not run the manage.py file on secondary sites
## we need to generate those files in an extra task.
shell: # noqa no-handler
cmd: |
set -o nounset -o pipefail -o errexit
./manage.py collectstatic --no-input
chdir: '{{ netbox__git_checkout + "/netbox" }}'
executable: 'bash'
environment:
VIRTUAL_ENV: '{{ netbox__virtualenv }}'
PATH: '{{ netbox__virtualenv_env_path }}'
become: True
become_user: '{{ netbox__user }}'
when: (netbox__register_checkout is changed and
not netbox__primary|bool)
register: netbox__register_collectstatic
changed_when: not netbox__register_collectstatic.stdout is search('0 static files copied')

- name: Create local session directory
file:
path: '{{ netbox__data + "/sessions" }}'
owner: '{{ netbox__user }}'
group: '{{ netbox__group }}'
mode: '0770'
access_time: preserve
modification_time: preserve
state: directory
become: True
become_user: '{{ netbox__user }}'
when: (not netbox__primary|bool)

- name: Cleanup stale contenttypes and sessions
## Since we do not run the manage.py file on secondary sites
## we need to run the cleanup in an extra task.
shell: # noqa no-handler
cmd: |
set -o nounset -o pipefail -o errexit
./manage.py remove_stale_contenttypes --no-input
./manage.py clearsessions
chdir: '{{ netbox__git_checkout + "/netbox" }}'
executable: 'bash'
environment:
VIRTUAL_ENV: '{{ netbox__virtualenv }}'
PATH: '{{ netbox__virtualenv_env_path }}'
become: True
become_user: '{{ netbox__user }}'
when: (netbox__register_checkout is changed and
not netbox__primary|bool)
changed_when: false

- name: Create Django superuser account
environment:
DJANGO_SUPERUSER_PASSWORD: '{{ netbox__superuser_password }}'
Expand All @@ -225,7 +277,8 @@
virtualenv: '{{ netbox__virtualenv }}'
become: True
become_user: '{{ netbox__user }}'
when: (not netbox__register_installed.stat.exists|bool and
when: (netbox__primary|bool and
not netbox__register_installed.stat.exists|bool and
not netbox__register_migration.stdout is search('No migrations to apply.'))
no_log: '{{ debops__no_log | d(True) }}'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ SCRIPTS_ROOT = '{{ netbox__config_scripts_root }}'
# By default, NetBox will store session data in the database. Alternatively, a file path can be specified here to use
# local file storage instead. (This can be useful for enabling authentication on a standby instance with read-only
# database access.) Note that the user as which NetBox runs must have read and write permissions to this path.
SESSION_FILE_PATH = {{ netbox__config_session_file_path }}
SESSION_FILE_PATH = '{{ netbox__config_session_file_path }}'

# Time zone (default: UTC)
TIME_ZONE = '{{ netbox__config_time_zone }}'
Expand Down

0 comments on commit ce5148d

Please sign in to comment.