installer.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import { ensureDir, readdir, remove } from 'fs-extra'
  2. import passwordGenerator from 'password-generator'
  3. import { join } from 'path'
  4. import { isTestOrDevInstance } from '@server/helpers/core-utils'
  5. import { getNodeABIVersion } from '@server/helpers/version'
  6. import { UserRole } from '@shared/models'
  7. import { logger } from '../helpers/logger'
  8. import { buildUser, createApplicationActor, createUserAccountAndChannelAndPlaylist } from '../lib/user'
  9. import { ApplicationModel } from '../models/application/application'
  10. import { OAuthClientModel } from '../models/oauth/oauth-client'
  11. import { applicationExist, clientsExist, usersExist } from './checker-after-init'
  12. import { CONFIG } from './config'
  13. import { DIRECTORIES, FILES_CACHE, LAST_MIGRATION_VERSION } from './constants'
  14. import { sequelizeTypescript } from './database'
  15. async function installApplication () {
  16. try {
  17. await Promise.all([
  18. // Database related
  19. sequelizeTypescript.sync()
  20. .then(() => {
  21. return Promise.all([
  22. createApplicationIfNotExist(),
  23. createOAuthClientIfNotExist(),
  24. createOAuthAdminIfNotExist()
  25. ])
  26. }),
  27. // Directories
  28. removeCacheAndTmpDirectories()
  29. .then(() => createDirectoriesIfNotExist())
  30. ])
  31. } catch (err) {
  32. logger.error('Cannot install application.', { err })
  33. process.exit(-1)
  34. }
  35. }
  36. // ---------------------------------------------------------------------------
  37. export {
  38. installApplication
  39. }
  40. // ---------------------------------------------------------------------------
  41. function removeCacheAndTmpDirectories () {
  42. const cacheDirectories = Object.keys(FILES_CACHE)
  43. .map(k => FILES_CACHE[k].DIRECTORY)
  44. const tasks: Promise<any>[] = []
  45. // Cache directories
  46. for (const dir of cacheDirectories) {
  47. tasks.push(removeDirectoryOrContent(dir))
  48. }
  49. tasks.push(removeDirectoryOrContent(CONFIG.STORAGE.TMP_DIR))
  50. return Promise.all(tasks)
  51. }
  52. async function removeDirectoryOrContent (dir: string) {
  53. try {
  54. await remove(dir)
  55. } catch (err) {
  56. logger.debug('Cannot remove directory %s. Removing content instead.', dir, { err })
  57. const files = await readdir(dir)
  58. for (const file of files) {
  59. await remove(join(dir, file))
  60. }
  61. }
  62. }
  63. function createDirectoriesIfNotExist () {
  64. const storage = CONFIG.STORAGE
  65. const cacheDirectories = Object.keys(FILES_CACHE)
  66. .map(k => FILES_CACHE[k].DIRECTORY)
  67. const tasks: Promise<void>[] = []
  68. for (const key of Object.keys(storage)) {
  69. const dir = storage[key]
  70. tasks.push(ensureDir(dir))
  71. }
  72. // Cache directories
  73. for (const dir of cacheDirectories) {
  74. tasks.push(ensureDir(dir))
  75. }
  76. tasks.push(ensureDir(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE))
  77. tasks.push(ensureDir(DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC))
  78. tasks.push(ensureDir(DIRECTORIES.VIDEOS.PUBLIC))
  79. tasks.push(ensureDir(DIRECTORIES.VIDEOS.PRIVATE))
  80. // Resumable upload directory
  81. tasks.push(ensureDir(DIRECTORIES.RESUMABLE_UPLOAD))
  82. return Promise.all(tasks)
  83. }
  84. async function createOAuthClientIfNotExist () {
  85. const exist = await clientsExist()
  86. // Nothing to do, clients already exist
  87. if (exist === true) return undefined
  88. logger.info('Creating a default OAuth Client.')
  89. const id = passwordGenerator(32, false, /[a-z0-9]/)
  90. const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
  91. const client = new OAuthClientModel({
  92. clientId: id,
  93. clientSecret: secret,
  94. grants: [ 'password', 'refresh_token' ],
  95. redirectUris: null
  96. })
  97. const createdClient = await client.save()
  98. logger.info('Client id: ' + createdClient.clientId)
  99. logger.info('Client secret: ' + createdClient.clientSecret)
  100. return undefined
  101. }
  102. async function createOAuthAdminIfNotExist () {
  103. const exist = await usersExist()
  104. // Nothing to do, users already exist
  105. if (exist === true) return undefined
  106. logger.info('Creating the administrator.')
  107. const username = 'root'
  108. const role = UserRole.ADMINISTRATOR
  109. const email = CONFIG.ADMIN.EMAIL
  110. let validatePassword = true
  111. let password = ''
  112. // Do not generate a random password for test and dev environments
  113. if (isTestOrDevInstance()) {
  114. password = 'test'
  115. if (process.env.NODE_APP_INSTANCE) {
  116. password += process.env.NODE_APP_INSTANCE
  117. }
  118. // Our password is weak so do not validate it
  119. validatePassword = false
  120. } else if (process.env.PT_INITIAL_ROOT_PASSWORD) {
  121. password = process.env.PT_INITIAL_ROOT_PASSWORD
  122. } else {
  123. password = passwordGenerator(16, true)
  124. }
  125. const user = buildUser({
  126. username,
  127. email,
  128. password,
  129. role,
  130. emailVerified: true,
  131. videoQuota: -1,
  132. videoQuotaDaily: -1
  133. })
  134. await createUserAccountAndChannelAndPlaylist({ userToCreate: user, channelNames: undefined, validateUser: validatePassword })
  135. logger.info('Username: ' + username)
  136. logger.info('User password: ' + password)
  137. }
  138. async function createApplicationIfNotExist () {
  139. const exist = await applicationExist()
  140. // Nothing to do, application already exist
  141. if (exist === true) return undefined
  142. logger.info('Creating application account.')
  143. const application = await ApplicationModel.create({
  144. migrationVersion: LAST_MIGRATION_VERSION,
  145. nodeVersion: process.version,
  146. nodeABIVersion: getNodeABIVersion()
  147. })
  148. return createApplicationActor(application.id)
  149. }