]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/installer.ts
Fix lint
[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 () {
f981dae8
C
41 const storages = CONFIG.STORAGE
42 const cacheDirectories = CACHE.DIRECTORIES
37dc07b2 43
6fcd19ba
C
44 const tasks = []
45 Object.keys(storages).forEach(key => {
37dc07b2 46 const dir = storages[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
C
59function createOAuthClientIfNotExist () {
60 return clientsExist().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
C
84function createOAuthAdminIfNotExist () {
85 return usersExist().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
C
114 password,
115 role
67bf9b96 116 }
69b0a27c 117
6fcd19ba 118 return db.User.create(userData, createOptions).then(createdUser => {
26d7d31b
C
119 logger.info('Username: ' + username)
120 logger.info('User password: ' + password)
37dc07b2 121
b769007f 122 logger.info('Creating Application table.')
6fcd19ba 123 return db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
37dc07b2
C
124 })
125 })
126}