]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/initializers/installer.ts
Fix database benchmark in prod mode
[github/Chocobozzz/PeerTube.git] / server / initializers / installer.ts
index 2f9b589916b9bc6e28c0f50b945639f70cd31656..10b74b85fd50cb8b2334f52716d4e4fc2522b21d 100644 (file)
@@ -1,37 +1,18 @@
-import { join } from 'path'
-import * as config from 'config'
-import { each, series } from 'async'
-import * as mkdirp from 'mkdirp'
 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 })
     })
   })
 }