]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/installer.ts
Set correctly preview image for open graph
[github/Chocobozzz/PeerTube.git] / server / initializers / installer.ts
CommitLineData
65fcc311 1import { join } from 'path'
4d4e5cd4 2import * as config from 'config'
4d4e5cd4 3import * as passwordGenerator from 'password-generator'
6fcd19ba 4import * as Promise from 'bluebird'
37dc07b2 5
e02643f3 6import { database as db } from './database'
f981dae8 7import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION, CACHE } from './constants'
65fcc311 8import { clientsExist, usersExist } from './checker'
f981dae8 9import { logger, createCertsIfNotExist, root, mkdirpPromise, rimrafPromise } from '../helpers'
6fcd19ba
C
10
11function installApplication () {
12 return db.sequelize.sync()
f981dae8 13 .then(() => removeCacheDirectories())
6fcd19ba
C
14 .then(() => createDirectoriesIfNotExist())
15 .then(() => createCertsIfNotExist())
16 .then(() => createOAuthClientIfNotExist())
17 .then(() => createOAuthAdminIfNotExist())
37dc07b2
C
18}
19
20// ---------------------------------------------------------------------------
21
65fcc311
C
22export {
23 installApplication
24}
37dc07b2
C
25
26// ---------------------------------------------------------------------------
27
f981dae8
C
28function removeCacheDirectories () {
29 const cacheDirectories = CACHE.DIRECTORIES
30
31 const tasks = []
32
33 // Cache directories
34 Object.keys(cacheDirectories).forEach(key => {
35 const dir = cacheDirectories[key]
36 tasks.push(rimrafPromise(dir))
37 })
38
39 return Promise.all(tasks)
40}
41
6fcd19ba 42function createDirectoriesIfNotExist () {
f981dae8
C
43 const storages = CONFIG.STORAGE
44 const cacheDirectories = CACHE.DIRECTORIES
37dc07b2 45
6fcd19ba
C
46 const tasks = []
47 Object.keys(storages).forEach(key => {
37dc07b2 48 const dir = storages[key]
f981dae8
C
49 tasks.push(mkdirpPromise(dir))
50 })
51
52 // Cache directories
53 Object.keys(cacheDirectories).forEach(key => {
54 const dir = cacheDirectories[key]
55 tasks.push(mkdirpPromise(dir))
6fcd19ba 56 })
37dc07b2 57
6fcd19ba
C
58 return Promise.all(tasks)
59}
37dc07b2 60
6fcd19ba
C
61function createOAuthClientIfNotExist () {
62 return clientsExist().then(exist => {
37dc07b2 63 // Nothing to do, clients already exist
6fcd19ba 64 if (exist === true) return undefined
37dc07b2
C
65
66 logger.info('Creating a default OAuth Client.')
67
feb4bdfd
C
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,
69b0a27c 72 clientSecret: secret,
e02643f3
C
73 grants: [ 'password', 'refresh_token' ],
74 redirectUris: null
69b0a27c
C
75 })
76
6fcd19ba 77 return client.save().then(createdClient => {
feb4bdfd 78 logger.info('Client id: ' + createdClient.clientId)
69b0a27c 79 logger.info('Client secret: ' + createdClient.clientSecret)
37dc07b2 80
6fcd19ba 81 return undefined
37dc07b2
C
82 })
83 })
84}
85
6fcd19ba
C
86function createOAuthAdminIfNotExist () {
87 return usersExist().then(exist => {
37dc07b2 88 // Nothing to do, users already exist
6fcd19ba 89 if (exist === true) return undefined
37dc07b2
C
90
91 logger.info('Creating the administrator.')
92
bb1e6d0c 93 const username = 'root'
65fcc311
C
94 const role = USER_ROLES.ADMIN
95 const email = CONFIG.ADMIN.EMAIL
96 const createOptions: { validate?: boolean } = {}
d14b3e37
C
97 let password = ''
98
99 // Do not generate a random password for tests
100 if (process.env.NODE_ENV === 'test') {
101 password = 'test'
102
103 if (process.env.NODE_APP_INSTANCE) {
104 password += process.env.NODE_APP_INSTANCE
105 }
67bf9b96
C
106
107 // Our password is weak so do not validate it
108 createOptions.validate = false
d14b3e37
C
109 } else {
110 password = passwordGenerator(8, true)
111 }
37dc07b2 112
67bf9b96 113 const userData = {
c4403b29 114 username,
ad4a8a1c 115 email,
c4403b29
C
116 password,
117 role
67bf9b96 118 }
69b0a27c 119
6fcd19ba 120 return db.User.create(userData, createOptions).then(createdUser => {
26d7d31b
C
121 logger.info('Username: ' + username)
122 logger.info('User password: ' + password)
37dc07b2 123
b769007f 124 logger.info('Creating Application table.')
6fcd19ba 125 return db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
37dc07b2
C
126 })
127 })
128}