destroy 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env python3
  2. # This file a part of SHM. Copyright (C) Tom Li 2013.
  3. # License: LGPL version 3 or later.
  4. from sh import userdel, ln, chown, rm, setquota, rc_update
  5. import os
  6. from getpass import getpass
  7. import argparse
  8. import mysql.connector as db
  9. from config import Config
  10. CONFIG = "/var/shm/config"
  11. def delete_user(username):
  12. try:
  13. userdel("-r", username)
  14. except Exception as e:
  15. print("Warning: %s" % e)
  16. def delete_database(username):
  17. while 1:
  18. password = getpass("Enter root password of MySQL: ")
  19. try:
  20. conn = db.connect(host='localhost', user='root', password=password)
  21. break
  22. except:
  23. print("Wrong password, try again!")
  24. continue
  25. cursor = conn.cursor()
  26. try:
  27. cursor.execute(r"DROP DATABASE %s;" % (username))
  28. except Exception as e:
  29. print("Warning: %s" % e)
  30. try:
  31. cursor.execute(r"DROP USER '%s'@'localhost';"
  32. % (username))
  33. except Exception as e:
  34. print("Warning %s" % e)
  35. conn.close()
  36. def delete_quota(username):
  37. setquota("-u", username, 0, 0, "0", "0", "/home")
  38. def disable_domain(username, domain):
  39. script_name = "php-fcgi-%s" % domain
  40. try:
  41. rm("/etc/nginx/sites-enabled/%s.conf" % domain)
  42. except Exception as e:
  43. print("Warning: %s" % e)
  44. rc_update("del", script_name)
  45. def delete_domain(username, domain):
  46. try:
  47. rm("/etc/nginx/sites-enabled/%s.conf" % domain)
  48. rm("/etc/nginx/sites-available/%s.conf" % domain)
  49. os.system("/etc/init.d/php-fcgi-%s stop" % domain)
  50. rm("/etc/init.d/php-fcgi-%s" % domain)
  51. except Exception as e:
  52. print("Warning: %s" % e)
  53. domain_config = Config(CONFIG, username)
  54. domain_config.remove_domain(domain)
  55. def delete_all_domain(username):
  56. domain_config = Config(CONFIG, username)
  57. domain = domain_config.domain[0]
  58. delete_domain(username, domain)
  59. def main():
  60. parser = argparse.ArgumentParser(description="Delete a user")
  61. parser.add_argument("username",
  62. action="store",
  63. default=False,
  64. help="Delete the user",
  65. type=str)
  66. parser.add_argument("-q", "--quota",
  67. action="store_true",
  68. dest="quota",
  69. default=False,
  70. help="Remove the limit value of space usage for the user")
  71. results = parser.parse_args()
  72. if results.username:
  73. if results.quota:
  74. delete_quota(results.username)
  75. if not (results.quota):
  76. os.system("/etc/init.d/nginx stop")
  77. delete_all_domain(results.username)
  78. delete_user(results.username)
  79. delete_database(results.username)
  80. os.system("/etc/init.d/nginx start")
  81. main()