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.
 
 
 
 
 
 

148 regels
4.6 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. import os,sys
  25. class Installer:
  26. def __init__(self, root_login, root_password):
  27. import webnotes
  28. import webnotes.db
  29. self.root_password = root_password
  30. from webnotes.model.db_schema import DbManager
  31. self.conn = webnotes.db.Database(user=root_login, password=root_password)
  32. webnotes.conn=self.conn
  33. webnotes.session= {'user':'Administrator'}
  34. self.dbman = DbManager(self.conn)
  35. def import_from_db(self, target, source_path='', password = 'admin', verbose=0):
  36. """
  37. a very simplified version, just for the time being..will eventually be deprecated once the framework stabilizes.
  38. """
  39. import conf
  40. # delete user (if exists)
  41. self.dbman.delete_user(target)
  42. # create user and db
  43. self.dbman.create_user(target, conf.db_password)
  44. if verbose: print "Created user %s" % target
  45. # create a database
  46. self.dbman.create_database(target)
  47. if verbose: print "Created database %s" % target
  48. # grant privileges to user
  49. self.dbman.grant_all_privileges(target,target)
  50. if verbose: print "Granted privileges to user %s and database %s" % (target, target)
  51. # flush user privileges
  52. self.dbman.flush_privileges()
  53. self.conn.use(target)
  54. # import in target
  55. if verbose: print "Starting database import..."
  56. # get the path of the sql file to import
  57. source_given = True
  58. if not source_path:
  59. source_given = False
  60. source_path = os.path.join(os.path.sep.join(os.path.abspath(webnotes.__file__).split(os.path.sep)[:-3]), 'data', 'Framework.sql')
  61. self.dbman.restore_database(target, source_path, self.root_password)
  62. if verbose: print "Imported from database %s" % source_path
  63. # fresh app
  64. if 'Framework.sql' in source_path:
  65. from webnotes.model.sync import sync_install
  66. print "Building tables from all module..."
  67. sync_install()
  68. # framework cleanups
  69. self.framework_cleanups(target)
  70. if verbose: print "Ran framework startups on %s" % target
  71. return target
  72. def framework_cleanups(self, target):
  73. """create framework internal tables"""
  74. import webnotes
  75. self.create_sessions_table()
  76. self.create_scheduler_log()
  77. self.create_session_cache()
  78. self.create_cache_item()
  79. # set the basic passwords
  80. webnotes.conn.begin()
  81. webnotes.conn.sql("""update tabProfile set password = password('admin')
  82. where name='Administrator'""")
  83. webnotes.conn.commit()
  84. def create_sessions_table(self):
  85. """create sessions table"""
  86. import webnotes
  87. self.dbman.drop_table('tabSessions')
  88. webnotes.conn.sql("""CREATE TABLE `tabSessions` (
  89. `user` varchar(40) DEFAULT NULL,
  90. `sid` varchar(120) DEFAULT NULL,
  91. `sessiondata` longtext,
  92. `ipaddress` varchar(16) DEFAULT NULL,
  93. `lastupdate` datetime DEFAULT NULL,
  94. `status` varchar(20) DEFAULT NULL,
  95. KEY `sid` (`sid`)
  96. ) ENGINE=InnoDB DEFAULT CHARSET=utf8""")
  97. def create_scheduler_log(self):
  98. import webnotes
  99. self.dbman.drop_table('__SchedulerLog')
  100. webnotes.conn.sql("""create table __SchedulerLog (
  101. `timestamp` timestamp,
  102. method varchar(200),
  103. error text
  104. ) engine=MyISAM""")
  105. def create_session_cache(self):
  106. import webnotes
  107. self.dbman.drop_table('__SessionCache')
  108. webnotes.conn.sql("""create table `__SessionCache` (
  109. user VARCHAR(120),
  110. country VARCHAR(120),
  111. cache LONGTEXT)""")
  112. def create_cache_item(self):
  113. import webnotes
  114. self.dbman.drop_table('__CacheItem')
  115. webnotes.conn.sql("""create table __CacheItem(
  116. `key` VARCHAR(180) NOT NULL PRIMARY KEY,
  117. `value` TEXT,
  118. `expires_on` TIMESTAMP
  119. )""")