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.
 
 
 
 
 
 

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