]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/installer.js
Server: remove useless hash affectations
[github/Chocobozzz/PeerTube.git] / server / initializers / installer.js
1 'use strict'
2
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')
10
11 const checker = require('./checker')
12 const constants = require('./constants')
13 const logger = require('../helpers/logger')
14 const peertubeCrypto = require('../helpers/peertube-crypto')
15
16 const Application = mongoose.model('Application')
17 const Client = mongoose.model('OAuthClient')
18 const User = mongoose.model('User')
19
20 const installer = {
21 installApplication
22 }
23
24 function installApplication (callback) {
25 series([
26 function createDirectories (callbackAsync) {
27 createDirectoriesIfNotExist(callbackAsync)
28 },
29
30 function createCertificates (callbackAsync) {
31 peertubeCrypto.createCertsIfNotExist(callbackAsync)
32 },
33
34 function createOAuthClient (callbackAsync) {
35 createOAuthClientIfNotExist(callbackAsync)
36 },
37
38 function createOAuthUser (callbackAsync) {
39 createOAuthAdminIfNotExist(callbackAsync)
40 }
41 ], callback)
42 }
43
44 // ---------------------------------------------------------------------------
45
46 module.exports = installer
47
48 // ---------------------------------------------------------------------------
49
50 function createDirectoriesIfNotExist (callback) {
51 const storages = config.get('storage')
52
53 each(Object.keys(storages), function (key, callbackEach) {
54 const dir = storages[key]
55 mkdirp(path.join(__dirname, '..', '..', dir), callbackEach)
56 }, callback)
57 }
58
59 function createOAuthClientIfNotExist (callback) {
60 checker.clientsExist(function (err, exist) {
61 if (err) return callback(err)
62
63 // Nothing to do, clients already exist
64 if (exist === true) return callback(null)
65
66 logger.info('Creating a default OAuth Client.')
67
68 const secret = passwordGenerator(32, false)
69 const client = new Client({
70 clientSecret: secret,
71 grants: [ 'password', 'refresh_token' ]
72 })
73
74 client.save(function (err, createdClient) {
75 if (err) return callback(err)
76
77 logger.info('Client id: ' + createdClient._id)
78 logger.info('Client secret: ' + createdClient.clientSecret)
79
80 return callback(null)
81 })
82 })
83 }
84
85 function createOAuthAdminIfNotExist (callback) {
86 checker.usersExist(function (err, exist) {
87 if (err) return callback(err)
88
89 // Nothing to do, users already exist
90 if (exist === true) return callback(null)
91
92 logger.info('Creating the administrator.')
93
94 const username = 'root'
95 const role = constants.USER_ROLES.ADMIN
96 let password = ''
97
98 // Do not generate a random password for tests
99 if (process.env.NODE_ENV === 'test') {
100 password = 'test'
101
102 if (process.env.NODE_APP_INSTANCE) {
103 password += process.env.NODE_APP_INSTANCE
104 }
105 } else {
106 password = passwordGenerator(8, true)
107 }
108
109 const user = new User({
110 username,
111 password,
112 role
113 })
114
115 user.save(function (err, createdUser) {
116 if (err) return callback(err)
117
118 logger.info('Username: ' + username)
119 logger.info('User password: ' + password)
120
121 logger.info('Creating Application collection.')
122 const application = new Application({ mongoSchemaVersion: constants.LAST_MONGO_SCHEMA_VERSION })
123 application.save(callback)
124 })
125 })
126 }