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

RestoreDB new Task #98

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
23 changes: 21 additions & 2 deletions dockerfiles/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ def up(c, search=True, init=False, reload=True, webpack=False, ext_theme=False,

c.run(' '.join(cmd), pty=True)


@task(help={
'running': 'Open the shell in a running container',
'container': 'Container to open the shell (default: web)'
Expand Down Expand Up @@ -100,7 +99,7 @@ def manage(c, command, running=True, backupdb=False):
})
def attach(c, container):
"""Attach a tty to a running container (useful for pdb)."""
prefix = c['container_prefix'] # readthedocsorg or readthedocs-corporate
prefix = c['container_prefix'] # readthedocsorg or readthedocs-corporate
c.run(f'docker attach --sig-proxy=false --detach-keys="ctrl-p,ctrl-p" {prefix}_{container}_1', pty=True)

@task(help={
Expand Down Expand Up @@ -155,3 +154,23 @@ def buildassets(c):
"""Build all assets for the application and push them to backend storage"""
c.run(f'docker-compose -f {DOCKER_COMPOSE_ASSETS} run --rm assets bash -c "npm ci && node_modules/bower/bin/bower --allow-root update && npm run build"', pty=True)
c.run(f'{DOCKER_COMPOSE_COMMAND} run --rm web python3 manage.py collectstatic --noinput', pty=True)

@task(help={
'file': 'SQL File that should be use to go back old state',
'running': 'Execute in a running container',
})
def restoredb(c, file, running=True):
"""Restore to old db state when running --backupdb migrate"""
subcmd = 'run --rm'
if running:
subcmd = 'exec'

c.run(f"{DOCKER_COMPOSE_COMMAND} 'start database'", pty=True)

c.run(f"docker cp {file} community_database_1:/tmp/dump.sql")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you need to use the same trick we use in https://github.com/readthedocs/common/pull/98/files#diff-302a2219ef42d627f0daf3e4752a1838c41e64b43d7912f149f10cd04f6c30c0R109 because community_ is only for our community code, but for corporate code (readthedocs.com) that should be commercial_ instead.

So, you need to grab that value from the context.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vitorsv1 would you like to update this PR with this change requested?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be possible to use {DOCKER_COMPOSE_PROJECT_NAME}_database_1 in this string following #145


c.run(f'{DOCKER_COMPOSE_COMMAND} {subcmd} database /bin/bash', pty=True)

c.run(f'{DOCKER_COMPOSE_COMMAND} {subcmd} dropdb -U docs_user docs_db', pty=True)
c.run(f'{DOCKER_COMPOSE_COMMAND} {subcmd} createdb -U docs_user docs_db', pty=True)
c.run(f'{DOCKER_COMPOSE_COMMAND} {subcmd} psql -U docs_user docs_db < /tmp/dump.sql', pty=True)