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.
 
 
 
 
 
 

177 lines
5.8 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. # called from wnf.py
  23. # lib/wnf.py --install [rootpassword] [dbname] [source]
  24. from __future__ import unicode_literals
  25. import os, sys
  26. import webnotes, conf
  27. import webnotes.db
  28. import getpass
  29. from webnotes.model.db_schema import DbManager
  30. from webnotes.model.sync import sync_for
  31. class Installer:
  32. def __init__(self, root_login, root_password=None):
  33. if root_login:
  34. if not root_password:
  35. root_password = getattr(conf, "root_password", None)
  36. if not root_password:
  37. root_password = getpass.getpass("MySQL root password: ")
  38. self.root_password = root_password
  39. self.conn = webnotes.db.Database(user=root_login, password=root_password)
  40. webnotes.conn=self.conn
  41. webnotes.session= webnotes._dict({'user':'Administrator'})
  42. self.dbman = DbManager(self.conn)
  43. def import_from_db(self, target, source_path='', password = 'admin', verbose=0):
  44. """
  45. a very simplified version, just for the time being..will eventually be deprecated once the framework stabilizes.
  46. """
  47. # delete user (if exists)
  48. self.dbman.delete_user(target)
  49. # create user and db
  50. self.dbman.create_user(target, conf.db_password)
  51. if verbose: print "Created user %s" % target
  52. # create a database
  53. self.dbman.create_database(target)
  54. if verbose: print "Created database %s" % target
  55. # grant privileges to user
  56. self.dbman.grant_all_privileges(target,target)
  57. if verbose: print "Granted privileges to user %s and database %s" % (target, target)
  58. # flush user privileges
  59. self.dbman.flush_privileges()
  60. self.conn.use(target)
  61. # import in target
  62. if verbose: print "Starting database import..."
  63. # get the path of the sql file to import
  64. source_given = True
  65. if not source_path:
  66. source_given = False
  67. source_path = os.path.join(os.path.sep.join(os.path.abspath(webnotes.__file__).split(os.path.sep)[:-3]), 'data', 'Framework.sql')
  68. self.dbman.restore_database(target, source_path, target, conf.db_password)
  69. if verbose: print "Imported from database %s" % source_path
  70. # fresh app
  71. if 'Framework.sql' in source_path:
  72. print "Installing app..."
  73. self.install_app()
  74. # update admin password
  75. self.create_auth_table()
  76. self.update_admin_password(password)
  77. return target
  78. def install_app(self):
  79. try:
  80. from startup import install
  81. except ImportError, e:
  82. print "No app install found"
  83. sync_for("lib", force=True, sync_everything=True)
  84. self.import_core_docs()
  85. install.pre_import()
  86. sync_for("app", force=True, sync_everything=True)
  87. print "Completing App Import..."
  88. install.post_import()
  89. print "Updating patches..."
  90. self.set_all_patches_as_completed()
  91. def update_admin_password(self, password):
  92. from webnotes.auth import update_password
  93. webnotes.conn.begin()
  94. update_password("Administrator", getattr(conf, "admin_password", password))
  95. webnotes.conn.commit()
  96. def import_core_docs(self):
  97. install_docs = [
  98. {'doctype':'Module Def', 'name': 'Core', 'module_name':'Core'},
  99. # roles
  100. {'doctype':'Role', 'role_name': 'Administrator', 'name': 'Administrator'},
  101. {'doctype':'Role', 'role_name': 'All', 'name': 'All'},
  102. {'doctype':'Role', 'role_name': 'System Manager', 'name': 'System Manager'},
  103. {'doctype':'Role', 'role_name': 'Report Manager', 'name': 'Report Manager'},
  104. {'doctype':'Role', 'role_name': 'Guest', 'name': 'Guest'},
  105. # profiles
  106. {'doctype':'Profile', 'name':'Administrator', 'first_name':'Administrator',
  107. 'email':'admin@localhost', 'enabled':1},
  108. {'doctype':'Profile', 'name':'Guest', 'first_name':'Guest',
  109. 'email':'guest@localhost', 'enabled':1},
  110. # userroles
  111. {'doctype':'UserRole', 'parent': 'Administrator', 'role': 'Administrator',
  112. 'parenttype':'Profile', 'parentfield':'user_roles'},
  113. {'doctype':'UserRole', 'parent': 'Guest', 'role': 'Guest',
  114. 'parenttype':'Profile', 'parentfield':'user_roles'}
  115. ]
  116. webnotes.conn.begin()
  117. for d in install_docs:
  118. doc = webnotes.doc(fielddata=d)
  119. doc.insert()
  120. webnotes.conn.commit()
  121. # from webnotes.modules.import_file import import_files
  122. #
  123. # import_files([
  124. # ["core", "doctype", "docperm"],
  125. # ["core", "doctype", "docfield"],
  126. # ["core", "doctype", "doctype"],
  127. # ])
  128. def set_all_patches_as_completed(self):
  129. try:
  130. from patches.patch_list import patch_list
  131. except ImportError, e:
  132. print "No patches to update."
  133. return
  134. webnotes.conn.begin()
  135. for patch in patch_list:
  136. webnotes.doc({
  137. "doctype": "Patch Log",
  138. "patch": patch
  139. }).insert()
  140. webnotes.conn.commit()
  141. def create_auth_table(self):
  142. webnotes.conn.sql("""drop table if exists __Auth""")
  143. webnotes.conn.sql("""create table __Auth (
  144. `user` VARCHAR(180) NOT NULL PRIMARY KEY,
  145. `password` VARCHAR(180) NOT NULL
  146. ) ENGINE=InnoDB DEFAULT CHARSET=utf8""")