Repository navigation
Expand file tree
/
Copy pathcli.py
More file actions
662 lines (545 loc) · 20.4 KB
/
Copy pathcli.py
File metadata and controls
662 lines (545 loc) · 20.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
import logging
import os
import sys
import click
import uvicorn
import asyncio
from alembic import command as alembic_command
from alembic.config import Config as AlembicConfig
from tabulate import tabulate
from dispatch import __version__, config
from dispatch.enums import UserRoles
from .main import * # noqa
from .database import Base, engine
from .exceptions import DispatchException
from .plugins.base import plugins
from .scheduler import scheduler
from .logging import configure_logging
from dispatch.plugins.dispatch_slack import socket_mode
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
log = logging.getLogger(__name__)
def abort_if_false(ctx, param, value):
if not value:
ctx.abort()
@click.group()
@click.version_option(version=__version__)
def dispatch_cli():
"""Command-line interface to Dispatch."""
configure_logging()
@dispatch_cli.group("plugins")
def plugins_group():
"""All commands for plugin manipulation."""
pass
@plugins_group.command("list")
def list_plugins():
"""Shows all available plugins"""
from dispatch.database import SessionLocal
from dispatch.plugin import service as plugin_service
db_session = SessionLocal()
table = []
for p in plugins.all():
record = plugin_service.get_by_slug(db_session=db_session, slug=p.slug)
if not record:
log.warning(
f"Plugin {p.slug} available, but not installed. Run `dispatch plugins install` to install it."
)
continue
table.append(
[
record.title,
record.slug,
record.version,
record.enabled,
record.type,
record.author,
record.description,
]
)
click.secho(
tabulate(
table,
headers=[
"Title",
"Slug",
"Version",
"Enabled",
"Type",
"Author",
"Description",
],
),
fg="blue",
)
@plugins_group.command("install")
@click.option(
"-f",
"--force",
is_flag=True,
help="Force a plugin to update all details about itself, this will overwrite the current database entry.",
)
def install_plugins(force):
"""Installs all plugins, or only one."""
from dispatch.database import SessionLocal
from dispatch.plugin import service as plugin_service
from dispatch.plugin.models import Plugin
db_session = SessionLocal()
for p in plugins.all():
record = plugin_service.get_by_slug(db_session=db_session, slug=p.slug)
if not record:
click.secho(f"Installing plugin... Slug: {p.slug} Version: {p.version}", fg="blue")
record = Plugin(
title=p.title,
slug=p.slug,
type=p.type,
version=p.version,
author=p.author,
author_url=p.author_url,
required=p.required,
multiple=p.multiple,
description=p.description,
enabled=p.enabled,
)
db_session.add(record)
if force:
click.secho(f"Updating plugin... Slug: {p.slug} Version: {p.version}", fg="blue")
# we only update values that should change
record.tile = p.title
record.version = p.version
record.author = p.author
record.author_url = p.author_url
record.description = p.description
record.required = p.required
record.type = p.type
db_session.add(record)
db_session.commit()
@plugins_group.command("uninstall")
@click.argument("plugins", nargs=-1)
def uninstall_plugins(plugins):
"""Uninstalls all plugins, or only one."""
from dispatch.database import SessionLocal
from dispatch.plugin import service as plugin_service
db_session = SessionLocal()
for plugin_slug in plugins:
plugin = plugin_service.get_by_slug(db_session=db_session, slug=plugin_slug)
if not plugin:
click.secho(
f"Plugin slug {plugin_slug} does not exist. Make sure you're passing the plugin's slug.",
fg="red",
)
plugin_service.delete(db_session=db_session, plugin_id=plugin.id)
def sync_triggers():
from sqlalchemy_searchable import sync_trigger
sync_trigger(engine, "definition", "search_vector", ["text"])
sync_trigger(engine, "document", "search_vector", ["name"])
sync_trigger(engine, "incident", "search_vector", ["name", "title", "description"])
sync_trigger(engine, "incident_type", "search_vector", ["name", "description"])
sync_trigger(
engine, "individual_contact", "search_vector", ["name", "title", "company", "notes"]
)
sync_trigger(engine, "plugin", "search_vector", ["title"])
sync_trigger(engine, "search_filter", "search_vector", ["name", "description"])
sync_trigger(engine, "report", "search_vector", ["details_raw"])
sync_trigger(engine, "service", "search_vector", ["name"])
sync_trigger(engine, "tag", "search_vector", ["name"])
sync_trigger(engine, "task", "search_vector", ["description"])
sync_trigger(engine, "team_contact", "search_vector", ["name", "company", "notes"])
sync_trigger(engine, "term", "search_vector", ["text"])
sync_trigger(engine, "dispatch_user", "search_vector", ["email"])
sync_trigger(engine, "workflow", "search_vector", ["name", "description"])
@dispatch_cli.group("user")
def dispatch_user():
"""Container for all user commands."""
pass
@dispatch_user.command("update")
@click.argument("email")
@click.option(
"--role",
"-r",
type=click.Choice(UserRoles),
help="Role to be assigned to the user.",
)
def update_user(email: str, role: str):
"""Updates a user's roles."""
from dispatch.database import SessionLocal
from dispatch.auth import service as user_service
from dispatch.auth.models import UserUpdate
db_session = SessionLocal()
user = user_service.get_by_email(email=email, db_session=db_session)
if not user:
click.secho(f"No user found. Email: {email}", fg="red")
return
user_service.update(user=user, user_in=UserUpdate(id=user.id, role=role), db_session=db_session)
click.secho("User successfully updated.", fg="green")
@dispatch_user.command("reset")
@click.argument("email")
@click.password_option()
def reset_user_password(email: str, password: str):
"""Resets a user's password."""
from dispatch.database import SessionLocal
from dispatch.auth import service as user_service
from dispatch.auth.models import UserUpdate
db_session = SessionLocal()
user = user_service.get_by_email(email=email, db_session=db_session)
if not user:
click.secho(f"No user found. Email: {email}", fg="red")
return
user_service.update(
user=user, user_in=UserUpdate(id=user.id, password=password), db_session=db_session
)
click.secho("User successfully updated.", fg="green")
@dispatch_cli.group("database")
def dispatch_database():
"""Container for all dispatch database commands."""
pass
@dispatch_database.command("sync-triggers")
def database_trigger_sync():
"""Ensures that all database triggers have been installed."""
sync_triggers()
click.secho("Success.", fg="green")
@dispatch_database.command("init")
def init_database():
"""Initializes a new database."""
from sqlalchemy_utils import create_database, database_exists
if not database_exists(str(config.SQLALCHEMY_DATABASE_URI)):
create_database(str(config.SQLALCHEMY_DATABASE_URI))
Base.metadata.create_all(engine)
alembic_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "alembic.ini")
alembic_cfg = AlembicConfig(alembic_path)
alembic_command.stamp(alembic_cfg, "head")
sync_triggers()
click.secho("Success.", fg="green")
@dispatch_database.command("restore")
@click.option(
"--dump-file",
default="dispatch-backup.dump",
help="Path to a PostgreSQL text format dump file.",
)
def restore_database(dump_file):
"""Restores the database via psql."""
from sh import psql, createdb, ErrorReturnCode_1
from dispatch.config import (
DATABASE_HOSTNAME,
DATABASE_NAME,
DATABASE_PORT,
DATABASE_CREDENTIALS,
)
username, password = str(DATABASE_CREDENTIALS).split(":")
try:
print(
createdb(
"-h",
DATABASE_HOSTNAME,
"-p",
DATABASE_PORT,
"-U",
username,
DATABASE_NAME,
_env={"PGPASSWORD": password},
)
)
except ErrorReturnCode_1:
print("Database already exists.")
print(
psql(
"-h",
DATABASE_HOSTNAME,
"-p",
DATABASE_PORT,
"-U",
username,
"-d",
DATABASE_NAME,
"-f",
dump_file,
_env={"PGPASSWORD": password},
)
)
click.secho("Success.", fg="green")
@dispatch_database.command("dump")
@click.option(
"--dump-file",
default="dispatch-backup.dump",
help="Path to a PostgreSQL text format dump file.",
)
def dump_database(dump_file):
"""Dumps the database via pg_dump."""
from sh import pg_dump
from dispatch.config import (
DATABASE_HOSTNAME,
DATABASE_NAME,
DATABASE_PORT,
DATABASE_CREDENTIALS,
)
username, password = str(DATABASE_CREDENTIALS).split(":")
pg_dump(
"-f",
dump_file,
"-h",
DATABASE_HOSTNAME,
"-p",
DATABASE_PORT,
"-U",
username,
DATABASE_NAME,
_env={"PGPASSWORD": password},
)
@dispatch_database.command("drop")
@click.option("--yes", is_flag=True, help="Silences all confirmation prompts.")
def drop_database(yes):
"""Drops all data in database."""
from sqlalchemy_utils import drop_database
if yes:
drop_database(str(config.SQLALCHEMY_DATABASE_URI))
click.secho("Success.", fg="green")
if click.confirm(
f"Are you sure you want to drop: '{config.DATABASE_HOSTNAME}:{config.DATABASE_NAME}'?"
):
drop_database(str(config.SQLALCHEMY_DATABASE_URI))
click.secho("Success.", fg="green")
@dispatch_database.command("upgrade")
@click.option(
"--tag", default=None, help="Arbitrary 'tag' name - can be used by custom env.py scripts."
)
@click.option(
"--sql",
is_flag=True,
default=False,
help="Don't emit SQL to database - dump to standard output instead.",
)
@click.option("--revision", nargs=1, default="head", help="Revision identifier.")
def upgrade_database(tag, sql, revision):
"""Upgrades database schema to newest version."""
from sqlalchemy_utils import database_exists, create_database
from alembic.migration import MigrationContext
alembic_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "alembic.ini")
alembic_cfg = AlembicConfig(alembic_path)
if not database_exists(str(config.SQLALCHEMY_DATABASE_URI)):
create_database(str(config.SQLALCHEMY_DATABASE_URI))
Base.metadata.create_all(engine)
alembic_command.stamp(alembic_cfg, "head")
else:
conn = engine.connect()
context = MigrationContext.configure(conn)
current_rev = context.get_current_revision()
if not current_rev:
Base.metadata.create_all(engine)
alembic_command.stamp(alembic_cfg, "head")
else:
alembic_command.upgrade(alembic_cfg, revision, sql=sql, tag=tag)
sync_triggers()
click.secho("Success.", fg="green")
@dispatch_database.command("merge")
@click.argument("revisions", nargs=-1)
@click.option("--message")
def merge_revisions(revisions, message):
"""Combines two revisions."""
alembic_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "alembic.ini")
alembic_cfg = AlembicConfig(alembic_path)
alembic_command.merge(alembic_cfg, revisions, message=message)
@dispatch_database.command("heads")
def head_database():
"""Shows the heads of the database."""
alembic_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "alembic.ini")
alembic_cfg = AlembicConfig(alembic_path)
alembic_command.heads(alembic_cfg)
@dispatch_database.command("history")
def history_database():
"""Shows the history of the database."""
alembic_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "alembic.ini")
alembic_cfg = AlembicConfig(alembic_path)
alembic_command.history(alembic_cfg)
@dispatch_database.command("downgrade")
@click.option(
"--tag", default=None, help="Arbitrary 'tag' name - can be used by custom env.py scripts."
)
@click.option(
"--sql",
is_flag=True,
default=False,
help="Don't emit SQL to database - dump to standard output instead.",
)
@click.option("--revision", nargs=1, default="head", help="Revision identifier.")
def downgrade_database(tag, sql, revision):
"""Downgrades database schema to next newest version."""
alembic_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "alembic.ini")
alembic_cfg = AlembicConfig(alembic_path)
if sql and revision == "-1":
revision = "head:-1"
alembic_command.downgrade(alembic_cfg, revision, sql=sql, tag=tag)
click.secho("Success.", fg="green")
@dispatch_database.command("stamp")
@click.argument("revision", nargs=1, default="head")
@click.option(
"--tag", default=None, help="Arbitrary 'tag' name - can be used by custom env.py scripts."
)
@click.option(
"--sql",
is_flag=True,
default=False,
help="Don't emit SQL to database - dump to standard output instead.",
)
def stamp_database(revision, tag, sql):
"""Forces the database to a given revision."""
alembic_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "alembic.ini")
alembic_cfg = AlembicConfig(alembic_path)
alembic_command.stamp(alembic_cfg, revision, sql=sql, tag=tag)
@dispatch_database.command("revision")
@click.option(
"-d", "--directory", default=None, help=('migration script directory (default is "migrations")')
)
@click.option("-m", "--message", default=None, help="Revision message")
@click.option(
"--autogenerate",
is_flag=True,
help=(
"Populate revision script with candidate migration "
"operations, based on comparison of database to model"
),
)
@click.option(
"--sql", is_flag=True, help=("Don't emit SQL to database - dump to standard output " "instead")
)
@click.option(
"--head",
default="head",
help=("Specify head revision or <branchname>@head to base new " "revision on"),
)
@click.option(
"--splice", is_flag=True, help=('Allow a non-head revision as the "head" to splice onto')
)
@click.option(
"--branch-label", default=None, help=("Specify a branch label to apply to the new revision")
)
@click.option(
"--version-path", default=None, help=("Specify specific path from config for version file")
)
@click.option(
"--rev-id", default=None, help=("Specify a hardcoded revision id instead of generating " "one")
)
def revision_database(
directory, message, autogenerate, sql, head, splice, branch_label, version_path, rev_id
):
"""Create new database revision."""
alembic_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "alembic.ini")
alembic_cfg = AlembicConfig(alembic_path)
alembic_command.revision(
alembic_cfg,
message,
autogenerate=autogenerate,
sql=sql,
head=head,
splice=splice,
branch_label=branch_label,
version_path=version_path,
rev_id=rev_id,
)
@dispatch_cli.group("scheduler")
def dispatch_scheduler():
"""Container for all dispatch scheduler commands."""
# we need scheduled tasks to be imported
from .document.scheduled import sync_document_terms # noqa
from .feedback.scheduled import feedback_daily_digest # noqa
from .incident.scheduled import daily_report, auto_tagger # noqa
from .report.scheduled import incident_report_reminders # noqa
from .tag.scheduled import sync_tags, build_tag_models # noqa
from .task.scheduled import sync_tasks, create_task_reminders # noqa
from .term.scheduled import sync_terms # noqa
from .workflow.scheduled import sync_workflows, sync_active_stable_workflows # noqa
@dispatch_scheduler.command("list")
def list_tasks():
"""Prints and runs all currently configured periodic tasks, in seperate event loop."""
table = []
for task in scheduler.registered_tasks:
table.append([task["name"], task["job"].period, task["job"].at_time])
click.secho(tabulate(table, headers=["Task Name", "Period", "At Time"]), fg="blue")
@dispatch_scheduler.command("start")
@click.argument("tasks", nargs=-1)
@click.option("--eager", is_flag=True, default=False, help="Run the tasks immediately.")
def start_tasks(tasks, eager):
"""Starts the scheduler."""
if tasks:
for task in scheduler.registered_tasks:
if task["name"] not in tasks:
scheduler.remove(task)
if eager:
for task in tasks:
for r_task in scheduler.registered_tasks:
if task == r_task["name"]:
click.secho(f"Eagerly running: {task}", fg="blue")
r_task["func"]()
break
else:
click.secho(f"Task not found. TaskName: {task}", fg="red")
click.secho("Starting scheduler...", fg="blue")
scheduler.start()
@dispatch_cli.group("server")
def dispatch_server():
"""Container for all dispatch server commands."""
pass
@dispatch_server.command("routes")
def show_routes():
"""Prints all available routes."""
from dispatch.main import api_router
table = []
for r in api_router.routes:
auth = False
for d in r.dependencies:
if d.dependency.__name__ == "get_current_user": # TODO this is fragile
auth = True
table.append([r.path, auth, ",".join(r.methods)])
click.secho(tabulate(table, headers=["Path", "Authenticated", "Methods"]), fg="blue")
@dispatch_server.command("config")
def show_config():
"""Prints the current config as dispatch sees it."""
import sys
import inspect
from dispatch import config
func_members = inspect.getmembers(sys.modules[config.__name__])
table = []
for key, value in func_members:
if key.isupper():
table.append([key, value])
click.secho(tabulate(table, headers=["Key", "Value"]), fg="blue")
@dispatch_server.command("develop")
@click.option(
"--log-level",
type=click.Choice(["debug", "info", "error", "warning", "critical"]),
default="debug",
help="Log level to use.",
)
def run_server(log_level):
"""Runs a simple server for development."""
# Uvicorn expects lowercase logging levels; the logging package expects upper.
os.environ["LOG_LEVEL"] = log_level.upper()
if not config.STATIC_DIR:
import atexit
from subprocess import Popen
# take our frontend vars and export them for the frontend to consume
envvars = os.environ.copy()
envvars.update({x: getattr(config, x) for x in dir(config) if x.startswith("VUE_APP_")})
p = Popen(["npm", "run", "serve"], cwd="src/dispatch/static/dispatch", env=envvars)
atexit.register(p.terminate)
uvicorn.run("dispatch.main:app", debug=True, log_level=log_level)
dispatch_server.add_command(uvicorn.main, name="start")
@dispatch_server.command("slack")
def run_slack_websocket():
"""Runs the slack websocket process."""
asyncio.run(socket_mode.run_websocket_process())
@dispatch_server.command("shell")
@click.argument("ipython_args", nargs=-1, type=click.UNPROCESSED)
def shell(ipython_args):
"""Starts an ipython shell importing our app. Useful for debugging."""
import IPython
from IPython.terminal.ipapp import load_default_config
config = load_default_config()
config.TerminalInteractiveShell.banner1 = f"""Python {sys.version} on {sys.platform}
IPython: {IPython.__version__}"""
IPython.start_ipython(argv=ipython_args, user_ns={}, config=config)
def entrypoint():
"""The entry that the CLI is executed from"""
try:
dispatch_cli()
except DispatchException as e:
click.secho(f"ERROR: {e}", bold=True, fg="red")
if __name__ == "__main__":
entrypoint()