import * as passwordGenerator from 'password-generator'
-import * as Bluebird from 'bluebird'
-
+import { UserRole } from '../../shared'
+import { logger, mkdirpPromise, rimrafPromise } from '../helpers'
+import { createUserAccountAndChannel } from '../lib'
+import { createLocalAccountWithoutKeys } from '../lib/user'
+import { applicationExist, clientsExist, usersExist } from './checker'
+import { CACHE, CONFIG, LAST_MIGRATION_VERSION, SERVER_ACCOUNT_NAME } from './constants'
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'
+import { createPrivateAndPublicKeys } from '../helpers/peertube-crypto'
async function installApplication () {
- await db.sequelize.sync()
- await removeCacheDirectories()
- await createDirectoriesIfNotExist()
- await createCertsIfNotExist()
- await createOAuthClientIfNotExist()
- await createOAuthAdminIfNotExist()
+ try {
+ await db.sequelize.sync()
+ await removeCacheDirectories()
+ await createDirectoriesIfNotExist()
+ await createApplicationIfNotExist()
+ await createOAuthClientIfNotExist()
+ await createOAuthAdminIfNotExist()
+ } catch (err) {
+ logger.error('Cannot install application.', err)
+ throw err
+ }
}
// ---------------------------------------------------------------------------
function removeCacheDirectories () {
const cacheDirectories = CACHE.DIRECTORIES
- const tasks: Bluebird<any>[] = []
+ const tasks: Promise<any>[] = []
// Cache directories
for (const key of Object.keys(cacheDirectories)) {
logger.info('Creating the administrator.')
const username = 'root'
- const role = USER_ROLES.ADMIN
+ const role = UserRole.ADMINISTRATOR
const email = CONFIG.ADMIN.EMAIL
let validatePassword = true
let password = ''
}
const user = db.User.build(userData)
- await createUserAuthorAndChannel(user, validatePassword)
+ await createUserAccountAndChannel(user, validatePassword)
logger.info('Username: ' + username)
logger.info('User password: ' + password)
+}
+
+async function createApplicationIfNotExist () {
+ const exist = await applicationExist(db.Application)
+ // Nothing to do, application already exist
+ if (exist === true) return undefined
logger.info('Creating Application table.')
- await db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
+ const applicationInstance = await db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
+
+ logger.info('Creating application account.')
+
+ const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACCOUNT_NAME, null, applicationInstance.id, undefined)
+
+ const { publicKey, privateKey } = await createPrivateAndPublicKeys()
+ accountCreated.set('publicKey', publicKey)
+ accountCreated.set('privateKey', privateKey)
+
+ return accountCreated.save()
}