]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/installer.ts
Change how we handle resolution
[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
9 function installApplication () {
10 return db.sequelize.sync()
11 .then(() => removeCacheDirectories())
12 .then(() => createDirectoriesIfNotExist())
13 .then(() => createCertsIfNotExist())
14 .then(() => createOAuthClientIfNotExist())
15 .then(() => createOAuthAdminIfNotExist())
16 }
17
18 // ---------------------------------------------------------------------------
19
20 export {
21 installApplication
22 }
23
24 // ---------------------------------------------------------------------------
25
26 function removeCacheDirectories () {
27 const cacheDirectories = CACHE.DIRECTORIES
28
29 const tasks = []
30
31 // Cache directories
32 Object.keys(cacheDirectories).forEach(key => {
33 const dir = cacheDirectories[key]
34 tasks.push(rimrafPromise(dir))
35 })
36
37 return Promise.all(tasks)
38 }
39
40 function createDirectoriesIfNotExist () {
41 const storage = CONFIG.STORAGE
42 const cacheDirectories = CACHE.DIRECTORIES
43
44 const tasks = []
45 Object.keys(storage).forEach(key => {
46 const dir = storage[key]
47 tasks.push(mkdirpPromise(dir))
48 })
49
50 // Cache directories
51 Object.keys(cacheDirectories).forEach(key => {
52 const dir = cacheDirectories[key]
53 tasks.push(mkdirpPromise(dir))
54 })
55
56 return Promise.all(tasks)
57 }
58
59 function createOAuthClientIfNotExist () {
60 return clientsExist(db.OAuthClient).then(exist => {
61 // Nothing to do, clients already exist
62 if (exist === true) return undefined
63
64 logger.info('Creating a default OAuth Client.')
65
66 const id = passwordGenerator(32, false, /[a-z0-9]/)
67 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
68 const client = db.OAuthClient.build({
69 clientId: id,
70 clientSecret: secret,
71 grants: [ 'password', 'refresh_token' ],
72 redirectUris: null
73 })
74
75 return client.save().then(createdClient => {
76 logger.info('Client id: ' + createdClient.clientId)
77 logger.info('Client secret: ' + createdClient.clientSecret)
78
79 return undefined
80 })
81 })
82 }
83
84 function createOAuthAdminIfNotExist () {
85 return usersExist(db.User).then(exist => {
86 // Nothing to do, users already exist
87 if (exist === true) return undefined
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 videoQuota: -1
117 }
118
119 return db.User.create(userData, createOptions).then(createdUser => {
120 logger.info('Username: ' + username)
121 logger.info('User password: ' + password)
122
123 logger.info('Creating Application table.')
124 return db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
125 })
126 })
127 }