user.py 1.6 KB

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