]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/initializers/installer.ts
server: serve files from storage/well-known (#5214)
[github/Chocobozzz/PeerTube.git] / server / initializers / installer.ts
index 5221b81a5bc3de95042f848f1b8403b4f5c215a6..b02be956702505add2383ba165777c202f692f13 100644 (file)
@@ -1,24 +1,38 @@
-import * as passwordGenerator from 'password-generator'
-import { UserRole } from '../../shared'
-import { logger, mkdirpPromise, rimrafPromise } from '../helpers'
-import { createUserAccountAndChannel } from '../lib'
-import { createLocalAccount } from '../lib/user'
-import { clientsExist, usersExist } from './checker'
-import { CACHE, CONFIG, LAST_MIGRATION_VERSION } from './constants'
-
-import { database as db } from './database'
+import { ensureDir, readdir, remove } from 'fs-extra'
+import passwordGenerator from 'password-generator'
+import { join } from 'path'
+import { isTestOrDevInstance } from '@server/helpers/core-utils'
+import { getNodeABIVersion } from '@server/helpers/version'
+import { UserRole } from '@shared/models'
+import { logger } from '../helpers/logger'
+import { buildUser, createApplicationActor, createUserAccountAndChannelAndPlaylist } from '../lib/user'
+import { ApplicationModel } from '../models/application/application'
+import { OAuthClientModel } from '../models/oauth/oauth-client'
+import { applicationExist, clientsExist, usersExist } from './checker-after-init'
+import { CONFIG } from './config'
+import { FILES_CACHE, HLS_STREAMING_PLAYLIST_DIRECTORY, LAST_MIGRATION_VERSION, RESUMABLE_UPLOAD_DIRECTORY } from './constants'
+import { sequelizeTypescript } from './database'
 
 async function installApplication () {
   try {
-    await db.sequelize.sync()
-    await removeCacheDirectories()
-    await createDirectoriesIfNotExist()
-    await createOAuthClientIfNotExist()
-    await createOAuthAdminIfNotExist()
-    await createApplicationIfNotExist()
+    await Promise.all([
+      // Database related
+      sequelizeTypescript.sync()
+        .then(() => {
+          return Promise.all([
+            createApplicationIfNotExist(),
+            createOAuthClientIfNotExist(),
+            createOAuthAdminIfNotExist()
+          ])
+        }),
+
+      // Directories
+      removeCacheAndTmpDirectories()
+        .then(() => createDirectoriesIfNotExist())
+    ])
   } catch (err) {
-    logger.error('Cannot install application.', err)
-    throw err
+    logger.error('Cannot install application.', { err })
+    process.exit(-1)
   }
 }
 
@@ -30,41 +44,65 @@ export {
 
 // ---------------------------------------------------------------------------
 
-function removeCacheDirectories () {
-  const cacheDirectories = CACHE.DIRECTORIES
+function removeCacheAndTmpDirectories () {
+  const cacheDirectories = Object.keys(FILES_CACHE)
+    .map(k => FILES_CACHE[k].DIRECTORY)
 
   const tasks: Promise<any>[] = []
 
   // Cache directories
   for (const key of Object.keys(cacheDirectories)) {
     const dir = cacheDirectories[key]
-    tasks.push(rimrafPromise(dir))
+    tasks.push(removeDirectoryOrContent(dir))
   }
 
+  tasks.push(removeDirectoryOrContent(CONFIG.STORAGE.TMP_DIR))
+
   return Promise.all(tasks)
 }
 
+async function removeDirectoryOrContent (dir: string) {
+  try {
+    await remove(dir)
+  } catch (err) {
+    logger.debug('Cannot remove directory %s. Removing content instead.', dir, { err })
+
+    const files = await readdir(dir)
+
+    for (const file of files) {
+      await remove(join(dir, file))
+    }
+  }
+}
+
 function createDirectoriesIfNotExist () {
   const storage = CONFIG.STORAGE
-  const cacheDirectories = CACHE.DIRECTORIES
+  const cacheDirectories = Object.keys(FILES_CACHE)
+                                 .map(k => FILES_CACHE[k].DIRECTORY)
 
-  const tasks = []
+  const tasks: Promise<void>[] = []
   for (const key of Object.keys(storage)) {
     const dir = storage[key]
-    tasks.push(mkdirpPromise(dir))
+    tasks.push(ensureDir(dir))
   }
 
   // Cache directories
   for (const key of Object.keys(cacheDirectories)) {
     const dir = cacheDirectories[key]
-    tasks.push(mkdirpPromise(dir))
+    tasks.push(ensureDir(dir))
   }
 
+  // Playlist directories
+  tasks.push(ensureDir(HLS_STREAMING_PLAYLIST_DIRECTORY))
+
+  // Resumable upload directory
+  tasks.push(ensureDir(RESUMABLE_UPLOAD_DIRECTORY))
+
   return Promise.all(tasks)
 }
 
 async function createOAuthClientIfNotExist () {
-  const exist = await clientsExist(db.OAuthClient)
+  const exist = await clientsExist()
   // Nothing to do, clients already exist
   if (exist === true) return undefined
 
@@ -72,7 +110,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' ],
@@ -87,7 +125,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
 
@@ -99,8 +137,8 @@ async function createOAuthAdminIfNotExist () {
   let validatePassword = true
   let password = ''
 
-  // Do not generate a random password for tests
-  if (process.env.NODE_ENV === 'test') {
+  // Do not generate a random password for test and dev environments
+  if (isTestOrDevInstance()) {
     password = 'test'
 
     if (process.env.NODE_APP_INSTANCE) {
@@ -109,28 +147,39 @@ async function createOAuthAdminIfNotExist () {
 
     // Our password is weak so do not validate it
     validatePassword = false
+  } else if (process.env.PT_INITIAL_ROOT_PASSWORD) {
+    password = process.env.PT_INITIAL_ROOT_PASSWORD
   } else {
-    password = passwordGenerator(8, true)
+    password = passwordGenerator(16, true)
   }
 
-  const userData = {
+  const user = buildUser({
     username,
     email,
     password,
     role,
-    videoQuota: -1
-  }
-  const user = db.User.build(userData)
+    emailVerified: true,
+    videoQuota: -1,
+    videoQuotaDaily: -1
+  })
 
-  await createUserAccountAndChannel(user, validatePassword)
+  await createUserAccountAndChannelAndPlaylist({ userToCreate: user, channelNames: undefined, validateUser: validatePassword })
   logger.info('Username: ' + username)
   logger.info('User password: ' + password)
 }
 
 async function createApplicationIfNotExist () {
-  logger.info('Creating Application table.')
-  const applicationInstance = await db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
+  const exist = await applicationExist()
+  // Nothing to do, application already exist
+  if (exist === true) return undefined
 
   logger.info('Creating application account.')
-  return createLocalAccount('peertube', null, applicationInstance.id, undefined)
+
+  const application = await ApplicationModel.create({
+    migrationVersion: LAST_MIGRATION_VERSION,
+    nodeVersion: process.version,
+    nodeABIVersion: getNodeABIVersion()
+  })
+
+  return createApplicationActor(application.id)
 }