aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-01-12 15:20:03 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-01-12 15:20:03 +0100
commit99fe265a5fc077cb66c322e7f3d191ff7110aea0 (patch)
treec9e04ccfcc5496d2300d7c26db5833e494b4cdad /scripts
parentfcc5f77b95d330bfcb439c172b7fcc58f3162e4d (diff)
parent91cc839af88730ba55f84997c56b85ea100070a7 (diff)
downloadPeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.tar.gz
PeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.tar.zst
PeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.zip
Merge branch 'postgresql'
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/clean/server/test.sh3
-rw-r--r--scripts/danger/clean/cleaner.js31
-rwxr-xr-xscripts/danger/clean/dev.sh3
-rwxr-xr-xscripts/danger/clean/prod.sh3
-rwxr-xr-xscripts/mongo-to-postgre.js244
-rwxr-xr-xscripts/play.sh4
-rwxr-xr-xscripts/update-host.js25
7 files changed, 278 insertions, 35 deletions
diff --git a/scripts/clean/server/test.sh b/scripts/clean/server/test.sh
index 927671dd4..35d3ad50f 100755
--- a/scripts/clean/server/test.sh
+++ b/scripts/clean/server/test.sh
@@ -1,6 +1,7 @@
1#!/usr/bin/env sh 1#!/usr/bin/env sh
2 2
3for i in $(seq 1 6); do 3for i in $(seq 1 6); do
4 printf "use peertube-test%s;\ndb.dropDatabase();" "$i" | mongo 4 dropdb "peertube_test$i"
5 rm -rf "./test$i" 5 rm -rf "./test$i"
6 createdb "peertube_test$i"
6done 7done
diff --git a/scripts/danger/clean/cleaner.js b/scripts/danger/clean/cleaner.js
index 10e91e700..1a1e3dee7 100644
--- a/scripts/danger/clean/cleaner.js
+++ b/scripts/danger/clean/cleaner.js
@@ -1,24 +1,25 @@
1const eachSeries = require('async/eachSeries')
1const rimraf = require('rimraf') 2const rimraf = require('rimraf')
2const mongoose = require('mongoose')
3mongoose.Promise = global.Promise
4 3
5const constants = require('../../../server/initializers/constants') 4const constants = require('../../../server/initializers/constants')
5const db = require('../../../server/initializers/database')
6 6
7const mongodbUrl = 'mongodb://' + constants.CONFIG.DATABASE.HOSTNAME + ':' + constants.CONFIG.DATABASE.PORT + '/' + constants.CONFIG.DATABASE.DBNAME 7db.init(true, function () {
8mongoose.connect(mongodbUrl, function () { 8 db.sequelize.drop().asCallback(function (err) {
9 console.info('Deleting MongoDB %s database.', constants.CONFIG.DATABASE.DBNAME) 9 if (err) throw err
10 mongoose.connection.dropDatabase(function () {
11 mongoose.connection.close()
12 })
13})
14 10
15const STORAGE = constants.CONFIG.STORAGE 11 console.info('Tables of %s deleted.', db.sequelize.config.database)
16Object.keys(STORAGE).forEach(function (storage) {
17 const storageDir = STORAGE[storage]
18 12
19 rimraf(storageDir, function (err) { 13 const STORAGE = constants.CONFIG.STORAGE
20 if (err) throw err 14 eachSeries(Object.keys(STORAGE), function (storage, callbackEach) {
15 const storageDir = STORAGE[storage]
21 16
22 console.info('Deleting %s.', storageDir) 17 rimraf(storageDir, function (err) {
18 console.info('%s deleted.', storageDir)
19 return callbackEach(err)
20 })
21 }, function () {
22 process.exit(0)
23 })
23 }) 24 })
24}) 25})
diff --git a/scripts/danger/clean/dev.sh b/scripts/danger/clean/dev.sh
index 5f09565cf..f4a5d1e8a 100755
--- a/scripts/danger/clean/dev.sh
+++ b/scripts/danger/clean/dev.sh
@@ -1,6 +1,7 @@
1#!/bin/bash 1#!/bin/bash
2 2
3read -p "This will remove all directories and Mongo database. Are you sure? " -n 1 -r 3read -p "This will remove all directories and SQL tables. Are you sure? (y/*) " -n 1 -r
4echo
4 5
5if [[ "$REPLY" =~ ^[Yy]$ ]]; then 6if [[ "$REPLY" =~ ^[Yy]$ ]]; then
6 NODE_ENV=test node "./scripts/danger/clean/cleaner" 7 NODE_ENV=test node "./scripts/danger/clean/cleaner"
diff --git a/scripts/danger/clean/prod.sh b/scripts/danger/clean/prod.sh
index e6c92671d..7ee13ba83 100755
--- a/scripts/danger/clean/prod.sh
+++ b/scripts/danger/clean/prod.sh
@@ -1,6 +1,7 @@
1#!/bin/bash 1#!/bin/bash
2 2
3read -p "This will remove all directories and Mongo database. Are you sure? " -n 1 -r 3read -p "This will remove all directories and SQL tables. Are you sure? (y/*) " -n 1 -r
4echo
4 5
5if [[ "$REPLY" =~ ^[Yy]$ ]]; then 6if [[ "$REPLY" =~ ^[Yy]$ ]]; then
6 NODE_ENV=production node "./scripts/danger/clean/cleaner" 7 NODE_ENV=production node "./scripts/danger/clean/cleaner"
diff --git a/scripts/mongo-to-postgre.js b/scripts/mongo-to-postgre.js
new file mode 100755
index 000000000..4a581b46a
--- /dev/null
+++ b/scripts/mongo-to-postgre.js
@@ -0,0 +1,244 @@
1#!/usr/bin/env node
2
3'use strict'
4
5// TODO: document this script
6
7const program = require('commander')
8const eachSeries = require('async/eachSeries')
9const series = require('async/series')
10const waterfall = require('async/waterfall')
11const fs = require('fs')
12const path = require('path')
13const MongoClient = require('mongodb').MongoClient
14
15const constants = require('../server/initializers/constants')
16
17program
18 .option('-mh, --mongo-host [host]', 'MongoDB host', 'localhost')
19 .option('-mp, --mongo-port [weight]', 'MongoDB port', '27017')
20 .option('-md, --mongo-database [dbname]', 'MongoDB database')
21 .parse(process.argv)
22
23if (!program.mongoDatabase) {
24 console.error('The mongodb database is mandatory.')
25 process.exit(-1)
26}
27
28const mongoUrl = 'mongodb://' + program.mongoHost + ':' + program.mongoPort + '/' + program.mongoDatabase
29const dbSequelize = require('../server/initializers/database')
30
31console.log('Connecting to ' + mongoUrl)
32MongoClient.connect(mongoUrl, function (err, dbMongo) {
33 if (err) throw err
34
35 console.log('Connected to ' + mongoUrl)
36
37 const videoMongo = dbMongo.collection('videos')
38 const userMongo = dbMongo.collection('users')
39 const podMongo = dbMongo.collection('pods')
40
41 podMongo.count(function (err, podsLength) {
42 if (err) throw err
43
44 if (podsLength > 0) {
45 console.error('You need to quit friends first.')
46 process.exit(-1)
47 }
48
49 console.log('Connecting to ' + dbSequelize.sequelize.config.database)
50 dbSequelize.init(true, function (err) {
51 if (err) throw err
52
53 console.log('Connected to SQL database %s.', dbSequelize.sequelize.config.database)
54
55 series([
56 function (next) {
57 dbSequelize.sequelize.sync({ force: true }).asCallback(next)
58 },
59
60 function (next) {
61 migrateVideos(videoMongo, dbSequelize, next)
62 },
63
64 function (next) {
65 migrateUsers(userMongo, dbSequelize, next)
66 }
67 ], function (err) {
68 if (err) console.error(err)
69
70 process.exit(0)
71 })
72 })
73 })
74})
75
76// ---------------------------------------------------------------------------
77
78function migrateUsers (userMongo, dbSequelize, callback) {
79 userMongo.find().toArray(function (err, mongoUsers) {
80 if (err) return callback(err)
81
82 eachSeries(mongoUsers, function (mongoUser, callbackEach) {
83 console.log('Migrating user %s', mongoUser.username)
84
85 const userData = {
86 username: mongoUser.username,
87 password: mongoUser.password,
88 role: mongoUser.role
89 }
90 const options = {
91 hooks: false
92 }
93
94 dbSequelize.User.create(userData, options).asCallback(callbackEach)
95 }, callback)
96 })
97}
98
99function migrateVideos (videoMongo, dbSequelize, finalCallback) {
100 videoMongo.find().toArray(function (err, mongoVideos) {
101 if (err) return finalCallback(err)
102
103 eachSeries(mongoVideos, function (mongoVideo, callbackEach) {
104 console.log('Migrating video %s.', mongoVideo.name)
105
106 waterfall([
107
108 function startTransaction (callback) {
109 dbSequelize.sequelize.transaction().asCallback(function (err, t) {
110 return callback(err, t)
111 })
112 },
113
114 function findOrCreatePod (t, callback) {
115 if (mongoVideo.remoteId === null) return callback(null, t, null)
116
117 const query = {
118 where: {
119 host: mongoVideo.podHost
120 },
121 defaults: {
122 host: mongoVideo.podHost
123 },
124 transaction: t
125 }
126
127 dbSequelize.Pod.findOrCreate(query).asCallback(function (err, result) {
128 // [ instance, wasCreated ]
129 return callback(err, t, result[0])
130 })
131 },
132
133 function findOrCreateAuthor (t, pod, callback) {
134 const podId = pod ? pod.id : null
135 const username = mongoVideo.author
136
137 const query = {
138 where: {
139 podId,
140 name: username
141 },
142 defaults: {
143 podId,
144 name: username
145 },
146 transaction: t
147 }
148
149 dbSequelize.Author.findOrCreate(query).asCallback(function (err, result) {
150 // [ instance, wasCreated ]
151 return callback(err, t, result[0])
152 })
153 },
154
155 function findOrCreateTags (t, author, callback) {
156 const tags = mongoVideo.tags
157 const tagInstances = []
158
159 eachSeries(tags, function (tag, callbackEach) {
160 const query = {
161 where: {
162 name: tag
163 },
164 defaults: {
165 name: tag
166 },
167 transaction: t
168 }
169
170 dbSequelize.Tag.findOrCreate(query).asCallback(function (err, res) {
171 if (err) return callbackEach(err)
172
173 // res = [ tag, isCreated ]
174 const tag = res[0]
175 tagInstances.push(tag)
176 return callbackEach()
177 })
178 }, function (err) {
179 return callback(err, t, author, tagInstances)
180 })
181 },
182
183 function createVideoObject (t, author, tagInstances, callback) {
184 const videoData = {
185 name: mongoVideo.name,
186 remoteId: mongoVideo.remoteId,
187 extname: mongoVideo.extname,
188 infoHash: mongoVideo.magnet.infoHash,
189 description: mongoVideo.description,
190 authorId: author.id,
191 duration: mongoVideo.duration,
192 createdAt: mongoVideo.createdDate
193 }
194
195 const video = dbSequelize.Video.build(videoData)
196
197 return callback(null, t, tagInstances, video)
198 },
199
200 function moveVideoFile (t, tagInstances, video, callback) {
201 const basePath = constants.CONFIG.STORAGE.VIDEOS_DIR
202 const src = path.join(basePath, mongoVideo._id.toString()) + video.extname
203 const dst = path.join(basePath, video.id) + video.extname
204 fs.rename(src, dst, function (err) {
205 if (err) return callback(err)
206
207 return callback(null, t, tagInstances, video)
208 })
209 },
210
211 function insertVideoIntoDB (t, tagInstances, video, callback) {
212 const options = {
213 transaction: t
214 }
215
216 video.save(options).asCallback(function (err, videoCreated) {
217 return callback(err, t, tagInstances, videoCreated)
218 })
219 },
220
221 function associateTagsToVideo (t, tagInstances, video, callback) {
222 const options = { transaction: t }
223
224 video.setTags(tagInstances, options).asCallback(function (err) {
225 return callback(err, t)
226 })
227 }
228
229 ], function (err, t) {
230 if (err) {
231 // Abort transaction?
232 if (t) t.rollback()
233
234 return callbackEach(err)
235 }
236
237 // Commit transaction
238 t.commit()
239
240 return callbackEach()
241 })
242 }, finalCallback)
243 })
244}
diff --git a/scripts/play.sh b/scripts/play.sh
index 33dc1a545..ab812025c 100755
--- a/scripts/play.sh
+++ b/scripts/play.sh
@@ -5,7 +5,9 @@ if [ ! -f server.js ]; then
5 exit -1 5 exit -1
6fi 6fi
7 7
8for i in 1 2 3; do 8max=${1:-3}
9
10for i in $(seq 1 $max); do
9 NODE_ENV=test NODE_APP_INSTANCE=$i node server.js & 11 NODE_ENV=test NODE_APP_INSTANCE=$i node server.js &
10 sleep 1 12 sleep 1
11done 13done
diff --git a/scripts/update-host.js b/scripts/update-host.js
index 8a17f2402..91051e68b 100755
--- a/scripts/update-host.js
+++ b/scripts/update-host.js
@@ -5,31 +5,24 @@
5// TODO: document this script 5// TODO: document this script
6 6
7const fs = require('fs') 7const fs = require('fs')
8const mongoose = require('mongoose')
9const parseTorrent = require('parse-torrent') 8const parseTorrent = require('parse-torrent')
10 9
11const constants = require('../server/initializers/constants') 10const constants = require('../server/initializers/constants')
12const database = require('../server/initializers/database') 11const db = require('../server/initializers/database')
13
14database.connect()
15 12
16const friends = require('../server/lib/friends') 13const friends = require('../server/lib/friends')
17const Video = mongoose.model('Video')
18
19friends.hasFriends(function (err, hasFriends) {
20 if (err) throw err
21 14
22 if (hasFriends === true) { 15db.init(true, function () {
23 console.log('Cannot update host because you have friends!') 16 friends.hasFriends(function (err, hasFriends) {
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 17 if (err) throw err
30 18
19 if (hasFriends === true) {
20 console.log('Cannot update host because you have friends!')
21 process.exit(-1)
22 }
23
31 console.log('Updating torrent files.') 24 console.log('Updating torrent files.')
32 Video.find().lean().exec(function (err, videos) { 25 db.Video.list(function (err, videos) {
33 if (err) throw err 26 if (err) throw err
34 27
35 videos.forEach(function (video) { 28 videos.forEach(function (video) {