host.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from setux.core.deploy import Deployers, Deployer
  2. class hostanme(Deployer):
  3. @property
  4. def label(self):
  5. return 'hostname'
  6. def check(self):
  7. ret, out, err = self.target.run('hostname')
  8. return out[0] == self.hostname
  9. def deploy(self):
  10. ret, out, err = self.target.run(f'hostname {self.hostname}')
  11. if ret: raise RuntimeError('!! ' + err[0])
  12. return ret == 0
  13. class etc_hostanme(Deployer):
  14. @property
  15. def label(self):
  16. return '/etc/hostname'
  17. def check(self):
  18. current = self.target.read('/etc/hostname').strip()
  19. return current == self.hostname
  20. def deploy(self):
  21. ok = self.target.write(
  22. '/etc/hostname',
  23. f'{self.hostname}\n',
  24. )
  25. return ok
  26. class etc_hosts(Deployer):
  27. @property
  28. def label(self):
  29. return '/etc/hosts'
  30. def check(self):
  31. current = self.target.read('/etc/hosts')
  32. return self.hostname in current
  33. def deploy(self):
  34. current = self.target.read('/etc/hostname').strip()
  35. new_hosts = self.target.read('/etc/hosts')
  36. new_hosts = new_hosts.replace(current, self.hostname)
  37. ok = self.target.write('/etc/hosts', new_hosts)
  38. return ok
  39. class Hostname(Deployers):
  40. @property
  41. def label(self):
  42. return f'hostname {self.hostname}'
  43. @property
  44. def deployers(self):
  45. return [
  46. etc_hosts,
  47. etc_hostanme,
  48. hostanme,
  49. ]