Verified Commit a247b425 authored by les's avatar les
Browse files

cleaning last setup step

parent b8931ee8
const URL = require('url')
const log = require('../../log')
const db = require('../models/index.js')
const Umzug = require('umzug')
const path = require('path')
const config = require('../../config')
const Sequelize = require('sequelize')
const settingsController = require('./settings')
const crypto = require('crypto')
const { promisify } = require('util')
const randomBytes = promisify(crypto.randomBytes)
......@@ -26,59 +24,63 @@ const setupController = {
}
try {
const sequelize = await db.connect(dbConf)
const users = await sequelize.query('SELECT * from users').catch(e => {})
config.db = dbConf
if (users && users.length) {
// try to connect
dbConf.logging = false
await db.connect(dbConf)
// is empty ?
const isEmpty = await db.isEmpty()
if (!isEmpty) {
log.warn(' ⚠ Non empty db! Please move your current db elsewhere than retry.')
return res.status(400).send(' ⚠ Non empty db! Please move your current db elsewhere than retry.')
} else {
// run migrations...
const umzug = new Umzug({
storage: 'sequelize',
storageOptions: { sequelize },
logging: log.debug.bind(log),
migrations: {
wrap: fun => {
return () =>
fun(sequelize.queryInterface, Sequelize).catch(e => {
log.error(e)
return false
})
},
path: path.resolve(__dirname, '..', '..', 'migrations')
}
})
await umzug.up()
config.firstrun = false
config.db.logging = false
const settingsController = require('./settings')
await settingsController.load()
return res.sendStatus(200)
}
}
await db.runMigrations()
config.db = dbConf
config.firstrun = false
config.db.logging = false
const settingsController = require('./settings')
await settingsController.load()
return res.sendStatus(200)
} catch (e) {
return res.status(400).send(String(e))
}
},
async restart (req, res) {
config.write()
// create admin user
const password = await randomString()
const email = `admin@${req.settings.hostname}`
const User = require('../models/user')
await User.create({
email,
password,
is_admin: true,
is_active: true
})
try {
res.json({ password, email })
// write configuration
config.write()
// exit process so pm2 || docker could restart me
process.exit()
// calculate default settings values
await settingsController.set('theme.is_dark', true)
await settingsController.set('instance_name', settingsController.settings.title.toLowerCase().replace(/ /g, ''))
await settingsController.set('baseurl', req.protocol + '://' + req.headers.host)
await settingsController.set('hostname', new URL.URL(settingsController.settings.baseurl).hostname)
// create admin
const password = await randomString()
const email = `admin@${settingsController.settings.hostname}`
const User = require('../models/user')
await User.create({
email,
password,
is_admin: true,
is_active: true
})
res.json({ password, email })
// exit process so pm2 || docker could restart me || service
log.info(`Admin: ${email} / password: ${password}`)
log.info('Restart needed')
process.exit()
} catch (e) {
return res.status(400).send(String(e))
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment