aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/installer.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-05-15 22:22:03 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-05-20 09:57:40 +0200
commit65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch)
tree4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/initializers/installer.ts
parentd5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff)
downloadPeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip
First typescript iteration
Diffstat (limited to 'server/initializers/installer.ts')
-rw-r--r--server/initializers/installer.ts128
1 files changed, 128 insertions, 0 deletions
diff --git a/server/initializers/installer.ts b/server/initializers/installer.ts
new file mode 100644
index 000000000..cd1404d48
--- /dev/null
+++ b/server/initializers/installer.ts
@@ -0,0 +1,128 @@
1import { join } from 'path'
2import config = require('config')
3import { each, series } from 'async'
4import mkdirp = require('mkdirp')
5import passwordGenerator = require('password-generator')
6
7const db = require('./database')
8import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION } from './constants'
9import { clientsExist, usersExist } from './checker'
10import { logger, createCertsIfNotExist } from '../helpers'
11
12function installApplication (callback) {
13 series([
14 function createDatabase (callbackAsync) {
15 db.sequelize.sync().asCallback(callbackAsync)
16 // db.sequelize.sync({ force: true }).asCallback(callbackAsync)
17 },
18
19 function createDirectories (callbackAsync) {
20 createDirectoriesIfNotExist(callbackAsync)
21 },
22
23 function createCertificates (callbackAsync) {
24 createCertsIfNotExist(callbackAsync)
25 },
26
27 function createOAuthClient (callbackAsync) {
28 createOAuthClientIfNotExist(callbackAsync)
29 },
30
31 function createOAuthUser (callbackAsync) {
32 createOAuthAdminIfNotExist(callbackAsync)
33 }
34 ], callback)
35}
36
37// ---------------------------------------------------------------------------
38
39export {
40 installApplication
41}
42
43// ---------------------------------------------------------------------------
44
45function createDirectoriesIfNotExist (callback) {
46 const storages = config.get('storage')
47
48 each(Object.keys(storages), function (key, callbackEach) {
49 const dir = storages[key]
50 mkdirp(join(__dirname, '..', '..', dir), callbackEach)
51 }, callback)
52}
53
54function createOAuthClientIfNotExist (callback) {
55 clientsExist(function (err, exist) {
56 if (err) return callback(err)
57
58 // Nothing to do, clients already exist
59 if (exist === true) return callback(null)
60
61 logger.info('Creating a default OAuth Client.')
62
63 const id = passwordGenerator(32, false, /[a-z0-9]/)
64 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
65 const client = db.OAuthClient.build({
66 clientId: id,
67 clientSecret: secret,
68 grants: [ 'password', 'refresh_token' ]
69 })
70
71 client.save().asCallback(function (err, createdClient) {
72 if (err) return callback(err)
73
74 logger.info('Client id: ' + createdClient.clientId)
75 logger.info('Client secret: ' + createdClient.clientSecret)
76
77 return callback(null)
78 })
79 })
80}
81
82function createOAuthAdminIfNotExist (callback) {
83 usersExist(function (err, exist) {
84 if (err) return callback(err)
85
86 // Nothing to do, users already exist
87 if (exist === true) return callback(null)
88
89 logger.info('Creating the administrator.')
90
91 const username = 'root'
92 const role = USER_ROLES.ADMIN
93 const email = CONFIG.ADMIN.EMAIL
94 const createOptions: { validate?: boolean } = {}
95 let password = ''
96
97 // Do not generate a random password for tests
98 if (process.env.NODE_ENV === 'test') {
99 password = 'test'
100
101 if (process.env.NODE_APP_INSTANCE) {
102 password += process.env.NODE_APP_INSTANCE
103 }
104
105 // Our password is weak so do not validate it
106 createOptions.validate = false
107 } else {
108 password = passwordGenerator(8, true)
109 }
110
111 const userData = {
112 username,
113 email,
114 password,
115 role
116 }
117
118 db.User.create(userData, createOptions).asCallback(function (err, createdUser) {
119 if (err) return callback(err)
120
121 logger.info('Username: ' + username)
122 logger.info('User password: ' + password)
123
124 logger.info('Creating Application table.')
125 db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION }).asCallback(callback)
126 })
127 })
128}