2023-04-21 14:55:10 +02:00
|
|
|
import { FindOptions } from 'sequelize'
|
2024-02-22 10:12:04 +01:00
|
|
|
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { MRunner } from '@server/types/models/runners/index.js'
|
|
|
|
import { Runner } from '@peertube/peertube-models'
|
2024-02-22 10:12:04 +01:00
|
|
|
import { SequelizeModel, getSort } from '../shared/index.js'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { RunnerRegistrationTokenModel } from './runner-registration-token.js'
|
|
|
|
import { CONSTRAINTS_FIELDS } from '@server/initializers/constants.js'
|
2023-04-21 14:55:10 +02:00
|
|
|
|
|
|
|
@Table({
|
|
|
|
tableName: 'runner',
|
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'runnerToken' ],
|
|
|
|
unique: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'runnerRegistrationTokenId' ]
|
2023-07-12 10:53:46 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'name' ],
|
|
|
|
unique: true
|
2023-04-21 14:55:10 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2024-02-22 10:12:04 +01:00
|
|
|
export class RunnerModel extends SequelizeModel<RunnerModel> {
|
2023-04-21 14:55:10 +02:00
|
|
|
|
|
|
|
// Used to identify the appropriate runner when it uses the runner REST API
|
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
runnerToken: string
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
name: string
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.RUNNERS.DESCRIPTION.max))
|
|
|
|
description: string
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
lastContact: Date
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
ip: string
|
|
|
|
|
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
|
|
|
|
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
|
|
|
|
|
|
|
@ForeignKey(() => RunnerRegistrationTokenModel)
|
|
|
|
@Column
|
|
|
|
runnerRegistrationTokenId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => RunnerRegistrationTokenModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2023-07-31 14:34:36 +02:00
|
|
|
RunnerRegistrationToken: Awaited<RunnerRegistrationTokenModel>
|
2023-04-21 14:55:10 +02:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static load (id: number) {
|
|
|
|
return RunnerModel.findByPk(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
static loadByToken (runnerToken: string) {
|
|
|
|
const query = {
|
|
|
|
where: { runnerToken }
|
|
|
|
}
|
|
|
|
|
|
|
|
return RunnerModel.findOne(query)
|
|
|
|
}
|
|
|
|
|
2023-07-12 10:53:46 +02:00
|
|
|
static loadByName (name: string) {
|
|
|
|
const query = {
|
|
|
|
where: { name }
|
|
|
|
}
|
|
|
|
|
|
|
|
return RunnerModel.findOne(query)
|
|
|
|
}
|
|
|
|
|
2023-04-21 14:55:10 +02:00
|
|
|
static listForApi (options: {
|
|
|
|
start: number
|
|
|
|
count: number
|
|
|
|
sort: string
|
|
|
|
}) {
|
|
|
|
const { start, count, sort } = options
|
|
|
|
|
|
|
|
const query: FindOptions = {
|
|
|
|
offset: start,
|
|
|
|
limit: count,
|
|
|
|
order: getSort(sort)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all([
|
|
|
|
RunnerModel.count(query),
|
|
|
|
RunnerModel.findAll<MRunner>(query)
|
|
|
|
]).then(([ total, data ]) => ({ total, data }))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
toFormattedJSON (this: MRunner): Runner {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
|
|
|
|
name: this.name,
|
|
|
|
description: this.description,
|
|
|
|
|
|
|
|
ip: this.ip,
|
|
|
|
lastContact: this.lastContact,
|
|
|
|
|
|
|
|
createdAt: this.createdAt,
|
|
|
|
updatedAt: this.updatedAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|