X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Finitializers%2Finstaller.ts;h=10b74b85fd50cb8b2334f52716d4e4fc2522b21d;hb=769d332177a5b02d5c2ffc134687d3b4ed65bae9;hp=46716410718a69519ccbd1bad36f89156b920189;hpb=e02643f32e4c97ca307f8fc5b69be79c40d70a3b;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/initializers/installer.ts b/server/initializers/installer.ts index 467164107..10b74b85f 100644 --- a/server/initializers/installer.ts +++ b/server/initializers/installer.ts @@ -1,37 +1,18 @@ -import { join } from 'path' -import config = require('config') -import { each, series } from 'async' -import mkdirp = require('mkdirp') -import passwordGenerator = require('password-generator') +import * as passwordGenerator from 'password-generator' +import * as Promise from 'bluebird' import { database as db } from './database' -import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION } from './constants' +import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION, CACHE } from './constants' import { clientsExist, usersExist } from './checker' -import { logger, createCertsIfNotExist, root } from '../helpers' - -function installApplication (callback) { - series([ - function createDatabase (callbackAsync) { - db.sequelize.sync().asCallback(callbackAsync) - // db.sequelize.sync({ force: true }).asCallback(callbackAsync) - }, - - function createDirectories (callbackAsync) { - createDirectoriesIfNotExist(callbackAsync) - }, - - function createCertificates (callbackAsync) { - createCertsIfNotExist(callbackAsync) - }, - - function createOAuthClient (callbackAsync) { - createOAuthClientIfNotExist(callbackAsync) - }, - - function createOAuthUser (callbackAsync) { - createOAuthAdminIfNotExist(callbackAsync) - } - ], callback) +import { logger, createCertsIfNotExist, mkdirpPromise, rimrafPromise } from '../helpers' + +function installApplication () { + return db.sequelize.sync() + .then(() => removeCacheDirectories()) + .then(() => createDirectoriesIfNotExist()) + .then(() => createCertsIfNotExist()) + .then(() => createOAuthClientIfNotExist()) + .then(() => createOAuthAdminIfNotExist()) } // --------------------------------------------------------------------------- @@ -42,21 +23,43 @@ export { // --------------------------------------------------------------------------- -function createDirectoriesIfNotExist (callback) { - const storages = config.get('storage') +function removeCacheDirectories () { + const cacheDirectories = CACHE.DIRECTORIES + + const tasks = [] + + // Cache directories + Object.keys(cacheDirectories).forEach(key => { + const dir = cacheDirectories[key] + tasks.push(rimrafPromise(dir)) + }) - each(Object.keys(storages), function (key, callbackEach) { - const dir = storages[key] - mkdirp(join(root(), dir), callbackEach) - }, callback) + return Promise.all(tasks) } -function createOAuthClientIfNotExist (callback) { - clientsExist(function (err, exist) { - if (err) return callback(err) +function createDirectoriesIfNotExist () { + const storage = CONFIG.STORAGE + const cacheDirectories = CACHE.DIRECTORIES + const tasks = [] + Object.keys(storage).forEach(key => { + const dir = storage[key] + tasks.push(mkdirpPromise(dir)) + }) + + // Cache directories + Object.keys(cacheDirectories).forEach(key => { + const dir = cacheDirectories[key] + tasks.push(mkdirpPromise(dir)) + }) + + return Promise.all(tasks) +} + +function createOAuthClientIfNotExist () { + return clientsExist(db.OAuthClient).then(exist => { // Nothing to do, clients already exist - if (exist === true) return callback(null) + if (exist === true) return undefined logger.info('Creating a default OAuth Client.') @@ -69,23 +72,19 @@ function createOAuthClientIfNotExist (callback) { redirectUris: null }) - client.save().asCallback(function (err, createdClient) { - if (err) return callback(err) - + return client.save().then(createdClient => { logger.info('Client id: ' + createdClient.clientId) logger.info('Client secret: ' + createdClient.clientSecret) - return callback(null) + return undefined }) }) } -function createOAuthAdminIfNotExist (callback) { - usersExist(function (err, exist) { - if (err) return callback(err) - +function createOAuthAdminIfNotExist () { + return usersExist(db.User).then(exist => { // Nothing to do, users already exist - if (exist === true) return callback(null) + if (exist === true) return undefined logger.info('Creating the administrator.') @@ -113,17 +112,16 @@ function createOAuthAdminIfNotExist (callback) { username, email, password, - role + role, + videoQuota: -1 } - db.User.create(userData, createOptions).asCallback(function (err, createdUser) { - if (err) return callback(err) - + return db.User.create(userData, createOptions).then(createdUser => { logger.info('Username: ' + username) logger.info('User password: ' + password) logger.info('Creating Application table.') - db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION }).asCallback(callback) + return db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION }) }) }) }