#505 Rewrite odcs-manager to not use flask-script
Merged 2 years ago by hlin. Opened 2 years ago by hlin.
hlin/odcs master  into  master

file modified
+18 -67
@@ -19,47 +19,17 @@ 

  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

  # SOFTWARE.

  

- from flask_script import Manager

- from functools import wraps

- import flask_migrate

  import logging

  import os

  import ssl

  

- from odcs.server import app, conf, db

- from odcs.server import models

- 

- 

- manager = Manager(app)

- help_args = ("-?", "--help")

- manager.help_args = help_args

- migrations_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), "migrations")

- migrate = flask_migrate.Migrate(app, db, directory=migrations_dir)

- manager.add_command("db", flask_migrate.MigrateCommand)

- 

- 

- def console_script_help(f):

-     @wraps(f)

-     def wrapped(*args, **kwargs):

-         import sys

- 

-         if any([arg in help_args for arg in sys.argv[1:]]):

-             command = os.path.basename(sys.argv[0])

-             print(

-                 """{0}

- 

- Usage: {0} [{1}]

+ import click

+ import flask_migrate

  

- See also:

-   odcs-manager(1)""".format(

-                     command, "|".join(help_args)

-                 )

-             )

-             sys.exit(2)

-         r = f(*args, **kwargs)

-         return r

+ from flask.cli import FlaskGroup

+ from werkzeug.serving import run_simple

  

-     return wrapped

+ from odcs.server import app, conf, db

  

  

  def _establish_ssl_context():
@@ -87,29 +57,16 @@ 

      return ssl_ctx

  

  

- @console_script_help

- @manager.command

- def upgradedb():

-     """Upgrades the database schema to the latest revision"""

-     app.config["SERVER_NAME"] = "localhost"

-     migrations_dir = os.path.join(

-         os.path.abspath(os.path.dirname(__file__)), "migrations"

-     )

-     with app.app_context():

-         flask_migrate.upgrade(directory=migrations_dir)

+ @click.group(cls=FlaskGroup, create_app=lambda *args, **kwargs: app)

+ def cli():

+     """Manage ODCS application"""

  

  

- @console_script_help

- @manager.command

- def cleardb():

-     """Clears the database"""

-     models.Event.query.delete()

-     models.ArtifactBuild.query.delete()

-     db.session.commit()

+ migrations_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), "migrations")

+ flask_migrate.Migrate(app, db, directory=migrations_dir)

  

  

- @manager.command

- @console_script_help

+ @cli.command()

  def generatelocalhostcert():

      """Creates a public/private key pair for message signing and the frontend"""

      from OpenSSL import crypto
@@ -146,23 +103,17 @@ 

          cert_file.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert))

  

  

- @console_script_help

- @manager.command

- def runssl(host=conf.host, port=conf.port, debug=conf.debug):

+ @cli.command()

+ @click.option("-h", "--host", default=conf.host, help="Bind to this address")

+ @click.option("-p", "--port", type=int, default=conf.port, help="Listen on this port")

+ @click.option("-d", "--debug", is_flag=True, default=conf.debug, help="Debug mode")

+ def runssl(host, port, debug):

      """Runs the Flask app with the HTTPS settings configured in config.py"""

      logging.info("Starting ODCS frontend")

  

      ssl_ctx = _establish_ssl_context()

-     app.run(host=host, port=port, ssl_context=ssl_ctx, debug=debug)

- 

- 

- def manager_wrapper():

-     """

-     Runs the manager. We have separate method for this so we can use it in

-     `console_scripts` part of setup.py

-     """

-     manager.run()

+     run_simple(host, port, app, use_debugger=debug, ssl_context=ssl_ctx)

  

  

  if __name__ == "__main__":

-     manager_wrapper()

+     cli()

file modified
+1 -2
@@ -10,10 +10,9 @@ 

  # qpid-python  # Python2 only; not necessary; just to hide warning messages

  six

  sqlalchemy

- Flask

+ Flask >= 0.11

  Flask-Migrate

  Flask-SQLAlchemy

- Flask-Script

  Flask-Login < 0.5.0

  requests

  jinja2

file modified
+1 -1
@@ -87,7 +87,7 @@ 

              "odcs-gencert = odcs.server.manage:generatelocalhostcert [server]",

              "odcs-frontend = odcs.server.manage:runssl [server]",

              "odcs-mock-runroot = odcs.server.mock_runroot:mock_runroot_main [server]",

-             "odcs-manager = odcs.server.manage:manager_wrapper [server]",

+             "odcs-manager = odcs.server.manage:cli [server]",

          ],

      },

      data_files=[

Rewrite using flask command line interface as flask-script is deprecated
for a long time and this means flask >= 0.11 is required (RHEL7 only
provides python-flask-0.10).

Refer to https://flask.palletsprojects.com/en/0.12.x/cli/

FIXES: https://pagure.io/odcs/issue/504
JIRA: RHELCMP-126
Signed-off-by: Haibo Lin hlin@redhat.com

I tried playing with this in a new virtual env, and while it generally looks fine, I'm wondering if flask_migrate usage should be updated too. The latest version doesn't provide MigrateCommand anymore. Or maybe the version should be limited in requirements.txt?

rebased onto c17e5a0

2 years ago

1 new commit added

  • Drop cleardb and upgradedb command
2 years ago

I tried playing with this in a new virtual env, and while it generally looks fine, I'm wondering if flask_migrate usage should be updated too. The latest version doesn't provide MigrateCommand anymore. Or maybe the version should be limited in requirements.txt?

I deleted code using MigrateCommand as it's no longer needed when using Flask.cli and flask-migrate already supports Flask.cli since v2.0.0 (the version used in odcs prod).

Additionally deleted cleardb and upgradedb commands:

cleardb command clears non-exist models in odcs.
upgradedb command could be replaced by db upgrade command.

2 new commits added

  • Drop cleardb and upgradedb command
  • Rewrite odcs-manager to not use flask-script
2 years ago

2 new commits added

  • Drop cleardb and upgradedb command
  • Rewrite odcs-manager to not use flask-script
2 years ago

Looks good to me.
Our playbooks are using the upgradedb command and will need to be updated. The cleanup here is great!

Pull-Request has been merged by hlin

2 years ago