main.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. # -*- coding: utf-8 -*-
  2. """
  3. flaskbb.cli.commands
  4. ~~~~~~~~~~~~~~~~~~~~
  5. This module contains the main commands.
  6. :copyright: (c) 2016 by the FlaskBB Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. import binascii
  10. import logging
  11. import os
  12. import sys
  13. import time
  14. import traceback
  15. from datetime import datetime
  16. import click
  17. import click_log
  18. from celery.bin.celery import CeleryCommand
  19. from flask import current_app
  20. from flask.cli import FlaskGroup, ScriptInfo, with_appcontext
  21. from flask_alembic import alembic_click
  22. from jinja2 import Environment, FileSystemLoader
  23. from sqlalchemy_utils.functions import database_exists
  24. from werkzeug.utils import import_string
  25. from flaskbb import create_app
  26. from flaskbb.cli.utils import (EmailType, FlaskBBCLIError, get_version,
  27. prompt_config_path, prompt_save_user,
  28. write_config)
  29. from flaskbb.extensions import alembic, celery, db, whooshee
  30. from flaskbb.utils.populate import (create_default_groups,
  31. create_default_settings, create_latest_db,
  32. create_test_data, create_welcome_forum,
  33. insert_bulk_data, run_plugin_migrations,
  34. update_settings_from_fixture)
  35. from flaskbb.utils.translations import compile_translations
  36. logger = logging.getLogger(__name__)
  37. click_log.basic_config(logger)
  38. class FlaskBBGroup(FlaskGroup):
  39. def __init__(self, *args, **kwargs):
  40. super(FlaskBBGroup, self).__init__(*args, **kwargs)
  41. self._loaded_flaskbb_plugins = False
  42. def _load_flaskbb_plugins(self, ctx):
  43. if self._loaded_flaskbb_plugins:
  44. return
  45. try:
  46. app = ctx.ensure_object(ScriptInfo).load_app()
  47. app.pluggy.hook.flaskbb_cli(cli=self, app=app)
  48. self._loaded_flaskbb_plugins = True
  49. except Exception:
  50. logger.error(
  51. "Error while loading CLI Plugins",
  52. exc_info=traceback.format_exc()
  53. )
  54. else:
  55. shell_context_processors = app.pluggy.hook.flaskbb_shell_context()
  56. for p in shell_context_processors:
  57. app.shell_context_processor(p)
  58. def get_command(self, ctx, name):
  59. self._load_flaskbb_plugins(ctx)
  60. return super(FlaskBBGroup, self).get_command(ctx, name)
  61. def list_commands(self, ctx):
  62. self._load_flaskbb_plugins(ctx)
  63. return super(FlaskBBGroup, self).list_commands(ctx)
  64. def make_app(script_info):
  65. config_file = getattr(script_info, "config_file", None)
  66. instance_path = getattr(script_info, "instance_path", None)
  67. return create_app(config_file, instance_path)
  68. def set_config(ctx, param, value):
  69. """This will pass the config file to the create_app function."""
  70. ctx.ensure_object(ScriptInfo).config_file = value
  71. def set_instance(ctx, param, value):
  72. """This will pass the instance path on the script info which can then
  73. be used in 'make_app'."""
  74. ctx.ensure_object(ScriptInfo).instance_path = value
  75. @click.group(cls=FlaskBBGroup, create_app=make_app, add_version_option=False,
  76. invoke_without_command=True)
  77. @click.option("--config", expose_value=False, callback=set_config,
  78. required=False, is_flag=False, is_eager=True, metavar="CONFIG",
  79. help="Specify the config to use either in dotted module "
  80. "notation e.g. 'flaskbb.configs.default.DefaultConfig' "
  81. "or by using a path like '/path/to/flaskbb.cfg'")
  82. @click.option("--instance", expose_value=False, callback=set_instance,
  83. required=False, is_flag=False, is_eager=True, metavar="PATH",
  84. help="Specify the instance path to use. By default the folder "
  85. "'instance' next to the package or module is assumed to "
  86. "be the instance path.")
  87. @click.option("--version", expose_value=False, callback=get_version,
  88. is_flag=True, is_eager=True, help="Show the FlaskBB version.")
  89. @click.pass_context
  90. @click_log.simple_verbosity_option(logger)
  91. def flaskbb(ctx):
  92. """This is the commandline interface for flaskbb."""
  93. if ctx.invoked_subcommand is None:
  94. # show the help text instead of an error
  95. # when just '--config' option has been provided
  96. click.echo(ctx.get_help())
  97. flaskbb.add_command(alembic_click, "db")
  98. @flaskbb.command()
  99. @click.option("--welcome", "-w", default=True, is_flag=True,
  100. help="Disable the welcome forum.")
  101. @click.option("--force", "-f", default=False, is_flag=True,
  102. help="Doesn't ask for confirmation.")
  103. @click.option("--username", "-u", help="The username of the user.")
  104. @click.option("--email", "-e", type=EmailType(),
  105. help="The email address of the user.")
  106. @click.option("--password", "-p", help="The password of the user.")
  107. @click.option("--no-plugins", "-n", default=False, is_flag=True,
  108. help="Don't run the migrations for the default plugins.")
  109. @with_appcontext
  110. def install(welcome, force, username, email, password, no_plugins):
  111. """Installs flaskbb. If no arguments are used, an interactive setup
  112. will be run.
  113. """
  114. click.secho("[+] Installing FlaskBB...", fg="cyan")
  115. if database_exists(db.engine.url):
  116. if force or click.confirm(click.style(
  117. "Existing database found. Do you want to delete the old one and "
  118. "create a new one?", fg="magenta")
  119. ):
  120. db.drop_all()
  121. else:
  122. sys.exit(0)
  123. # creating database from scratch and 'stamping it'
  124. create_latest_db()
  125. click.secho("[+] Creating default settings...", fg="cyan")
  126. create_default_groups()
  127. create_default_settings()
  128. click.secho("[+] Creating admin user...", fg="cyan")
  129. prompt_save_user(username, email, password, "admin")
  130. if welcome:
  131. click.secho("[+] Creating welcome forum...", fg="cyan")
  132. create_welcome_forum()
  133. if not no_plugins:
  134. click.secho("[+] Installing default plugins...", fg="cyan")
  135. run_plugin_migrations()
  136. click.secho("[+] Compiling translations...", fg="cyan")
  137. compile_translations()
  138. click.secho("[+] FlaskBB has been successfully installed!",
  139. fg="green", bold=True)
  140. @flaskbb.command()
  141. @click.option("--test-data", "-t", default=False, is_flag=True,
  142. help="Adds some test data.")
  143. @click.option("--bulk-data", "-b", default=False, is_flag=True,
  144. help="Adds a lot of data.")
  145. @click.option("--posts", default=100,
  146. help="Number of posts to create in each topic (default: 100).")
  147. @click.option("--topics", default=100,
  148. help="Number of topics to create (default: 100).")
  149. @click.option("--force", "-f", is_flag=True,
  150. help="Will delete the database before populating it.")
  151. @click.option("--initdb", "-i", is_flag=True,
  152. help="Initializes the database before populating it.")
  153. def populate(bulk_data, test_data, posts, topics, force, initdb):
  154. """Creates the necessary tables and groups for FlaskBB."""
  155. if force:
  156. click.secho("[+] Recreating database...", fg="cyan")
  157. db.drop_all()
  158. # do not initialize the db if -i is passed
  159. if not initdb:
  160. create_latest_db()
  161. if initdb:
  162. click.secho("[+] Initializing database...", fg="cyan")
  163. create_latest_db()
  164. run_plugin_migrations()
  165. if test_data:
  166. click.secho("[+] Adding some test data...", fg="cyan")
  167. create_test_data()
  168. if bulk_data:
  169. click.secho("[+] Adding a lot of test data...", fg="cyan")
  170. timer = time.time()
  171. rv = insert_bulk_data(int(topics), int(posts))
  172. if not rv and not test_data:
  173. create_test_data()
  174. rv = insert_bulk_data(int(topics), int(posts))
  175. elapsed = time.time() - timer
  176. click.secho("[+] It took {:.2f} seconds to create {} topics and {} "
  177. "posts.".format(elapsed, rv[0], rv[1]), fg="cyan")
  178. # this just makes the most sense for the command name; use -i to
  179. # init the db as well
  180. if not test_data and not bulk_data:
  181. click.secho("[+] Populating the database with some defaults...",
  182. fg="cyan")
  183. create_default_groups()
  184. create_default_settings()
  185. @flaskbb.command()
  186. def reindex():
  187. """Reindexes the search index."""
  188. click.secho("[+] Reindexing search index...", fg="cyan")
  189. whooshee.reindex()
  190. @flaskbb.command()
  191. @click.option("all_latest", "--all", "-a", default=False, is_flag=True,
  192. help="Upgrades migrations AND fixtures to the latest version.")
  193. @click.option("--fixture/", "-f", default=None,
  194. help="The fixture which should be upgraded or installed.")
  195. @click.option("--force", default=False, is_flag=True,
  196. help="Forcefully upgrades the fixtures.")
  197. def upgrade(all_latest, fixture, force):
  198. """Updates the migrations and fixtures."""
  199. if all_latest:
  200. click.secho("[+] Upgrading migrations to the latest version...",
  201. fg="cyan")
  202. alembic.upgrade()
  203. if fixture or all_latest:
  204. try:
  205. settings = import_string(
  206. "flaskbb.fixtures.{}".format(fixture)
  207. )
  208. settings = settings.fixture
  209. except ImportError:
  210. raise FlaskBBCLIError("{} fixture is not available"
  211. .format(fixture), fg="red")
  212. click.secho("[+] Updating fixtures...", fg="cyan")
  213. count = update_settings_from_fixture(
  214. fixture=settings, overwrite_group=force, overwrite_setting=force
  215. )
  216. click.secho("[+] {settings} settings in {groups} setting groups "
  217. "updated.".format(groups=len(count), settings=sum(
  218. len(settings) for settings in count.values())
  219. ), fg="green")
  220. @flaskbb.command("celery", add_help_option=False,
  221. context_settings={"ignore_unknown_options": True,
  222. "allow_extra_args": True})
  223. @click.pass_context
  224. @with_appcontext
  225. def start_celery(ctx):
  226. """Preconfigured wrapper around the 'celery' command."""
  227. CeleryCommand(celery).execute_from_commandline(
  228. ["flaskbb celery"] + ctx.args
  229. )
  230. @flaskbb.command("shell", short_help="Runs a shell in the app context.")
  231. @with_appcontext
  232. def shell_command():
  233. """Runs an interactive Python shell in the context of a given
  234. Flask application. The application will populate the default
  235. namespace of this shell according to it"s configuration.
  236. This is useful for executing small snippets of management code
  237. without having to manually configuring the application.
  238. This code snippet is taken from Flask"s cli module and modified to
  239. run IPython and falls back to the normal shell if IPython is not
  240. available.
  241. """
  242. import code
  243. banner = "Python %s on %s\nInstance Path: %s" % (
  244. sys.version,
  245. sys.platform,
  246. current_app.instance_path,
  247. )
  248. ctx = {"db": db}
  249. # Support the regular Python interpreter startup script if someone
  250. # is using it.
  251. startup = os.environ.get("PYTHONSTARTUP")
  252. if startup and os.path.isfile(startup):
  253. with open(startup, "r") as f:
  254. eval(compile(f.read(), startup, "exec"), ctx)
  255. ctx.update(current_app.make_shell_context())
  256. try:
  257. import IPython
  258. IPython.embed(banner1=banner, user_ns=ctx)
  259. except ImportError:
  260. code.interact(banner=banner, local=ctx)
  261. @flaskbb.command("urls", short_help="Show routes for the app.")
  262. @click.option("--route", "-r", "order_by", flag_value="rule", default=True,
  263. help="Order by route")
  264. @click.option("--endpoint", "-e", "order_by", flag_value="endpoint",
  265. help="Order by endpoint")
  266. @click.option("--methods", "-m", "order_by", flag_value="methods",
  267. help="Order by methods")
  268. @with_appcontext
  269. def list_urls(order_by):
  270. """Lists all available routes."""
  271. from flask import current_app
  272. rules = sorted(
  273. current_app.url_map.iter_rules(),
  274. key=lambda rule: getattr(rule, order_by)
  275. )
  276. max_rule_len = max(len(rule.rule) for rule in rules)
  277. max_rule_len = max(max_rule_len, len("Route"))
  278. max_endpoint_len = max(len(rule.endpoint) for rule in rules)
  279. max_endpoint_len = max(max_endpoint_len, len("Endpoint"))
  280. max_method_len = max(len(", ".join(rule.methods)) for rule in rules)
  281. max_method_len = max(max_method_len, len("Methods"))
  282. column_header_len = max_rule_len + max_endpoint_len + max_method_len + 4
  283. column_template = "{:<%s} {:<%s} {:<%s}" % (
  284. max_rule_len, max_endpoint_len, max_method_len
  285. )
  286. click.secho(column_template.format("Route", "Endpoint", "Methods"),
  287. fg="blue", bold=True)
  288. click.secho("=" * column_header_len, bold=True)
  289. for rule in rules:
  290. methods = ", ".join(rule.methods)
  291. click.echo(column_template.format(rule.rule, rule.endpoint, methods))
  292. @flaskbb.command("makeconfig")
  293. @click.option("--development", "-d", default=False, is_flag=True,
  294. help="Creates a development config with DEBUG set to True.")
  295. @click.option("--output", "-o", required=False,
  296. help="The path where the config file will be saved at. "
  297. "Defaults to the flaskbb's root folder.")
  298. @click.option("--force", "-f", default=False, is_flag=True,
  299. help="Overwrite any existing config file if one exists.")
  300. def generate_config(development, output, force):
  301. """Generates a FlaskBB configuration file."""
  302. config_env = Environment(
  303. loader=FileSystemLoader(os.path.join(current_app.root_path, "configs"))
  304. )
  305. config_template = config_env.get_template('config.cfg.template')
  306. if output:
  307. config_path = os.path.abspath(output)
  308. else:
  309. config_path = os.path.dirname(current_app.root_path)
  310. if os.path.exists(config_path) and not os.path.isfile(config_path):
  311. config_path = os.path.join(config_path, "flaskbb.cfg")
  312. # An override to handle database location paths on Windows environments
  313. database_path = "sqlite:///" + os.path.join(
  314. os.path.dirname(current_app.instance_path), "flaskbb.sqlite")
  315. if os.name == 'nt':
  316. database_path = database_path.replace("\\", r"\\")
  317. default_conf = {
  318. "is_debug": False,
  319. "server_name": "example.org",
  320. "use_https": True,
  321. "database_uri": database_path,
  322. "redis_enabled": False,
  323. "redis_uri": "redis://localhost:6379",
  324. "mail_server": "localhost",
  325. "mail_port": 25,
  326. "mail_use_tls": False,
  327. "mail_use_ssl": False,
  328. "mail_username": "",
  329. "mail_password": "",
  330. "mail_sender_name": "FlaskBB Mailer",
  331. "mail_sender_address": "noreply@yourdomain",
  332. "mail_admin_address": "admin@yourdomain",
  333. "secret_key": binascii.hexlify(os.urandom(24)).decode(),
  334. "csrf_secret_key": binascii.hexlify(os.urandom(24)).decode(),
  335. "timestamp": datetime.utcnow().strftime("%A, %d. %B %Y at %H:%M"),
  336. "log_config_path": "",
  337. "deprecation_level": "default"
  338. }
  339. if not force:
  340. config_path = prompt_config_path(config_path)
  341. if force and os.path.exists(config_path):
  342. click.secho("Overwriting existing config file: {}".format(config_path),
  343. fg="yellow")
  344. if development:
  345. default_conf["is_debug"] = True
  346. default_conf["use_https"] = False
  347. default_conf["server_name"] = "localhost:5000"
  348. write_config(default_conf, config_template, config_path)
  349. sys.exit(0)
  350. # SERVER_NAME
  351. click.secho("The name and port number of the exposed server.\n"
  352. "If FlaskBB is accesible on port 80 you can just omit the "
  353. "port.\n For example, if FlaskBB is accessible via "
  354. "example.org:8080 than this is also what you would set here.",
  355. fg="cyan")
  356. default_conf["server_name"] = click.prompt(
  357. click.style("Server Name", fg="magenta"), type=str,
  358. default=default_conf.get("server_name"))
  359. # HTTPS or HTTP
  360. click.secho("Is HTTPS (recommended) or HTTP used for to serve FlaskBB?",
  361. fg="cyan")
  362. default_conf["use_https"] = click.confirm(
  363. click.style("Use HTTPS?", fg="magenta"),
  364. default=default_conf.get("use_https"))
  365. # SQLALCHEMY_DATABASE_URI
  366. click.secho("For Postgres use:\n"
  367. " postgresql://flaskbb@localhost:5432/flaskbb\n"
  368. "For more options see the SQLAlchemy docs:\n"
  369. " http://docs.sqlalchemy.org/en/latest/core/engines.html",
  370. fg="cyan")
  371. default_conf["database_uri"] = click.prompt(
  372. click.style("Database URI", fg="magenta"),
  373. default=default_conf.get("database_uri"))
  374. # REDIS_ENABLED
  375. click.secho("Redis will be used for things such as the task queue, "
  376. "caching and rate limiting.", fg="cyan")
  377. default_conf["redis_enabled"] = click.confirm(
  378. click.style("Would you like to use redis?", fg="magenta"),
  379. default=True) # default_conf.get("redis_enabled") is False
  380. # REDIS_URI
  381. if default_conf.get("redis_enabled", False):
  382. default_conf["redis_uri"] = click.prompt(
  383. click.style("Redis URI", fg="magenta"),
  384. default=default_conf.get("redis_uri"))
  385. else:
  386. default_conf["redis_uri"] = ""
  387. # MAIL_SERVER
  388. click.secho("To use 'localhost' make sure that you have sendmail or\n"
  389. "something similar installed. Gmail is also supprted.",
  390. fg="cyan")
  391. default_conf["mail_server"] = click.prompt(
  392. click.style("Mail Server", fg="magenta"),
  393. default=default_conf.get("mail_server"))
  394. # MAIL_PORT
  395. click.secho("The port on which the SMTP server is listening on.",
  396. fg="cyan")
  397. default_conf["mail_port"] = click.prompt(
  398. click.style("Mail Server SMTP Port", fg="magenta"),
  399. default=default_conf.get("mail_port"))
  400. # MAIL_USE_TLS
  401. click.secho("If you are using a local SMTP server like sendmail this is "
  402. "not needed. For external servers it is required.",
  403. fg="cyan")
  404. default_conf["mail_use_tls"] = click.confirm(
  405. click.style("Use TLS for sending mails?", fg="magenta"),
  406. default=default_conf.get("mail_use_tls"))
  407. # MAIL_USE_SSL
  408. click.secho("Same as above. TLS is the successor to SSL.", fg="cyan")
  409. default_conf["mail_use_ssl"] = click.confirm(
  410. click.style("Use SSL for sending mails?", fg="magenta"),
  411. default=default_conf.get("mail_use_ssl"))
  412. # MAIL_USERNAME
  413. click.secho("Not needed if you are using a local smtp server.\nFor gmail "
  414. "you have to put in your email address here.", fg="cyan")
  415. default_conf["mail_username"] = click.prompt(
  416. click.style("Mail Username", fg="magenta"),
  417. default=default_conf.get("mail_username"))
  418. # MAIL_PASSWORD
  419. click.secho("Not needed if you are using a local smtp server.\nFor gmail "
  420. "you have to put in your gmail password here.", fg="cyan")
  421. default_conf["mail_password"] = click.prompt(
  422. click.style("Mail Password", fg="magenta"),
  423. default=default_conf.get("mail_password"))
  424. # MAIL_DEFAULT_SENDER
  425. click.secho("The name of the sender. You probably want to change it to "
  426. "something like '<your_community> Mailer'.", fg="cyan")
  427. default_conf["mail_sender_name"] = click.prompt(
  428. click.style("Mail Sender Name", fg="magenta"),
  429. default=default_conf.get("mail_sender_name"))
  430. click.secho("On localhost you want to use a noreply address here. "
  431. "Use your email address for gmail here.", fg="cyan")
  432. default_conf["mail_sender_address"] = click.prompt(
  433. click.style("Mail Sender Address", fg="magenta"),
  434. default=default_conf.get("mail_sender_address"))
  435. # ADMINS
  436. click.secho("Logs and important system messages are sent to this address. "
  437. "Use your email address for gmail here.", fg="cyan")
  438. default_conf["mail_admin_address"] = click.prompt(
  439. click.style("Mail Admin Email", fg="magenta"),
  440. default=default_conf.get("mail_admin_address"))
  441. click.secho("Optional filepath to load a logging configuration file from. "
  442. "See the Python logging documentation for more detail.\n"
  443. "\thttps://docs.python.org/library/logging.config.html#logging-config-fileformat", # noqa
  444. fg="cyan")
  445. default_conf["log_config_path"] = click.prompt(
  446. click.style("Logging Config Path", fg="magenta"),
  447. default=default_conf.get("log_config_path"))
  448. deprecation_mesg = (
  449. "Warning level for deprecations. options are: \n"
  450. "\terror\tturns deprecation warnings into exceptions\n"
  451. "\tignore\tnever warns about deprecations\n"
  452. "\talways\talways warns about deprecations even if the warning has been issued\n" # noqa
  453. "\tdefault\tshows deprecation warning once per usage\n"
  454. "\tmodule\tshows deprecation warning once per module\n"
  455. "\tonce\tonly shows deprecation warning once regardless of location\n"
  456. "If you are unsure, select default\n"
  457. "for more details see: https://docs.python.org/3/library/warnings.html#the-warnings-filter" # noqa
  458. )
  459. click.secho(deprecation_mesg, fg="cyan")
  460. default_conf["deprecation_level"] = click.prompt(
  461. click.style("Deperecation warning level", fg="magenta"),
  462. default=default_conf.get("deprecation_level")
  463. )
  464. write_config(default_conf, config_template, config_path)
  465. # Finished
  466. click.secho("The configuration file has been saved to:\n{cfg}\n"
  467. "Feel free to adjust it as needed."
  468. .format(cfg=config_path), fg="blue", bold=True)
  469. click.secho("Usage: \nflaskbb --config {cfg} run"
  470. .format(cfg=config_path), fg="green")