コード例 #1
0
def run_task(task_id: int) -> Response:
    """Run a task."""
    task = Task.query.filter_by(id=task_id).first()
    redis_client.delete("runner_" + str(task_id) + "_attempt")
    if task:
        try:
            requests.get(app.config["SCHEDULER_HOST"] + "/run/" + str(task_id))
            log = TaskLog(  # type: ignore[call-arg]
                task_id=task.id,
                status_id=7,
                message=(current_user.full_name or "none") +
                ": Task manually run.",
            )
            db.session.add(log)
            db.session.commit()
            flash("Task run started.")
        # pylint: disable=broad-except
        except BaseException as e:
            log = TaskLog(  # type: ignore[call-arg]
                status_id=7,
                error=1,
                task_id=task_id,
                message=((current_user.full_name or "none") +
                         ": Failed to manually run task. (" + str(task_id) +
                         ")\n" + str(e)),
            )
            db.session.add(log)
            db.session.commit()
            flash("Failed to run task.")

        return redirect(url_for("task_bp.one_task", task_id=task_id))

    flash("Task does not exist.")
    return redirect(url_for("task_bp.all_tasks"))
コード例 #2
0
def disable_project(project_list: List[int]) -> str:
    """Disabling project."""
    project_id = project_list[0]
    tasks = Task.query.filter_by(project_id=project_id).all()

    try:
        for task in tasks:

            sub_disable_task(task.id)

            log = TaskLog(
                task_id=task.id,
                status_id=7,
                message=(current_user.full_name or "none") +
                ": Task disabled.",
            )
            db.session.add(log)
            db.session.commit()

    # pylint: disable=broad-except
    except BaseException as e:
        log = TaskLog(
            status_id=7,
            error=1,
            message=((current_user.full_name or "none") +
                     ": Failed to disable task. (" + str(task.id) + ")\n" +
                     str(e)),
        )
        db.session.add(log)
        db.session.commit()
        return "Failed to disable project, see task logs."

    return "Project disabled."
コード例 #3
0
def send_task_to_scheduler(task_id: int) -> None:
    """Silently send task or raise an error."""
    try:
        requests.get(app.config["SCHEDULER_HOST"] + "/add/" + str(task_id))
        log = TaskLog(
            task_id=task_id,
            status_id=7,
            message=(current_user.full_name or "none") + ": Task scheduled.",
        )
        db.session.add(log)
        db.session.commit()

    # pylint: disable=broad-except
    except (requests.exceptions.ConnectionError,
            urllib3.exceptions.NewConnectionError):
        # set task to disabled.
        Task.query.filter_by(id=task_id).first().enabled = 0
        db.session.commit()

        log = TaskLog(
            status_id=7,
            error=1,
            task_id=task_id,
            message=((current_user.full_name or "none") +
                     ": Failed to schedule task. (" + str(task_id) +
                     ")\nScheduler is offline."),
        )
        db.session.add(log)
        db.session.commit()
        # pylint: disable=W0707
        raise ValueError("Failed to schedule, scheduler is offline.")
コード例 #4
0
def send_task_to_runner(task_id: int) -> None:
    """Silently send task or raise an error."""
    task = Task.query.filter_by(id=task_id).first()

    # task only runs if not sequence, or first in sequence.
    try:

        if task.project and task.project.sequence_tasks == 1:
            # only add job if its first in sequence
            if (Task.query.filter_by(
                    project_id=task.project_id).filter_by(enabled=1).order_by(
                        Task.order.asc(),
                        Task.name.asc())  # type: ignore[union-attr]
                    .first()).id == int(task_id):
                requests.get(app.config["SCHEDULER_HOST"] + "/run/" +
                             str(task.id))

                log = TaskLog(
                    task_id=task_id,
                    status_id=7,
                    message=(current_user.full_name or "none") +
                    ": Task sent to runner.",
                )
                db.session.add(log)
                db.session.commit()

            else:
                log = TaskLog(
                    task_id=task_id,
                    status_id=7,
                    message=(current_user.full_name or "none") +
                    ": Task not sent to runner - it is a sequence task that should not run first.",
                )
                db.session.add(log)
                db.session.commit()
                raise ValueError(
                    "Task was not scheduled. It is not the first sequence task."
                )
        else:

            requests.get(app.config["SCHEDULER_HOST"] + "/run/" + str(task.id))

    except (requests.exceptions.ConnectionError,
            urllib3.exceptions.NewConnectionError):

        logging.error({"empty_msg": "Error - Scheduler offline."})
        log = TaskLog(
            task_id=task_id,
            status_id=7,
            error=1,
            message=(current_user.full_name or "none") +
            ": Failed to send task to runner.",
        )
        db.session.add(log)
        db.session.commit()
        # pylint: disable=W0707
        raise ValueError("Error - Scheduler offline.")
コード例 #5
0
def delete_task(task_id: int) -> Response:
    """Delete a task."""
    task = Task.query.filter_by(id=task_id).first()

    if task:
        project_id = task.project_id

        submit_executor("disable_task", task_id)

        TaskLog.query.filter_by(task_id=task_id).delete()
        db.session.commit()
        TaskFile.query.filter_by(task_id=task_id).delete()
        db.session.commit()
        Task.query.filter_by(id=task_id).delete()
        db.session.commit()

        log = TaskLog(  # type: ignore[call-arg]
            status_id=7,
            message=(current_user.full_name or "none") + ": Task deleted. (" +
            str(task_id) + ")",
        )
        db.session.add(log)
        db.session.commit()

        flash("Deleting task.")

        return redirect(
            url_for("project_bp.one_project", project_id=project_id))

    flash("Task does not exist.")
    return redirect(url_for("task_bp.all_tasks"))
コード例 #6
0
def task_endretry(task_id: int) -> Response:
    """Stop a task from performing the scheduled retry.

    First, remove the redis key for reruns
    Then, force the task to reschedule, which will
    clear any scheduled jobs that do not belong to
    the primary schedule.
    """
    task = Task.query.filter_by(id=task_id).first()
    if task:

        if task.enabled == 1:
            submit_executor("enable_task", task_id)
        else:
            submit_executor("disable_task", task_id)

        log = TaskLog(  # type: ignore[call-arg]
            status_id=7,
            task_id=task_id,
            message="%s: Task retry canceled." %
            (current_user.full_name or "none"),
        )
        db.session.add(log)
        db.session.commit()

        return redirect(url_for("task_bp.one_task", task_id=task_id))

    flash("Task does not exist.")
    return redirect(url_for("task_bp.all_tasks"))
コード例 #7
0
def edit_connection_gpg(connection_id: int,
                        gpg_id: int) -> Union[str, Response]:
    """Edit a GPG connection."""
    gpg = ConnectionGpg.query.filter_by(
        id=gpg_id, connection_id=connection_id).first_or_404()
    if request.method == "GET":
        return render_template(
            "pages/connection/gpg_edit.html.j2",
            gpg=gpg,
            connection=gpg.connection,
            title=f"Editing Connection {gpg.connection}",
        )

    form = request.form
    this_gpg = ConnectionGpg.query.filter_by(id=gpg.id)
    this_gpg.update({
        "name":
        form.get("name", "undefined", type=str).strip(),
        "key": (em_encrypt(
            form.get("key", "", type=str).strip(), app.config["PASS_KEY"])
                if form.get("key", type=str) else None),
    })

    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: GPG Connection edited. ({gpg.id}) {gpg}",
    )
    db.session.add(log)
    db.session.commit()
    flash("Connection updated.")
    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))
コード例 #8
0
def enable_project(project_list: List[int]) -> str:
    """Enabling project."""
    project_id = project_list[0]
    tasks = Task.query.filter_by(project_id=project_id).all()

    try:
        # first enable all, so we get sequence right
        for task in tasks:
            task.enabled = 1
            db.session.commit()

        for task in tasks:
            sub_enable_task(task.id)

    # pylint: disable=broad-except
    except BaseException as e:
        task.enabled = 0
        db.session.commit()
        log = TaskLog(
            status_id=7,
            error=1,
            message=((current_user.full_name or "none") +
                     ": Failed to enable project.\n" + str(e)),
        )
        db.session.add(log)
        db.session.commit()
        return "Failed to enable project, see task logs."

    return "Project enabled."
