]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/installer.ts
b997de07f16824c3fdefe56b13036892b7a40184
[github/Chocobozzz/PeerTube.git] / server / initializers / installer.ts
1 import * as passwordGenerator from 'password-generator'
2 import * as Promise from 'bluebird'
3
4 import { database as db } from './database'
5 import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION, CACHE } from './constants'
6 import { clientsExist, usersExist } from './checker'
7 import { logger, createCertsIfNotExist, mkdirpPromise, rimrafPromise } from '../helpers'
8 import { createUserAuthorAndChannel } from '../lib'
9
10 function installApplication () {
11 return db.sequelize.sync()
12 .then(() => removeCacheDirectories())
13 .then(() => createDirectoriesIfNotExist())
14 .then(() => createCertsIfNotExist())
15 .then(() => createOAuthClientIfNotExist())
16 .then(() => createOAuthAdminIfNotExist())
17 }
18
19 // ---------------------------------------------------------------------------
20
21 export {
22 installApplication
23 }
24
25 // ---------------------------------------------------------------------------
26
27 function removeCacheDirectories () {
28 const cacheDirectories = CACHE.DIRECTORIES
29
30 const tasks = []
31
32 // Cache directories
33 Object.keys(cacheDirectories).forEach(key => {
34 const dir = cacheDirectories[key]
35 tasks.push(rimrafPromise(dir))
36 })
37
38 return Promise.all(tasks)
39 }
40
41 function createDirectoriesIfNotExist () {
42 const storage = CONFIG.STORAGE
43 const cacheDirectories = CACHE.DIRECTORIES
44
45 const tasks = []
46 Object.keys(storage).forEach(key => {
47 const dir = storage[key]
48 tasks.push(mkdirpPromise(dir))
49 })
50
51 // Cache directories
52 Object.keys(cacheDirectories).forEach(key => {
53 const dir = cacheDirectories[key]
54 tasks.push(mkdirpPromise(dir))
55 })
56
57 return Promise.all(tasks)
58 }
59
60 function createOAuthClientIfNotExist () {
61 return clientsExist(db.OAuthClient).then(exist => {
62 // Nothing to do, clients already exist
63 if (exist === true) return undefined
64
65 logger.info('Creating a default OAuth Client.')
66
67 const id = passwordGenerator(32, false, /[a-z0-9]/)
68 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
69 const client = db.OAuthClient.build({
70 clientId: id,
71 clientSecret: secret,
72 grants: [ 'password', 'refresh_token' ],
73 redirectUris: null
74 })
75
76 return client.save().then(createdClient => {
77 logger.info('Client id: ' + createdClient.clientId)
78 logger.info('Client secret: ' + createdClient.clientSecret)
79
80 return undefined
81 })
82 })
83 }
84
85 function createOAuthAdminIfNotExist () {
86 return usersExist(db.User).then(exist => {
87 // Nothing to do, users already exist
88 if (exist === true) return undefined
89
90 logger.info('Creating the administrator.')
91
92 const username = 'root'
93 const role = USER_ROLES.ADMIN
94 const email = CONFIG.ADMIN.EMAIL
95 let validatePassword = true
96 let password = ''
97
98 // Do not generate a random password for tests
99 if (process.env.NODE_ENV === 'test') {
100 password = 'test'
101
102 if (process.env.NODE_APP_INSTANCE) {
103 password += process.env.NODE_APP_INSTANCE
104 }
105
106 // Our password is weak so do not validate it
107 validatePassword = false
108 } else {
109 password = passwordGenerator(8, true)
110 }
111
112 const userData = {
113 username,
114 email,
115 password,
116 role,
117 videoQuota: -1
118 }
119 const user = db.User.build(userData)
120
121 return createUserAuthorAndChannel(user, validatePassword)
122 .then(({ user }) => {
123 logger.info('Username: ' + username)
124 logger.info('User password: ' + password)
125
126 logger.info('Creating Application table.')
127 return db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
128 })
129 })
130 }