]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
f0f5567b | 3 | const config = require('config') |
1a42c9e2 C |
4 | const each = require('async/each') |
5 | const eachLimit = require('async/eachLimit') | |
6 | const eachSeries = require('async/eachSeries') | |
f0f5567b | 7 | const fs = require('fs') |
aaf61f38 | 8 | const mongoose = require('mongoose') |
f0f5567b | 9 | const request = require('request') |
1a42c9e2 | 10 | const waterfall = require('async/waterfall') |
f0f5567b C |
11 | |
12 | const constants = require('../initializers/constants') | |
13 | const logger = require('../helpers/logger') | |
5f698b82 | 14 | const peertubeCrypto = require('../helpers/peertube-crypto') |
f0f5567b | 15 | const requests = require('../helpers/requests') |
f0f5567b C |
16 | |
17 | const http = config.get('webserver.https') ? 'https' : 'http' | |
18 | const host = config.get('webserver.host') | |
19 | const port = config.get('webserver.port') | |
a3ee6fa2 | 20 | const Pod = mongoose.model('Pod') |
00057e85 | 21 | const Request = mongoose.model('Request') |
aaf61f38 | 22 | const Video = mongoose.model('Video') |
f0f5567b | 23 | |
a3ee6fa2 | 24 | const friends = { |
9f10b292 C |
25 | addVideoToFriends: addVideoToFriends, |
26 | hasFriends: hasFriends, | |
cbe2f7c3 | 27 | getMyCertificate: getMyCertificate, |
9f10b292 C |
28 | makeFriends: makeFriends, |
29 | quitFriends: quitFriends, | |
528a9efa C |
30 | removeVideoToFriends: removeVideoToFriends, |
31 | sendOwnedVideosToPod: sendOwnedVideosToPod | |
9f10b292 C |
32 | } |
33 | ||
34 | function addVideoToFriends (video) { | |
00057e85 | 35 | createRequest('add', video) |
9f10b292 C |
36 | } |
37 | ||
38 | function hasFriends (callback) { | |
a3ee6fa2 | 39 | Pod.countAll(function (err, count) { |
9f10b292 C |
40 | if (err) return callback(err) |
41 | ||
bc503c2a C |
42 | const hasFriends = (count !== 0) |
43 | callback(null, hasFriends) | |
9f10b292 C |
44 | }) |
45 | } | |
46 | ||
cbe2f7c3 C |
47 | function getMyCertificate (callback) { |
48 | fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', callback) | |
49 | } | |
50 | ||
9f10b292 | 51 | function makeFriends (callback) { |
bc503c2a | 52 | const podsScore = {} |
9f10b292 C |
53 | |
54 | logger.info('Make friends!') | |
cbe2f7c3 | 55 | getMyCertificate(function (err, cert) { |
9f10b292 C |
56 | if (err) { |
57 | logger.error('Cannot read public cert.') | |
58 | return callback(err) | |
59 | } | |
c173e565 | 60 | |
f0f5567b | 61 | const urls = config.get('network.friends') |
c173e565 | 62 | |
1a42c9e2 | 63 | eachSeries(urls, function (url, callbackEach) { |
bc503c2a | 64 | computeForeignPodsList(url, podsScore, callbackEach) |
89d1d8ba | 65 | }, function (err) { |
c173e565 C |
66 | if (err) return callback(err) |
67 | ||
bc503c2a C |
68 | logger.debug('Pods scores computed.', { podsScore: podsScore }) |
69 | const podsList = computeWinningPods(urls, podsScore) | |
70 | logger.debug('Pods that we keep.', { podsToKeep: podsList }) | |
9f10b292 | 71 | |
bc503c2a | 72 | makeRequestsToWinningPods(cert, podsList, callback) |
c173e565 | 73 | }) |
9f10b292 | 74 | }) |
9f10b292 C |
75 | } |
76 | ||
77 | function quitFriends (callback) { | |
78 | // Stop pool requests | |
00057e85 | 79 | Request.deactivate() |
9f10b292 | 80 | // Flush pool requests |
00057e85 | 81 | Request.flush() |
9f10b292 | 82 | |
1a42c9e2 | 83 | waterfall([ |
e7ea2817 | 84 | function getPodsList (callbackAsync) { |
a3ee6fa2 | 85 | return Pod.list(callbackAsync) |
e7ea2817 C |
86 | }, |
87 | ||
88 | function announceIQuitMyFriends (pods, callbackAsync) { | |
528a9efa | 89 | const requestParams = { |
e7ea2817 C |
90 | method: 'POST', |
91 | path: '/api/' + constants.API_VERSION + '/pods/remove', | |
528a9efa | 92 | sign: true |
c173e565 C |
93 | } |
94 | ||
e7ea2817 | 95 | // Announce we quit them |
528a9efa C |
96 | // We don't care if the request fails |
97 | // The other pod will exclude us automatically after a while | |
1a42c9e2 | 98 | eachLimit(pods, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) { |
528a9efa C |
99 | requestParams.toPod = pod |
100 | requests.makeSecureRequest(requestParams, callbackEach) | |
101 | }, function (err) { | |
102 | if (err) { | |
103 | logger.error('Some errors while quitting friends.', { err: err }) | |
104 | // Don't stop the process | |
105 | } | |
106 | ||
107 | return callbackAsync() | |
e7ea2817 C |
108 | }) |
109 | }, | |
c173e565 | 110 | |
e7ea2817 | 111 | function removePodsFromDB (callbackAsync) { |
a3ee6fa2 | 112 | Pod.removeAll(function (err) { |
e7ea2817 C |
113 | return callbackAsync(err) |
114 | }) | |
115 | }, | |
c173e565 | 116 | |
e7ea2817 C |
117 | function listRemoteVideos (callbackAsync) { |
118 | logger.info('Broke friends, so sad :(') | |
c173e565 | 119 | |
aaf61f38 | 120 | Video.listRemotes(callbackAsync) |
e7ea2817 | 121 | }, |
c173e565 | 122 | |
e7ea2817 | 123 | function removeTheRemoteVideos (videosList, callbackAsync) { |
1a42c9e2 | 124 | each(videosList, function (video, callbackEach) { |
aaf61f38 C |
125 | video.remove(callbackEach) |
126 | }, callbackAsync) | |
e7ea2817 C |
127 | } |
128 | ], function (err) { | |
129 | // Don't forget to re activate the scheduler, even if there was an error | |
00057e85 | 130 | Request.activate() |
e7ea2817 C |
131 | |
132 | if (err) return callback(err) | |
133 | ||
134 | logger.info('Removed all remote videos.') | |
135 | return callback(null) | |
9f10b292 C |
136 | }) |
137 | } | |
c173e565 | 138 | |
00057e85 C |
139 | function removeVideoToFriends (videoParams) { |
140 | createRequest('remove', videoParams) | |
528a9efa C |
141 | } |
142 | ||
143 | function sendOwnedVideosToPod (podId) { | |
aaf61f38 | 144 | Video.listOwned(function (err, videosList) { |
528a9efa C |
145 | if (err) { |
146 | logger.error('Cannot get the list of videos we own.') | |
147 | return | |
148 | } | |
149 | ||
150 | videosList.forEach(function (video) { | |
aaf61f38 | 151 | video.toRemoteJSON(function (err, remoteVideo) { |
528a9efa C |
152 | if (err) { |
153 | logger.error('Cannot convert video to remote.', { error: err }) | |
154 | // Don't break the process | |
155 | return | |
156 | } | |
157 | ||
00057e85 | 158 | createRequest('add', remoteVideo, [ podId ]) |
528a9efa C |
159 | }) |
160 | }) | |
161 | }) | |
9f10b292 | 162 | } |
c173e565 | 163 | |
9f10b292 | 164 | // --------------------------------------------------------------------------- |
c173e565 | 165 | |
a3ee6fa2 | 166 | module.exports = friends |
c173e565 | 167 | |
9f10b292 | 168 | // --------------------------------------------------------------------------- |
c173e565 | 169 | |
bc503c2a | 170 | function computeForeignPodsList (url, podsScore, callback) { |
bc503c2a | 171 | getForeignPodsList(url, function (err, foreignPodsList) { |
89d1d8ba | 172 | if (err) return callback(err) |
528a9efa C |
173 | |
174 | if (!foreignPodsList) foreignPodsList = [] | |
175 | ||
176 | // Let's give 1 point to the pod we ask the friends list | |
177 | foreignPodsList.push({ url: url }) | |
89d1d8ba | 178 | |
bc503c2a | 179 | foreignPodsList.forEach(function (foreignPod) { |
528a9efa | 180 | const foreignPodUrl = foreignPod.url |
89d1d8ba | 181 | |
528a9efa C |
182 | if (podsScore[foreignPodUrl]) podsScore[foreignPodUrl]++ |
183 | else podsScore[foreignPodUrl] = 1 | |
89d1d8ba | 184 | }) |
cbe2f7c3 C |
185 | |
186 | callback() | |
89d1d8ba C |
187 | }) |
188 | } | |
189 | ||
bc503c2a | 190 | function computeWinningPods (urls, podsScore) { |
89d1d8ba C |
191 | // Build the list of pods to add |
192 | // Only add a pod if it exists in more than a half base pods | |
bc503c2a C |
193 | const podsList = [] |
194 | const baseScore = urls.length / 2 | |
d6ea0175 | 195 | Object.keys(podsScore).forEach(function (pod) { |
bc503c2a | 196 | if (podsScore[pod] > baseScore) podsList.push({ url: pod }) |
89d1d8ba | 197 | }) |
e7ea2817 | 198 | |
bc503c2a | 199 | return podsList |
89d1d8ba C |
200 | } |
201 | ||
9f10b292 | 202 | function getForeignPodsList (url, callback) { |
f0f5567b | 203 | const path = '/api/' + constants.API_VERSION + '/pods' |
c173e565 | 204 | |
9f10b292 C |
205 | request.get(url + path, function (err, response, body) { |
206 | if (err) return callback(err) | |
8425cb89 | 207 | |
9f10b292 C |
208 | callback(null, JSON.parse(body)) |
209 | }) | |
210 | } | |
89d1d8ba | 211 | |
bc503c2a | 212 | function makeRequestsToWinningPods (cert, podsList, callback) { |
89d1d8ba | 213 | // Stop pool requests |
00057e85 | 214 | Request.deactivate() |
89d1d8ba | 215 | // Flush pool requests |
00057e85 | 216 | Request.forceSend() |
89d1d8ba | 217 | |
1a42c9e2 | 218 | eachLimit(podsList, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) { |
528a9efa C |
219 | const params = { |
220 | url: pod.url + '/api/' + constants.API_VERSION + '/pods/', | |
221 | method: 'POST', | |
222 | json: { | |
223 | url: http + '://' + host + ':' + port, | |
224 | publicKey: cert | |
225 | } | |
89d1d8ba C |
226 | } |
227 | ||
528a9efa C |
228 | requests.makeRetryRequest(params, function (err, res, body) { |
229 | if (err) { | |
230 | logger.error('Error with adding %s pod.', pod.url, { error: err }) | |
231 | // Don't break the process | |
232 | return callbackEach() | |
233 | } | |
89d1d8ba | 234 | |
528a9efa | 235 | if (res.statusCode === 200) { |
a3ee6fa2 C |
236 | const podObj = new Pod({ url: pod.url, publicKey: body.cert }) |
237 | podObj.save(function (err, podCreated) { | |
238 | if (err) { | |
239 | logger.error('Cannot add friend %s pod.', pod.url, { error: err }) | |
240 | return callbackEach() | |
241 | } | |
89d1d8ba | 242 | |
528a9efa C |
243 | // Add our videos to the request scheduler |
244 | sendOwnedVideosToPod(podCreated._id) | |
89d1d8ba | 245 | |
528a9efa C |
246 | return callbackEach() |
247 | }) | |
248 | } else { | |
249 | logger.error('Status not 200 for %s pod.', pod.url) | |
250 | return callbackEach() | |
89d1d8ba | 251 | } |
528a9efa C |
252 | }) |
253 | }, function endRequests () { | |
254 | // Final callback, we've ended all the requests | |
255 | // Now we made new friends, we can re activate the pool of requests | |
00057e85 | 256 | Request.activate() |
528a9efa C |
257 | |
258 | logger.debug('makeRequestsToWinningPods finished.') | |
259 | return callback() | |
89d1d8ba C |
260 | }) |
261 | } | |
00057e85 C |
262 | |
263 | function createRequest (type, data, to) { | |
264 | const req = new Request({ | |
265 | request: { | |
266 | type: type, | |
267 | data: data | |
268 | } | |
269 | }) | |
270 | ||
271 | if (to) { | |
272 | req.to = to | |
273 | } | |
274 | ||
275 | req.save(function (err) { | |
276 | if (err) logger.error('Cannot save the request.', { error: err }) | |
277 | }) | |
278 | } |