]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - scripts/create-move-video-storage-job.ts
Stop using tsconfig register
[github/Chocobozzz/PeerTube.git] / scripts / create-move-video-storage-job.ts
1 import { program } from 'commander'
2 import { CONFIG } from '@server/initializers/config'
3 import { initDatabaseModels } from '@server/initializers/database'
4 import { JobQueue } from '@server/lib/job-queue'
5 import { moveToExternalStorageState } from '@server/lib/video-state'
6 import { VideoModel } from '@server/models/video/video'
7 import { VideoState, VideoStorage } from '@shared/models'
8
9 program
10 .description('Move videos to another storage.')
11 .option('-o, --to-object-storage', 'Move videos in object storage')
12 .option('-v, --video [videoUUID]', 'Move a specific video')
13 .option('-a, --all-videos', 'Migrate all videos')
14 .parse(process.argv)
15
16 const options = program.opts()
17
18 if (!options['toObjectStorage']) {
19 console.error('You need to choose where to send video files.')
20 process.exit(-1)
21 }
22
23 if (!options['video'] && !options['allVideos']) {
24 console.error('You need to choose which videos to move.')
25 process.exit(-1)
26 }
27
28 if (options['toObjectStorage'] && !CONFIG.OBJECT_STORAGE.ENABLED) {
29 console.error('Object storage is not enabled on this instance.')
30 process.exit(-1)
31 }
32
33 run()
34 .then(() => process.exit(0))
35 .catch(err => console.error(err))
36
37 async function run () {
38 await initDatabaseModels(true)
39
40 JobQueue.Instance.init(true)
41
42 let ids: number[] = []
43
44 if (options['video']) {
45 const video = await VideoModel.load(options['video'])
46
47 if (!video) {
48 console.error('Unknown video ' + options['video'])
49 process.exit(-1)
50 }
51
52 if (video.remote === true) {
53 console.error('Cannot process a remote video')
54 process.exit(-1)
55 }
56
57 if (video.isLive) {
58 console.error('Cannot process live video')
59 process.exit(-1)
60 }
61
62 if (video.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) {
63 console.error('This video is already being moved to external storage')
64 process.exit(-1)
65 }
66
67 ids.push(video.id)
68 } else {
69 ids = await VideoModel.listLocalIds()
70 }
71
72 for (const id of ids) {
73 const videoFull = await VideoModel.loadAndPopulateAccountAndServerAndTags(id)
74
75 const files = videoFull.VideoFiles || []
76 const hls = videoFull.getHLSPlaylist()
77
78 if (files.some(f => f.storage === VideoStorage.FILE_SYSTEM) || hls?.storage === VideoStorage.FILE_SYSTEM) {
79 console.log('Processing video %s.', videoFull.name)
80
81 const success = await moveToExternalStorageState(videoFull, false, undefined)
82
83 if (!success) {
84 console.error(
85 'Cannot create move job for %s: job creation may have failed or there may be pending transcoding jobs for this video',
86 videoFull.name
87 )
88 }
89 }
90
91 console.log(`Created move-to-object-storage job for ${videoFull.name}.`)
92 }
93 }