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