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