~martijnbraam/pts-centralcontroller

72807b5af01a127663ef20dc0ba8f2f00ba1d52e — Martijn Braam 2 months ago ab55449
Add artifact storage to the model
A migrations/versions/11aaff247a81_artifacts.py => migrations/versions/11aaff247a81_artifacts.py +52 -0
@@ 0,0 1,52 @@
"""artifacts

Revision ID: 11aaff247a81
Revises: 9daa42b8d4c7
Create Date: 2022-09-25 14:12:04.937995

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '11aaff247a81'
down_revision = '9daa42b8d4c7'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('artifact',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('created', sa.DateTime(), nullable=True),
    sa.Column('job_id', sa.Integer(), nullable=True),
    sa.Column('task_id', sa.Integer(), nullable=True),
    sa.Column('name', sa.String(), nullable=True),
    sa.Column('size', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['job_id'], ['job.id'], name='fk_job_id'),
    sa.ForeignKeyConstraint(['task_id'], ['task.id'], name='fk_task_id'),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('controller', schema=None) as batch_op:
        batch_op.add_column(sa.Column('last_contact', sa.DateTime(), nullable=True))

    with op.batch_alter_table('task', schema=None) as batch_op:
        batch_op.add_column(sa.Column('devicetype_id', sa.Integer(), nullable=True))
        batch_op.create_foreign_key('fk_devicetype_id', 'device_type', ['devicetype_id'], ['id'])

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('task', schema=None) as batch_op:
        batch_op.drop_constraint('fk_devicetype_id', type_='foreignkey')
        batch_op.drop_column('devicetype_id')

    with op.batch_alter_table('controller', schema=None) as batch_op:
        batch_op.drop_column('last_contact')

    op.drop_table('artifact')
    # ### end Alembic commands ###

A pts_central/frontend/artifact.py => pts_central/frontend/artifact.py +20 -0
@@ 0,0 1,20 @@
import os

from flask import Blueprint, render_template, send_from_directory, send_file

from pts_central import app, db
from pts_central.models import Artifact

blueprint_artifact = Blueprint('artifact', __name__)


@blueprint_artifact.route('/artifacts/<int:artifact_id>')
def get(artifact_id):
    artifact = Artifact.get_or_404(artifact_id)
    if artifact.job_id is not None:
        job_id = artifact.job_id
    else:
        job_id = artifact.task.job_id

    path = os.path.join(app.config['DATA_DIR'], 'artifacts', str(job_id), str(artifact_id))
    return send_file(path, as_attachment=True, download_name=artifact.name)

M requirements.txt => requirements.txt +3 -1
@@ 3,5 3,7 @@ Flask-Login
Flask-Migrate
Flask-SqlAlchemy
Flask-WTF
Flask-MultiMQTT
SQLAlchemy-Utils
WTForms-SQLAlchemy
\ No newline at end of file
WTForms-SQLAlchemy
waitress
\ No newline at end of file

A serve.py => serve.py +6 -0
@@ 0,0 1,6 @@
import os

from waitress import serve
import filedropper

serve(filedropper.app, host='0.0.0.0', port=5000, url_scheme=os.environ.get('URL_SCHEME') or 'http')
\ No newline at end of file