transfer.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from pybrary.ascii import clean_name
  2. from setux.logger import silent
  3. from setux.core.action import Action
  4. class Downloader(Action):
  5. '''Download a file
  6. Params:
  7. url = source
  8. dest = destination path
  9. name = action name
  10. sudo = user
  11. mode = destination mode
  12. '''
  13. @property
  14. def label(self):
  15. return f'<- {self.name}'
  16. def check(self):
  17. dest = self.target.file.fetch(self.dest, sudo=self.sudo)
  18. return dest.size and dest.size > 0
  19. def deploy(self):
  20. if self.sudo:
  21. bak = self.dest
  22. self.target.dir('/tmp/setux/dload', mode=777, sudo=self.sudo, verbose=False)
  23. self.dest = f'/tmp/setux/dload/{clean_name(self.name)}'
  24. ok = self.target.download(
  25. url = self.url,
  26. dst = self.dest,
  27. )
  28. if self.sudo:
  29. ret, out, err = self.target.run(f'cp {self.dest} {bak}', sudo=self.sudo, report='quiet')
  30. ok = ok and ret==0
  31. self.dest = bak
  32. self.target.file(self.dest, verbose=False,
  33. user = self.user,
  34. group = self.group,
  35. mode = self.mode,
  36. sudo = self.sudo,
  37. )
  38. return ok
  39. class Sender(Action):
  40. '''Upload a file'''
  41. @property
  42. def labeler(self):
  43. return silent
  44. @property
  45. def label(self):
  46. dst = f' -> {self.dst}' if self.dst != self.src else ''
  47. return f'send {self.src}{dst}'
  48. def check(self):
  49. lhash = self.local.file(self.src, verbose=False).hash
  50. rhash = self.target.file(self.dst, sudo=self.sudo, verbose=False).hash
  51. return rhash == lhash
  52. def deploy(self):
  53. if hasattr(self, 'sudo') and self.sudo:
  54. return self.target.do_send_as(self.sudo, self.src, self.dst)
  55. else:
  56. return self.target.do_send(self.src, self.dst)
  57. class Syncer(Action):
  58. '''Sync a directory'''
  59. @property
  60. def label(self):
  61. dst = self.dst if hasattr(self, 'dst') else self.src
  62. dst = f' -> {dst}' if dst != self.src else ''
  63. return f'sync {self.src}{dst}'
  64. def check(self):
  65. dst = self.dst if hasattr(self, 'dst') else self.src
  66. lhash = self.local.dir(self.src, verbose=False).hash
  67. rhash = self.target.dir(dst, verbose=False).hash
  68. return rhash == lhash
  69. def deploy(self):
  70. dst = self.dst if hasattr(self, 'dst') else self.src
  71. return self.target.do_sync(self.src, dst)