Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

172 Zeilen
5.2 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, conf
  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):
  14. if root_login:
  15. if not root_password:
  16. root_password = getattr(conf, "root_password", None)
  17. if not root_password:
  18. root_password = getpass.getpass("MySQL root password: ")
  19. self.root_password = root_password
  20. self.conn = webnotes.db.Database(user=root_login, password=root_password)
  21. webnotes.conn=self.conn
  22. webnotes.session= webnotes._dict({'user':'Administrator'})
  23. self.dbman = DbManager(self.conn)
  24. def import_from_db(self, target, source_path='', password = 'admin', verbose=0):
  25. """
  26. a very simplified version, just for the time being..will eventually be deprecated once the framework stabilizes.
  27. """
  28. # delete user (if exists)
  29. self.dbman.delete_user(target)
  30. # create user and db
  31. self.dbman.create_user(target, conf.db_password)
  32. if verbose: print "Created user %s" % target
  33. # create a database
  34. self.dbman.create_database(target)
  35. if verbose: print "Created database %s" % target
  36. # grant privileges to user
  37. self.dbman.grant_all_privileges(target,target)
  38. if verbose: print "Granted privileges to user %s and database %s" % (target, target)
  39. # flush user privileges
  40. self.dbman.flush_privileges()
  41. self.conn.use(target)
  42. # import in target
  43. if verbose: print "Starting database import..."
  44. # get the path of the sql file to import
  45. source_given = True
  46. if not source_path:
  47. source_given = False
  48. source_path = os.path.join(os.path.dirname(webnotes.__file__), "..", 'conf', 'Framework.sql')
  49. self.dbman.restore_database(target, source_path, target, conf.db_password)
  50. if verbose: print "Imported from database %s" % source_path
  51. # fresh app
  52. if 'Framework.sql' in source_path:
  53. print "Installing app..."
  54. self.install_app()
  55. # update admin password
  56. self.create_auth_table()
  57. self.update_admin_password(password)
  58. return target
  59. def install_app(self):
  60. sync_for("lib", force=True, sync_everything=True)
  61. self.import_core_docs()
  62. try:
  63. from startup import install
  64. except ImportError, e:
  65. install = None
  66. if os.path.exists("app"):
  67. sync_for("app", force=True, sync_everything=True)
  68. if os.path.exists(os.path.join("app", "startup", "install_fixtures")):
  69. self.import_fixtures()
  70. print "Completing App Import..."
  71. install and install.post_import()
  72. print "Updating patches..."
  73. self.set_all_patches_as_completed()
  74. self.assign_all_role_to_administrator()
  75. def update_admin_password(self, password):
  76. from webnotes.auth import update_password
  77. webnotes.conn.begin()
  78. update_password("Administrator", getattr(conf, "admin_password", password))
  79. webnotes.conn.commit()
  80. def import_fixtures(self):
  81. print "Importing install fixtures..."
  82. for basepath, folders, files in os.walk(os.path.join("app", "startup", "install_fixtures")):
  83. for f in files:
  84. if f.endswith(".json"):
  85. print "Importing " + f
  86. with open(os.path.join(basepath, f), "r") as infile:
  87. webnotes.bean(json.loads(infile.read())).insert_or_update()
  88. webnotes.conn.commit()
  89. if f.endswith(".csv"):
  90. from core.page.data_import_tool import data_import_tool
  91. from webnotes.utils.datautils import read_csv_content
  92. print "Importing " + f
  93. with open(os.path.join(basepath, f), "r") as infile:
  94. data_import_tool.upload(rows = read_csv_content(infile.read()))
  95. webnotes.conn.commit()
  96. def import_core_docs(self):
  97. install_docs = [
  98. # profiles
  99. {'doctype':'Profile', 'name':'Administrator', 'first_name':'Administrator',
  100. 'email':'admin@localhost', 'enabled':1},
  101. {'doctype':'Profile', 'name':'Guest', 'first_name':'Guest',
  102. 'email':'guest@localhost', 'enabled':1},
  103. # userroles
  104. {'doctype':'UserRole', 'parent': 'Administrator', 'role': 'Administrator',
  105. 'parenttype':'Profile', 'parentfield':'user_roles'},
  106. {'doctype':'UserRole', 'parent': 'Guest', 'role': 'Guest',
  107. 'parenttype':'Profile', 'parentfield':'user_roles'},
  108. {'doctype': "Role", "role_name": "Report Manager"}
  109. ]
  110. webnotes.conn.begin()
  111. for d in install_docs:
  112. doc = webnotes.doc(fielddata=d)
  113. doc.insert()
  114. webnotes.conn.commit()
  115. def set_all_patches_as_completed(self):
  116. try:
  117. from patches.patch_list import patch_list
  118. except ImportError, e:
  119. print "No patches to update."
  120. return
  121. for patch in patch_list:
  122. webnotes.doc({
  123. "doctype": "Patch Log",
  124. "patch": patch
  125. }).insert()
  126. webnotes.conn.commit()
  127. def assign_all_role_to_administrator(self):
  128. webnotes.bean("Profile", "Administrator").get_controller().add_roles(*webnotes.conn.sql_list("""
  129. select name from tabRole"""))
  130. webnotes.conn.commit()
  131. def create_auth_table(self):
  132. webnotes.conn.sql("""drop table if exists __Auth""")
  133. webnotes.conn.sql("""create table __Auth (
  134. `user` VARCHAR(180) NOT NULL PRIMARY KEY,
  135. `password` VARCHAR(180) NOT NULL
  136. ) ENGINE=InnoDB DEFAULT CHARSET=utf8""")