]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | const config = require('config') | |
4 | const each = require('async/each') | |
5 | const mkdirp = require('mkdirp') | |
6 | const passwordGenerator = require('password-generator') | |
7 | const path = require('path') | |
8 | const series = require('async/series') | |
9 | ||
10 | const checker = require('./checker') | |
11 | const constants = require('./constants') | |
12 | const db = require('./database') | |
13 | const logger = require('../helpers/logger') | |
14 | const peertubeCrypto = require('../helpers/peertube-crypto') | |
15 | ||
16 | const installer = { | |
17 | installApplication | |
18 | } | |
19 | ||
20 | function installApplication (callback) { | |
21 | series([ | |
22 | function createDatabase (callbackAsync) { | |
23 | db.sequelize.sync().asCallback(callbackAsync) | |
24 | // db.sequelize.sync({ force: true }).asCallback(callbackAsync) | |
25 | }, | |
26 | ||
27 | function createDirectories (callbackAsync) { | |
28 | createDirectoriesIfNotExist(callbackAsync) | |
29 | }, | |
30 | ||
31 | function createCertificates (callbackAsync) { | |
32 | peertubeCrypto.createCertsIfNotExist(callbackAsync) | |
33 | }, | |
34 | ||
35 | function createOAuthClient (callbackAsync) { | |
36 | createOAuthClientIfNotExist(callbackAsync) | |
37 | }, | |
38 | ||
39 | function createOAuthUser (callbackAsync) { | |
40 | createOAuthAdminIfNotExist(callbackAsync) | |
41 | } | |
42 | ], callback) | |
43 | } | |
44 | ||
45 | // --------------------------------------------------------------------------- | |
46 | ||
47 | module.exports = installer | |
48 | ||
49 | // --------------------------------------------------------------------------- | |
50 | ||
51 | function createDirectoriesIfNotExist (callback) { | |
52 | const storages = config.get('storage') | |
53 | ||
54 | each(Object.keys(storages), function (key, callbackEach) { | |
55 | const dir = storages[key] | |
56 | mkdirp(path.join(__dirname, '..', '..', dir), callbackEach) | |
57 | }, callback) | |
58 | } | |
59 | ||
60 | function createOAuthClientIfNotExist (callback) { | |
61 | checker.clientsExist(function (err, exist) { | |
62 | if (err) return callback(err) | |
63 | ||
64 | // Nothing to do, clients already exist | |
65 | if (exist === true) return callback(null) | |
66 | ||
67 | logger.info('Creating a default OAuth Client.') | |
68 | ||
69 | const id = passwordGenerator(32, false, /[a-z0-9]/) | |
70 | const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/) | |
71 | const client = db.OAuthClient.build({ | |
72 | clientId: id, | |
73 | clientSecret: secret, | |
74 | grants: [ 'password', 'refresh_token' ] | |
75 | }) | |
76 | ||
77 | client.save().asCallback(function (err, createdClient) { | |
78 | if (err) return callback(err) | |
79 | ||
80 | logger.info('Client id: ' + createdClient.clientId) | |
81 | logger.info('Client secret: ' + createdClient.clientSecret) | |
82 | ||
83 | return callback(null) | |
84 | }) | |
85 | }) | |
86 | } | |
87 | ||
88 | function createOAuthAdminIfNotExist (callback) { | |
89 | checker.usersExist(function (err, exist) { | |
90 | if (err) return callback(err) | |
91 | ||
92 | // Nothing to do, users already exist | |
93 | if (exist === true) return callback(null) | |
94 | ||
95 | logger.info('Creating the administrator.') | |
96 | ||
97 | const username = 'root' | |
98 | const role = constants.USER_ROLES.ADMIN | |
99 | const createOptions = {} | |
100 | let password = '' | |
101 | ||
102 | // Do not generate a random password for tests | |
103 | if (process.env.NODE_ENV === 'test') { | |
104 | password = 'test' | |
105 | ||
106 | if (process.env.NODE_APP_INSTANCE) { | |
107 | password += process.env.NODE_APP_INSTANCE | |
108 | } | |
109 | ||
110 | // Our password is weak so do not validate it | |
111 | createOptions.validate = false | |
112 | } else { | |
113 | password = passwordGenerator(8, true) | |
114 | } | |
115 | ||
116 | const userData = { | |
117 | username, | |
118 | password, | |
119 | role | |
120 | } | |
121 | ||
122 | db.User.create(userData, createOptions).asCallback(function (err, createdUser) { | |
123 | if (err) return callback(err) | |
124 | ||
125 | logger.info('Username: ' + username) | |
126 | logger.info('User password: ' + password) | |
127 | ||
128 | logger.info('Creating Application table.') | |
129 | db.Application.create({ migrationVersion: constants.LAST_MIGRATION_VERSION }).asCallback(callback) | |
130 | }) | |
131 | }) | |
132 | } |