host.py 1.7 KB

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