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

Fix DB Import/Export Service Name #73

Merged
merged 5 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 0 additions & 1 deletion environments/docker-compose.base.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---
version: "3.8"
x-nautobot-build: &nautobot-build
build:
args:
Expand Down
1 change: 0 additions & 1 deletion environments/docker-compose.ldap.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---
version: "3.8"
x-nautobot-build: &nautobot-build
build:
args:
Expand Down
1 change: 0 additions & 1 deletion environments/docker-compose.local.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---
version: "3.8"
services:
nautobot:
command: "nautobot-server runserver 0.0.0.0:8080"
Expand Down
1 change: 0 additions & 1 deletion environments/docker-compose.mysql.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---
version: "3.8"
services:
nautobot:
environment:
Expand Down
1 change: 0 additions & 1 deletion environments/docker-compose.postgres.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---
version: "3.8"
services:
db:
image: "postgres:13-alpine"
Expand Down
15 changes: 11 additions & 4 deletions tasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Development Tasks."""

from distutils.util import strtobool
from time import sleep
import os
Expand Down Expand Up @@ -245,11 +246,14 @@ def import_nautobot_data(context):
@task
def db_export(context):
"""Export the database from the dev environment to nautobot.sql."""
docker_compose(context, "up -d postgres")
docker_compose(context, "up -d db")
sleep(2) # Wait for the database to be ready

print("Exporting the database as an SQL dump...")
export_cmd = 'exec postgres sh -c "pg_dump -h localhost -d \${NAUTOBOT_DB_NAME} -U \${NAUTOBOT_DB_USER} > /tmp/nautobot.sql"' # noqa: W605 pylint: disable=anomalous-backslash-in-string
if "docker-compose.mysql.yml" in context.nautobot_docker_compose.compose_files:
export_cmd = 'exec db sh -c "mysqldump -u \${NAUTOBOT_DB_USER} –p \${NAUTOBOT_DB_PASSWORD} \${NAUTOBOT_DB_NAME} nautobot > /tmp/nautobot.sql"' # noqa: W605 pylint: disable=anomalous-backslash-in-string
else:
export_cmd = 'exec db sh -c "pg_dump -h localhost -d \${NAUTOBOT_DB_NAME} -U \${NAUTOBOT_DB_USER} > /tmp/nautobot.sql"' # noqa: W605 pylint: disable=anomalous-backslash-in-string
docker_compose(context, export_cmd, pty=True)

copy_cmd = f"docker cp {context.nautobot_docker_compose.project_name}_postgres_1:/tmp/nautobot.sql dev/nautobot.sql"
Expand All @@ -263,13 +267,16 @@ def db_import(context):
print("Importing Database into Development...\n")

print("Starting Postgres for DB import...\n")
docker_compose(context, "up -d postgres")
docker_compose(context, "up -d db")
sleep(2)

print("Copying DB Dump to DB container...\n")
copy_cmd = f"docker cp dev/nautobot.sql {context.nautobot_docker_compose.project_name}_postgres_1:/tmp/nautobot.sql"
context.run(copy_cmd)

print("Importing DB...\n")
import_cmd = 'exec postgres sh -c "psql -h localhost -U \${NAUTOBOT_DB_USER} < /tmp/nautobot.sql"' # noqa: W605 pylint: disable=anomalous-backslash-in-string
if "docker-compose.mysql.yml" in context.nautobot_docker_compose.compose_files:
import_cmd = 'exec db sh -c "mysql -u \${NAUTOBOT_DB_USER} –p \${NAUTOBOT_DB_PASSWORD} < /tmp/nautobot.sql"' # noqa: W605 pylint: disable=anomalous-backslash-in-string
else:
import_cmd = 'exec db sh -c "psql -h localhost -U \${NAUTOBOT_DB_USER} < /tmp/nautobot.sql"' # noqa: W605 pylint: disable=anomalous-backslash-in-string
docker_compose(context, import_cmd, pty=True)
Loading