aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/migrations
diff options
context:
space:
mode:
authorkontrollanten <6680299+kontrollanten@users.noreply.github.com>2021-11-09 11:05:35 +0100
committerGitHub <noreply@github.com>2021-11-09 11:05:35 +0100
commite1ab52d7ec7370a6f9f5937192d6003206af1ac0 (patch)
treeaecc8b696b0021e073fd205dd6e126fb4f178e8f /scripts/migrations
parentc49c366ac320fe5ac3dc08f5891fe5898c1b34e3 (diff)
downloadPeerTube-e1ab52d7ec7370a6f9f5937192d6003206af1ac0.tar.gz
PeerTube-e1ab52d7ec7370a6f9f5937192d6003206af1ac0.tar.zst
PeerTube-e1ab52d7ec7370a6f9f5937192d6003206af1ac0.zip
Add migrate-to-object-storage script (#4481)
* add migrate-to-object-storage-script closes #4467 * add migrate-to-unique-playlist-filenames script * fix(migrate-to-unique-playlist-filenames): update master/segments256 run updateMasterHLSPlaylist and updateSha256VODSegments after file rename. * Improve move to object storage scripts * PR remarks Co-authored-by: Chocobozzz <me@florianbigard.com>
Diffstat (limited to 'scripts/migrations')
-rw-r--r--scripts/migrations/peertube-4.0.ts107
1 files changed, 107 insertions, 0 deletions
diff --git a/scripts/migrations/peertube-4.0.ts b/scripts/migrations/peertube-4.0.ts
new file mode 100644
index 000000000..387f6dc9c
--- /dev/null
+++ b/scripts/migrations/peertube-4.0.ts
@@ -0,0 +1,107 @@
1import { registerTSPaths } from '../../server/helpers/register-ts-paths'
2registerTSPaths()
3
4import { join } from 'path'
5import { JobQueue } from '@server/lib/job-queue'
6import { initDatabaseModels } from '../../server/initializers/database'
7import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getHlsResolutionPlaylistFilename } from '@server/lib/paths'
8import { VideoPathManager } from '@server/lib/video-path-manager'
9import { VideoModel } from '@server/models/video/video'
10import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
11import { move, readFile, writeFile } from 'fs-extra'
12import Bluebird from 'bluebird'
13import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
14
15run()
16 .then(() => process.exit(0))
17 .catch(err => {
18 console.error(err)
19 process.exit(-1)
20 })
21
22async function run () {
23 console.log('Migrate old HLS paths to new format.')
24
25 await initDatabaseModels(true)
26
27 JobQueue.Instance.init(true)
28
29 const ids = await VideoModel.listLocalIds()
30
31 await Bluebird.map(ids, async id => {
32 try {
33 await processVideo(id)
34 } catch (err) {
35 console.error('Cannot process video %s.', { err })
36 }
37 }, { concurrency: 5 })
38
39 console.log('Migration finished!')
40}
41
42async function processVideo (videoId: number) {
43 const video = await VideoModel.loadWithFiles(videoId)
44
45 const hls = video.getHLSPlaylist()
46 if (!hls || hls.playlistFilename !== 'master.m3u8' || hls.VideoFiles.length === 0) {
47 return
48 }
49
50 console.log(`Renaming HLS playlist files of video ${video.name}.`)
51
52 const playlist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
53 const hlsDirPath = VideoPathManager.Instance.getFSHLSOutputPath(video)
54
55 const masterPlaylistPath = join(hlsDirPath, playlist.playlistFilename)
56 let masterPlaylistContent = await readFile(masterPlaylistPath, 'utf8')
57
58 for (const videoFile of hls.VideoFiles) {
59 const srcName = `${videoFile.resolution}.m3u8`
60 const dstName = getHlsResolutionPlaylistFilename(videoFile.filename)
61
62 const src = join(hlsDirPath, srcName)
63 const dst = join(hlsDirPath, dstName)
64
65 try {
66 await move(src, dst)
67
68 masterPlaylistContent = masterPlaylistContent.replace(new RegExp('^' + srcName + '$', 'm'), dstName)
69 } catch (err) {
70 console.error('Cannot move video file %s to %s.', src, dst, err)
71 }
72 }
73
74 await writeFile(masterPlaylistPath, masterPlaylistContent)
75
76 if (playlist.segmentsSha256Filename === 'segments-sha256.json') {
77 try {
78 const newName = generateHlsSha256SegmentsFilename(video.isLive)
79
80 const dst = join(hlsDirPath, newName)
81 await move(join(hlsDirPath, playlist.segmentsSha256Filename), dst)
82 playlist.segmentsSha256Filename = newName
83 } catch (err) {
84 console.error(`Cannot rename ${video.name} segments-sha256.json file to a new name`, err)
85 }
86 }
87
88 if (playlist.playlistFilename === 'master.m3u8') {
89 try {
90 const newName = generateHLSMasterPlaylistFilename(video.isLive)
91
92 const dst = join(hlsDirPath, newName)
93 await move(join(hlsDirPath, playlist.playlistFilename), dst)
94 playlist.playlistFilename = newName
95 } catch (err) {
96 console.error(`Cannot rename ${video.name} master.m3u8 file to a new name`, err)
97 }
98 }
99
100 // Everything worked, we can save the playlist now
101 await playlist.save()
102
103 const allVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.id)
104 await federateVideoIfNeeded(allVideo, false)
105
106 console.log(`Successfully moved HLS files of ${video.name}.`)
107}