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.

install.py 6.7 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import os,sys
  2. cgi_bin_path = os.path.sep.join(__file__.split(os.path.sep)[:-3])
  3. sys.path.append(cgi_bin_path)
  4. #
  5. # make a copy of defs.py (if not exists)
  6. #
  7. def copy_defs():
  8. global cgi_bin_path
  9. if not os.path.exists(os.path.join(cgi_bin_path, 'webnotes', 'defs.py')):
  10. ret = os.system('cp '+ os.path.join(cgi_bin_path, 'webnotes', 'defs_template.py')+\
  11. ' '+os.path.join(cgi_bin_path, 'webnotes', 'defs.py'))
  12. print 'Made copy of defs.py'
  13. #
  14. # Main Installer Class
  15. #
  16. class Installer:
  17. def __init__(self, root_login, root_password):
  18. import webnotes
  19. import webnotes.db
  20. import webnotes.defs
  21. self.root_password = root_password
  22. from webnotes.model.db_schema import DbManager
  23. self.conn = webnotes.db.Database(user=root_login, password=root_password)
  24. webnotes.conn=self.conn
  25. webnotes.session= {'user':'Administrator'}
  26. self.dbman = DbManager(self.conn)
  27. self.mysql_path = hasattr(webnotes.defs, 'mysql_path') and webnotes.defs.mysql_path or ''
  28. #
  29. # run framework related cleanups
  30. #
  31. def framework_cleanups(self, target):
  32. import webnotes
  33. self.dbman.drop_table('__DocTypeCache')
  34. webnotes.conn.sql("create table `__DocTypeCache` (name VARCHAR(120), modified DATETIME, content TEXT, server_code_compiled TEXT)")
  35. # set the basic passwords
  36. webnotes.conn.begin()
  37. webnotes.conn.sql("update tabProfile set password = password('admin') where name='Administrator'")
  38. webnotes.conn.commit()
  39. def import_core_module(self):
  40. """
  41. Imports the "Core" module from .txt file and creates
  42. Creates profile Administrator
  43. """
  44. import webnotes
  45. from webnotes.modules import Module
  46. core = Module('core')
  47. core.reload('doctype','doctype')
  48. core.reload('doctype','docfield')
  49. core.reload('doctype','docperm')
  50. core.sync_all(verbose=1)
  51. def create_users(self):
  52. """
  53. Create Administrator / Guest
  54. """
  55. webnotes.conn.begin()
  56. from webnotes.model.doc import Document
  57. p = Document('Profile')
  58. p.name = p.first_name = 'Administrator'
  59. p.email = 'admin@localhost'
  60. p.save(new = 1)
  61. ur = Document('UserRole')
  62. ur.parent = 'Administrator'
  63. ur.role = 'Administrator'
  64. ur.parenttype = 'Profile'
  65. ur.parentfield = 'userroles'
  66. p.enabled = 1
  67. ur.save(1)
  68. p = Document('Profile')
  69. p.name = p.first_name = 'Guest'
  70. p.email = 'guest@localhost'
  71. p.enabled = 1
  72. p.save(new = 1)
  73. ur = Document('UserRole')
  74. ur.parent = 'Guest'
  75. ur.role = 'Guest'
  76. ur.parenttype = 'Profile'
  77. ur.parentfield = 'userroles'
  78. ur.save(1)
  79. webnotes.conn.commit()
  80. def get_db_password(self, db_name):
  81. """
  82. Get the db_password by method
  83. """
  84. import webnotes.defs
  85. if hasattr(webnotes.defs, 'get_db_password'):
  86. return webnotes.defs.get_db_password(db_name)
  87. if hasattr(webnotes.defs, 'db_password'):
  88. return webnotes.defs.db_password
  89. return ''
  90. def import_from_db(self, target, source_path='', password = 'admin', verbose=0):
  91. """
  92. a very simplified version, just for the time being..will eventually be deprecated once the framework stabilizes.
  93. """
  94. import webnotes.defs
  95. # delete user (if exists)
  96. self.dbman.delete_user(target)
  97. # create user and db
  98. self.dbman.create_user(target,self.get_db_password(target))
  99. if verbose: print "Created user %s" % target
  100. # create a database
  101. self.dbman.create_database(target)
  102. if verbose: print "Created database %s" % target
  103. # grant privileges to user
  104. self.dbman.grant_all_privileges(target,target)
  105. if verbose: print "Granted privileges to user %s and database %s" % (target, target)
  106. # flush user privileges
  107. self.dbman.flush_privileges()
  108. self.conn.use(target)
  109. # import in target
  110. if verbose: print "Starting database import..."
  111. # get the path of the sql file to import
  112. source_given = True
  113. if not source_path:
  114. source_given = False
  115. source_path = os.path.join(os.path.sep.join(os.path.abspath(webnotes.__file__).split(os.path.sep)[:-3]), 'data', 'Framework.sql')
  116. self.dbman.restore_database(target, source_path, self.root_password)
  117. if verbose: print "Imported from database %s" % source_path
  118. if not source_given:
  119. if verbose: print "Importing core module..."
  120. self.import_core_module()
  121. self.create_users()
  122. # framework cleanups
  123. self.framework_cleanups(target)
  124. if verbose: print "Ran framework startups on %s" % target
  125. return target
  126. def make_scheduler(root_login, root_password, verbose):
  127. """
  128. Make the database where all scheduler events will be stored from multiple datbases
  129. See webnotes.utils.scheduler for more information
  130. """
  131. conn = webnotes.db.Database(user=root_login, password=root_password)
  132. from webnotes.model.db_schema import DbManager
  133. dbman = DbManager(conn)
  134. # delete user (if exists)
  135. dbman.delete_user('master_scheduler')
  136. # create user and db
  137. dbman.create_user('master_scheduler', getattr(webnotes.defs,'scheduler_password',None))
  138. if verbose: print "Created user master_scheduler"
  139. # create a database
  140. dbman.create_database('master_scheduler')
  141. if verbose: print "Created database master_scheduler"
  142. # grant privileges to user
  143. dbman.grant_all_privileges('master_scheduler','master_scheduler')
  144. # flush user privileges
  145. dbman.flush_privileges()
  146. conn.use('master_scheduler')
  147. # create events table
  148. conn.sql("""create table Event(
  149. `db_name` varchar(60),
  150. `event` varchar(180),
  151. `interval` int(20),
  152. `next_execution` timestamp,
  153. `recurring` int(1),
  154. primary key (`db_name`, `event`),
  155. index next_execution(next_execution)
  156. )""")
  157. conn.sql("""create table EventLog(
  158. `db_name` varchar(180),
  159. `event` varchar(180),
  160. `executed_on` timestamp,
  161. `log` text,
  162. index executed_on(executed_on))
  163. """)
  164. #
  165. # load the options
  166. #
  167. def get_parser():
  168. from optparse import OptionParser
  169. parser = OptionParser(usage="usage: %prog [options] ROOT_LOGIN ROOT_PASSWORD DBNAME")
  170. parser.add_option("-x", "--database-password", dest="password", default="admin", help="Optional: New password for the Framework Administrator, default 'admin'")
  171. parser.add_option("-s", "--source", dest="source_path", default=None, help="Optional: Path of the sql file from which you want to import the instance, default 'data/Framework.sql'")
  172. return parser
  173. #
  174. # execution here
  175. #
  176. if __name__=='__main__':
  177. parser = get_parser()
  178. (options, args) = parser.parse_args()
  179. try:
  180. import webnotes
  181. import webnotes.db
  182. import webnotes.defs
  183. except ImportError:
  184. copy_defs()
  185. import webnotes
  186. import webnotes.db
  187. import webnotes.defs
  188. if len(args)==3:
  189. root_login, root_password, db_name = args[0], args[1], args[2]
  190. if db_name=='master_scheduler':
  191. make_scheduler(root_login, root_password, 1)
  192. else:
  193. inst = Installer(root_login, root_password)
  194. inst.import_from_db(db_name, source_path=options.source_path, \
  195. password = options.password, verbose = 1)
  196. print "Database created, please edit defs.py to get started"
  197. else:
  198. parser.print_help()