コード例 #9
0
def disabled_scheduled_tasks(*args: Any) -> str:
    """Disabling scheduled tasks."""
    # Basically dump the scheduler and set all tasks to disabled.

    try:
        requests.get(app.config["SCHEDULER_HOST"] + "/delete")

        tasks = Task.query.filter_by(enabled=1).all()

        for task in tasks:
            task.enabled = 0
            db.session.commit()

            log = TaskLog(
                task_id=task.id,
                status_id=7,
                message=(current_user.full_name or "none") +
                ": Task disabled.",
            )
            db.session.add(log)
            db.session.commit()
        return "Disabled scheduled tasks."
    except (requests.exceptions.ConnectionError,
            urllib3.exceptions.NewConnectionError):
        logging.error({"empty_msg": "Error - Scheduler offline."})

        return "Failed to disable, Scheduler is offline."
コード例 #10
0
def new_connection_gpg(connection_id: int) -> Union[Response, str]:
    """Create a GPG connection."""
    connection = Connection.query.filter_by(id=connection_id).first_or_404()
    if request.method == "GET":
        return render_template(
            "pages/connection/gpg_edit.html.j2",
            connection=connection,
            title="New GPG Connection",
        )
    form = request.form

    gpg = ConnectionGpg(
        connection_id=connection_id,
        name=form.get("name", "undefined", type=str).strip(),
        key=(em_encrypt(
            form.get("key", "", type=str).strip(), app.config["PASS_KEY"])
             if form.get("key", type=str) else None),
    )

    db.session.add(gpg)
    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: GPG Connection added. ({gpg.id}) {gpg}",
    )
    db.session.add(log)
    db.session.commit()

    flash("Connection added.")
    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))
コード例 #11
0
def task_get_source_code(task_id: int) -> str:
    """Get source code for a task."""
    try:
        code = json.loads(
            requests.get(app.config["RUNNER_HOST"] + "/" + task_id +
                         "/source_code").text)["code"]
    # pylint: disable=broad-except
    except BaseException as e:
        log = TaskLog(
            status_id=7,
            error=1,
            task_id=task_id,
            message=((current_user.full_name or "none") +
                     ": Failed to get source code. (" + str(task_id) + ")\n" +
                     str(e)),
        )
        db.session.add(log)
        db.session.commit()
        code = "error."

    task = Task.query.filter_by(id=task_id).first()
    return render_template(
        "pages/task/code.html.j2",
        code=code,
        cache_enabled=task.enable_source_cache,
        task_id=task_id,
    )
コード例 #12
0
def new_connection() -> Union[str, Response]:
    """Add a new connection."""
    if request.method == "GET":
        return render_template(
            "pages/connection/new.html.j2",
            title="New connection",
        )

    form = request.form

    me = Connection(
        name=form.get("name", "undefined", type=str).strip(),
        description=form.get("description", "", type=str).strip(),
        address=form.get("address", "", type=str).strip(),
        primary_contact=form.get("contact", "", type=str).strip(),
        primary_contact_email=form.get("email", "", type=str).strip(),
        primary_contact_phone=form.get("phone", "", type=str).strip(),
    )

    db.session.add(me)
    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: Connection group added. ({me.id}) {me}",
    )

    db.session.add(log)
    db.session.commit()
    flash("Connection added.")
    return redirect(
        url_for("connection_bp.one_connection", connection_id=me.id))
コード例 #13
0
def add_user_log(message: str, error_code: int) -> None:
    """Add log entry prefixed by username."""
    log = TaskLog(  # type: ignore[call-arg]
        status_id=7,
        error=error_code,
        message=(current_user.full_name or "none") + ": " + message,
    )
    db.session.add(log)
    db.session.commit()
コード例 #14
0
def disable_task(task_list: List[int]) -> str:
    """Disabling task."""
    task_id: int = task_list[0]
    try:
        sub_disable_task(task_id)
        requests.get(app.config["SCHEDULER_HOST"] + "/delete/" + str(task_id))

        task = Task.query.filter_by(id=task_id).first()
        task.enabled = 0
        task.next_run = None
        db.session.commit()

        log = TaskLog(
            task_id=task_id,
            status_id=7,
            message=(current_user.full_name or "none") + ": Task disabled.",
        )
        db.session.add(log)
        db.session.commit()

        if task.project and task.project.sequence_tasks == 1:
            # update sequence
            for task in Task.query.filter_by(project_id=task.project_id,
                                             enabled=1).all():
                sub_enable_task(task.id)
            return "Task disabled, sequence updated."

        return "Task disabled."

    # pylint: disable=broad-except
    except BaseException as e:
        log = TaskLog(
            status_id=7,
            error=1,
            message=((current_user.full_name or "none") +
                     ": Failed to disable task. (" + str(task_id) + ")\n" +
                     str(e)),
        )
        db.session.add(log)
        db.session.commit()
        return "Failed to disable task."
