コード例 #1
0
ファイル: service.py プロジェクト: alibaba/easydispatch
def create(*, db_session, service_plugin_in: ServicePluginCreate) -> ServicePlugin:
    """
    Create a new service_plugin.
    """
    service = plugin = None
    # service = Service()
    if service_plugin_in.service:
        if service_plugin_in.service.org_id or service_plugin_in.service.org_id == 0:
            service = service_service.get_by_code_and_org_code(
                db_session=db_session, code=service_plugin_in.service.code, org_id=service_plugin_in.service.org_id
            )
        else:
            service = service_service.get_by_code(
                db_session=db_session, code=service_plugin_in.service.code
            )

    plugin = Plugin()
    if service_plugin_in.plugin:
        plugin = plugin_service.get_by_slug(
            db_session=db_session, slug=service_plugin_in.plugin.slug)

    if service is None or plugin is None:
        raise HTTPException(status_code=404, detail="service is None or plugin is None.")
    service_plugin = ServicePlugin(
        **service_plugin_in.dict(exclude={"service", "plugin", "config"}), service_id=service.id, plugin_id=plugin.id, config=plugin.config
    )

    db_session.add(service_plugin)
    db_session.commit()
    return service_plugin
コード例 #2
0
ファイル: service.py プロジェクト: alibaba/easydispatch
def update(
    *, db_session, service_plugin: ServicePlugin, service_plugin_in: ServicePluginUpdate,
) -> ServicePlugin:
    """
    Updates a service_plugin.
    """

    if service_plugin_in.service:
        service = service_service.get_by_code(
            db_session=db_session, code=service_plugin_in.service.code)
        service_plugin.service = service

    if service_plugin_in.plugin:
        plugin = plugin_service.get_by_slug(
            db_session=db_session, slug=service_plugin_in.plugin.slug)
        service_plugin.plugin = plugin

    service_plugin_data = jsonable_encoder(service_plugin)

    update_data = service_plugin_in.dict(skip_defaults=True, exclude={"service", "plugin"})

    for field in service_plugin_data:
        if field in update_data:
            setattr(service_plugin, field, update_data[field])

    db_session.add(service_plugin)
    db_session.commit()
    return service_plugin
コード例 #3
0
ファイル: service.py プロジェクト: jixjia/easydispatch
def create(*, db_session,
           service_plugin_in: ServicePluginCreate) -> ServicePlugin:
    """
    Create a new service_plugin.
    """
    service = plugin = None
    # service = Service()
    if service_plugin_in.service:
        service = service_service.get_by_code(
            db_session=db_session, code=service_plugin_in.service.code)

    plugin = Plugin()
    if service_plugin_in.plugin:
        plugin = plugin_service.get_by_slug(db_session=db_session,
                                            slug=service_plugin_in.plugin.slug)

    if service is None or plugin is None:
        raise HTTPException(status_code=204,
                            detail="service is None or plugin is None.")
    service_plugin = ServicePlugin(
        **service_plugin_in.dict(exclude={"service", "plugin"}),
        service=service,
        plugin=plugin,
    )

    db_session.add(service_plugin)
    db_session.commit()
    return service_plugin
コード例 #4
0
def create_task_reminders(db_session=None):
    """Creates multiple task reminders."""
    tasks = task_service.get_overdue_tasks(db_session=db_session)
    log.debug(f"New tasks that need reminders. NumTasks: {len(tasks)}")
    # lets only remind for active incidents for now
    tasks = [t for t in tasks if t.incident.status == IncidentStatus.active]
    if tasks:
        contact_fullname = contact_weblink = DISPATCH_HELP_EMAIL
        # NOTE INCIDENT_ONCALL_SERVICE_ID is optional
        if INCIDENT_ONCALL_SERVICE_ID:
            oncall_service = service_service.get_by_external_id(
                db_session=db_session, external_id=INCIDENT_ONCALL_SERVICE_ID)
            oncall_plugin = plugin_service.get_by_slug(
                db_session=db_session, slug=oncall_service.type)
            if oncall_plugin.enabled:
                log.warning(
                    f"Unable to resolve oncall, INCIDENT_ONCALL_SERVICE_ID configured but associated plugin ({oncall_plugin.name}) is not enabled."
                )
                oncall_email = oncall_plugin.instance.get(
                    service_id=INCIDENT_ONCALL_SERVICE_ID)
                oncall_individual = individual_service.resolve_user_by_email(
                    oncall_email, db_session)
                contact_fullname = oncall_individual["fullname"]
                contact_weblink = oncall_individual["weblink"]

        grouped_tasks = group_tasks_by_assignee(tasks)
        for assignee, tasks in grouped_tasks.items():
            create_reminder(db_session, assignee, tasks, contact_fullname,
                            contact_weblink)
