host.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from setux.core.action import Actions, Action
  2. class hostanme(Action):
  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}', sudo='root')
  11. if ret: raise RuntimeError('!! ' + err[0])
  12. return ret == 0
  13. class etc_hostanme(Action):
  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. sudo = 'root',
  25. )
  26. return ok
  27. class etc_hosts(Action):
  28. @property
  29. def label(self):
  30. return '/etc/hosts'
  31. def check(self):
  32. current = self.target.read('/etc/hosts')
  33. return self.hostname in current
  34. def deploy(self):
  35. current = self.target.read('/etc/hostname').strip()
  36. new_hosts = self.target.read('/etc/hosts')
  37. new_hosts = new_hosts.replace(current, self.hostname)
  38. ok = self.target.write(
  39. '/etc/hosts',
  40. new_hosts,
  41. sudo = 'root',
  42. )
  43. return ok
  44. class Hostname(Actions):
  45. @property
  46. def label(self):
  47. return f'hostname {self.hostname}'
  48. @property
  49. def actions(self):
  50. return [
  51. etc_hosts,
  52. etc_hostanme,
  53. hostanme,
  54. ]