X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Finitializers%2Finstaller.js;h=fb63b81ac64c1e962446bc4adf283c60f4d6836e;hb=67bf9b96bbcd92b069fe86d9223fe0f8b9c6e677;hp=e0ae822cfef2936253db786733b222e9efba27e5;hpb=69b0a27cbbd69ca019eb7db5f917b1dd06dc82cd;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/initializers/installer.js b/server/initializers/installer.js index e0ae822cf..fb63b81ac 100644 --- a/server/initializers/installer.js +++ b/server/initializers/installer.js @@ -1,25 +1,29 @@ 'use strict' -const async = require('async') const config = require('config') +const each = require('async/each') const mkdirp = require('mkdirp') -const mongoose = require('mongoose') const passwordGenerator = require('password-generator') const path = require('path') +const series = require('async/series') const checker = require('./checker') +const constants = require('./constants') +const db = require('./database') const logger = require('../helpers/logger') -const peertubeCrypto = require('../helpers/peertubeCrypto') - -const Client = mongoose.model('OAuthClient') -const User = mongoose.model('User') +const peertubeCrypto = require('../helpers/peertube-crypto') const installer = { - installApplication: installApplication + installApplication } function installApplication (callback) { - async.series([ + series([ + function createDatabase (callbackAsync) { + db.sequelize.sync().asCallback(callbackAsync) + // db.sequelize.sync({ force: true }).asCallback(callbackAsync) + }, + function createDirectories (callbackAsync) { createDirectoriesIfNotExist(callbackAsync) }, @@ -33,7 +37,7 @@ function installApplication (callback) { }, function createOAuthUser (callbackAsync) { - createOAuthUserIfNotExist(callbackAsync) + createOAuthAdminIfNotExist(callbackAsync) } ], callback) } @@ -47,7 +51,7 @@ module.exports = installer function createDirectoriesIfNotExist (callback) { const storages = config.get('storage') - async.each(Object.keys(storages), function (key, callbackEach) { + each(Object.keys(storages), function (key, callbackEach) { const dir = storages[key] mkdirp(path.join(__dirname, '..', '..', dir), callbackEach) }, callback) @@ -62,16 +66,18 @@ function createOAuthClientIfNotExist (callback) { logger.info('Creating a default OAuth Client.') - const secret = passwordGenerator(32, false) - const client = new Client({ + const id = passwordGenerator(32, false, /[a-z0-9]/) + const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/) + const client = db.OAuthClient.build({ + clientId: id, clientSecret: secret, - grants: [ 'password' ] + grants: [ 'password', 'refresh_token' ] }) - client.save(function (err, createdClient) { + client.save().asCallback(function (err, createdClient) { if (err) return callback(err) - logger.info('Client id: ' + createdClient._id) + logger.info('Client id: ' + createdClient.clientId) logger.info('Client secret: ' + createdClient.clientSecret) return callback(null) @@ -79,7 +85,7 @@ function createOAuthClientIfNotExist (callback) { }) } -function createOAuthUserIfNotExist (callback) { +function createOAuthAdminIfNotExist (callback) { checker.usersExist(function (err, exist) { if (err) return callback(err) @@ -89,6 +95,8 @@ function createOAuthUserIfNotExist (callback) { logger.info('Creating the administrator.') const username = 'root' + const role = constants.USER_ROLES.ADMIN + const createOptions = {} let password = '' // Do not generate a random password for tests @@ -98,22 +106,27 @@ function createOAuthUserIfNotExist (callback) { if (process.env.NODE_APP_INSTANCE) { password += process.env.NODE_APP_INSTANCE } + + // Our password is weak so do not validate it + createOptions.validate = false } else { password = passwordGenerator(8, true) } - const user = new User({ - username: username, - password: password - }) + const userData = { + username, + password, + role + } - user.save(function (err, createdUser) { + db.User.create(userData, createOptions).asCallback(function (err, createdUser) { if (err) return callback(err) - logger.info('Username: ' + createdUser.username) - logger.info('User password: ' + createdUser.password) + logger.info('Username: ' + username) + logger.info('User password: ' + password) - return callback(null) + logger.info('Creating Application table.') + db.Application.create({ migrationVersion: constants.LAST_MIGRATION_VERSION }).asCallback(callback) }) }) }