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