コード例 #5
0
ファイル: cli.py プロジェクト: waoywssy/dispatch
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)

        installed = True
        if not record:
            installed = False

        table.append([
            p.title, p.slug, p.version, installed, p.type, p.author,
            p.description
        ])

    click.secho(
        tabulate(
            table,
            headers=[
                "Title", "Slug", "Version", "Installed", "Type", "Author",
                "Description"
            ],
        ),
        fg="blue",
    )
コード例 #6
0
ファイル: manager.py プロジェクト: tribe-health/dispatch
    def register(self, cls):
        from dispatch.database import SessionLocal
        from dispatch.plugin import service as plugin_service
        from dispatch.plugin.models import Plugin

        db_session = SessionLocal()
        record = plugin_service.get_by_slug(db_session=db_session,
                                            slug=cls.slug)
        if not record:
            plugin = Plugin(
                title=cls.title,
                slug=cls.slug,
                type=cls.type,
                version=cls.version,
                author=cls.author,
                author_url=cls.author_url,
                required=cls.required,
                multiple=cls.multiple,
                description=cls.description,
                enabled=cls.enabled,
            )
            db_session.add(plugin)
        else:
            # we only update values that should change
            record.tile = cls.title
            record.version = cls.version
            record.author = cls.author
            record.author_url = cls.author_url
            record.description = cls.description
            db_session.add(record)

        db_session.commit()
        self.add(f"{cls.__module__}.{cls.__name__}")
        return cls
コード例 #7
0
def update(*, db_session, service: Service, service_in: ServiceUpdate) -> Service:
    """Updates an existing service."""
    service_data = jsonable_encoder(service)

    terms = [term_service.get_or_create(db_session=db_session, term_in=t) for t in service_in.terms]
    incident_priorities = [
        incident_priority_service.get_by_name(db_session=db_session, name=n.name)
        for n in service_in.incident_priorities
    ]
    incident_types = [
        incident_type_service.get_by_name(db_session=db_session, name=n.name)
        for n in service_in.incident_types
    ]
    update_data = service_in.dict(
        skip_defaults=True, exclude={"terms", "incident_priorities", "incident_types"}
    )

    if service_in.is_active:  # user wants to enable the service
        oncall_plugin = plugin_service.get_by_slug(db_session=db_session, slug=service_in.type)
        if not oncall_plugin.enabled:
            raise InvalidConfiguration(
                f"Cannot enable service: {service.name}. Its associated plugin {oncall_plugin.title} is not enabled."
            )

    for field in service_data:
        if field in update_data:
            setattr(service, field, update_data[field])

    service.terms = terms
    service.incident_priorities = incident_priorities
    service.incident_types = incident_types
    db_session.add(service)
    db_session.commit()
    return service
コード例 #8
0
ファイル: plugin.py プロジェクト: mohamadalipour/dispatch
    def get(
        self,
        incident_type: IncidentType,
        incident_priority: IncidentPriority,
        incident_description: str,
        db_session=None,
    ):
        """Fetches participants from Dispatch."""
        route_in = {
            "text": incident_description,
            "context": {
                "incident_priorities": [incident_priority],
                "incident_types": [incident_type],
                "terms": [],
            },
        }

        route_in = RouteRequest(**route_in)
        recommendation = route_service.get(db_session=db_session,
                                           route_in=route_in)

        log.debug(f"Recommendation: {recommendation}")
        individual_contacts = [(x, None)
                               for x in recommendation.individual_contacts]
        # we need to resolve our service contacts to individuals
        for s in recommendation.service_contacts:
            plugin = plugin_service.get_by_slug(db_session=db_session,
                                                slug=s.type)

            if plugin:
                if plugin.enabled:
                    log.debug(
                        f"Resolving service contact. ServiceContact: {s}")
                    individual_email = plugin.instance.get(s.external_id)

                    individual = individual_service.get_or_create(
                        db_session=db_session, email=individual_email)
                    individual_contacts.append((individual, s))
                    recommendation.individual_contacts.append(individual)
                else:
                    log.warning(
                        f"Skipping service contact. Service: {s.name} Reason: Associated service plugin not enabled."
                    )
            else:
                log.warning(
                    f"Skipping service contact. Service: {s.name} Reason: Associated service plugin not found."
                )

        db_session.commit()
        return list(individual_contacts), list(recommendation.team_contacts)
