main.py 20 KB

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