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