]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/installer.ts
Add playlist rest tests
[github/Chocobozzz/PeerTube.git] / server / initializers / installer.ts
1 import * as passwordGenerator from 'password-generator'
2 import { UserRole } from '../../shared'
3 import { logger } from '../helpers/logger'
4 import { createApplicationActor, createUserAccountAndChannelAndPlaylist } 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-after-init'
9 import { CACHE, CONFIG, HLS_PLAYLIST_DIRECTORY, LAST_MIGRATION_VERSION } from './constants'
10 import { sequelizeTypescript } from './database'
11 import { remove, ensureDir } from 'fs-extra'
12
13 async function installApplication () {
14 try {
15 await Promise.all([
16 // Database related
17 sequelizeTypescript.sync()
18 .then(() => {
19 return Promise.all([
20 createApplicationIfNotExist(),
21 createOAuthClientIfNotExist(),
22 createOAuthAdminIfNotExist()
23 ])
24 }),
25
26 // Directories
27 removeCacheDirectories()
28 .then(() => createDirectoriesIfNotExist())
29 ])
30 } catch (err) {
31 logger.error('Cannot install application.', { err })
32 process.exit(-1)
33 }
34 }
35
36 // ---------------------------------------------------------------------------
37
38 export {
39 installApplication
40 }
41
42 // ---------------------------------------------------------------------------
43
44 function removeCacheDirectories () {
45 const cacheDirectories = Object.keys(CACHE)
46 .map(k => CACHE[k].DIRECTORY)
47
48 const tasks: Promise<any>[] = []
49
50 // Cache directories
51 for (const key of Object.keys(cacheDirectories)) {
52 const dir = cacheDirectories[key]
53 tasks.push(remove(dir))
54 }
55
56 return Promise.all(tasks)
57 }
58
59 function createDirectoriesIfNotExist () {
60 const storage = CONFIG.STORAGE
61 const cacheDirectories = Object.keys(CACHE)
62 .map(k => CACHE[k].DIRECTORY)
63
64 const tasks: Promise<void>[] = []
65 for (const key of Object.keys(storage)) {
66 const dir = storage[key]
67 tasks.push(ensureDir(dir))
68 }
69
70 // Cache directories
71 for (const key of Object.keys(cacheDirectories)) {
72 const dir = cacheDirectories[key]
73 tasks.push(ensureDir(dir))
74 }
75
76 // Playlist directories
77 tasks.push(ensureDir(HLS_PLAYLIST_DIRECTORY))
78
79 return Promise.all(tasks)
80 }
81
82 async function createOAuthClientIfNotExist () {
83 const exist = await clientsExist()
84 // Nothing to do, clients already exist
85 if (exist === true) return undefined
86
87 logger.info('Creating a default OAuth Client.')
88
89 const id = passwordGenerator(32, false, /[a-z0-9]/)
90 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
91 const client = new OAuthClientModel({
92 clientId: id,
93 clientSecret: secret,
94 grants: [ 'password', 'refresh_token' ],
95 redirectUris: null
96 })
97
98 const createdClient = await client.save()
99 logger.info('Client id: ' + createdClient.clientId)
100 logger.info('Client secret: ' + createdClient.clientSecret)
101
102 return undefined
103 }
104
105 async function createOAuthAdminIfNotExist () {
106 const exist = await usersExist()
107 // Nothing to do, users already exist
108 if (exist === true) return undefined
109
110 logger.info('Creating the administrator.')
111
112 const username = 'root'
113 const role = UserRole.ADMINISTRATOR
114 const email = CONFIG.ADMIN.EMAIL
115 let validatePassword = true
116 let password = ''
117
118 // Do not generate a random password for tests
119 if (process.env.NODE_ENV === 'test') {
120 password = 'test'
121
122 if (process.env.NODE_APP_INSTANCE) {
123 password += process.env.NODE_APP_INSTANCE
124 }
125
126 // Our password is weak so do not validate it
127 validatePassword = false
128 } else {
129 password = passwordGenerator(16, true)
130 }
131
132 const userData = {
133 username,
134 email,
135 password,
136 role,
137 verified: true,
138 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
139 videoQuota: -1,
140 videoQuotaDaily: -1
141 }
142 const user = new UserModel(userData)
143
144 await createUserAccountAndChannelAndPlaylist(user, validatePassword)
145 logger.info('Username: ' + username)
146 logger.info('User password: ' + password)
147 }
148
149 async function createApplicationIfNotExist () {
150 const exist = await applicationExist()
151 // Nothing to do, application already exist
152 if (exist === true) return undefined
153
154 logger.info('Creating application account.')
155
156 const application = await ApplicationModel.create({
157 migrationVersion: LAST_MIGRATION_VERSION
158 })
159
160 return createApplicationActor(application.id)
161 }