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