コード例 #15
0
def add_user_log(message: str, error_code: int) -> None:
    """Add log entry prefixed by username.

    :param message str: message to save
    :param error_code: 1 (error) or 0 (no error)
    """
    log = TaskLog(
        status_id=7,
        error=error_code,
        message=(current_user.full_name or "none") + ": " + message,
    )  # type: ignore
    db.session.add(log)
    db.session.commit()
コード例 #16
0
def run_project(project_list: List[int]) -> str:
    """Running enabled project tasks."""
    project_id: int = project_list[0]
    project = Project.query.filter_by(id=project_id).first()

    tasks = Task.query.filter_by(project_id=project_id, enabled=1).order_by(
        Task.order.asc(),
        Task.name.asc()  # type: ignore[union-attr]
    )

    if project.sequence_tasks == 1:
        # run first enabled, order by rank, name
        send_task_to_runner(tasks.first().id)
        log = TaskLog(
            task_id=tasks.first().id,
            status_id=7,
            message=(current_user.full_name or "none") +
            ": Task manually run.",
        )
        db.session.add(log)
        db.session.commit()
        return "Project sequence run started."

    for task in tasks:
        send_task_to_runner(task.id)
        log = TaskLog(
            task_id=task.id,
            status_id=7,
            message=(current_user.full_name or "none") +
            ": Task manually run.",
        )
        db.session.add(log)
        db.session.commit()

    if len(tasks.all()):
        return "Run started."

    return "No enabled tasks to run."
コード例 #17
0
def delete_connection_gpg(connection_id: int, gpg_id: int) -> Response:
    """Delete a GPG connection."""
    ConnectionGpg.query.filter_by(connection_id=connection_id,
                                  id=gpg_id).delete()
    db.session.commit()
    log = TaskLog(
        status_id=7,
        message=f"{current_user.full_name}: GPG Connection deleted. ({gpg_id})",
    )
    db.session.add(log)
    db.session.commit()
    flash("Connection deleted.")
    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))
コード例 #18
0
def delete_connection_database(connection_id: int,
                               database_id: int) -> Response:
    """Delete a database connection."""
    ConnectionDatabase.query.filter_by(connection_id=connection_id,
                                       id=database_id).delete()
    db.session.commit()
    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: Database Connection deleted. ({database_id})",
    )
    db.session.add(log)
    db.session.commit()
    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))
コード例 #19
0
def edit_connection_sftp(connection_id: int,
                         sftp_id: int) -> Union[Response, str]:
    """Edit a SFTP connection."""
    sftp = ConnectionSftp.query.filter_by(
        id=sftp_id, connection_id=connection_id).first_or_404()
    if request.method == "GET":
        return render_template(
            "pages/connection/sftp_edit.html.j2",
            sftp=sftp,
            connection=sftp.connection,
            title=f"Editing Connection {sftp.connection}",
        )

    form = request.form
    this_sftp = ConnectionSftp.query.filter_by(id=sftp.id)
    this_sftp.update({
        "name":
        form.get("name", "undefined", type=str).strip(),
        "address":
        form.get("address", "", type=str).strip(),
        "port":
        form.get("port", 22, type=int),
        "path":
        form.get("path", "", type=str).strip(),
        "username":
        form.get("username", "", type=str).strip(),
        "key": (em_encrypt(
            form.get("key", "", type=str).strip(), app.config["PASS_KEY"])
                if form.get("key", type=str) else None),
        "password": (em_encrypt(
            form.get("password", "", type=str).strip(), app.config["PASS_KEY"])
                     if form.get("password", type=str) else None),
    })

    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: SFTP Connection edited. ({sftp_id}) {sftp}",
    )
    db.session.add(log)
    db.session.commit()

    flash("Connection updated.")
    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))
コード例 #20
0
def delete_connection_sftp(connection_id: int, sftp_id: int) -> Response:
    """Delete a SFTP connection."""
    ConnectionSftp.query.filter_by(connection_id=connection_id,
                                   id=sftp_id).delete()
    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: SFTP Connection deleted. ({sftp_id})",
    )
    db.session.add(log)
    db.session.commit()
    flash("Connection deleted.")

    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))
コード例 #21
0
def edit_connection_smb(connection_id: int,
                        smb_id: int) -> Union[Response, str]:
    """Edit a SMB connection."""
    smb = ConnectionSmb.query.filter_by(
        id=smb_id, connection_id=connection_id).first_or_404()
    if request.method == "GET":
        return render_template(
            "pages/connection/smb_edit.html.j2",
            smb=smb,
            connection=smb.connection,
            title=f"Editing Connection {smb.connection}",
        )

    form = request.form
    this_smb = ConnectionSmb.query.filter_by(id=smb.id)
    this_smb.update({
        "name":
        form.get("name", "undefined", type=str),
        "server_name":
        form.get("server_name", "", type=str).strip(),
        "server_ip":
        form.get("server_ip", "", type=str).strip(),
        "share_name":
        form.get("share_name", "", type=str).strip(),
        "path":
        form.get("path", "", type=str).strip(),
        "username":
        form.get("username", "", type=str).strip(),
        "password": (em_encrypt(
            form.get("password", "", type=str).strip(), app.config["PASS_KEY"])
                     if form.get("password", type=str) else None),
    })

    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: SMB Connection added. ({smb.id}) {smb}",
    )
    db.session.add(log)
    db.session.commit()
    flash("Connection updated.")
    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))
コード例 #22
0
def sub_enable_task(task_id: int) -> None:
    """Shared function for enabling a task."""
    redis_client.delete("runner_" + str(task_id) + "_attempt")
    task = Task.query.filter_by(id=task_id).first()

    # task only goes to scheduler if not sequence, or first in sequence.
    if task.project and task.project.sequence_tasks == 1:
        # only add job if its first in sequence
        if Task.query.filter(
                or_(  # type: ignore[type-var]
                    and_(Task.project_id == task.project_id, Task.enabled
                         == 1),
                    Task.id == task_id,
                )).order_by(
                    Task.order.asc(),
                    Task.name.asc()  # type: ignore[union-attr]
                ).first() is not None and (Task.query.filter(
                    or_(  # type: ignore[type-var]
                        and_(Task.project_id == task.project_id, Task.enabled
                             == 1),
                        Task.id == task_id,
                    )).order_by(Task.order.asc(),
                                Task.name.asc())  # type: ignore[union-attr]
                                           .first()).id == int(task_id):
            send_task_to_scheduler(task_id)
        else:
            # make sure it is not in the scheduler.
            requests.get(app.config["SCHEDULER_HOST"] + "/delete/" +
                         str(task_id))
    else:
        send_task_to_scheduler(task_id)

    # show as enabled only if we made it to scheduler
    task.enabled = 1
    db.session.commit()

    log = TaskLog(
        task_id=task_id,
        status_id=7,
        message=(current_user.full_name or "none") + ": Task enabled.",
    )
    db.session.add(log)
    db.session.commit()
コード例 #23
0
def reset_task(task_id: int) -> Response:
    """Reset a task status to completed."""
    task = Task.query.filter_by(id=task_id).first()
    if task:
        task.status_id = 4
        db.session.commit()

        log = TaskLog(  # type: ignore[call-arg]
            task_id=task.id,
            status_id=7,
            message=(current_user.full_name or "none") +
            ": Reset task status to completed.",
        )
        db.session.add(log)
        db.session.commit()
        flash("Task has been reset to completed.")
        return redirect(url_for("task_bp.one_task", task_id=task_id))
    flash("Task does not exist.")
    return redirect(url_for("task_bp.all_tasks"))
コード例 #24
0
def delete_connection(connection_id: int) -> Response:
    """Delete a connection."""
    ConnectionSftp.query.filter_by(connection_id=connection_id).delete()
    ConnectionGpg.query.filter_by(connection_id=connection_id).delete()
    ConnectionFtp.query.filter_by(connection_id=connection_id).delete()
    ConnectionSmb.query.filter_by(connection_id=connection_id).delete()
    ConnectionSsh.query.filter_by(connection_id=connection_id).delete()
    ConnectionDatabase.query.filter_by(connection_id=connection_id).delete()
    Connection.query.filter_by(id=connection_id).delete()
    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: Connection deleted. ({connection_id})",
    )
    db.session.add(log)
    db.session.commit()
    flash("Connection deleted.")
    return redirect(url_for("connection_bp.all_connections"))
コード例 #25
0
def edit_connection_database(connection_id: int,
                             database_id: int) -> Union[Response, str]:
    """Edit a database connection."""
    database = ConnectionDatabase.query.filter_by(
        id=database_id, connection_id=connection_id).first_or_404()
    if request.method == "GET":
        return render_template(
            "pages/connection/database_edit.html.j2",
            database=database,
            connection=database.connection,
            database_types=ConnectionDatabaseType.query.all(),
            title=f"Editing Database Connection {database.connection}",
        )

    form = request.form
    this_database = ConnectionDatabase.query.filter_by(id=database.id)
    this_database.update({
        "name":
        form.get("name", "undefined", type=str).strip(),
        "type_id":
        form.get("database_type", 1, type=int),
        "connection_string": (em_encrypt(form.get(
            "connection_string", None), app.config["PASS_KEY"]) if form.get(
                "connection_string", None) else None),
        "timeout":
        form.get("timeout", app.config["DEFAULT_SQL_TIMEOUT"], type=int),
    })

    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: Database Connection edited. ({database.id}) {database}",
    )
    db.session.add(log)
    db.session.commit()

    flash("Changes saved.")
    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))
コード例 #26
0
def edit_connection(connection_id: int) -> Union[str, Response]:
    """Edit a connection."""
    connection = Connection.query.filter_by(id=connection_id).first_or_404()

    if request.method == "GET":
        return render_template(
            "pages/connection/new.html.j2",
            connection=connection,
            title=f"Editing connection {connection}",
        )

    form = request.form
    my_connection = Connection.query.filter_by(id=connection.id)
    my_connection.update({
        "name":
        form.get("name", "undefined", type=str).strip(),
        "description":
        form.get("description", "", type=str).strip(),
        "address":
        form.get("address", "", type=str).strip(),
        "primary_contact":
        form.get("contact", type=str),
        "primary_contact_email":
        form.get("email", type=str),
        "primary_contact_phone":
        form.get("phone", type=str),
    })

    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: Connection group edited. ({connection.id}) {connection}",
    )
    db.session.add(log)
    db.session.commit()
    flash("Connection edited.")
    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))
コード例 #27
0
def new_connection_sftp(connection_id: int) -> Union[str, Response]:
    """Create a SFTP connection."""
    connection = Connection.query.filter_by(id=connection_id).first_or_404()
    if request.method == "GET":
        return render_template(
            "pages/connection/sftp_edit.html.j2",
            connection=connection,
            title="New SFTP Connection",
        )
    form = request.form

    sftp = ConnectionSftp(
        connection_id=connection_id,
        name=form.get("name", "undefined", type=str).strip(),
        address=form.get("address", "", type=str).strip(),
        port=form.get("port", 22, type=int),
        path=form.get("path", "", type=str).strip(),
        username=form.get("username", "", type=str).strip(),
        key=(em_encrypt(
            form.get("key", "", type=str).strip(), app.config["PASS_KEY"])
             if form.get("key", type=str) else None),
        password=(em_encrypt(
            form.get("password", "", type=str).strip(), app.config["PASS_KEY"])
                  if form.get("password", type=str) else None),
    )

    db.session.add(sftp)
    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: SFTP Connection added. ({sftp.id}) {sftp}",
    )
    db.session.add(log)
    db.session.commit()

    flash("Connection added.")
    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))
コード例 #28
0
def new_connection_smb(connection_id: int) -> Union[Response, str]:
    """Create a SMB connection."""
    connection = Connection.query.filter_by(id=connection_id).first_or_404()
    if request.method == "GET":
        return render_template(
            "pages/connection/smb_edit.html.j2",
            connection=connection,
            title="New SMB Connection",
        )
    form = request.form

    smb = ConnectionSmb(
        connection_id=connection_id,
        name=form.get("name", "undefined", type=str),
        server_name=form.get("server_name", "", type=str).strip(),
        server_ip=form.get("server_ip", "", type=str).strip(),
        share_name=form.get("share_name", "", type=str).strip(),
        path=form.get("path", "", type=str).strip(),
        username=form.get("username", "", type=str).strip(),
        password=(em_encrypt(
            form.get("password", "", type=str).strip(), app.config["PASS_KEY"])
                  if form.get("password", type=str) else None),
    )

    db.session.add(smb)
    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: SMB Connection added. ({smb.id}) {smb}",
    )
    db.session.add(log)
    db.session.commit()

    flash("Connection added.")
    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))
コード例 #29
0
def new_connection_database(connection_id: int) -> Union[Response, str]:
    """Create a database connection."""
    connection = Connection.query.filter_by(id=connection_id).first_or_404()
    if request.method == "GET":
        return render_template(
            "pages/connection/database_edit.html.j2",
            connection=connection,
            database_types=ConnectionDatabaseType.query.all(),
            title="New Database Connection",
        )
    form = request.form
    database = ConnectionDatabase(
        connection_id=connection_id,
        name=form.get("name", "undefined", type=str).strip(),
        type_id=form.get("database_type", 1, type=int),
        connection_string=(em_encrypt(form.get("connection_string"),
                                      app.config["PASS_KEY"])
                           if form.get("connection_string") else None),
        timeout=form.get("timeout",
                         app.config["DEFAULT_SQL_TIMEOUT"],
                         type=int),
    )

    db.session.add(database)
    db.session.commit()

    log = TaskLog(
        status_id=7,
        message=
        f"{current_user.full_name}: Database Connection added. ({database.id}) {database}",
    )
    db.session.add(log)
    db.session.commit()
    flash("Connection added.")
    return redirect(
        url_for("connection_bp.one_connection", connection_id=connection_id))
