installer.ts 5.8 KB

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