コード例 #9
0
    def register(self, cls):
        from dispatch.database import SessionLocal
        from dispatch.plugin import service as plugin_service
        from dispatch.plugin.models import Plugin

        db_session = SessionLocal()
        record = plugin_service.get_by_slug(db_session=db_session,
                                            slug=cls.slug)
        if cls.slug == "kandbox_env":
            logger.debug("Debugging loading kandbox_env")
        if not record:

            config = {}
            config_spec = {}
            try:
                config_spec = cls.config_form_spec
                config = cls.default_config
            except Exception as e:
                logger.warn(
                    f"Unable to find config spec for plugin: {cls.slug}, type = {cls.type} "
                )
            plugin = Plugin(
                title=cls.title,
                slug=cls.slug,
                type=cls.type,
                version=cls.version,
                author=cls.author,
                author_url=cls.author_url,
                required=cls.required,
                multiple=cls.multiple,
                description=cls.description,
                enabled=cls.enabled,
                config=config,
                config_form_spec=config_spec,
            )
            db_session.add(plugin)
        else:
            # we only update values that should change
            record.tile = cls.title
            record.version = cls.version
            record.author = cls.author
            record.author_url = cls.author_url
            record.description = cls.description
            db_session.add(record)

        db_session.commit()
        db_session.close()
        self.add(f"{cls.__module__}.{cls.__name__}")
        return cls
コード例 #10
0
def uninstall_plugins(plugins):
    """Uninstalls all plugins, or only one."""
    from dispatch.database.core 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)
コード例 #11
0
def list_plugins():
    """Shows all available plugins"""
    from dispatch.database.core 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",
    )
コード例 #12
0
def install_plugins(force):
    """Installs all plugins, or only one."""
    from dispatch.database.core import SessionLocal
    from dispatch.plugin import service as plugin_service
    from dispatch.plugin.models import Plugin
    from dispatch.common.utils.cli import install_plugins
    from dispatch.plugins.base import plugins

    install_plugins()

    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,
                multiple=p.multiple,
                description=p.description,
            )
            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.title = p.title
            record.version = p.version
            record.author = p.author
            record.author_url = p.author_url
            record.description = p.description
            record.type = p.type
            db_session.add(record)

        db_session.commit()
コード例 #13
0
ファイル: service.py プロジェクト: alibaba/easydispatch
def switch_agent_plugin_for_service(db_session, service_name="planner", agent_slug=""):
    the_planner_service = service_service.get_by_name(db_session=db_session, name=service_name)
    the_planner_service_agents = service_plugin_service.get_by_service_id_and_type(
        db_session=db_session,
        service_id=the_planner_service.id,
        service_plugin_type=KandboxPlannerPluginType.kandbox_agent,
    )
    for k_agent in the_planner_service_agents:
        service_plugin_service.delete(db_session=db_session, service_plugin_id=k_agent.id)

    """Fetches a given plugin """
    plugin = plugin_service.get_by_slug(db_session=db_session, slug=agent_slug)

    new_service_plugin = ServicePlugin(
        planning_plugin_type=KandboxPlannerPluginType.kandbox_agent,
        service=the_planner_service,
        plugin=plugin,
    )
    db_session.add(new_service_plugin)
    db_session.commit()
    return new_service_plugin
コード例 #14
0
ファイル: cli.py プロジェクト: waoywssy/dispatch
def install_plugins():
    """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():
        click.secho(
            f"Installing plugin... Slug: {p.slug} Version: {p.version}",
            fg="blue")
        record = plugin_service.get_by_slug(db_session=db_session, slug=p.slug)
        if not record:
            plugin = 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(plugin)
        else:
            # 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()