Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Predefined SuperUser #75

Merged
merged 3 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
README.md
nautobot.png
.gitignore
.github/
.vscode/
pyproject.toml
poetry.lock
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ ENV NAPALM_USERNAME=""

ENV NAPALM_PASSWORD=""

ENV NAUTOBOT_USERNAME="demo"

ENV NAUTOBOT_EMAIL="opensource@networktocode.com"

ENV NAUTOBOT_PASSWORD="nautobot"

ENV NAUTOBOT_TOKEN="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

ENV DEBIAN_FRONTEND=noninteractive

ENV DEBCONF_NONINTERACTIVE_SEEN=true
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,18 @@ The container can be built locally, if preferred.
docker run -itd --name nautobot -p 8000:8000 nautobot-lab
```

## The Default Account

The nautobot-lab container comes with a user pre-defined. The username is `demo` and its corresponding password is `nautobot`. If you choose to build your own container, you can define your own attributes for the default account by setting the following environment variables before executing the `docker build` command.

* NAUTOBOT_USERNAME
* NAUTOBOT_EMAIL
* NAUTOBOT_PASSWORD
* NAUTOBOT_TOKEN

## Creating a Super User

Once the container has started and all the services have stabilized, you will need to create a Super User account to start exploring Nautobot. The `nautobot-server createsuperuser` command will prompt you for a username, email address, and password. The email address is unused in this particular workflow and can be left blank.
Once the container has started and all the services have stabilized, you can create a Super User account to start exploring Nautobot. The `nautobot-server createsuperuser` command will prompt you for a username, email address, and password. The email address is unused in this particular workflow and can be left blank.

```shell
% docker exec -it nautobot nautobot-server createsuperuser
Expand All @@ -79,6 +88,7 @@ The Nautobot Lab container comes with the following applications pre-installed:
* [Capacity Metrics](https://github.com/nautobot/nautobot-plugin-capacity-metrics)
* [Golden Config](https://docs.nautobot.com/projects/golden-config/en/latest/user/app_overview/)
* [Nornir](https://docs.nautobot.com/projects/plugin-nornir/en/latest/user/app_overview/)
* [Nautobot SSoT](https://docs.nautobot.com/projects/ssot/en/latest/user/app_overview/)

These applications can give you a sense of what is possible with Nautobot as a network automation platform and can assist you with populating the lab environment with data that is unique to you. For the applications that require access to network devices, you can set the `NAPALM_USERNAME` AND `NAPALM_PASSWORD` environment variables when starting the container.

Expand Down
5 changes: 5 additions & 0 deletions pb_nautobot_install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
- "nautobot-capacity-metrics"
- "nautobot-golden-config"
- "nautobot-plugin-nornir"
- "nautobot-ssot"
- "supervisor"
loop_control:
loop_var: "nautobot_item"
Expand All @@ -115,6 +116,10 @@
# yamllint disable-line rule:line-length
ansible.builtin.command: "{{ nautobot_root }}/bin/nautobot-server post_upgrade"

- name: "SETUP NAUTOBOT DEFAULT ACCOUNT"
ansible.builtin.command:
cmd: "{{ nautobot_root }}/bin/python3 ./templates/default_user.py"

- name: "SET NAUTOBOT DIRECTORY PERMISSIONS"
ansible.builtin.file:
path: "{{ nautobot_root }}"
Expand Down
35 changes: 35 additions & 0 deletions templates/default_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python3

import os
import django
import nautobot

print("Initialize Nautobot and Django Settings.")
nautobot.setup()
django.setup()

from django.contrib.auth import get_user_model
from nautobot.users.models import Token

print("Create User() model instance.")
User = get_user_model()

print(f"Create {os.getenv('NAUTOBOT_USERNAME')} account.")
account = User(
username=os.getenv("NAUTOBOT_USERNAME"),
email=os.getenv("NAUTOBOT_EMAIL"),
is_superuser=True,
is_staff=True,
)
account.set_password(
raw_password=os.getenv("NAUTOBOT_PASSWORD")
)
account.save()

print(f"Create {os.getenv('NAUTOBOT_USERNAME')} API token.")
token = Token(
key=os.getenv("NAUTOBOT_TOKEN"),
write_enabled=True,
user_id=account.pk
)
token.save()
4 changes: 3 additions & 1 deletion templates/nautobot_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@
"nautobot_data_validation_engine",
"nautobot_capacity_metrics",
"nautobot_golden_config",
"nautobot_plugin_nornir"
"nautobot_plugin_nornir",
"nautobot_ssot"
]

# Plugins configuration settings. These settings are used by various plugins that the user may have installed.
Expand All @@ -241,6 +242,7 @@
# }
# }
PLUGINS_CONFIG = {
"nautobot_ssot": {},
"nautobot_device_onboarding": {},
"nautobot_circuit_maintenance": {
"raw_notification_initial_days_since": 100,
Expand Down
18 changes: 4 additions & 14 deletions templates/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import subprocess
import pynautobot
import os
import pytest
import pynautobot


@pytest.fixture
def nautobot():
token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
create_token = [
"u=User.objects.get_or_create(username='demo', password='nautobot', is_staff=True, is_superuser=True)",
f"Token.objects.get_or_create(key='{token}', user_id=u[0].pk, write_enabled=True)",
]
nautobot_shell = [
"nautobot-server",
"nbshell",
"--command",
"; ".join(create_token)
]
subprocess.run(nautobot_shell, check=True)
token = os.getenv("NAUTOBOT_TOKEN")

return pynautobot.api(
url="http://localhost:8000",
token=f"{token}",
Expand Down