user.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from setux.core.action import Actions, Action
  2. class User_(Action):
  3. '''Ensure User'''
  4. @property
  5. def label(self):
  6. return 'user'
  7. def check(self):
  8. ret, out, err = self.target.run(f'which {self.shell}')
  9. shell = out[0]
  10. usr = self.target.user.fetch(self.user,
  11. uid = self.uid,
  12. gid = self.gid,
  13. shell = shell,
  14. home = f'/home/{self.user}',
  15. )
  16. ok = usr.check() is True
  17. if not ok: return False
  18. ok = usr.home.check() is True
  19. return ok
  20. def deploy(self):
  21. kw = dict()
  22. try:
  23. kw['uid'] = self.uid
  24. except Exception: pass
  25. try:
  26. kw['gid'] = self.gid
  27. except Exception: pass
  28. try:
  29. ret, out, err = self.target.run(f'which {self.shell}')
  30. kw['shell'] = out[0]
  31. except Exception: pass
  32. try:
  33. kw['home'] = f'/home/{self.user}'
  34. except Exception: pass
  35. usr = self.target.user.fetch(self.user, **kw)
  36. return usr.deploy() is True
  37. class Groups(Action):
  38. '''Ensure Group'''
  39. @property
  40. def label(self):
  41. return 'groups'
  42. def check(self):
  43. grp = self.target.groups.fetch(self.user, *self.groups.split())
  44. return grp.check() is True
  45. def deploy(self):
  46. grp = self.target.groups.fetch(self.user, *self.groups.split())
  47. return grp.deploy() is True
  48. class User(Actions):
  49. '''Manage User and its Groups'''
  50. @property
  51. def label(self):
  52. return f'User {self.user}'
  53. @property
  54. def actions(self):
  55. return [
  56. User_,
  57. Groups,
  58. ]