]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/installer.ts
Redirect to uuid video route after upload
[github/Chocobozzz/PeerTube.git] / server / initializers / installer.ts
1 import * as passwordGenerator from 'password-generator'
2 import { UserRole } from '../../shared'
3 import { logger, mkdirpPromise, rimrafPromise } from '../helpers'
4 import { createApplicationActor, createUserAccountAndChannel } from '../lib/user'
5 import { UserModel } from '../models/account/user'
6 import { ApplicationModel } from '../models/application/application'
7 import { OAuthClientModel } from '../models/oauth/oauth-client'
8 import { applicationExist, clientsExist, usersExist } from './checker'
9 import { CACHE, CONFIG, LAST_MIGRATION_VERSION } from './constants'
10 import { sequelizeTypescript } from './database'
11
12 async function installApplication () {
13 try {
14 await sequelizeTypescript.sync()
15 await removeCacheDirectories()
16 await createDirectoriesIfNotExist()
17 await createApplicationIfNotExist()
18 await createOAuthClientIfNotExist()
19 await createOAuthAdminIfNotExist()
20 } catch (err) {
21 logger.error('Cannot install application.', err)
22 throw err
23 }
24 }
25
26 // ---------------------------------------------------------------------------
27
28 export {
29 installApplication
30 }
31
32 // ---------------------------------------------------------------------------
33
34 function removeCacheDirectories () {
35 const cacheDirectories = CACHE.DIRECTORIES
36
37 const tasks: Promise<any>[] = []
38
39 // Cache directories
40 for (const key of Object.keys(cacheDirectories)) {
41 const dir = cacheDirectories[key]
42 tasks.push(rimrafPromise(dir))
43 }
44
45 return Promise.all(tasks)
46 }
47
48 function createDirectoriesIfNotExist () {
49 const storage = CONFIG.STORAGE
50 const cacheDirectories = CACHE.DIRECTORIES
51
52 const tasks = []
53 for (const key of Object.keys(storage)) {
54 const dir = storage[key]
55 tasks.push(mkdirpPromise(dir))
56 }
57
58 // Cache directories
59 for (const key of Object.keys(cacheDirectories)) {
60 const dir = cacheDirectories[key]
61 tasks.push(mkdirpPromise(dir))
62 }
63
64 return Promise.all(tasks)
65 }
66
67 async function createOAuthClientIfNotExist () {
68 const exist = await clientsExist()
69 // Nothing to do, clients already exist
70 if (exist === true) return undefined
71
72 logger.info('Creating a default OAuth Client.')
73
74 const id = passwordGenerator(32, false, /[a-z0-9]/)
75 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
76 const client = new OAuthClientModel({
77 clientId: id,
78 clientSecret: secret,
79 grants: [ 'password', 'refresh_token' ],
80 redirectUris: null
81 })
82
83 const createdClient = await client.save()
84 logger.info('Client id: ' + createdClient.clientId)
85 logger.info('Client secret: ' + createdClient.clientSecret)
86
87 return undefined
88 }
89
90 async function createOAuthAdminIfNotExist () {
91 const exist = await usersExist()
92 // Nothing to do, users already exist
93 if (exist === true) return undefined
94
95 logger.info('Creating the administrator.')
96
97 const username = 'root'
98 const role = UserRole.ADMINISTRATOR
99 const email = CONFIG.ADMIN.EMAIL
100 let validatePassword = true
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 validatePassword = false
113 } else {
114 password = passwordGenerator(8, true)
115 }
116
117 const userData = {
118 username,
119 email,
120 password,
121 role,
122 videoQuota: -1
123 }
124 const user = new UserModel(userData)
125
126 await createUserAccountAndChannel(user, validatePassword)
127 logger.info('Username: ' + username)
128 logger.info('User password: ' + password)
129 }
130
131 async function createApplicationIfNotExist () {
132 const exist = await applicationExist()
133 // Nothing to do, application already exist
134 if (exist === true) return undefined
135
136 logger.info('Creating Application table.')
137
138 logger.info('Creating application account.')
139
140 const application = await ApplicationModel.create({
141 migrationVersion: LAST_MIGRATION_VERSION
142 })
143
144 return createApplicationActor(application.id)
145 }