]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/initializers/installer.js
Server: add webserver url log at startup
[github/Chocobozzz/PeerTube.git] / server / initializers / installer.js
index e0ae822cfef2936253db786733b222e9efba27e5..1df300ba8d467fb76d3a7b5647fe1efc76559294 100644 (file)
@@ -1,25 +1,28 @@
 'use strict'
 
-const async = require('async')
 const config = require('config')
+const each = require('async/each')
 const mkdirp = require('mkdirp')
 const mongoose = require('mongoose')
 const passwordGenerator = require('password-generator')
 const path = require('path')
+const series = require('async/series')
 
 const checker = require('./checker')
+const constants = require('./constants')
 const logger = require('../helpers/logger')
-const peertubeCrypto = require('../helpers/peertubeCrypto')
+const peertubeCrypto = require('../helpers/peertube-crypto')
 
+const Application = mongoose.model('Application')
 const Client = mongoose.model('OAuthClient')
 const User = mongoose.model('User')
 
 const installer = {
-  installApplication: installApplication
+  installApplication
 }
 
 function installApplication (callback) {
-  async.series([
+  series([
     function createDirectories (callbackAsync) {
       createDirectoriesIfNotExist(callbackAsync)
     },
@@ -33,7 +36,7 @@ function installApplication (callback) {
     },
 
     function createOAuthUser (callbackAsync) {
-      createOAuthUserIfNotExist(callbackAsync)
+      createOAuthAdminIfNotExist(callbackAsync)
     }
   ], callback)
 }
@@ -47,7 +50,7 @@ module.exports = installer
 function createDirectoriesIfNotExist (callback) {
   const storages = config.get('storage')
 
-  async.each(Object.keys(storages), function (key, callbackEach) {
+  each(Object.keys(storages), function (key, callbackEach) {
     const dir = storages[key]
     mkdirp(path.join(__dirname, '..', '..', dir), callbackEach)
   }, callback)
@@ -65,7 +68,7 @@ function createOAuthClientIfNotExist (callback) {
     const secret = passwordGenerator(32, false)
     const client = new Client({
       clientSecret: secret,
-      grants: [ 'password' ]
+      grants: [ 'password', 'refresh_token' ]
     })
 
     client.save(function (err, createdClient) {
@@ -79,7 +82,7 @@ function createOAuthClientIfNotExist (callback) {
   })
 }
 
-function createOAuthUserIfNotExist (callback) {
+function createOAuthAdminIfNotExist (callback) {
   checker.usersExist(function (err, exist) {
     if (err) return callback(err)
 
@@ -89,6 +92,7 @@ function createOAuthUserIfNotExist (callback) {
     logger.info('Creating the administrator.')
 
     const username = 'root'
+    const role = constants.USER_ROLES.ADMIN
     let password = ''
 
     // Do not generate a random password for tests
@@ -103,17 +107,20 @@ function createOAuthUserIfNotExist (callback) {
     }
 
     const user = new User({
-      username: username,
-      password: password
+      username,
+      password,
+      role
     })
 
     user.save(function (err, createdUser) {
       if (err) return callback(err)
 
-      logger.info('Username: ' + createdUser.username)
-      logger.info('User password: ' + createdUser.password)
+      logger.info('Username: ' + username)
+      logger.info('User password: ' + password)
 
-      return callback(null)
+      logger.info('Creating Application collection.')
+      const application = new Application({ mongoSchemaVersion: constants.LAST_MONGO_SCHEMA_VERSION })
+      application.save(callback)
     })
   })
 }