Remove max duration/filesize constraints
[oweals/peertube.git] / server / initializers / installer.ts
index 4c04290fc455542728504eae71322883b8d529a9..9545160571457f63decf73fb9d55ec3f316407f9 100644 (file)
@@ -1,19 +1,25 @@
 import * as passwordGenerator from 'password-generator'
-import * as Bluebird from 'bluebird'
-
+import { UserRole } from '../../shared'
+import { logger, mkdirpPromise, rimrafPromise } from '../helpers'
+import { createUserAccountAndChannel } from '../lib'
+import { createLocalAccountWithoutKeys } from '../lib/user'
+import { applicationExist, clientsExist, usersExist } from './checker'
+import { CACHE, CONFIG, LAST_MIGRATION_VERSION, SERVER_ACCOUNT_NAME } from './constants'
 import { database as db } from './database'
-import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION, CACHE } from './constants'
-import { clientsExist, usersExist } from './checker'
-import { logger, createCertsIfNotExist, mkdirpPromise, rimrafPromise } from '../helpers'
-import { createUserAuthorAndChannel } from '../lib'
+import { createPrivateAndPublicKeys } from '../helpers/peertube-crypto'
 
 async function installApplication () {
-  await db.sequelize.sync()
-  await removeCacheDirectories()
-  await createDirectoriesIfNotExist()
-  await createCertsIfNotExist()
-  await createOAuthClientIfNotExist()
-  await createOAuthAdminIfNotExist()
+  try {
+    await db.sequelize.sync()
+    await removeCacheDirectories()
+    await createDirectoriesIfNotExist()
+    await createApplicationIfNotExist()
+    await createOAuthClientIfNotExist()
+    await createOAuthAdminIfNotExist()
+  } catch (err) {
+    logger.error('Cannot install application.', err)
+    throw err
+  }
 }
 
 // ---------------------------------------------------------------------------
@@ -27,7 +33,7 @@ export {
 function removeCacheDirectories () {
   const cacheDirectories = CACHE.DIRECTORIES
 
-  const tasks: Bluebird<any>[] = []
+  const tasks: Promise<any>[] = []
 
   // Cache directories
   for (const key of Object.keys(cacheDirectories)) {
@@ -88,7 +94,7 @@ async function createOAuthAdminIfNotExist () {
   logger.info('Creating the administrator.')
 
   const username = 'root'
-  const role = USER_ROLES.ADMIN
+  const role = UserRole.ADMINISTRATOR
   const email = CONFIG.ADMIN.EMAIL
   let validatePassword = true
   let password = ''
@@ -116,10 +122,26 @@ async function createOAuthAdminIfNotExist () {
   }
   const user = db.User.build(userData)
 
-  await createUserAuthorAndChannel(user, validatePassword)
+  await createUserAccountAndChannel(user, validatePassword)
   logger.info('Username: ' + username)
   logger.info('User password: ' + password)
+}
+
+async function createApplicationIfNotExist () {
+  const exist = await applicationExist(db.Application)
+  // Nothing to do, application already exist
+  if (exist === true) return undefined
 
   logger.info('Creating Application table.')
-  await db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
+  const applicationInstance = await db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
+
+  logger.info('Creating application account.')
+
+  const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACCOUNT_NAME, null, applicationInstance.id, undefined)
+
+  const { publicKey, privateKey } = await createPrivateAndPublicKeys()
+  accountCreated.set('publicKey', publicKey)
+  accountCreated.set('privateKey', privateKey)
+
+  return accountCreated.save()
 }