25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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