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