diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-15 22:22:03 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-20 09:57:40 +0200 |
commit | 65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch) | |
tree | 4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/initializers/installer.js | |
parent | d5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff) | |
download | PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip |
First typescript iteration
Diffstat (limited to 'server/initializers/installer.js')
-rw-r--r-- | server/initializers/installer.js | 134 |
1 files changed, 0 insertions, 134 deletions
diff --git a/server/initializers/installer.js b/server/initializers/installer.js deleted file mode 100644 index 837a987dd..000000000 --- a/server/initializers/installer.js +++ /dev/null | |||
@@ -1,134 +0,0 @@ | |||
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 | } | ||