2020-02-19 15:39:35 +01:00
|
|
|
import { elasticSearch } from '../helpers/elastic-search'
|
|
|
|
import { CONFIG } from '../initializers/constants'
|
2020-09-03 14:25:57 +02:00
|
|
|
import { listIndexInstancesHost } from './instances-index'
|
2020-02-19 15:39:35 +01:00
|
|
|
|
|
|
|
async function buildInstanceHosts () {
|
2020-05-28 15:00:37 +02:00
|
|
|
let indexHosts = await listIndexInstancesHost()
|
|
|
|
|
2020-06-11 11:04:00 +02:00
|
|
|
if (CONFIG.INSTANCES_INDEX.WHITELIST.ENABLED) {
|
2020-09-03 14:25:57 +02:00
|
|
|
const whitelistHosts = Array.isArray(CONFIG.INSTANCES_INDEX.WHITELIST.HOSTS)
|
|
|
|
? CONFIG.INSTANCES_INDEX.WHITELIST.HOSTS
|
|
|
|
: []
|
2020-05-28 15:00:37 +02:00
|
|
|
|
|
|
|
indexHosts = indexHosts.filter(h => whitelistHosts.includes(h))
|
|
|
|
}
|
2020-02-19 15:39:35 +01:00
|
|
|
|
2020-09-03 14:25:57 +02:00
|
|
|
const dbHosts = await listDBInstances()
|
2020-02-19 15:39:35 +01:00
|
|
|
const removedHosts = getRemovedHosts(dbHosts, indexHosts)
|
|
|
|
|
|
|
|
return { indexHosts, removedHosts }
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
buildInstanceHosts
|
|
|
|
}
|
2020-09-03 14:25:57 +02:00
|
|
|
|
|
|
|
// ##################################################
|
|
|
|
|
|
|
|
async function listDBInstances () {
|
|
|
|
const setResult = new Set<string>()
|
|
|
|
const indexes = [
|
|
|
|
CONFIG.ELASTIC_SEARCH.INDEXES.VIDEOS,
|
|
|
|
CONFIG.ELASTIC_SEARCH.INDEXES.CHANNELS
|
|
|
|
]
|
|
|
|
|
|
|
|
for (const index of indexes) {
|
|
|
|
const res = await elasticSearch.search({
|
|
|
|
index,
|
|
|
|
body: {
|
|
|
|
size: 0,
|
|
|
|
aggs: {
|
|
|
|
hosts: {
|
|
|
|
terms: {
|
|
|
|
size: 5000,
|
|
|
|
field: 'host'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
for (const b of res.body.aggregations.hosts.buckets) {
|
|
|
|
setResult.add(b.key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Array.from(setResult)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRemovedHosts (dbHosts: string[], indexHosts: string[]) {
|
|
|
|
return dbHosts.filter(dbHost => indexHosts.includes(dbHost) === false)
|
|
|
|
}
|