aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/installer.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/initializers/installer.js')
-rw-r--r--server/initializers/installer.js36
1 files changed, 21 insertions, 15 deletions
diff --git a/server/initializers/installer.js b/server/initializers/installer.js
index 1df300ba8..fb63b81ac 100644
--- a/server/initializers/installer.js
+++ b/server/initializers/installer.js
@@ -3,26 +3,27 @@
3const config = require('config') 3const config = require('config')
4const each = require('async/each') 4const each = require('async/each')
5const mkdirp = require('mkdirp') 5const mkdirp = require('mkdirp')
6const mongoose = require('mongoose')
7const passwordGenerator = require('password-generator') 6const passwordGenerator = require('password-generator')
8const path = require('path') 7const path = require('path')
9const series = require('async/series') 8const series = require('async/series')
10 9
11const checker = require('./checker') 10const checker = require('./checker')
12const constants = require('./constants') 11const constants = require('./constants')
12const db = require('./database')
13const logger = require('../helpers/logger') 13const logger = require('../helpers/logger')
14const peertubeCrypto = require('../helpers/peertube-crypto') 14const peertubeCrypto = require('../helpers/peertube-crypto')
15 15
16const Application = mongoose.model('Application')
17const Client = mongoose.model('OAuthClient')
18const User = mongoose.model('User')
19
20const installer = { 16const installer = {
21 installApplication 17 installApplication
22} 18}
23 19
24function installApplication (callback) { 20function installApplication (callback) {
25 series([ 21 series([
22 function createDatabase (callbackAsync) {
23 db.sequelize.sync().asCallback(callbackAsync)
24 // db.sequelize.sync({ force: true }).asCallback(callbackAsync)
25 },
26
26 function createDirectories (callbackAsync) { 27 function createDirectories (callbackAsync) {
27 createDirectoriesIfNotExist(callbackAsync) 28 createDirectoriesIfNotExist(callbackAsync)
28 }, 29 },
@@ -65,16 +66,18 @@ function createOAuthClientIfNotExist (callback) {
65 66
66 logger.info('Creating a default OAuth Client.') 67 logger.info('Creating a default OAuth Client.')
67 68
68 const secret = passwordGenerator(32, false) 69 const id = passwordGenerator(32, false, /[a-z0-9]/)
69 const client = new Client({ 70 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
71 const client = db.OAuthClient.build({
72 clientId: id,
70 clientSecret: secret, 73 clientSecret: secret,
71 grants: [ 'password', 'refresh_token' ] 74 grants: [ 'password', 'refresh_token' ]
72 }) 75 })
73 76
74 client.save(function (err, createdClient) { 77 client.save().asCallback(function (err, createdClient) {
75 if (err) return callback(err) 78 if (err) return callback(err)
76 79
77 logger.info('Client id: ' + createdClient._id) 80 logger.info('Client id: ' + createdClient.clientId)
78 logger.info('Client secret: ' + createdClient.clientSecret) 81 logger.info('Client secret: ' + createdClient.clientSecret)
79 82
80 return callback(null) 83 return callback(null)
@@ -93,6 +96,7 @@ function createOAuthAdminIfNotExist (callback) {
93 96
94 const username = 'root' 97 const username = 'root'
95 const role = constants.USER_ROLES.ADMIN 98 const role = constants.USER_ROLES.ADMIN
99 const createOptions = {}
96 let password = '' 100 let password = ''
97 101
98 // Do not generate a random password for tests 102 // Do not generate a random password for tests
@@ -102,25 +106,27 @@ function createOAuthAdminIfNotExist (callback) {
102 if (process.env.NODE_APP_INSTANCE) { 106 if (process.env.NODE_APP_INSTANCE) {
103 password += process.env.NODE_APP_INSTANCE 107 password += process.env.NODE_APP_INSTANCE
104 } 108 }
109
110 // Our password is weak so do not validate it
111 createOptions.validate = false
105 } else { 112 } else {
106 password = passwordGenerator(8, true) 113 password = passwordGenerator(8, true)
107 } 114 }
108 115
109 const user = new User({ 116 const userData = {
110 username, 117 username,
111 password, 118 password,
112 role 119 role
113 }) 120 }
114 121
115 user.save(function (err, createdUser) { 122 db.User.create(userData, createOptions).asCallback(function (err, createdUser) {
116 if (err) return callback(err) 123 if (err) return callback(err)
117 124
118 logger.info('Username: ' + username) 125 logger.info('Username: ' + username)
119 logger.info('User password: ' + password) 126 logger.info('User password: ' + password)
120 127
121 logger.info('Creating Application collection.') 128 logger.info('Creating Application table.')
122 const application = new Application({ mongoSchemaVersion: constants.LAST_MONGO_SCHEMA_VERSION }) 129 db.Application.create({ migrationVersion: constants.LAST_MIGRATION_VERSION }).asCallback(callback)
123 application.save(callback)
124 }) 130 })
125 }) 131 })
126} 132}