Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added restore_remote_database command

Conflicts:
	deploytool/tasks/remote.py
  • Loading branch information...
commit e46c83ad267e4a526ce66633a64c6d7bc98aa42f 1 parent c0f5379
@mbraak mbraak authored
View
34 deploytool/db/mysql.py
@@ -1,5 +1,5 @@
from fabric.colors import yellow
-from fabric.operations import prompt, sudo, run
+from fabric.operations import prompt, sudo, run, local
class DatabaseOperations(object):
@@ -34,28 +34,29 @@ def backup_database(self, database_name, username, password, file_path):
)
run(command)
- def restore_database(self, database_name, username, password, file_path):
+ def restore_database(self, database_name, username, password, file_path, run_command=run):
def drop_database():
- run(
+ run_command(
'mysqladmin -f --user="%s" --password="%s" drop "%s"' % (username, password, database_name)
)
def create_database():
- run(
+ run_command(
'mysqladmin --user="%s" --password="%s" create "%s"' % (username, password, database_name)
)
drop_database()
create_database()
- self.execute_file(username, password, file_path)
+ self.execute_file(database_name, username, password, file_path, run_command=run_command)
- def execute_file(self, username, password, file_path, options=''):
- return run(
- 'mysql --batch --user=%s --password=%s %s < %s' % (
- username,
- password,
- options,
- file_path
+ def execute_file(self, database_name, username, password, file_path, options='', run_command=run):
+ return run_command(
+ 'mysql --batch --user=%(user)s --password=%(password)s --database=name %(database)s %(options)s < %(file)s' % dict(
+ user=username,
+ password=password,
+ database=database_name,
+ options=options,
+ file=file_path
)
)
@@ -72,4 +73,11 @@ def get_root_password(self):
if not hasattr(self, '_root_password'):
self._root_password = prompt(yellow('Password for mysql root user:'))
- return self._root_password
+ return self._root_password
+
+ def restore_local_database(self, backup_file, django_settings):
+ database_name = django_settings.DATABASES['default']['NAME']
+ database_user = django_settings.DATABASES['default']['USER']
+ database_password = django_settings.DATABASES['default']['PASSWORD']
+
+ self.restore_database(database_name, database_user, database_password, backup_file, run_command=local)
View
19 deploytool/db/postgresql.py
@@ -1,4 +1,4 @@
-from fabric.operations import sudo, run
+from fabric.operations import sudo, run, local
class DatabaseOperations(object):
@@ -11,7 +11,7 @@ def database_exists(self, database_name):
def create_database(self, database_name, owner, password):
if not self.user_exists(owner):
- self.sudo_postgres('createuser %s --createdb' % owner)
+ self.sudo_postgres('createuser %s --createdb --no-superuser --no-createrole' % owner)
if not self.database_exists(database_name):
self._create_database(database_name, owner, True)
@@ -45,4 +45,17 @@ def sudo_postgres(self, command):
def user_exists(self, user):
output = self.execute("SELECT 1 FROM pg_roles WHERE rolname='%s'" % user)
- return output == '1'
+ return output == '1'
+
+ def restore_local_database(self, backup_file, django_settings):
+ database_name = django_settings.DATABASES['default']['NAME']
+ database_user = django_settings.DATABASES['default']['USER']
+
+ result = local('dropdb --if-exists %s' % database_name)
+
+ if result.return_code != 0:
+ raise Exception('Could not remove local database')
+ else:
+ local('createdb %s --owner=%s --encoding=utf8' % (database_name, database_user))
+ local('psql -d %s -f %s' % (database_name, backup_file))
+ local('rm %s' % backup_file)
View
2  deploytool/tasks/generic.py
@@ -39,7 +39,7 @@ def run(self):
'description': _description,
})
- max_name_length = max(max_name_length, len(_name))
+ max_name_length = max(max_name_length, len(_name))
# display pretty custom categorized list
print(yellow('\n+-----------------+\n| Available tasks |\n+-----------------+'))
View
46 deploytool/tasks/remote.py
@@ -1,6 +1,6 @@
import os
+import sys
from datetime import datetime
-import uuid
from fabric.api import *
from fabric.colors import *
@@ -9,9 +9,12 @@
from fabric.operations import require
from fabric.operations import open_shell
from fabric.tasks import Task
+from fabric.contrib import django
+from deploytool.db import get_database_operations
import deploytool.utils as utils
from deploytool.utils.commands import get_python_version
+from deploytool.utils.instance import backup_and_download_database
class RemoteHost(Task):
@@ -532,25 +535,10 @@ class Database(RemoteTask):
name = 'database'
def __call__(self, output_filename=None):
- def generate_output_file():
- timestamp = datetime.today().strftime('%y%m%d%H%M')
- return '%s%s_%s.sql' % (env.project_name_prefix, env.database_name, timestamp)
-
- if not output_filename:
- output_filename = generate_output_file()
-
- remote_filename = os.path.join(env.backup_path, str(uuid.uuid4()))
-
- cwd = os.getcwd()
-
- print(green('\nCreating backup.'))
- utils.instance.backup_database(remote_filename)
-
- print(green('\nDownloading and removing remote backup.'))
- utils.commands.download_file(remote_filename, output_filename)
+ output_filename = backup_and_download_database(output_filename)
print(green('\nSaved backup to:'))
- print(os.path.join(cwd, output_filename))
+ print(output_filename)
class RestoreDatabase(RemoteTask):
@@ -585,3 +573,25 @@ def __call__(self, *args, **kwargs):
# test hook
if ('test' in env):
env.test(env, *args, **kwargs)
+
+
+class RestoreRemoteDatabase(RemoteTask):
+ """ REMO - Restore remote database """
+ name = 'restore_remote_database'
+
+ def __call__(self, *args, **kwargs):
+ settings = self.import_django_settings()
+
+ local_backup_file = backup_and_download_database()
+
+ print('Restoring database on local machine')
+ database_operations = get_database_operations(env.database_engine)
+ database_operations.restore_local_database(local_backup_file, settings)
+
+ def import_django_settings(self):
+ sys.path.append(os.getcwd())
+
+ django.project(env.project_path_name)
+ from django.conf import settings
+
+ return settings
View
21 deploytool/utils/instance.py
@@ -1,3 +1,5 @@
+from datetime import datetime
+import uuid
import os
import json
@@ -69,6 +71,25 @@ def restore_database(file_path):
)
+def backup_and_download_database(local_output_filename=''):
+ def generate_output_file():
+ timestamp = datetime.today().strftime('%y%m%d%H%M')
+ return '%s%s_%s.sql' % (env.project_name_prefix, env.database_name, timestamp)
+
+ if not local_output_filename:
+ local_output_filename = generate_output_file()
+
+ remote_filename = os.path.join(env.backup_path, str(uuid.uuid4()))
+
+ print(green('\nCreating backup.'))
+ backup_database(remote_filename)
+
+ print(green('\nDownloading and removing remote backup.'))
+ commands.download_file(remote_filename, local_output_filename)
+
+ return os.path.join(os.getcwd(), local_output_filename)
+
+
def create_virtualenv(virtualenv_path):
""" Creates virtual environment for instance """
Please sign in to comment.
Something went wrong with that request. Please try again.