You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

259 lines
7.5 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. # MIT License. See license.txt
  3. # called from wnf.py
  4. # lib/wnf.py --install [rootpassword] [dbname] [source]
  5. from __future__ import unicode_literals
  6. import os, sys, json
  7. import webnotes
  8. import webnotes.db
  9. import getpass
  10. from webnotes.model.db_schema import DbManager
  11. from webnotes.model.sync import sync_for
  12. class Installer:
  13. def __init__(self, root_login, root_password=None, db_name=None, site=None):
  14. make_conf(db_name, site=site)
  15. self.site = site
  16. self.make_connection(root_login, root_password, site)
  17. webnotes.local.conn = self.conn
  18. webnotes.local.session = webnotes._dict({'user':'Administrator'})
  19. self.dbman = DbManager(self.conn)
  20. def make_connection(self, root_login, root_password, site):
  21. if root_login:
  22. if not root_password:
  23. try:
  24. webnotes.init(site=site)
  25. root_password = webnotes.conf.get("root_password") or None
  26. except ImportError:
  27. pass
  28. if not root_password:
  29. root_password = getpass.getpass("MySQL root password: ")
  30. self.root_password = root_password
  31. self.conn = webnotes.db.Database(user=root_login, password=root_password)
  32. def install(self, db_name, source_sql=None, admin_password = 'admin', verbose=0,
  33. force=0):
  34. if force or (db_name not in self.dbman.get_database_list()):
  35. # delete user (if exists)
  36. self.dbman.delete_user(db_name)
  37. else:
  38. raise Exception("Database %s already exists" % (db_name,))
  39. # create user and db
  40. self.dbman.create_user(db_name, webnotes.conf.db_password)
  41. if verbose: print "Created user %s" % db_name
  42. # create a database
  43. self.dbman.create_database(db_name)
  44. if verbose: print "Created database %s" % db_name
  45. # grant privileges to user
  46. self.dbman.grant_all_privileges(db_name, db_name)
  47. if verbose: print "Granted privileges to user %s and database %s" % (db_name, db_name)
  48. # flush user privileges
  49. self.dbman.flush_privileges()
  50. # close root connection
  51. self.conn.close()
  52. webnotes.connect(db_name=db_name, site=self.site)
  53. self.dbman = DbManager(webnotes.conn)
  54. # import in db_name
  55. if verbose: print "Starting database import..."
  56. # get the path of the sql file to import
  57. if not source_sql:
  58. source_sql = os.path.join(os.path.dirname(webnotes.__file__), "..",
  59. 'conf', 'Framework.sql')
  60. self.dbman.restore_database(db_name, source_sql, db_name, webnotes.conf.db_password)
  61. if verbose: print "Imported from database %s" % source_sql
  62. self.create_auth_table()
  63. # fresh app
  64. if 'Framework.sql' in source_sql:
  65. print "Installing app..."
  66. self.install_app()
  67. # update admin password
  68. self.update_admin_password(admin_password)
  69. # create public folder
  70. from webnotes.install_lib import setup_public_folder
  71. setup_public_folder.make(site=self.site)
  72. if not self.site:
  73. from webnotes.build import bundle
  74. bundle(False)
  75. return db_name
  76. def install_app(self):
  77. sync_for("lib", force=True, sync_everything=True)
  78. self.import_core_docs()
  79. try:
  80. from startup import install
  81. except ImportError, e:
  82. install = None
  83. if os.path.exists("app"):
  84. sync_for("app", force=True, sync_everything=True)
  85. if os.path.exists(os.path.join("app", "startup", "install_fixtures")):
  86. install_fixtures()
  87. print "Completing App Import..."
  88. install and install.post_import()
  89. print "Updating patches..."
  90. self.set_all_patches_as_completed()
  91. self.assign_all_role_to_administrator()
  92. def update_admin_password(self, password):
  93. from webnotes.auth import _update_password
  94. webnotes.conn.begin()
  95. _update_password("Administrator", webnotes.conf.get("admin_password") or password)
  96. webnotes.conn.commit()
  97. def import_core_docs(self):
  98. install_docs = [
  99. # profiles
  100. {'doctype':'Profile', 'name':'Administrator', 'first_name':'Administrator',
  101. 'email':'admin@localhost', 'enabled':1},
  102. {'doctype':'Profile', 'name':'Guest', 'first_name':'Guest',
  103. 'email':'guest@localhost', 'enabled':1},
  104. # userroles
  105. {'doctype':'UserRole', 'parent': 'Administrator', 'role': 'Administrator',
  106. 'parenttype':'Profile', 'parentfield':'user_roles'},
  107. {'doctype':'UserRole', 'parent': 'Guest', 'role': 'Guest',
  108. 'parenttype':'Profile', 'parentfield':'user_roles'},
  109. {'doctype': "Role", "role_name": "Report Manager"}
  110. ]
  111. webnotes.conn.begin()
  112. for d in install_docs:
  113. bean = webnotes.bean(d)
  114. bean.insert()
  115. webnotes.conn.commit()
  116. def set_all_patches_as_completed(self):
  117. try:
  118. from patches.patch_list import patch_list
  119. except ImportError, e:
  120. print "No patches to update."
  121. return
  122. for patch in patch_list:
  123. webnotes.doc({
  124. "doctype": "Patch Log",
  125. "patch": patch
  126. }).insert()
  127. webnotes.conn.commit()
  128. def assign_all_role_to_administrator(self):
  129. webnotes.bean("Profile", "Administrator").get_controller().add_roles(*webnotes.conn.sql_list("""
  130. select name from tabRole"""))
  131. webnotes.conn.commit()
  132. def create_auth_table(self):
  133. webnotes.conn.sql_ddl("""drop table if exists __Auth""")
  134. webnotes.conn.sql_ddl("""create table __Auth (
  135. `user` VARCHAR(180) NOT NULL PRIMARY KEY,
  136. `password` VARCHAR(180) NOT NULL
  137. ) ENGINE=InnoDB DEFAULT CHARSET=utf8""")
  138. def make_conf(db_name=None, db_password=None, site=None, site_config=None):
  139. try:
  140. import conf
  141. if site:
  142. # conf exists and site is specified, create site_config.json
  143. make_site_config(site, db_name, db_password, site_config)
  144. elif os.path.exists("conf.py"):
  145. print "conf.py exists"
  146. else:
  147. # pyc file exists but py doesn't
  148. raise ImportError
  149. except ImportError:
  150. if site:
  151. raise Exception("conf.py does not exist")
  152. else:
  153. # create conf.py
  154. with open(os.path.join("lib", "conf", "conf.py"), "r") as confsrc:
  155. with open("conf.py", "w") as conftar:
  156. conftar.write(confsrc.read() % get_conf_params(db_name, db_password))
  157. webnotes.destroy()
  158. webnotes.init(site=site)
  159. def make_site_config(site, db_name=None, db_password=None, site_config=None):
  160. import conf
  161. if not getattr(conf, "sites_dir", None):
  162. raise Exception("sites_dir missing in conf.py")
  163. site_path = os.path.join(conf.sites_dir, site)
  164. if not os.path.exists(site_path):
  165. os.mkdir(site_path)
  166. site_file = os.path.join(site_path, "site_config.json")
  167. if not os.path.exists(site_file):
  168. if not site_config:
  169. site_config = get_conf_params(db_name, db_password)
  170. with open(site_file, "w") as f:
  171. f.write(json.dumps(site_config, indent=1))
  172. def get_conf_params(db_name=None, db_password=None):
  173. if not db_name:
  174. db_name = raw_input("Database Name: ")
  175. if not db_name:
  176. raise Exception("Database Name Required")
  177. if not db_password:
  178. from webnotes.utils import random_string
  179. db_password = random_string(16)
  180. return {"db_name": db_name, "db_password": db_password}
  181. def install_fixtures():
  182. print "Importing install fixtures..."
  183. for basepath, folders, files in os.walk(os.path.join("app", "startup", "install_fixtures")):
  184. for f in files:
  185. if f.endswith(".json"):
  186. print "Importing " + f
  187. with open(os.path.join(basepath, f), "r") as infile:
  188. webnotes.bean(json.loads(infile.read())).insert_or_update()
  189. webnotes.conn.commit()
  190. if f.endswith(".csv"):
  191. from core.page.data_import_tool.data_import_tool import import_file_by_path
  192. import_file_by_path(os.path.join(basepath, f), ignore_links = True, overwrite=True)
  193. webnotes.conn.commit()
  194. if os.path.exists(os.path.join("app", "startup", "install_fixtures", "files")):
  195. if not os.path.exists(os.path.join("public", "files")):
  196. os.makedirs(os.path.join("public", "files"))
  197. os.system("cp -r %s %s" % (os.path.join("app", "startup", "install_fixtures", "files", "*"),
  198. os.path.join("public", "files")))