]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/initializers/installer.ts
Add new name/terms/description config options
[github/Chocobozzz/PeerTube.git] / server / initializers / installer.ts
index 865495722feb71523380ac0ceab31ceb8708dde5..324a2c2e5bdd4eefa0fca36f6627c2804db696bf 100644 (file)
@@ -1,15 +1,18 @@
 import * as passwordGenerator from 'password-generator'
 import { UserRole } from '../../shared'
-import { logger, mkdirpPromise, rimrafPromise } from '../helpers'
-import { createUserAccountAndChannel } from '../lib'
-import { createLocalAccountWithoutKeys } from '../lib/user'
+import { mkdirpPromise, rimrafPromise } from '../helpers/core-utils'
+import { logger } from '../helpers/logger'
+import { createApplicationActor, createUserAccountAndChannel } from '../lib/user'
+import { UserModel } from '../models/account/user'
+import { ApplicationModel } from '../models/application/application'
+import { OAuthClientModel } from '../models/oauth/oauth-client'
 import { applicationExist, clientsExist, usersExist } from './checker'
-import { CACHE, CONFIG, LAST_MIGRATION_VERSION, SERVER_ACCOUNT_NAME } from './constants'
-import { database as db } from './database'
+import { CACHE, CONFIG, LAST_MIGRATION_VERSION } from './constants'
+import { sequelizeTypescript } from './database'
 
 async function installApplication () {
   try {
-    await db.sequelize.sync()
+    await sequelizeTypescript.sync()
     await removeCacheDirectories()
     await createDirectoriesIfNotExist()
     await createApplicationIfNotExist()
@@ -17,7 +20,7 @@ async function installApplication () {
     await createOAuthAdminIfNotExist()
   } catch (err) {
     logger.error('Cannot install application.', err)
-    throw err
+    process.exit(-1)
   }
 }
 
@@ -63,7 +66,7 @@ function createDirectoriesIfNotExist () {
 }
 
 async function createOAuthClientIfNotExist () {
-  const exist = await clientsExist(db.OAuthClient)
+  const exist = await clientsExist()
   // Nothing to do, clients already exist
   if (exist === true) return undefined
 
@@ -71,7 +74,7 @@ async function createOAuthClientIfNotExist () {
 
   const id = passwordGenerator(32, false, /[a-z0-9]/)
   const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
-  const client = db.OAuthClient.build({
+  const client = new OAuthClientModel({
     clientId: id,
     clientSecret: secret,
     grants: [ 'password', 'refresh_token' ],
@@ -86,7 +89,7 @@ async function createOAuthClientIfNotExist () {
 }
 
 async function createOAuthAdminIfNotExist () {
-  const exist = await usersExist(db.User)
+  const exist = await usersExist()
   // Nothing to do, users already exist
   if (exist === true) return undefined
 
@@ -119,7 +122,7 @@ async function createOAuthAdminIfNotExist () {
     role,
     videoQuota: -1
   }
-  const user = db.User.build(userData)
+  const user = new UserModel(userData)
 
   await createUserAccountAndChannel(user, validatePassword)
   logger.info('Username: ' + username)
@@ -127,14 +130,17 @@ async function createOAuthAdminIfNotExist () {
 }
 
 async function createApplicationIfNotExist () {
-  const exist = await applicationExist(db.Application)
+  const exist = await applicationExist()
   // Nothing to do, application already exist
   if (exist === true) return undefined
 
   logger.info('Creating Application table.')
-  const applicationInstance = await db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
 
   logger.info('Creating application account.')
 
-  return createLocalAccountWithoutKeys(SERVER_ACCOUNT_NAME, null, applicationInstance.id, undefined)
+  const application = await ApplicationModel.create({
+    migrationVersion: LAST_MIGRATION_VERSION
+  })
+
+  return createApplicationActor(application.id)
 }