Add script when the host/port of a pod change
authorChocobozzz <florian.bigard@gmail.com>
Tue, 22 Nov 2016 21:12:13 +0000 (22:12 +0100)
committerChocobozzz <florian.bigard@gmail.com>
Tue, 22 Nov 2016 21:12:13 +0000 (22:12 +0100)
package.json
scripts/update-host.js [new file with mode: 0755]

index 93c748cd32b969bcb0f9bf6e9a5e07d366ea6fca..300af4867f322dc27dd29a0baa1b8246b6e554e9 100644 (file)
@@ -32,6 +32,7 @@
     "start": "node server",
     "check": "scripty",
     "upgrade": "scripty",
+    "update-host": "scripty",
     "test": "scripty",
     "help": "scripty",
     "postinstall": "cd client && npm install"
diff --git a/scripts/update-host.js b/scripts/update-host.js
new file mode 100755 (executable)
index 0000000..8a17f24
--- /dev/null
@@ -0,0 +1,51 @@
+#!/usr/bin/env node
+
+'use strict'
+
+// TODO: document this script
+
+const fs = require('fs')
+const mongoose = require('mongoose')
+const parseTorrent = require('parse-torrent')
+
+const constants = require('../server/initializers/constants')
+const database = require('../server/initializers/database')
+
+database.connect()
+
+const friends = require('../server/lib/friends')
+const Video = mongoose.model('Video')
+
+friends.hasFriends(function (err, hasFriends) {
+  if (err) throw err
+
+  if (hasFriends === true) {
+    console.log('Cannot update host because you have friends!')
+    process.exit(-1)
+  }
+
+  console.log('Updating videos host in database.')
+  Video.update({ }, { podHost: constants.CONFIG.WEBSERVER.HOST }, { multi: true }, function (err) {
+    if (err) throw err
+
+    console.log('Updating torrent files.')
+    Video.find().lean().exec(function (err, videos) {
+      if (err) throw err
+
+      videos.forEach(function (video) {
+        const torrentName = video._id + '.torrent'
+        const torrentPath = constants.CONFIG.STORAGE.TORRENTS_DIR + torrentName
+        const filename = video._id + video.extname
+
+        const parsed = parseTorrent(fs.readFileSync(torrentPath))
+        parsed.announce = [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOST + '/tracker/socket' ]
+        parsed.urlList = [ constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + filename ]
+
+        const buf = parseTorrent.toTorrentFile(parsed)
+        fs.writeFileSync(torrentPath, buf)
+      })
+
+      process.exit(0)
+    })
+  })
+})