Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

459 рядки
14 KiB

  1. from __future__ import unicode_literals, absolute_import
  2. import click
  3. import hashlib, os, sys
  4. import frappe
  5. from frappe.commands import pass_context, get_site
  6. from frappe.commands.scheduler import _is_scheduler_enabled
  7. from frappe.limits import update_limits, get_limits
  8. from frappe.installer import update_site_config
  9. from frappe.utils import touch_file, get_site_path
  10. @click.command('new-site')
  11. @click.argument('site')
  12. @click.option('--db-name', help='Database name')
  13. @click.option('--mariadb-root-username', default='root', help='Root username for MariaDB')
  14. @click.option('--mariadb-root-password', help='Root password for MariaDB')
  15. @click.option('--admin-password', help='Administrator password for new site', default=None)
  16. @click.option('--verbose', is_flag=True, default=False, help='Verbose')
  17. @click.option('--force', help='Force restore if site/database already exists', is_flag=True, default=False)
  18. @click.option('--source_sql', help='Initiate database with a SQL file')
  19. @click.option('--install-app', multiple=True, help='Install app after installation')
  20. def new_site(site, mariadb_root_username=None, mariadb_root_password=None, admin_password=None, verbose=False, install_apps=None, source_sql=None, force=None, install_app=None, db_name=None):
  21. "Create a new site"
  22. if not db_name:
  23. db_name = hashlib.sha1(site).hexdigest()[:10]
  24. frappe.init(site=site, new_site=True)
  25. _new_site(db_name, site, mariadb_root_username=mariadb_root_username, mariadb_root_password=mariadb_root_password, admin_password=admin_password,
  26. verbose=verbose, install_apps=install_app, source_sql=source_sql, force=force)
  27. if len(frappe.utils.get_sites()) == 1:
  28. use(site)
  29. def _new_site(db_name, site, mariadb_root_username=None, mariadb_root_password=None, admin_password=None,
  30. verbose=False, install_apps=None, source_sql=None,force=False, reinstall=False):
  31. """Install a new Frappe site"""
  32. from frappe.installer import install_db, make_site_dirs
  33. from frappe.installer import install_app as _install_app
  34. import frappe.utils.scheduler
  35. frappe.init(site=site)
  36. try:
  37. # enable scheduler post install?
  38. enable_scheduler = _is_scheduler_enabled()
  39. except:
  40. enable_scheduler = False
  41. make_site_dirs()
  42. try:
  43. installing = touch_file(get_site_path('locks', 'installing.lock'))
  44. install_db(root_login=mariadb_root_username, root_password=mariadb_root_password, db_name=db_name,
  45. admin_password=admin_password, verbose=verbose, source_sql=source_sql,force=force, reinstall=reinstall)
  46. apps_to_install = ['frappe'] + (frappe.conf.get("install_apps") or []) + (list(install_apps) or [])
  47. for app in apps_to_install:
  48. _install_app(app, verbose=verbose, set_as_patched=not source_sql)
  49. frappe.utils.scheduler.toggle_scheduler(enable_scheduler)
  50. frappe.db.commit()
  51. scheduler_status = "disabled" if frappe.utils.scheduler.is_scheduler_disabled() else "enabled"
  52. print "*** Scheduler is", scheduler_status, "***"
  53. finally:
  54. os.remove(installing)
  55. frappe.destroy()
  56. @click.command('restore')
  57. @click.argument('sql-file-path')
  58. @click.option('--mariadb-root-username', default='root', help='Root username for MariaDB')
  59. @click.option('--mariadb-root-password', help='Root password for MariaDB')
  60. @click.option('--db-name', help='Database name for site in case it is a new one')
  61. @click.option('--admin-password', help='Administrator password for new site')
  62. @click.option('--install-app', multiple=True, help='Install app after installation')
  63. @click.option('--with-public-files', help='Restores the public files of the site, given path to its tar file')
  64. @click.option('--with-private-files', help='Restores the private files of the site, given path to its tar file')
  65. @pass_context
  66. def restore(context, sql_file_path, mariadb_root_username=None, mariadb_root_password=None, db_name=None, verbose=None, install_app=None, admin_password=None, force=None, with_public_files=None, with_private_files=None):
  67. "Restore site database from an sql file"
  68. from frappe.installer import extract_sql_gzip, extract_tar_files
  69. # Extract the gzip file if user has passed *.sql.gz file instead of *.sql file
  70. if not os.path.exists(sql_file_path):
  71. sql_file_path = '../' + sql_file_path
  72. if not os.path.exists(sql_file_path):
  73. print 'Invalid path {0}' + sql_file_path[3:]
  74. sys.exit(1)
  75. if sql_file_path.endswith('sql.gz'):
  76. sql_file_path = extract_sql_gzip(os.path.abspath(sql_file_path))
  77. site = get_site(context)
  78. frappe.init(site=site)
  79. db_name = db_name or frappe.conf.db_name or hashlib.sha1(site).hexdigest()[:10]
  80. _new_site(db_name, site, mariadb_root_username=mariadb_root_username, mariadb_root_password=mariadb_root_password, admin_password=admin_password, verbose=context.verbose, install_apps=install_app, source_sql=sql_file_path, force=context.force)
  81. # Extract public and/or private files to the restored site, if user has given the path
  82. if with_public_files:
  83. public = extract_tar_files(site, with_public_files, 'public')
  84. os.remove(public)
  85. if with_private_files:
  86. private = extract_tar_files(site, with_private_files, 'private')
  87. os.remove(private)
  88. @click.command('reinstall')
  89. @pass_context
  90. def reinstall(context):
  91. "Reinstall site ie. wipe all data and start over"
  92. site = get_site(context)
  93. try:
  94. frappe.init(site=site)
  95. frappe.connect()
  96. frappe.clear_cache()
  97. installed = frappe.get_installed_apps()
  98. frappe.clear_cache()
  99. except Exception:
  100. installed = []
  101. finally:
  102. if frappe.db:
  103. frappe.db.close()
  104. frappe.destroy()
  105. frappe.init(site=site)
  106. _new_site(frappe.conf.db_name, site, verbose=context.verbose, force=True, reinstall=True, install_apps=installed)
  107. @click.command('install-app')
  108. @click.argument('app')
  109. @pass_context
  110. def install_app(context, app):
  111. "Install a new app to site"
  112. from frappe.installer import install_app as _install_app
  113. for site in context.sites:
  114. frappe.init(site=site)
  115. frappe.connect()
  116. try:
  117. _install_app(app, verbose=context.verbose)
  118. finally:
  119. frappe.destroy()
  120. @click.command('list-apps')
  121. @pass_context
  122. def list_apps(context):
  123. "List apps in site"
  124. site = get_site(context)
  125. frappe.init(site=site)
  126. frappe.connect()
  127. print "\n".join(frappe.get_installed_apps())
  128. frappe.destroy()
  129. @click.command('add-system-manager')
  130. @click.argument('email')
  131. @click.option('--first-name')
  132. @click.option('--last-name')
  133. @click.option('--send-welcome-email', default=False, is_flag=True)
  134. @pass_context
  135. def add_system_manager(context, email, first_name, last_name, send_welcome_email):
  136. "Add a new system manager to a site"
  137. import frappe.utils.user
  138. for site in context.sites:
  139. frappe.connect(site=site)
  140. try:
  141. frappe.utils.user.add_system_manager(email, first_name, last_name, send_welcome_email)
  142. frappe.db.commit()
  143. finally:
  144. frappe.destroy()
  145. @click.command('disable-user')
  146. @click.argument('email')
  147. @pass_context
  148. def disable_user(context, email):
  149. site = get_site(context)
  150. with frappe.init_site(site):
  151. frappe.connect()
  152. user = frappe.get_doc("User", email)
  153. user.enabled = 0
  154. user.save(ignore_permissions=True)
  155. frappe.db.commit()
  156. @click.command('migrate')
  157. @click.option('--rebuild-website', help="Rebuild webpages after migration")
  158. @pass_context
  159. def migrate(context, rebuild_website=False):
  160. "Run patches, sync schema and rebuild files/translations"
  161. from frappe.migrate import migrate
  162. for site in context.sites:
  163. print 'Migrating', site
  164. frappe.init(site=site)
  165. frappe.connect()
  166. try:
  167. migrate(context.verbose, rebuild_website=rebuild_website)
  168. finally:
  169. frappe.destroy()
  170. @click.command('run-patch')
  171. @click.argument('module')
  172. @pass_context
  173. def run_patch(context, module):
  174. "Run a particular patch"
  175. import frappe.modules.patch_handler
  176. for site in context.sites:
  177. frappe.init(site=site)
  178. try:
  179. frappe.connect()
  180. frappe.modules.patch_handler.run_single(module, force=context.force)
  181. finally:
  182. frappe.destroy()
  183. @click.command('reload-doc')
  184. @click.argument('module')
  185. @click.argument('doctype')
  186. @click.argument('docname')
  187. @pass_context
  188. def reload_doc(context, module, doctype, docname):
  189. "Reload schema for a DocType"
  190. for site in context.sites:
  191. try:
  192. frappe.init(site=site)
  193. frappe.connect()
  194. frappe.reload_doc(module, doctype, docname, force=context.force)
  195. frappe.db.commit()
  196. finally:
  197. frappe.destroy()
  198. @click.command('use')
  199. @click.argument('site')
  200. def _use(site, sites_path='.'):
  201. "Set a default site"
  202. use(site, sites_path=sites_path)
  203. def use(site, sites_path='.'):
  204. with open(os.path.join(sites_path, "currentsite.txt"), "w") as sitefile:
  205. sitefile.write(site)
  206. @click.command('backup')
  207. @click.option('--with-files', default=False, is_flag=True, help="Take backup with files")
  208. @pass_context
  209. def backup(context, with_files=False, backup_path_db=None, backup_path_files=None,
  210. backup_path_private_files=None, quiet=False):
  211. "Backup"
  212. from frappe.utils.backups import scheduled_backup
  213. verbose = context.verbose
  214. for site in context.sites:
  215. frappe.init(site=site)
  216. frappe.connect()
  217. odb = scheduled_backup(ignore_files=not with_files, backup_path_db=backup_path_db, backup_path_files=backup_path_files, backup_path_private_files=backup_path_private_files, force=True)
  218. if verbose:
  219. from frappe.utils import now
  220. print "database backup taken -", odb.backup_path_db, "- on", now()
  221. if with_files:
  222. print "files backup taken -", odb.backup_path_files, "- on", now()
  223. print "private files backup taken -", odb.backup_path_private_files, "- on", now()
  224. frappe.destroy()
  225. @click.command('remove-from-installed-apps')
  226. @click.argument('app')
  227. @pass_context
  228. def remove_from_installed_apps(context, app):
  229. "Remove app from site's installed-apps list"
  230. from frappe.installer import remove_from_installed_apps
  231. for site in context.sites:
  232. try:
  233. frappe.init(site=site)
  234. frappe.connect()
  235. remove_from_installed_apps(app)
  236. finally:
  237. frappe.destroy()
  238. @click.command('uninstall-app')
  239. @click.argument('app')
  240. @click.option('--dry-run', help='List all doctypes that will be deleted', is_flag=True, default=False)
  241. @pass_context
  242. def uninstall(context, app, dry_run=False):
  243. "Remove app and linked modules from site"
  244. from frappe.installer import remove_app
  245. for site in context.sites:
  246. try:
  247. frappe.init(site=site)
  248. frappe.connect()
  249. remove_app(app, dry_run)
  250. finally:
  251. frappe.destroy()
  252. @click.command('drop-site')
  253. @click.argument('site')
  254. @click.option('--root-login', default='root')
  255. @click.option('--root-password')
  256. @click.option('--archived-sites-path')
  257. def drop_site(site, root_login='root', root_password=None, archived_sites_path=None):
  258. "Remove site from database and filesystem"
  259. from frappe.installer import get_current_host, make_connection
  260. from frappe.model.db_schema import DbManager
  261. from frappe.utils.backups import scheduled_backup
  262. frappe.init(site=site)
  263. frappe.connect()
  264. scheduled_backup(ignore_files=False, force=True)
  265. db_name = frappe.local.conf.db_name
  266. frappe.local.db = make_connection(root_login, root_password)
  267. dbman = DbManager(frappe.local.db)
  268. dbman.delete_user(db_name, get_current_host())
  269. dbman.drop_database(db_name)
  270. if not archived_sites_path:
  271. archived_sites_path = os.path.join(frappe.get_app_path('frappe'), '..', '..', '..', 'archived_sites')
  272. if not os.path.exists(archived_sites_path):
  273. os.mkdir(archived_sites_path)
  274. move(archived_sites_path, site)
  275. def move(dest_dir, site):
  276. import os
  277. if not os.path.isdir(dest_dir):
  278. raise Exception, "destination is not a directory or does not exist"
  279. frappe.init(site)
  280. old_path = frappe.utils.get_site_path()
  281. new_path = os.path.join(dest_dir, site)
  282. # check if site dump of same name already exists
  283. site_dump_exists = True
  284. count = 0
  285. while site_dump_exists:
  286. final_new_path = new_path + (count and str(count) or "")
  287. site_dump_exists = os.path.exists(final_new_path)
  288. count = int(count or 0) + 1
  289. os.rename(old_path, final_new_path)
  290. frappe.destroy()
  291. return final_new_path
  292. @click.command('set-admin-password')
  293. @click.argument('admin-password')
  294. @pass_context
  295. def set_admin_password(context, admin_password):
  296. "Set Administrator password for a site"
  297. import getpass
  298. from frappe.utils.password import update_password
  299. for site in context.sites:
  300. try:
  301. frappe.init(site=site)
  302. while not admin_password:
  303. admin_password = getpass.getpass("Administrator's password for {0}: ".format(site))
  304. frappe.connect()
  305. update_password('Administrator', admin_password)
  306. frappe.db.commit()
  307. admin_password = None
  308. finally:
  309. frappe.destroy()
  310. @click.command('set-limit')
  311. @click.option('--site', help='site name')
  312. @click.argument('limit')
  313. @click.argument('value')
  314. @pass_context
  315. def set_limit(context, site, limit, value):
  316. """Sets user / space / email limit for a site"""
  317. _set_limits(context, site, ((limit, value),))
  318. @click.command('set-limits')
  319. @click.option('--site', help='site name')
  320. @click.option('--limit', 'limits', type=(unicode, unicode), multiple=True)
  321. @pass_context
  322. def set_limits(context, site, limits):
  323. _set_limits(context, site, limits)
  324. def _set_limits(context, site, limits):
  325. import datetime
  326. if not limits:
  327. return
  328. if not site:
  329. site = get_site(context)
  330. with frappe.init_site(site):
  331. new_limits = {}
  332. for limit, value in limits:
  333. if limit not in ('emails', 'space', 'users', 'email_group',
  334. 'expiry', 'support_email', 'support_chat', 'upgrade_url'):
  335. frappe.throw('Invalid limit {0}'.format(limit))
  336. if limit=='expiry':
  337. try:
  338. datetime.datetime.strptime(value, '%Y-%m-%d')
  339. except ValueError:
  340. raise ValueError("Incorrect data format, should be YYYY-MM-DD")
  341. elif limit=='space':
  342. value = float(value)
  343. elif limit in ('users', 'emails', 'email_group'):
  344. value = int(value)
  345. new_limits[limit] = value
  346. update_limits(new_limits)
  347. @click.command('clear-limits')
  348. @click.option('--site', help='site name')
  349. @click.argument('limits', nargs=-1, type=click.Choice(['emails', 'space', 'users', 'email_group',
  350. 'expiry', 'support_email', 'support_chat', 'upgrade_url']))
  351. @pass_context
  352. def clear_limits(context, site, limits):
  353. """Clears given limit from the site config, and removes limit from site config if its empty"""
  354. from frappe.limits import clear_limit as _clear_limit
  355. if not limits:
  356. return
  357. if not site:
  358. site = get_site(context)
  359. with frappe.init_site(site):
  360. _clear_limit(limits)
  361. # Remove limits from the site_config, if it's empty
  362. limits = get_limits()
  363. if not limits:
  364. update_site_config('limits', 'None', validate=False)
  365. commands = [
  366. add_system_manager,
  367. backup,
  368. drop_site,
  369. install_app,
  370. list_apps,
  371. migrate,
  372. new_site,
  373. reinstall,
  374. reload_doc,
  375. remove_from_installed_apps,
  376. restore,
  377. run_patch,
  378. set_admin_password,
  379. uninstall,
  380. set_limit,
  381. set_limits,
  382. clear_limits,
  383. disable_user,
  384. _use,
  385. ]