create 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 useradd, groupadd, ln, chown, rm, setquota, chmod, touch, rc_update, usermod
  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 create_user(username, password):
  12. groupadd(username)
  13. useradd("-g", username,
  14. "-G", "wheel",
  15. "-s", "/bin/bash",
  16. "-m",
  17. username)
  18. usermod("-G", username,
  19. "-a", "nginx")
  20. os.system("echo \"%s:%s\" | chpasswd" % (username, password))
  21. def create_database(username, password):
  22. while 1:
  23. root_password = getpass("Enter root password of MySQL: ")
  24. try:
  25. conn = db.connect(host='localhost', user='root', password=root_password)
  26. break
  27. except mysql.connector.errors.ProgrammingError:
  28. continue
  29. cursor = conn.cursor()
  30. cursor.execute(r"CREATE DATABASE %s;" % (username))
  31. cursor.execute(r"CREATE USER '%s'@'localhost' IDENTIFIED BY '%s';"
  32. % (username, password))
  33. cursor.execute(r"GRANT ALL PRIVILEGES ON %s.* to %s@localhost;"
  34. % (username, username))
  35. conn.close()
  36. def create_quota(username, hard):
  37. soft = int(hard * 0.85)
  38. setquota("-u", username, soft, hard, "0", "0", "/home")
  39. def create_domain(username, domain):
  40. config_name = "/etc/nginx/sites-available/%s.conf" % domain
  41. access_log_name = "/var/log/virtualhost/%s_access_log" % domain
  42. error_log_name = "/var/log/virtualhost/%s_error_log" % domain
  43. home_access_log = "/home/%s/logs/%s_access_log" % (username, domain)
  44. home_error_log = "/home/%s/logs/%s_error_log" % (username, domain)
  45. user_group = "%s:%s" % (username, username)
  46. os.system("./nginx_conf %s %s > %s" % (username, domain, config_name))
  47. chown("%s:%s" % (username, username), config_name)
  48. touch(access_log_name)
  49. touch(error_log_name)
  50. ln("-s", access_log_name, home_access_log)
  51. ln("-s", error_log_name, home_error_log)
  52. chown(user_group, access_log_name)
  53. chown(user_group, error_log_name)
  54. chown(user_group, home_access_log)
  55. chown(user_group, home_error_log)
  56. enable_domain(username, domain)
  57. domain_config = Config(CONFIG, username)
  58. domain_config.append_domain(domain)
  59. def create_php(username, domain):
  60. script_name = "/etc/init.d/php-fcgi-%s" % domain
  61. os.system("./php_fcgi %s %s > %s" % (username, domain, script_name))
  62. chmod("+x", script_name)
  63. enable_php(username, domain)
  64. def enable_domain(username, domain):
  65. config_name = "/etc/nginx/sites-enabled/%s.conf" % domain
  66. ln("-s", "/etc/nginx/sites-available/%s.conf" % domain,
  67. "/etc/nginx/sites-enabled/%s.conf" % domain)
  68. chown("%s:%s" % (username, username), config_name)
  69. ln("-s", "/etc/nginx/sites-available/%s.conf" % domain,
  70. "/home/%s/%s.conf" % (username, domain))
  71. def enable_php(username, domain):
  72. script_name = "php-fcgi-%s" % domain
  73. rc_update("add", script_name)
  74. os.system("/etc/init.d/%s start" % script_name)
  75. def main():
  76. parser = argparse.ArgumentParser(description="Add a user")
  77. parser.add_argument("-d", "--domain",
  78. action="store",
  79. dest="domain",
  80. default=False,
  81. help="Set a domain for the user",
  82. type=str)
  83. parser.add_argument("username",
  84. action="store",
  85. default=False,
  86. help="Set a username for the user",
  87. type=str)
  88. parser.add_argument("-p", "--password",
  89. action="store",
  90. dest="password",
  91. default=False,
  92. help="Set a password for the user",
  93. type=str)
  94. parser.add_argument("-q", "--quota",
  95. action="store",
  96. dest="quota",
  97. default=False,
  98. help="Set a limit value in KiB of space usage for the user",
  99. type=int)
  100. parser.add_argument("-s", "--enable-php",
  101. action="store",
  102. dest="php",
  103. default=True,
  104. help="Enable php-fpm for the user",
  105. type=bool)
  106. results = parser.parse_args()
  107. os.system("/etc/init.d/nginx stop")
  108. if results.username and (not results.password):
  109. results.password = getpass("Set a password: ")
  110. if results.username and results.password:
  111. create_user(results.username, results.password)
  112. create_database(results.username, results.password)
  113. if results.username and results.domain:
  114. create_domain(results.username, results.domain)
  115. if results.username and results.quota:
  116. create_quota(results.username, results.quota)
  117. if results.username and results.php:
  118. create_php(results.username, results.domain)
  119. os.system("/etc/init.d/nginx start")
  120. main()