PageRenderTime 122ms CodeModel.GetById 60ms app.highlight 9ms RepoModel.GetById 52ms app.codeStats 0ms

/silverlining/commands/restore.py

https://bitbucket.org/ianb/silverlining/
Python | 59 lines | 54 code | 4 blank | 1 comment | 9 complexity | 1f1632f0d18ea6d2981d017730096a56 MD5 | raw file
 1"""Cleans out unused instances from a server"""
 2import os
 3import time
 4from silversupport.shell import ssh, run
 5from silversupport.appdata import normalize_location
 6from silversupport import transfermethods
 7
 8
 9def command_restore(config):
10    backup = config.args.backup
11    hostname, path = normalize_location(config.args.location)
12    dir = move_to_server(backup, hostname)
13    ssh('www-mgr', hostname,
14        '/usr/local/share/silverlining/mgr-scripts/restore-services.py %s %s'
15        % (dir, config.args.location))
16
17
18def move_to_server(backup, hostname):
19    if os.path.exists(backup):
20        if not transfermethods.is_archive(backup):
21            fn = transfermethods.make_temp_name('test.tar.gz')
22            backup = os.path.abspath(backup)
23            run(['tar', 'fcz', fn, os.path.basename(backup)],
24                cwd=os.path.dirname(backup))
25        else:
26            fn = backup
27        dest_path = '/tmp/%s-%s-%s' % (os.getpid(), int(time.time()), os.path.basename(backup))
28        try:
29            run(['scp', '-r', backup, 'www-mgr@%s:%s' % (hostname, dest_path)])
30        finally:
31            if fn != backup:
32                os.unlink(fn)
33        return dest_path
34    if backup.startswith('remote:'):
35        dest_path = '/tmp/%s-%s' % (os.getpid(), time.time())
36        if transfermethods.is_archive(backup):
37            dest_path += os.path.splitext(backup)[1]
38        ssh('www-mgr', hostname,
39            ['cp', '-ar', backup[len('remote:')], dest_path])
40        return dest_path
41    if backup.startswith('site:'):
42        raise NotImplementedError('site: has not yet been implemented')
43    if backup.startswith('ssh:'):
44        backup = backup[len('ssh:'):].lstrip('/')
45        backup_hostname, path = backup.split('/', 1)
46        dest = '/tmp/%s-%s' % (os.getpid(), time.time())
47        ssh('www-mgr',
48            hostname,
49            'scp -r %s:%s %s' % (backup_hostname, path, dest))
50        return dest
51    if backup.startswith('rsync:'):
52        backup = backup[len('rsync:'):]
53        dest = '/tmp/%s-%s' % (os.getpid(), time.time())
54        ssh('www-mgr',
55            hostname,
56            'rsync -r %s %s' % backup, dest)
57        return dest
58    else:
59        assert 0, "Unknown backup location: %r" % backup