X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Finitializers%2Finstaller.ts;h=7e321fb76e8c9f30c3e34eed1db3ab10b5ba3009;hb=21d68e68039a1eefbe6213fbde46e737e520ee7d;hp=26e92be0b8d40129f5160acd70c5193e453f37a8;hpb=0b7db72af30403fb6c7d906a4c239a5519cf934d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/initializers/installer.ts b/server/initializers/installer.ts index 26e92be0b..7e321fb76 100644 --- a/server/initializers/installer.ts +++ b/server/initializers/installer.ts @@ -1,18 +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' - -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/models' +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) + } } // --------------------------------------------------------------------------- @@ -23,104 +42,129 @@ 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 storages = CONFIG.STORAGE - const cacheDirectories = CACHE.DIRECTORIES + const storage = CONFIG.STORAGE + const cacheDirectories = Object.keys(FILES_CACHE) + .map(k => FILES_CACHE[k].DIRECTORY) - const tasks = [] - Object.keys(storages).forEach(key => { - const dir = storages[key] - tasks.push(mkdirpPromise(dir)) - }) + const tasks: Promise[] = [] + for (const key of Object.keys(storage)) { + const dir = storage[key] + 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().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().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 - const createOptions: { validate?: boolean } = {} - 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 - createOptions.validate = false - } else { - password = passwordGenerator(8, true) + if (process.env.NODE_APP_INSTANCE) { + password += process.env.NODE_APP_INSTANCE } - const userData = { - username, - email, - password, - role - } + // 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, + p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED, + 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 db.User.create(userData, createOptions).then(createdUser => { - 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) }