1 import * as passwordGenerator from 'password-generator'
2 import { UserRole } from '../../shared'
3 import { logger, mkdirpPromise, rimrafPromise } from '../helpers'
4 import { createPrivateAndPublicKeys } from '../helpers/peertube-crypto'
5 import { createUserAccountAndChannel } from '../lib'
6 import { clientsExist, usersExist } from './checker'
7 import { CACHE, CONFIG, LAST_MIGRATION_VERSION } from './constants'
9 import { database as db } from './database'
10 import { createLocalAccount } from '../lib/user'
12 async function installApplication () {
13 await db.sequelize.sync()
14 await removeCacheDirectories()
15 await createDirectoriesIfNotExist()
16 await createOAuthClientIfNotExist()
17 await createOAuthAdminIfNotExist()
18 await createApplicationIfNotExist()
21 // ---------------------------------------------------------------------------
27 // ---------------------------------------------------------------------------
29 function removeCacheDirectories () {
30 const cacheDirectories = CACHE.DIRECTORIES
32 const tasks: Promise<any>[] = []
35 for (const key of Object.keys(cacheDirectories)) {
36 const dir = cacheDirectories[key]
37 tasks.push(rimrafPromise(dir))
40 return Promise.all(tasks)
43 function createDirectoriesIfNotExist () {
44 const storage = CONFIG.STORAGE
45 const cacheDirectories = CACHE.DIRECTORIES
48 for (const key of Object.keys(storage)) {
49 const dir = storage[key]
50 tasks.push(mkdirpPromise(dir))
54 for (const key of Object.keys(cacheDirectories)) {
55 const dir = cacheDirectories[key]
56 tasks.push(mkdirpPromise(dir))
59 return Promise.all(tasks)
62 async function createOAuthClientIfNotExist () {
63 const exist = await clientsExist(db.OAuthClient)
64 // Nothing to do, clients already exist
65 if (exist === true) return undefined
67 logger.info('Creating a default OAuth Client.')
69 const id = passwordGenerator(32, false, /[a-z0-9]/)
70 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
71 const client = db.OAuthClient.build({
74 grants: [ 'password', 'refresh_token' ],
78 const createdClient = await client.save()
79 logger.info('Client id: ' + createdClient.clientId)
80 logger.info('Client secret: ' + createdClient.clientSecret)
85 async function createOAuthAdminIfNotExist () {
86 const exist = await usersExist(db.User)
87 // Nothing to do, users already exist
88 if (exist === true) return undefined
90 logger.info('Creating the administrator.')
92 const username = 'root'
93 const role = UserRole.ADMINISTRATOR
94 const email = CONFIG.ADMIN.EMAIL
95 let validatePassword = true
98 // Do not generate a random password for tests
99 if (process.env.NODE_ENV === 'test') {
102 if (process.env.NODE_APP_INSTANCE) {
103 password += process.env.NODE_APP_INSTANCE
106 // Our password is weak so do not validate it
107 validatePassword = false
109 password = passwordGenerator(8, true)
119 const user = db.User.build(userData)
121 await createUserAccountAndChannel(user, validatePassword)
122 logger.info('Username: ' + username)
123 logger.info('User password: ' + password)
126 async function createApplicationIfNotExist () {
127 logger.info('Creating Application table.')
128 const applicationInstance = await db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
130 logger.info('Creating application account.')
131 return createLocalAccount('peertube', null, applicationInstance.id, undefined)