]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/installer.js
Update bittorrent-tracker and standard to v9
[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 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 email = constants.CONFIG.ADMIN.EMAIL
100 const createOptions = {}
101 let password = ''
102
103 // Do not generate a random password for tests
104 if (process.env.NODE_ENV === 'test') {
105 password = 'test'
106
107 if (process.env.NODE_APP_INSTANCE) {
108 password += process.env.NODE_APP_INSTANCE
109 }
110
111 // Our password is weak so do not validate it
112 createOptions.validate = false
113 } else {
114 password = passwordGenerator(8, true)
115 }
116
117 const userData = {
118 username,
119 email,
120 password,
121 role
122 }
123
124 db.User.create(userData, createOptions).asCallback(function (err, createdUser) {
125 if (err) return callback(err)
126
127 logger.info('Username: ' + username)
128 logger.info('User password: ' + password)
129
130 logger.info('Creating Application table.')
131 db.Application.create({ migrationVersion: constants.LAST_MIGRATION_VERSION }).asCallback(callback)
132 })
133 })
134 }