aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/installer.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-10-25 16:03:33 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-10-26 09:11:38 +0200
commitf5028693a896a3076dd286ac0030e3d8f78f5ebf (patch)
tree09144ed6357e49ea575fb110247f933283ad235e /server/initializers/installer.ts
parenteb08047657e739bcd9e592d76307befa3998482b (diff)
downloadPeerTube-f5028693a896a3076dd286ac0030e3d8f78f5ebf.tar.gz
PeerTube-f5028693a896a3076dd286ac0030e3d8f78f5ebf.tar.zst
PeerTube-f5028693a896a3076dd286ac0030e3d8f78f5ebf.zip
Use async/await in lib and initializers
Diffstat (limited to 'server/initializers/installer.ts')
-rw-r--r--server/initializers/installer.ts141
1 files changed, 68 insertions, 73 deletions
diff --git a/server/initializers/installer.ts b/server/initializers/installer.ts
index b997de07f..4c04290fc 100644
--- a/server/initializers/installer.ts
+++ b/server/initializers/installer.ts
@@ -1,5 +1,5 @@
1import * as passwordGenerator from 'password-generator' 1import * as passwordGenerator from 'password-generator'
2import * as Promise from 'bluebird' 2import * as Bluebird from 'bluebird'
3 3
4import { database as db } from './database' 4import { database as db } from './database'
5import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION, CACHE } from './constants' 5import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION, CACHE } from './constants'
@@ -7,13 +7,13 @@ import { clientsExist, usersExist } from './checker'
7import { logger, createCertsIfNotExist, mkdirpPromise, rimrafPromise } from '../helpers' 7import { logger, createCertsIfNotExist, mkdirpPromise, rimrafPromise } from '../helpers'
8import { createUserAuthorAndChannel } from '../lib' 8import { createUserAuthorAndChannel } from '../lib'
9 9
10function installApplication () { 10async function installApplication () {
11 return db.sequelize.sync() 11 await db.sequelize.sync()
12 .then(() => removeCacheDirectories()) 12 await removeCacheDirectories()
13 .then(() => createDirectoriesIfNotExist()) 13 await createDirectoriesIfNotExist()
14 .then(() => createCertsIfNotExist()) 14 await createCertsIfNotExist()
15 .then(() => createOAuthClientIfNotExist()) 15 await createOAuthClientIfNotExist()
16 .then(() => createOAuthAdminIfNotExist()) 16 await createOAuthAdminIfNotExist()
17} 17}
18 18
19// --------------------------------------------------------------------------- 19// ---------------------------------------------------------------------------
@@ -27,13 +27,13 @@ export {
27function removeCacheDirectories () { 27function removeCacheDirectories () {
28 const cacheDirectories = CACHE.DIRECTORIES 28 const cacheDirectories = CACHE.DIRECTORIES
29 29
30 const tasks = [] 30 const tasks: Bluebird<any>[] = []
31 31
32 // Cache directories 32 // Cache directories
33 Object.keys(cacheDirectories).forEach(key => { 33 for (const key of Object.keys(cacheDirectories)) {
34 const dir = cacheDirectories[key] 34 const dir = cacheDirectories[key]
35 tasks.push(rimrafPromise(dir)) 35 tasks.push(rimrafPromise(dir))
36 }) 36 }
37 37
38 return Promise.all(tasks) 38 return Promise.all(tasks)
39} 39}
@@ -43,88 +43,83 @@ function createDirectoriesIfNotExist () {
43 const cacheDirectories = CACHE.DIRECTORIES 43 const cacheDirectories = CACHE.DIRECTORIES
44 44
45 const tasks = [] 45 const tasks = []
46 Object.keys(storage).forEach(key => { 46 for (const key of Object.keys(storage)) {
47 const dir = storage[key] 47 const dir = storage[key]
48 tasks.push(mkdirpPromise(dir)) 48 tasks.push(mkdirpPromise(dir))
49 }) 49 }
50 50
51 // Cache directories 51 // Cache directories
52 Object.keys(cacheDirectories).forEach(key => { 52 for (const key of Object.keys(cacheDirectories)) {
53 const dir = cacheDirectories[key] 53 const dir = cacheDirectories[key]
54 tasks.push(mkdirpPromise(dir)) 54 tasks.push(mkdirpPromise(dir))
55 }) 55 }
56 56
57 return Promise.all(tasks) 57 return Promise.all(tasks)
58} 58}
59 59
60function createOAuthClientIfNotExist () { 60async function createOAuthClientIfNotExist () {
61 return clientsExist(db.OAuthClient).then(exist => { 61 const exist = await clientsExist(db.OAuthClient)
62 // Nothing to do, clients already exist 62 // Nothing to do, clients already exist
63 if (exist === true) return undefined 63 if (exist === true) return undefined
64
65 logger.info('Creating a default OAuth Client.')
66
67 const id = passwordGenerator(32, false, /[a-z0-9]/)
68 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
69 const client = db.OAuthClient.build({
70 clientId: id,
71 clientSecret: secret,
72 grants: [ 'password', 'refresh_token' ],
73 redirectUris: null
74 })
75 64
76 return client.save().then(createdClient => { 65 logger.info('Creating a default OAuth Client.')
77 logger.info('Client id: ' + createdClient.clientId)
78 logger.info('Client secret: ' + createdClient.clientSecret)
79 66
80 return undefined 67 const id = passwordGenerator(32, false, /[a-z0-9]/)
81 }) 68 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
69 const client = db.OAuthClient.build({
70 clientId: id,
71 clientSecret: secret,
72 grants: [ 'password', 'refresh_token' ],
73 redirectUris: null
82 }) 74 })
83}
84 75
85function createOAuthAdminIfNotExist () { 76 const createdClient = await client.save()
86 return usersExist(db.User).then(exist => { 77 logger.info('Client id: ' + createdClient.clientId)
87 // Nothing to do, users already exist 78 logger.info('Client secret: ' + createdClient.clientSecret)
88 if (exist === true) return undefined
89 79
90 logger.info('Creating the administrator.') 80 return undefined
81}
91 82
92 const username = 'root' 83async function createOAuthAdminIfNotExist () {
93 const role = USER_ROLES.ADMIN 84 const exist = await usersExist(db.User)
94 const email = CONFIG.ADMIN.EMAIL 85 // Nothing to do, users already exist
95 let validatePassword = true 86 if (exist === true) return undefined
96 let password = ''
97 87
98 // Do not generate a random password for tests 88 logger.info('Creating the administrator.')
99 if (process.env.NODE_ENV === 'test') {
100 password = 'test'
101 89
102 if (process.env.NODE_APP_INSTANCE) { 90 const username = 'root'
103 password += process.env.NODE_APP_INSTANCE 91 const role = USER_ROLES.ADMIN
104 } 92 const email = CONFIG.ADMIN.EMAIL
93 let validatePassword = true
94 let password = ''
105 95
106 // Our password is weak so do not validate it 96 // Do not generate a random password for tests
107 validatePassword = false 97 if (process.env.NODE_ENV === 'test') {
108 } else { 98 password = 'test'
109 password = passwordGenerator(8, true)
110 }
111 99
112 const userData = { 100 if (process.env.NODE_APP_INSTANCE) {
113 username, 101 password += process.env.NODE_APP_INSTANCE
114 email,
115 password,
116 role,
117 videoQuota: -1
118 } 102 }
119 const user = db.User.build(userData)
120 103
121 return createUserAuthorAndChannel(user, validatePassword) 104 // Our password is weak so do not validate it
122 .then(({ user }) => { 105 validatePassword = false
123 logger.info('Username: ' + username) 106 } else {
124 logger.info('User password: ' + password) 107 password = passwordGenerator(8, true)
125 108 }
126 logger.info('Creating Application table.') 109
127 return db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION }) 110 const userData = {
128 }) 111 username,
129 }) 112 email,
113 password,
114 role,
115 videoQuota: -1
116 }
117 const user = db.User.build(userData)
118
119 await createUserAuthorAndChannel(user, validatePassword)
120 logger.info('Username: ' + username)
121 logger.info('User password: ' + password)
122
123 logger.info('Creating Application table.')
124 await db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
130} 125}