X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Finitializers%2Finstaller.ts;h=4c04290fc455542728504eae71322883b8d529a9;hb=f5028693a896a3076dd286ac0030e3d8f78f5ebf;hp=26e92be0b8d40129f5160acd70c5193e453f37a8;hpb=0b7db72af30403fb6c7d906a4c239a5519cf934d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/initializers/installer.ts b/server/initializers/installer.ts index 26e92be0b..4c04290fc 100644 --- a/server/initializers/installer.ts +++ b/server/initializers/installer.ts @@ -1,18 +1,19 @@ import * as passwordGenerator from 'password-generator' -import * as Promise from 'bluebird' +import * as Bluebird 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 { createUserAuthorAndChannel } from '../lib' + +async function installApplication () { + await db.sequelize.sync() + await removeCacheDirectories() + await createDirectoriesIfNotExist() + await createCertsIfNotExist() + await createOAuthClientIfNotExist() + await createOAuthAdminIfNotExist() } // --------------------------------------------------------------------------- @@ -26,101 +27,99 @@ export { function removeCacheDirectories () { const cacheDirectories = CACHE.DIRECTORIES - const tasks = [] + const tasks: Bluebird[] = [] // Cache directories - Object.keys(cacheDirectories).forEach(key => { + for (const key of Object.keys(cacheDirectories)) { const dir = cacheDirectories[key] tasks.push(rimrafPromise(dir)) - }) + } return Promise.all(tasks) } function createDirectoriesIfNotExist () { - const storages = CONFIG.STORAGE + const storage = CONFIG.STORAGE const cacheDirectories = CACHE.DIRECTORIES const tasks = [] - Object.keys(storages).forEach(key => { - const dir = storages[key] + for (const key of Object.keys(storage)) { + const dir = storage[key] tasks.push(mkdirpPromise(dir)) - }) + } // Cache directories - Object.keys(cacheDirectories).forEach(key => { + for (const key of Object.keys(cacheDirectories)) { const dir = cacheDirectories[key] tasks.push(mkdirpPromise(dir)) - }) + } return Promise.all(tasks) } -function createOAuthClientIfNotExist () { - return clientsExist().then(exist => { - // Nothing to do, clients already exist - if (exist === true) return undefined - - logger.info('Creating a default OAuth Client.') +async function createOAuthClientIfNotExist () { + const exist = await clientsExist(db.OAuthClient) + // Nothing to do, clients already exist + if (exist === true) return undefined - 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 - }) + logger.info('Creating a default OAuth Client.') - return client.save().then(createdClient => { - logger.info('Client id: ' + createdClient.clientId) - logger.info('Client secret: ' + createdClient.clientSecret) - - return undefined - }) + 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 }) -} -function createOAuthAdminIfNotExist () { - return usersExist().then(exist => { - // Nothing to do, users already exist - if (exist === true) return undefined + const createdClient = await client.save() + logger.info('Client id: ' + createdClient.clientId) + logger.info('Client secret: ' + createdClient.clientSecret) - logger.info('Creating the administrator.') + return undefined +} - const username = 'root' - const role = USER_ROLES.ADMIN - const email = CONFIG.ADMIN.EMAIL - const createOptions: { validate?: boolean } = {} - let password = '' +async function createOAuthAdminIfNotExist () { + const exist = await usersExist(db.User) + // Nothing to do, users already exist + if (exist === true) return undefined - // Do not generate a random password for tests - if (process.env.NODE_ENV === 'test') { - password = 'test' + logger.info('Creating the administrator.') - if (process.env.NODE_APP_INSTANCE) { - password += process.env.NODE_APP_INSTANCE - } + const username = 'root' + const role = USER_ROLES.ADMIN + const email = CONFIG.ADMIN.EMAIL + let validatePassword = true + let password = '' - // Our password is weak so do not validate it - createOptions.validate = false - } else { - password = passwordGenerator(8, true) - } + // Do not generate a random password for tests + if (process.env.NODE_ENV === 'test') { + password = 'test' - const userData = { - username, - email, - password, - role + if (process.env.NODE_APP_INSTANCE) { + password += process.env.NODE_APP_INSTANCE } - return db.User.create(userData, createOptions).then(createdUser => { - logger.info('Username: ' + username) - logger.info('User password: ' + password) - - logger.info('Creating Application table.') - return db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION }) - }) - }) + // Our password is weak so do not validate it + validatePassword = false + } else { + password = passwordGenerator(8, true) + } + + const userData = { + username, + email, + password, + role, + videoQuota: -1 + } + const user = db.User.build(userData) + + await createUserAuthorAndChannel(user, validatePassword) + logger.info('Username: ' + username) + logger.info('User password: ' + password) + + logger.info('Creating Application table.') + await db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION }) }