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