X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Finitializers%2Finstaller.ts;h=75daeb5d89e55792c5a2e236caef15e64737db0d;hb=221ee1adc916684d4881d2a9c4c01954dcde986e;hp=b997de07f16824c3fdefe56b13036892b7a40184;hpb=72c7248b6fdcdb2175e726ff51b42e7555f2bd84;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/initializers/installer.ts b/server/initializers/installer.ts index b997de07f..75daeb5d8 100644 --- a/server/initializers/installer.ts +++ b/server/initializers/installer.ts @@ -1,19 +1,37 @@ -import * as passwordGenerator from 'password-generator' -import * as Promise from 'bluebird' - -import { database as db } from './database' -import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION, CACHE } from './constants' -import { clientsExist, usersExist } from './checker' -import { logger, createCertsIfNotExist, mkdirpPromise, rimrafPromise } from '../helpers' -import { createUserAuthorAndChannel } from '../lib' - -function installApplication () { - return db.sequelize.sync() - .then(() => removeCacheDirectories()) - .then(() => createDirectoriesIfNotExist()) - .then(() => createCertsIfNotExist()) - .then(() => createOAuthClientIfNotExist()) - .then(() => createOAuthAdminIfNotExist()) +import { ensureDir, remove } from 'fs-extra' +import passwordGenerator from 'password-generator' +import { UserRole } from '../../shared' +import { logger } from '../helpers/logger' +import { createApplicationActor, createUserAccountAndChannelAndPlaylist } from '../lib/user' +import { ApplicationModel } from '../models/application/application' +import { OAuthClientModel } from '../models/oauth/oauth-client' +import { UserModel } from '../models/user/user' +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 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 }) + process.exit(-1) + } } // --------------------------------------------------------------------------- @@ -24,107 +42,128 @@ export { // --------------------------------------------------------------------------- -function removeCacheDirectories () { - const cacheDirectories = CACHE.DIRECTORIES +function removeCacheAndTmpDirectories () { + const cacheDirectories = Object.keys(FILES_CACHE) + .map(k => FILES_CACHE[k].DIRECTORY) - const tasks = [] + const tasks: Promise[] = [] // Cache directories - Object.keys(cacheDirectories).forEach(key => { + for (const key of Object.keys(cacheDirectories)) { const dir = cacheDirectories[key] - tasks.push(rimrafPromise(dir)) - }) + tasks.push(remove(dir)) + } + + tasks.push(remove(CONFIG.STORAGE.TMP_DIR)) return Promise.all(tasks) } function createDirectoriesIfNotExist () { const storage = CONFIG.STORAGE - const cacheDirectories = CACHE.DIRECTORIES + const cacheDirectories = Object.keys(FILES_CACHE) + .map(k => FILES_CACHE[k].DIRECTORY) - const tasks = [] - Object.keys(storage).forEach(key => { + const tasks: Promise[] = [] + for (const key of Object.keys(storage)) { const dir = storage[key] - tasks.push(mkdirpPromise(dir)) - }) + tasks.push(ensureDir(dir)) + } // Cache directories - Object.keys(cacheDirectories).forEach(key => { + 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) } -function createOAuthClientIfNotExist () { - return clientsExist(db.OAuthClient).then(exist => { - // Nothing to do, clients already exist - if (exist === true) return undefined +async function createOAuthClientIfNotExist () { + const exist = await clientsExist() + // Nothing to do, clients already exist + if (exist === true) return undefined - logger.info('Creating a default OAuth Client.') + logger.info('Creating a default OAuth Client.') - const id = passwordGenerator(32, false, /[a-z0-9]/) - const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/) - const client = db.OAuthClient.build({ - clientId: id, - clientSecret: secret, - grants: [ 'password', 'refresh_token' ], - redirectUris: null - }) + const id = passwordGenerator(32, false, /[a-z0-9]/) + const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/) + const client = new OAuthClientModel({ + clientId: id, + clientSecret: secret, + grants: [ 'password', 'refresh_token' ], + redirectUris: null + }) - return client.save().then(createdClient => { - logger.info('Client id: ' + createdClient.clientId) - logger.info('Client secret: ' + createdClient.clientSecret) + const createdClient = await client.save() + logger.info('Client id: ' + createdClient.clientId) + logger.info('Client secret: ' + createdClient.clientSecret) - return undefined - }) - }) + return undefined } -function createOAuthAdminIfNotExist () { - return usersExist(db.User).then(exist => { - // Nothing to do, users already exist - if (exist === true) return undefined - - logger.info('Creating the administrator.') +async function createOAuthAdminIfNotExist () { + const exist = await usersExist() + // Nothing to do, users already exist + if (exist === true) return undefined - const username = 'root' - const role = USER_ROLES.ADMIN - const email = CONFIG.ADMIN.EMAIL - let validatePassword = true - let password = '' + logger.info('Creating the administrator.') - // Do not generate a random password for tests - if (process.env.NODE_ENV === 'test') { - password = 'test' + const username = 'root' + const role = UserRole.ADMINISTRATOR + const email = CONFIG.ADMIN.EMAIL + let validatePassword = true + let password = '' - if (process.env.NODE_APP_INSTANCE) { - password += process.env.NODE_APP_INSTANCE - } + // Do not generate a random password for tests + if (process.env.NODE_ENV === 'test') { + password = 'test' - // Our password is weak so do not validate it - validatePassword = false - } else { - password = passwordGenerator(8, true) + if (process.env.NODE_APP_INSTANCE) { + password += process.env.NODE_APP_INSTANCE } - const userData = { - username, - email, - password, - role, - videoQuota: -1 - } - const user = db.User.build(userData) + // 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(16, true) + } + + const userData = { + username, + email, + password, + role, + verified: true, + nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, + videoQuota: -1, + videoQuotaDaily: -1 + } + const user = new UserModel(userData) + + await createUserAccountAndChannelAndPlaylist({ userToCreate: user, channelNames: undefined, validateUser: validatePassword }) + logger.info('Username: ' + username) + logger.info('User password: ' + password) +} + +async function createApplicationIfNotExist () { + const exist = await applicationExist() + // Nothing to do, application already exist + if (exist === true) return undefined - return createUserAuthorAndChannel(user, validatePassword) - .then(({ user }) => { - logger.info('Username: ' + username) - logger.info('User password: ' + password) + logger.info('Creating application account.') - logger.info('Creating Application table.') - return db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION }) - }) + const application = await ApplicationModel.create({ + migrationVersion: LAST_MIGRATION_VERSION }) + + return createApplicationActor(application.id) }