3 const config = require('config')
4 const each = require('async/each')
5 const mkdirp = require('mkdirp')
6 const mongoose = require('mongoose')
7 const passwordGenerator = require('password-generator')
8 const path = require('path')
9 const series = require('async/series')
11 const checker = require('./checker')
12 const constants = require('./constants')
13 const logger = require('../helpers/logger')
14 const peertubeCrypto = require('../helpers/peertube-crypto')
16 const Application = mongoose.model('Application')
17 const Client = mongoose.model('OAuthClient')
18 const User = mongoose.model('User')
21 installApplication: installApplication
24 function installApplication (callback) {
26 function createDirectories (callbackAsync) {
27 createDirectoriesIfNotExist(callbackAsync)
30 function createCertificates (callbackAsync) {
31 peertubeCrypto.createCertsIfNotExist(callbackAsync)
34 function createOAuthClient (callbackAsync) {
35 createOAuthClientIfNotExist(callbackAsync)
38 function createOAuthUser (callbackAsync) {
39 createOAuthAdminIfNotExist(callbackAsync)
44 // ---------------------------------------------------------------------------
46 module.exports = installer
48 // ---------------------------------------------------------------------------
50 function createDirectoriesIfNotExist (callback) {
51 const storages = config.get('storage')
53 each(Object.keys(storages), function (key, callbackEach) {
54 const dir = storages[key]
55 mkdirp(path.join(__dirname, '..', '..', dir), callbackEach)
59 function createOAuthClientIfNotExist (callback) {
60 checker.clientsExist(function (err, exist) {
61 if (err) return callback(err)
63 // Nothing to do, clients already exist
64 if (exist === true) return callback(null)
66 logger.info('Creating a default OAuth Client.')
68 const secret = passwordGenerator(32, false)
69 const client = new Client({
71 grants: [ 'password', 'refresh_token' ]
74 client.save(function (err, createdClient) {
75 if (err) return callback(err)
77 logger.info('Client id: ' + createdClient._id)
78 logger.info('Client secret: ' + createdClient.clientSecret)
85 function createOAuthAdminIfNotExist (callback) {
86 checker.usersExist(function (err, exist) {
87 if (err) return callback(err)
89 // Nothing to do, users already exist
90 if (exist === true) return callback(null)
92 logger.info('Creating the administrator.')
94 const username = 'root'
95 const role = constants.USER_ROLES.ADMIN
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 password = passwordGenerator(8, true)
109 const user = new User({
115 user.save(function (err, createdUser) {
116 if (err) return callback(err)
118 logger.info('Username: ' + username)
119 logger.info('User password: ' + password)
121 logger.info('Creating Application collection.')
122 const application = new Application({ mongoSchemaVersion: constants.LAST_MONGO_SCHEMA_VERSION })
123 application.save(callback)