]>
Commit | Line | Data |
---|---|---|
37dc07b2 C |
1 | 'use strict' |
2 | ||
3 | const async = require('async') | |
4 | const config = require('config') | |
5 | const mkdirp = require('mkdirp') | |
bb1e6d0c | 6 | const passwordGenerator = require('password-generator') |
37dc07b2 C |
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) { | |
cefc718d C |
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) | |
37dc07b2 C |
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 | ||
bb1e6d0c | 62 | const secret = passwordGenerator(32, false) |
0c1cbbfe | 63 | Users.createClient(secret, [ 'password' ], function (err, id) { |
37dc07b2 C |
64 | if (err) return callback(err) |
65 | ||
66 | logger.info('Client id: ' + id) | |
0c1cbbfe | 67 | logger.info('Client secret: ' + secret) |
37dc07b2 C |
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 | ||
bb1e6d0c | 83 | const username = 'root' |
d14b3e37 C |
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 | } | |
37dc07b2 C |
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 | } |