2
1

installer.ts 4.7 KB

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