diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-11-22 22:12:13 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-11-22 22:12:13 +0100 |
commit | 93d4a3b551f8828090b8fdee9fea1f935669dcc7 (patch) | |
tree | 01b6fb754c033e3525c1cce08c2ddd81b9352117 /scripts/update-host.js | |
parent | 04968ab47717f0c90a3c05dc71fb24fc78599973 (diff) | |
download | PeerTube-93d4a3b551f8828090b8fdee9fea1f935669dcc7.tar.gz PeerTube-93d4a3b551f8828090b8fdee9fea1f935669dcc7.tar.zst PeerTube-93d4a3b551f8828090b8fdee9fea1f935669dcc7.zip |
Add script when the host/port of a pod change
Diffstat (limited to 'scripts/update-host.js')
-rwxr-xr-x | scripts/update-host.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/scripts/update-host.js b/scripts/update-host.js new file mode 100755 index 000000000..8a17f2402 --- /dev/null +++ b/scripts/update-host.js | |||
@@ -0,0 +1,51 @@ | |||
1 | #!/usr/bin/env node | ||
2 | |||
3 | 'use strict' | ||
4 | |||
5 | // TODO: document this script | ||
6 | |||
7 | const fs = require('fs') | ||
8 | const mongoose = require('mongoose') | ||
9 | const parseTorrent = require('parse-torrent') | ||
10 | |||
11 | const constants = require('../server/initializers/constants') | ||
12 | const database = require('../server/initializers/database') | ||
13 | |||
14 | database.connect() | ||
15 | |||
16 | const friends = require('../server/lib/friends') | ||
17 | const Video = mongoose.model('Video') | ||
18 | |||
19 | friends.hasFriends(function (err, hasFriends) { | ||
20 | if (err) throw err | ||
21 | |||
22 | if (hasFriends === true) { | ||
23 | console.log('Cannot update host because you have friends!') | ||
24 | process.exit(-1) | ||
25 | } | ||
26 | |||
27 | console.log('Updating videos host in database.') | ||
28 | Video.update({ }, { podHost: constants.CONFIG.WEBSERVER.HOST }, { multi: true }, function (err) { | ||
29 | if (err) throw err | ||
30 | |||
31 | console.log('Updating torrent files.') | ||
32 | Video.find().lean().exec(function (err, videos) { | ||
33 | if (err) throw err | ||
34 | |||
35 | videos.forEach(function (video) { | ||
36 | const torrentName = video._id + '.torrent' | ||
37 | const torrentPath = constants.CONFIG.STORAGE.TORRENTS_DIR + torrentName | ||
38 | const filename = video._id + video.extname | ||
39 | |||
40 | const parsed = parseTorrent(fs.readFileSync(torrentPath)) | ||
41 | parsed.announce = [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOST + '/tracker/socket' ] | ||
42 | parsed.urlList = [ constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + filename ] | ||
43 | |||
44 | const buf = parseTorrent.toTorrentFile(parsed) | ||
45 | fs.writeFileSync(torrentPath, buf) | ||
46 | }) | ||
47 | |||
48 | process.exit(0) | ||
49 | }) | ||
50 | }) | ||
51 | }) | ||