]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/installer.ts
Add ability to forbid user to upload video
[github/Chocobozzz/PeerTube.git] / server / initializers / installer.ts
CommitLineData
4d4e5cd4 1import * as passwordGenerator from 'password-generator'
6fcd19ba 2import * as Promise from 'bluebird'
37dc07b2 3
e02643f3 4import { database as db } from './database'
f981dae8 5import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION, CACHE } from './constants'
65fcc311 6import { clientsExist, usersExist } from './checker'
0b7db72a 7import { logger, createCertsIfNotExist, mkdirpPromise, rimrafPromise } from '../helpers'
6fcd19ba
C
8
9function installApplication () {
10 return db.sequelize.sync()
f981dae8 11 .then(() => removeCacheDirectories())
6fcd19ba
C
12 .then(() => createDirectoriesIfNotExist())
13 .then(() => createCertsIfNotExist())
14 .then(() => createOAuthClientIfNotExist())
15 .then(() => createOAuthAdminIfNotExist())
37dc07b2
C
16}
17
18// ---------------------------------------------------------------------------
19
65fcc311
C
20export {
21 installApplication
22}
37dc07b2
C
23
24// ---------------------------------------------------------------------------
25
f981dae8
C
26function 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
6fcd19ba 40function createDirectoriesIfNotExist () {
b0f9f39e 41 const storage = CONFIG.STORAGE
f981dae8 42 const cacheDirectories = CACHE.DIRECTORIES
37dc07b2 43
6fcd19ba 44 const tasks = []
b0f9f39e
C
45 Object.keys(storage).forEach(key => {
46 const dir = storage[key]
f981dae8
C
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))
6fcd19ba 54 })
37dc07b2 55
6fcd19ba
C
56 return Promise.all(tasks)
57}
37dc07b2 58
6fcd19ba 59function createOAuthClientIfNotExist () {
3482688c 60 return clientsExist(db.OAuthClient).then(exist => {
37dc07b2 61 // Nothing to do, clients already exist
6fcd19ba 62 if (exist === true) return undefined
37dc07b2
C
63
64 logger.info('Creating a default OAuth Client.')
65
feb4bdfd
C
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,
69b0a27c 70 clientSecret: secret,
e02643f3
C
71 grants: [ 'password', 'refresh_token' ],
72 redirectUris: null
69b0a27c
C
73 })
74
6fcd19ba 75 return client.save().then(createdClient => {
feb4bdfd 76 logger.info('Client id: ' + createdClient.clientId)
69b0a27c 77 logger.info('Client secret: ' + createdClient.clientSecret)
37dc07b2 78
6fcd19ba 79 return undefined
37dc07b2
C
80 })
81 })
82}
83
6fcd19ba 84function createOAuthAdminIfNotExist () {
3482688c 85 return usersExist(db.User).then(exist => {
37dc07b2 86 // Nothing to do, users already exist
6fcd19ba 87 if (exist === true) return undefined
37dc07b2
C
88
89 logger.info('Creating the administrator.')
90
bb1e6d0c 91 const username = 'root'
65fcc311
C
92 const role = USER_ROLES.ADMIN
93 const email = CONFIG.ADMIN.EMAIL
94 const createOptions: { validate?: boolean } = {}
d14b3e37
C
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 }
67bf9b96
C
104
105 // Our password is weak so do not validate it
106 createOptions.validate = false
d14b3e37
C
107 } else {
108 password = passwordGenerator(8, true)
109 }
37dc07b2 110
67bf9b96 111 const userData = {
c4403b29 112 username,
ad4a8a1c 113 email,
c4403b29 114 password,
b0f9f39e
C
115 role,
116 videoQuota: -1
67bf9b96 117 }
69b0a27c 118
6fcd19ba 119 return db.User.create(userData, createOptions).then(createdUser => {
26d7d31b
C
120 logger.info('Username: ' + username)
121 logger.info('User password: ' + password)
37dc07b2 122
b769007f 123 logger.info('Creating Application table.')
6fcd19ba 124 return db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
37dc07b2
C
125 })
126 })
127}