Skip to content

Commit

Permalink
Database: Use fabric instead of ssh subprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
kovshenin committed Oct 26, 2021
1 parent 8c423b2 commit 78ca12b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Added: New `site-verification.yaml` blueprint to add TXT records
* Changed: Deploys, rollbacks and release-tracking are now 100% client-side
* Changed: Split `--host` and `--root` arguments for `sail ssh shell`
* Changed: Use Fabric instead of SSH subprocess in db and full backups
* Changed: Some overall refactoring

## [0.9.17] - 2021-10-13
Expand Down
66 changes: 14 additions & 52 deletions sail/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def import_cmd(path):
'''Import a local .sql or .sql.gz file to the production MySQL database'''
root = util.find_root()
config = util.config()
c = util.connection()

path = pathlib.Path(path).resolve()
if not path.exists():
Expand Down Expand Up @@ -62,36 +63,16 @@ def import_cmd(path):
click.echo('- Importing database into MySQL')
cat_bin = 'zcat' if is_gz else 'cat'

p = subprocess.Popen(['ssh',
'-i', '%s/.sail/ssh.key' % root,
'-o', 'UserKnownHostsFile="%s/.sail/known_hosts"' % root,
'-o', 'IdentitiesOnly=yes',
'-o', 'IdentityFile="%s/.sail/ssh.key"' % root,
'root@%s' % config['hostname'],
'docker exec sail bash -c "%s /var/www/%s | mysql -uroot wordpress"' % (cat_bin, temp_name)
])

while p.poll() is None:
util.loader()

if p.returncode != 0:
try:
c.run('docker exec sail bash -c "%s /var/www/%s | mysql -uroot wordpress"' % (cat_bin, temp_name))
except:
raise click.ClickException('An error occurred in SSH. Please try again.')

click.echo('- Cleaning up production')

p = subprocess.Popen(['ssh',
'-i', '%s/.sail/ssh.key' % root,
'-o', 'UserKnownHostsFile="%s/.sail/known_hosts"' % root,
'-o', 'IdentitiesOnly=yes',
'-o', 'IdentityFile="%s/.sail/ssh.key"' % root,
'root@%s' % config['hostname'],
'docker exec sail rm /var/www/%s' % temp_name
])

while p.poll() is None:
util.loader()

if p.returncode != 0:
try:
c.run('docker exec sail rm /var/www/%s' % temp_name)
except:
raise click.ClickException('An error occurred in SSH. Please try again.')

click.echo('- Database imported')
Expand All @@ -101,26 +82,17 @@ def export():
'''Export the production database to a local .sql.gz file'''
root = util.find_root()
config = util.config()
c = util.connection()

backups_dir = pathlib.Path(root + '/.backups')
backups_dir.mkdir(parents=True, exist_ok=True)
filename = datetime.now().strftime('%Y-%m-%d-%H%M%S.sql.gz')

click.echo('# Exporting WordPress database')

p = subprocess.Popen(['ssh',
'-i', '%s/.sail/ssh.key' % root,
'-o', 'UserKnownHostsFile="%s/.sail/known_hosts"' % root,
'-o', 'IdentitiesOnly=yes',
'-o', 'IdentityFile="%s/.sail/ssh.key"' % root,
'root@%s' % config['hostname'],
'docker exec sail bash -c "mysqldump --quick --single-transaction --default-character-set=utf8mb4 -uroot wordpress | gzip -c9 > /var/www/%s"' % filename
])

while p.poll() is None:
util.loader()

if p.returncode != 0:
try:
c.run('docker exec sail bash -c "mysqldump --quick --single-transaction --default-character-set=utf8mb4 -uroot wordpress | gzip -c9 > /var/www/%s"' % filename)
except:
raise click.ClickException('An error occurred in SSH. Please try again.')

click.echo('- Export completed, downloading')
Expand All @@ -135,19 +107,9 @@ def export():

click.echo('- Cleaning up production')

p = subprocess.Popen(['ssh',
'-i', '%s/.sail/ssh.key' % root,
'-o', 'UserKnownHostsFile="%s/.sail/known_hosts"' % root,
'-o', 'IdentitiesOnly=yes',
'-o', 'IdentityFile="%s/.sail/ssh.key"' % root,
'root@%s' % config['hostname'],
'docker exec sail rm /var/www/%s' % filename
])

while p.poll() is None:
util.loader()

if p.returncode != 0:
try:
c.run('docker exec sail rm /var/www/%s' % filename)
except:
raise click.ClickException('An error occurred in SSH. Please try again.')

click.echo('- Database export saved to .backups/%s' % filename)

0 comments on commit 78ca12b

Please sign in to comment.