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.
 
 
 
 
 
 

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