コード例 #30
0
def task_new(project_id: int) -> Union[str, Response]:
    """Create a new task."""
    cache.clear()
    # create tasks
    form = request.form

    me = Task(
        name=form.get("name", "undefined").strip(),
        project_id=project_id,
        creator_id=current_user.id,
        updater_id=current_user.id,
        max_retries=form.get("task-retry", 0, type=int),
        order=form.get("task-rank", 0, type=int),
        source_type_id=form.get("sourceType", None, type=int),
        source_database_id=form.get("task-source-database", None, type=int),
        source_query_include_header=form.get(
            "task_include_query_headers", None, type=int
        ),
        source_smb_id=form.get("task-source-smb", None, type=int),
        source_smb_file=form.get("sourceSmbFile", None, type=str),
        source_smb_ignore_delimiter=form.get("task_smb_ignore_delimiter", 0, type=int),
        source_smb_delimiter=form.get("sourceSmbDelimiter", None, type=str),
        source_sftp_id=form.get("task-source-sftp", None, type=int),
        source_sftp_file=form.get("sourceSftpFile", None, type=str),
        source_sftp_ignore_delimiter=form.get(
            "task_sftp_ignore_delimiter", None, type=int
        ),
        enable_source_cache=form.get("task_enable_source_cache", None, type=int),
        source_require_sql_output=form.get("task_require_sql_output", None, type=int),
        source_sftp_delimiter=form.get("sourceSftpDelimiter", None, type=str),
        source_ftp_id=form.get("task-source-ftp", None, type=int),
        source_ftp_file=form.get("sourceFtpFile", None, type=str),
        source_ftp_ignore_delimiter=form.get(
            "task_ftp_ignore_delimiter", None, type=int
        ),
        source_ftp_delimiter=form.get("sourceFtpDelimiter", None, type=str),
        source_ssh_id=form.get("task-source-ssh", None, type=int),
        source_query_type_id=form.get("sourceQueryType", None, type=int),
        query_params=form.get("taskParams", "", type=str).strip(),
        source_git=form.get("sourceGit", None, type=str),
        query_smb_file=form.get("sourceQuerySmbFile", None, type=str),
        query_smb_id=form.get("task-query-smb", None, type=int),
        query_sftp_file=form.get("sourceQuerySftpFile", None, type=str),
        query_sftp_id=form.get("task-query-sftp", None, type=int),
        query_ftp_file=form.get("sourceQueryFtpFile", None, type=str),
        query_ftp_id=form.get("task-query-ftp", None, type=int),
        source_url=form.get("sourceURL", None, type=str),
        source_code=form.get("sourceCode", None, type=str),
        processing_type_id=form.get("processingType", None, type=int),
        processing_smb_file=form.get("processingSmbFile", None, type=str),
        processing_smb_id=form.get("task-processing-smb", None, type=int),
        processing_sftp_file=form.get("processingSftpFile", None, type=str),
        processing_sftp_id=form.get("task-processing-sftp", None, type=int),
        processing_ftp_file=form.get("processingFtpFile", None, type=str),
        processing_ftp_id=form.get("task-processing-ftp", None, type=int),
        processing_git=form.get("processingGit", None, type=str),
        processing_url=form.get("processingUrl", None, type=str),
        processing_code=form.get("processingCode", None, type=str),
        processing_command=form.get("processingCommand", None, type=str),
        destination_quote_level_id=form.get("quoteLevel", 3, type=int),
        destination_file_type_id=form.get("fileType", None, type=int),
        destination_file_name=form.get(
            "destinationFileName", form.get("name", "undefined", type=str), type=str
        ),
        destination_create_zip=form.get("task_create_zip", None, type=int),
        destination_zip_name=form.get(
            "destinationZipName", form.get("name", "undefined", type=str), type=str
        ),
        destination_file_delimiter=form.get("fileDelimiter", None, type=str),
        destination_file_line_terminator=form.get("fileTerminator", None, type=str),
        destination_ignore_delimiter=form.get(
            "task_ignore_file_delimiter", None, type=int
        ),
        file_gpg=form.get("task_file_gpg", 0, type=int),
        file_gpg_id=form.get("task-file-gpg", None, type=int),
        destination_sftp=form.get("task_save_sftp", 0, type=int),
        destination_sftp_id=form.get("task-destination-sftp", None, type=int),
        destination_sftp_overwrite=form.get("task_overwrite_sftp", None, type=int),
        destination_sftp_dont_send_empty_file=form.get(
            "task_sftp_dont_send_empty", 0, type=int
        ),
        destination_ftp=form.get("task_save_ftp", 0, type=int),
        destination_ftp_id=form.get("task-destination-ftp", None, type=int),
        destination_ftp_overwrite=form.get("task_overwrite_ftp", None, type=int),
        destination_ftp_dont_send_empty_file=form.get(
            "task_ftp_dont_send_empty", 0, type=int
        ),
        destination_smb=form.get("task_save_smb", 0, type=int),
        destination_smb_id=form.get("task-destination-smb", None, type=int),
        destination_smb_overwrite=form.get("task_overwrite_smb", None, type=int),
        destination_smb_dont_send_empty_file=form.get(
            "task_smb_dont_send_empty", 0, type=int
        ),
        email_completion=form.get("task_send_completion_email", 0, type=int),
        email_completion_recipients=form.get("completionEmailRecip", "", type=str),
        email_completion_message=form.get("completion_email_msg", "", type=str),
        email_completion_log=form.get("task_send_completion_email_log", 0, type=int),
        email_completion_file=form.get("task_send_output", 0, type=int),
        email_completion_dont_send_empty_file=form.get(
            "task_dont_send_empty", 0, type=int
        ),
        email_completion_file_embed=form.get("task_embed_output", 0, type=int),
        email_error=form.get("task_send_error_email", 0, type=int),
        email_error_recipients=form.get("errorEmailRecip", "", type=str),
        email_error_message=form.get("errorEmailMsg", "", type=str),
        enabled=form.get("task-ooff", 0, type=int),
    )

    db.session.add(me)
    db.session.commit()

    log = TaskLog(
        task_id=me.id,
        status_id=7,
        message=(current_user.full_name or "none") + ": Task created.",
    )
    db.session.add(log)
    db.session.commit()

    if me.enabled == 1:
        submit_executor("enable_task", me.id)

    return redirect(url_for("task_bp.one_task", task_id=me.id))