diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /scripts/migrations | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip |
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge
conflicts, but it's a major step forward:
* Server can be faster at startup because imports() are async and we can
easily lazy import big modules
* Angular doesn't seem to support ES import (with .js extension), so we
had to correctly organize peertube into a monorepo:
* Use yarn workspace feature
* Use typescript reference projects for dependencies
* Shared projects have been moved into "packages", each one is now a
node module (with a dedicated package.json/tsconfig.json)
* server/tools have been moved into apps/ and is now a dedicated app
bundled and published on NPM so users don't have to build peertube
cli tools manually
* server/tests have been moved into packages/ so we don't compile
them every time we want to run the server
* Use isolatedModule option:
* Had to move from const enum to const
(https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums)
* Had to explictely specify "type" imports when used in decorators
* Prefer tsx (that uses esbuild under the hood) instead of ts-node to
load typescript files (tests with mocha or scripts):
* To reduce test complexity as esbuild doesn't support decorator
metadata, we only test server files that do not import server
models
* We still build tests files into js files for a faster CI
* Remove unmaintained peertube CLI import script
* Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'scripts/migrations')
-rw-r--r-- | scripts/migrations/peertube-4.0.ts | 104 | ||||
-rw-r--r-- | scripts/migrations/peertube-4.2.ts | 124 | ||||
-rw-r--r-- | scripts/migrations/peertube-5.0.ts | 71 |
3 files changed, 0 insertions, 299 deletions
diff --git a/scripts/migrations/peertube-4.0.ts b/scripts/migrations/peertube-4.0.ts deleted file mode 100644 index b0891c2e6..000000000 --- a/scripts/migrations/peertube-4.0.ts +++ /dev/null | |||
@@ -1,104 +0,0 @@ | |||
1 | import Bluebird from 'bluebird' | ||
2 | import { move, readFile, writeFile } from 'fs-extra' | ||
3 | import { join } from 'path' | ||
4 | import { federateVideoIfNeeded } from '@server/lib/activitypub/videos' | ||
5 | import { JobQueue } from '@server/lib/job-queue' | ||
6 | import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getHlsResolutionPlaylistFilename } from '@server/lib/paths' | ||
7 | import { VideoPathManager } from '@server/lib/video-path-manager' | ||
8 | import { VideoModel } from '@server/models/video/video' | ||
9 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' | ||
10 | import { initDatabaseModels } from '../../server/initializers/database' | ||
11 | |||
12 | run() | ||
13 | .then(() => process.exit(0)) | ||
14 | .catch(err => { | ||
15 | console.error(err) | ||
16 | process.exit(-1) | ||
17 | }) | ||
18 | |||
19 | async function run () { | ||
20 | console.log('Migrate old HLS paths to new format.') | ||
21 | |||
22 | await initDatabaseModels(true) | ||
23 | |||
24 | JobQueue.Instance.init() | ||
25 | |||
26 | const ids = await VideoModel.listLocalIds() | ||
27 | |||
28 | await Bluebird.map(ids, async id => { | ||
29 | try { | ||
30 | await processVideo(id) | ||
31 | } catch (err) { | ||
32 | console.error('Cannot process video %s.', { err }) | ||
33 | } | ||
34 | }, { concurrency: 5 }) | ||
35 | |||
36 | console.log('Migration finished!') | ||
37 | } | ||
38 | |||
39 | async function processVideo (videoId: number) { | ||
40 | const video = await VideoModel.loadWithFiles(videoId) | ||
41 | |||
42 | const hls = video.getHLSPlaylist() | ||
43 | if (video.isLive || !hls || hls.playlistFilename !== 'master.m3u8' || hls.VideoFiles.length === 0) { | ||
44 | return | ||
45 | } | ||
46 | |||
47 | console.log(`Renaming HLS playlist files of video ${video.name}.`) | ||
48 | |||
49 | const playlist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id) | ||
50 | const hlsDirPath = VideoPathManager.Instance.getFSHLSOutputPath(video) | ||
51 | |||
52 | const masterPlaylistPath = join(hlsDirPath, playlist.playlistFilename) | ||
53 | let masterPlaylistContent = await readFile(masterPlaylistPath, 'utf8') | ||
54 | |||
55 | for (const videoFile of hls.VideoFiles) { | ||
56 | const srcName = `${videoFile.resolution}.m3u8` | ||
57 | const dstName = getHlsResolutionPlaylistFilename(videoFile.filename) | ||
58 | |||
59 | const src = join(hlsDirPath, srcName) | ||
60 | const dst = join(hlsDirPath, dstName) | ||
61 | |||
62 | try { | ||
63 | await move(src, dst) | ||
64 | |||
65 | masterPlaylistContent = masterPlaylistContent.replace(new RegExp('^' + srcName + '$', 'm'), dstName) | ||
66 | } catch (err) { | ||
67 | console.error('Cannot move video file %s to %s.', src, dst, err) | ||
68 | } | ||
69 | } | ||
70 | |||
71 | await writeFile(masterPlaylistPath, masterPlaylistContent) | ||
72 | |||
73 | if (playlist.segmentsSha256Filename === 'segments-sha256.json') { | ||
74 | try { | ||
75 | const newName = generateHlsSha256SegmentsFilename(video.isLive) | ||
76 | |||
77 | const dst = join(hlsDirPath, newName) | ||
78 | await move(join(hlsDirPath, playlist.segmentsSha256Filename), dst) | ||
79 | playlist.segmentsSha256Filename = newName | ||
80 | } catch (err) { | ||
81 | console.error(`Cannot rename ${video.name} segments-sha256.json file to a new name`, err) | ||
82 | } | ||
83 | } | ||
84 | |||
85 | if (playlist.playlistFilename === 'master.m3u8') { | ||
86 | try { | ||
87 | const newName = generateHLSMasterPlaylistFilename(video.isLive) | ||
88 | |||
89 | const dst = join(hlsDirPath, newName) | ||
90 | await move(join(hlsDirPath, playlist.playlistFilename), dst) | ||
91 | playlist.playlistFilename = newName | ||
92 | } catch (err) { | ||
93 | console.error(`Cannot rename ${video.name} master.m3u8 file to a new name`, err) | ||
94 | } | ||
95 | } | ||
96 | |||
97 | // Everything worked, we can save the playlist now | ||
98 | await playlist.save() | ||
99 | |||
100 | const allVideo = await VideoModel.loadFull(video.id) | ||
101 | await federateVideoIfNeeded(allVideo, false) | ||
102 | |||
103 | console.log(`Successfully moved HLS files of ${video.name}.`) | ||
104 | } | ||
diff --git a/scripts/migrations/peertube-4.2.ts b/scripts/migrations/peertube-4.2.ts deleted file mode 100644 index d8929692b..000000000 --- a/scripts/migrations/peertube-4.2.ts +++ /dev/null | |||
@@ -1,124 +0,0 @@ | |||
1 | import { minBy } from 'lodash' | ||
2 | import { join } from 'path' | ||
3 | import { getImageSize, processImage } from '@server/helpers/image-utils' | ||
4 | import { CONFIG } from '@server/initializers/config' | ||
5 | import { ACTOR_IMAGES_SIZE } from '@server/initializers/constants' | ||
6 | import { updateActorImages } from '@server/lib/activitypub/actors' | ||
7 | import { sendUpdateActor } from '@server/lib/activitypub/send' | ||
8 | import { getBiggestActorImage } from '@server/lib/actor-image' | ||
9 | import { JobQueue } from '@server/lib/job-queue' | ||
10 | import { AccountModel } from '@server/models/account/account' | ||
11 | import { ActorModel } from '@server/models/actor/actor' | ||
12 | import { VideoChannelModel } from '@server/models/video/video-channel' | ||
13 | import { MAccountDefault, MActorDefault, MChannelDefault } from '@server/types/models' | ||
14 | import { getLowercaseExtension } from '@shared/core-utils' | ||
15 | import { buildUUID } from '@shared/extra-utils' | ||
16 | import { ActorImageType } from '@shared/models' | ||
17 | import { initDatabaseModels } from '../../server/initializers/database' | ||
18 | |||
19 | run() | ||
20 | .then(() => process.exit(0)) | ||
21 | .catch(err => { | ||
22 | console.error(err) | ||
23 | process.exit(-1) | ||
24 | }) | ||
25 | |||
26 | async function run () { | ||
27 | console.log('Generate avatar miniatures from existing avatars.') | ||
28 | |||
29 | await initDatabaseModels(true) | ||
30 | JobQueue.Instance.init() | ||
31 | |||
32 | const accounts: AccountModel[] = await AccountModel.findAll({ | ||
33 | include: [ | ||
34 | { | ||
35 | model: ActorModel, | ||
36 | required: true, | ||
37 | where: { | ||
38 | serverId: null | ||
39 | } | ||
40 | }, | ||
41 | { | ||
42 | model: VideoChannelModel, | ||
43 | include: [ | ||
44 | { | ||
45 | model: AccountModel | ||
46 | } | ||
47 | ] | ||
48 | } | ||
49 | ] | ||
50 | }) | ||
51 | |||
52 | for (const account of accounts) { | ||
53 | try { | ||
54 | await fillAvatarSizeIfNeeded(account) | ||
55 | await generateSmallerAvatarIfNeeded(account) | ||
56 | } catch (err) { | ||
57 | console.error(`Cannot process account avatar ${account.name}`, err) | ||
58 | } | ||
59 | |||
60 | for (const videoChannel of account.VideoChannels) { | ||
61 | try { | ||
62 | await fillAvatarSizeIfNeeded(videoChannel) | ||
63 | await generateSmallerAvatarIfNeeded(videoChannel) | ||
64 | } catch (err) { | ||
65 | console.error(`Cannot process channel avatar ${videoChannel.name}`, err) | ||
66 | } | ||
67 | } | ||
68 | } | ||
69 | |||
70 | console.log('Generation finished!') | ||
71 | } | ||
72 | |||
73 | async function fillAvatarSizeIfNeeded (accountOrChannel: MAccountDefault | MChannelDefault) { | ||
74 | const avatars = accountOrChannel.Actor.Avatars | ||
75 | |||
76 | for (const avatar of avatars) { | ||
77 | if (avatar.width && avatar.height) continue | ||
78 | |||
79 | console.log('Filling size of avatars of %s.', accountOrChannel.name) | ||
80 | |||
81 | const { width, height } = await getImageSize(join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, avatar.filename)) | ||
82 | avatar.width = width | ||
83 | avatar.height = height | ||
84 | |||
85 | await avatar.save() | ||
86 | } | ||
87 | } | ||
88 | |||
89 | async function generateSmallerAvatarIfNeeded (accountOrChannel: MAccountDefault | MChannelDefault) { | ||
90 | const avatars = accountOrChannel.Actor.Avatars | ||
91 | if (avatars.length !== 1) { | ||
92 | return | ||
93 | } | ||
94 | |||
95 | console.log(`Processing ${accountOrChannel.name}.`) | ||
96 | |||
97 | await generateSmallerAvatar(accountOrChannel.Actor) | ||
98 | accountOrChannel.Actor = Object.assign(accountOrChannel.Actor, { Server: null }) | ||
99 | |||
100 | return sendUpdateActor(accountOrChannel, undefined) | ||
101 | } | ||
102 | |||
103 | async function generateSmallerAvatar (actor: MActorDefault) { | ||
104 | const bigAvatar = getBiggestActorImage(actor.Avatars) | ||
105 | |||
106 | const imageSize = minBy(ACTOR_IMAGES_SIZE[ActorImageType.AVATAR], 'width') | ||
107 | const sourceFilename = bigAvatar.filename | ||
108 | |||
109 | const newImageName = buildUUID() + getLowercaseExtension(sourceFilename) | ||
110 | const source = join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, sourceFilename) | ||
111 | const destination = join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, newImageName) | ||
112 | |||
113 | await processImage({ path: source, destination, newSize: imageSize, keepOriginal: true }) | ||
114 | |||
115 | const actorImageInfo = { | ||
116 | name: newImageName, | ||
117 | fileUrl: null, | ||
118 | height: imageSize.height, | ||
119 | width: imageSize.width, | ||
120 | onDisk: true | ||
121 | } | ||
122 | |||
123 | await updateActorImages(actor, ActorImageType.AVATAR, [ actorImageInfo ], undefined) | ||
124 | } | ||
diff --git a/scripts/migrations/peertube-5.0.ts b/scripts/migrations/peertube-5.0.ts deleted file mode 100644 index a0f51a64c..000000000 --- a/scripts/migrations/peertube-5.0.ts +++ /dev/null | |||
@@ -1,71 +0,0 @@ | |||
1 | import { ensureDir } from 'fs-extra' | ||
2 | import { Op } from 'sequelize' | ||
3 | import { updateTorrentMetadata } from '@server/helpers/webtorrent' | ||
4 | import { DIRECTORIES } from '@server/initializers/constants' | ||
5 | import { moveFilesIfPrivacyChanged } from '@server/lib/video-privacy' | ||
6 | import { VideoModel } from '@server/models/video/video' | ||
7 | import { MVideoFullLight } from '@server/types/models' | ||
8 | import { VideoPrivacy } from '@shared/models' | ||
9 | import { initDatabaseModels } from '../../server/initializers/database' | ||
10 | |||
11 | run() | ||
12 | .then(() => process.exit(0)) | ||
13 | .catch(err => { | ||
14 | console.error(err) | ||
15 | process.exit(-1) | ||
16 | }) | ||
17 | |||
18 | async function run () { | ||
19 | console.log('Moving private video files in dedicated folders.') | ||
20 | |||
21 | await ensureDir(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE) | ||
22 | await ensureDir(DIRECTORIES.VIDEOS.PRIVATE) | ||
23 | |||
24 | await initDatabaseModels(true) | ||
25 | |||
26 | const videos = await VideoModel.unscoped().findAll({ | ||
27 | attributes: [ 'uuid' ], | ||
28 | where: { | ||
29 | privacy: { | ||
30 | [Op.in]: [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL ] | ||
31 | } | ||
32 | } | ||
33 | }) | ||
34 | |||
35 | for (const { uuid } of videos) { | ||
36 | try { | ||
37 | console.log('Moving files of video %s.', uuid) | ||
38 | |||
39 | const video = await VideoModel.loadFull(uuid) | ||
40 | |||
41 | try { | ||
42 | await moveFilesIfPrivacyChanged(video, VideoPrivacy.PUBLIC) | ||
43 | } catch (err) { | ||
44 | console.error('Cannot move files of video %s.', uuid, err) | ||
45 | } | ||
46 | |||
47 | try { | ||
48 | await updateTorrents(video) | ||
49 | } catch (err) { | ||
50 | console.error('Cannot regenerate torrents of video %s.', uuid, err) | ||
51 | } | ||
52 | } catch (err) { | ||
53 | console.error('Cannot process video %s.', uuid, err) | ||
54 | } | ||
55 | } | ||
56 | } | ||
57 | |||
58 | async function updateTorrents (video: MVideoFullLight) { | ||
59 | for (const file of video.VideoFiles) { | ||
60 | await updateTorrentMetadata(video, file) | ||
61 | |||
62 | await file.save() | ||
63 | } | ||
64 | |||
65 | const playlist = video.getHLSPlaylist() | ||
66 | for (const file of (playlist?.VideoFiles || [])) { | ||
67 | await updateTorrentMetadata(playlist, file) | ||
68 | |||
69 | await file.save() | ||
70 | } | ||
71 | } | ||