2018-06-26 02:04:47 +00:00
|
|
|
|
const Model = require('objection').Model
|
|
|
|
|
const path = require('path')
|
2018-07-08 05:12:43 +00:00
|
|
|
|
const fs = require('fs-extra')
|
2018-06-26 02:04:47 +00:00
|
|
|
|
const _ = require('lodash')
|
2018-07-08 05:12:43 +00:00
|
|
|
|
const yaml = require('js-yaml')
|
2018-07-30 02:23:33 +00:00
|
|
|
|
const commonHelper = require('../helpers/common')
|
2018-06-26 02:04:47 +00:00
|
|
|
|
|
|
|
|
|
/* global WIKI */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Storage model
|
|
|
|
|
*/
|
|
|
|
|
module.exports = class Storage extends Model {
|
|
|
|
|
static get tableName() { return 'storage' }
|
2018-09-08 19:49:36 +00:00
|
|
|
|
static get idColumn() { return 'key' }
|
2018-06-26 02:04:47 +00:00
|
|
|
|
|
|
|
|
|
static get jsonSchema () {
|
|
|
|
|
return {
|
|
|
|
|
type: 'object',
|
2018-08-04 21:27:55 +00:00
|
|
|
|
required: ['key', 'isEnabled'],
|
2018-06-26 02:04:47 +00:00
|
|
|
|
|
|
|
|
|
properties: {
|
|
|
|
|
key: {type: 'string'},
|
|
|
|
|
isEnabled: {type: 'boolean'},
|
2019-02-10 00:10:34 +00:00
|
|
|
|
mode: {type: 'string'}
|
2018-06-26 02:04:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-10 00:10:34 +00:00
|
|
|
|
static get jsonAttributes() {
|
2019-02-23 02:37:26 +00:00
|
|
|
|
return ['config', 'state']
|
2019-02-10 00:10:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-26 02:04:47 +00:00
|
|
|
|
static async getTargets() {
|
2018-07-30 02:23:33 +00:00
|
|
|
|
return WIKI.models.storage.query()
|
2018-06-26 02:04:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async refreshTargetsFromDisk() {
|
2018-08-16 01:57:28 +00:00
|
|
|
|
let trx
|
2018-06-26 02:04:47 +00:00
|
|
|
|
try {
|
2018-07-30 02:23:33 +00:00
|
|
|
|
const dbTargets = await WIKI.models.storage.query()
|
2018-07-08 05:12:43 +00:00
|
|
|
|
|
|
|
|
|
// -> Fetch definitions from disk
|
|
|
|
|
const storageDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/storage'))
|
|
|
|
|
let diskTargets = []
|
|
|
|
|
for (let dir of storageDirs) {
|
|
|
|
|
const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/storage', dir, 'definition.yml'), 'utf8')
|
|
|
|
|
diskTargets.push(yaml.safeLoad(def))
|
|
|
|
|
}
|
2018-07-30 02:23:33 +00:00
|
|
|
|
WIKI.data.storage = diskTargets.map(target => ({
|
|
|
|
|
...target,
|
2019-02-03 22:08:06 +00:00
|
|
|
|
isAvailable: _.get(target, 'isAvailable', false),
|
2018-08-04 21:27:55 +00:00
|
|
|
|
props: commonHelper.parseModuleProps(target.props)
|
2018-07-30 02:23:33 +00:00
|
|
|
|
}))
|
2018-07-08 05:12:43 +00:00
|
|
|
|
|
|
|
|
|
// -> Insert new targets
|
2018-06-26 02:04:47 +00:00
|
|
|
|
let newTargets = []
|
2018-07-30 02:23:33 +00:00
|
|
|
|
for (let target of WIKI.data.storage) {
|
2018-06-26 02:04:47 +00:00
|
|
|
|
if (!_.some(dbTargets, ['key', target.key])) {
|
|
|
|
|
newTargets.push({
|
|
|
|
|
key: target.key,
|
|
|
|
|
isEnabled: false,
|
2019-02-03 22:08:06 +00:00
|
|
|
|
mode: target.defaultMode || 'push',
|
2019-02-17 06:32:35 +00:00
|
|
|
|
syncInterval: target.schedule || 'P0D',
|
2018-07-01 23:50:42 +00:00
|
|
|
|
config: _.transform(target.props, (result, value, key) => {
|
2018-07-30 02:23:33 +00:00
|
|
|
|
_.set(result, key, value.default)
|
2018-06-26 02:04:47 +00:00
|
|
|
|
return result
|
2019-02-17 06:32:35 +00:00
|
|
|
|
}, {}),
|
|
|
|
|
state: {
|
|
|
|
|
status: 'pending',
|
2019-02-25 04:48:28 +00:00
|
|
|
|
message: '',
|
|
|
|
|
lastAttempt: null
|
2019-02-17 06:32:35 +00:00
|
|
|
|
}
|
2018-06-26 02:04:47 +00:00
|
|
|
|
})
|
2018-07-30 02:23:33 +00:00
|
|
|
|
} else {
|
|
|
|
|
const targetConfig = _.get(_.find(dbTargets, ['key', target.key]), 'config', {})
|
|
|
|
|
await WIKI.models.storage.query().patch({
|
|
|
|
|
config: _.transform(target.props, (result, value, key) => {
|
|
|
|
|
if (!_.has(result, key)) {
|
|
|
|
|
_.set(result, key, value.default)
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}, targetConfig)
|
|
|
|
|
}).where('key', target.key)
|
2018-06-26 02:04:47 +00:00
|
|
|
|
}
|
2018-07-30 02:23:33 +00:00
|
|
|
|
}
|
2018-06-26 02:04:47 +00:00
|
|
|
|
if (newTargets.length > 0) {
|
2018-08-16 01:57:28 +00:00
|
|
|
|
trx = await WIKI.models.Objection.transaction.start(WIKI.models.knex)
|
|
|
|
|
for (let target of newTargets) {
|
|
|
|
|
await WIKI.models.storage.query(trx).insert(target)
|
|
|
|
|
}
|
|
|
|
|
await trx.commit()
|
2018-06-26 02:04:47 +00:00
|
|
|
|
WIKI.logger.info(`Loaded ${newTargets.length} new storage targets: [ OK ]`)
|
|
|
|
|
} else {
|
|
|
|
|
WIKI.logger.info(`No new storage targets found: [ SKIPPED ]`)
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
WIKI.logger.error(`Failed to scan or load new storage providers: [ FAILED ]`)
|
|
|
|
|
WIKI.logger.error(err)
|
2018-08-16 01:57:28 +00:00
|
|
|
|
if (trx) {
|
|
|
|
|
trx.rollback()
|
|
|
|
|
}
|
2018-06-26 02:04:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-22 20:25:39 +00:00
|
|
|
|
|
2019-02-18 02:48:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* Initialize active storage targets
|
|
|
|
|
*/
|
2019-02-03 07:48:30 +00:00
|
|
|
|
static async initTargets() {
|
2019-02-18 02:48:48 +00:00
|
|
|
|
this.targets = await WIKI.models.storage.query().where('isEnabled', true).orderBy('key')
|
2019-02-03 07:48:30 +00:00
|
|
|
|
try {
|
2019-02-18 02:48:48 +00:00
|
|
|
|
// -> Stop and delete existing jobs
|
|
|
|
|
const prevjobs = _.remove(WIKI.scheduler.jobs, job => job.name === `sync-storage`)
|
|
|
|
|
if (prevjobs.length > 0) {
|
|
|
|
|
prevjobs.forEach(job => job.stop())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -> Initialize targets
|
|
|
|
|
for(let target of this.targets) {
|
|
|
|
|
const targetDef = _.find(WIKI.data.storage, ['key', target.key])
|
2019-02-03 07:48:30 +00:00
|
|
|
|
target.fn = require(`../modules/storage/${target.key}/storage`)
|
2019-02-03 22:08:06 +00:00
|
|
|
|
target.fn.config = target.config
|
|
|
|
|
target.fn.mode = target.mode
|
2019-02-17 06:32:35 +00:00
|
|
|
|
try {
|
|
|
|
|
await target.fn.init()
|
2019-02-18 02:48:48 +00:00
|
|
|
|
|
|
|
|
|
// -> Save succeeded init state
|
2019-02-17 06:32:35 +00:00
|
|
|
|
await WIKI.models.storage.query().patch({
|
|
|
|
|
state: {
|
|
|
|
|
status: 'operational',
|
2019-02-25 04:48:28 +00:00
|
|
|
|
message: '',
|
|
|
|
|
lastAttempt: new Date().toISOString()
|
2019-02-17 06:32:35 +00:00
|
|
|
|
}
|
|
|
|
|
}).where('key', target.key)
|
2019-02-18 02:48:48 +00:00
|
|
|
|
|
|
|
|
|
// -> Set recurring sync job
|
|
|
|
|
if (targetDef.schedule && target.syncInterval !== `P0D`) {
|
|
|
|
|
WIKI.scheduler.registerJob({
|
|
|
|
|
name: `sync-storage`,
|
|
|
|
|
immediate: false,
|
|
|
|
|
schedule: target.syncInterval,
|
|
|
|
|
repeat: true
|
|
|
|
|
}, target.key)
|
|
|
|
|
}
|
2019-02-17 06:32:35 +00:00
|
|
|
|
} catch (err) {
|
2019-02-18 02:48:48 +00:00
|
|
|
|
// -> Save initialization error
|
2019-02-17 06:32:35 +00:00
|
|
|
|
await WIKI.models.storage.query().patch({
|
|
|
|
|
state: {
|
|
|
|
|
status: 'error',
|
2019-02-25 04:48:28 +00:00
|
|
|
|
message: err.message,
|
|
|
|
|
lastAttempt: new Date().toISOString()
|
2019-02-17 06:32:35 +00:00
|
|
|
|
}
|
|
|
|
|
}).where('key', target.key)
|
|
|
|
|
}
|
2019-02-03 07:48:30 +00:00
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
WIKI.logger.warn(err)
|
|
|
|
|
throw err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-30 02:23:33 +00:00
|
|
|
|
static async pageEvent({ event, page }) {
|
2019-02-03 07:48:30 +00:00
|
|
|
|
try {
|
2019-02-18 02:48:48 +00:00
|
|
|
|
for(let target of this.targets) {
|
2019-02-03 22:08:06 +00:00
|
|
|
|
await target.fn[event](page)
|
2019-02-03 07:48:30 +00:00
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
WIKI.logger.warn(err)
|
|
|
|
|
throw err
|
2018-07-22 20:25:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-26 02:04:47 +00:00
|
|
|
|
}
|