PageRenderTime 33ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/silverlining/commands/restore.py

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