[improve] do not ask useless info during docker install

This commit is contained in:
les
2019-09-25 19:07:15 +02:00
parent 297e070801
commit be20ac53de
3 changed files with 116 additions and 92 deletions

View File

@@ -5,10 +5,17 @@ const basename = path.basename(__filename)
const config = require('config') const config = require('config')
const consola = require('consola') const consola = require('consola')
const db = {} const db = {}
let sequelize = null
try {
sequelize = new Sequelize(config.db)
} catch(e) {
consola.warn(` ⚠️ Cannot connect to db, check your configuration => ${e}`)
process.exit(-1)
}
const sequelize = new Sequelize(config.db)
sequelize.authenticate().catch(e => { sequelize.authenticate().catch(e => {
consola.error('Error connecting to DB: ', String(e)) consola.error('Error connecting to DB: ', String(e))
process.exit(-1) process.exit(-1)
}) })

View File

@@ -9,6 +9,7 @@ const package = require('../package.json')
const firstrun = require('./firstrun') const firstrun = require('./firstrun')
const path = require('path') const path = require('path')
const mkdirp = require('mkdirp') const mkdirp = require('mkdirp')
const url = require('url')
const cwd = process.cwd() const cwd = process.cwd()
@@ -39,106 +40,105 @@ async function setupQuestionnaire(is_docker, db) {
} }
}) })
questions.push({ if (!is_docker) {
name: 'server.host', questions.push({
message: 'address to listen to', name: 'server.host',
default: is_docker ? '0.0.0.0' : 'localhost', message: 'address to listen to',
validate: notEmpty default: 'localhost',
}) validate: notEmpty
})
questions.push({ questions.push({
name: 'server.port', name: 'server.port',
message: 'port to listen to', message: 'port to listen to',
default: 13120, default: 13120,
}) })
questions.push({ questions.push({
name: 'db.dialect', name: 'db.dialect',
message: 'DB dialect', message: 'DB dialect',
type: 'list', type: 'list',
when: answers => !db, choices: ['sqlite', 'postgres']
choices: ['sqlite', 'postgres'] })
})
questions.push({ questions.push({
name: 'db.storage', name: 'db.storage',
message: 'sqlite db path', message: 'sqlite db path',
default: is_docker ? '/opt/gancio/db.sqlite' : './db.sqlite', default: './db.sqlite',
filter: p => path.resolve(cwd, p), filter: p => path.resolve(cwd, p),
when: answers => ((db && db==='sqlite') || (answers.db && answers.db.dialect === 'sqlite')) && !is_docker, when: answers => answers.db.dialect === 'sqlite',
validate: db_path => db_path.length>0 && fs.existsSync(path.dirname(db_path)) validate: db_path => db_path.length>0 && fs.existsSync(path.dirname(db_path))
}) })
questions.push({ questions.push({
name: 'db.host', name: 'db.host',
message: 'Postgres host', message: 'Postgres host',
default: is_docker ? 'db' : 'localhost', default: 'localhost',
when: answers => ((db && db==='postgresql') || (answers.db && answers.db.dialect === 'postgres')) && !is_docker, when: answers => answers.db.dialect === 'postgres',
validate: notEmpty validate: notEmpty
}) })
questions.push({ questions.push({
name: 'db.database', name: 'db.database',
message: 'DB name', message: 'DB name',
default: 'gancio', default: 'gancio',
when: answers => (db && db==='postgresql') || (answers.db && answers.db.dialect === 'postgres'), when: answers => answers.db.dialect === 'postgres',
validate: notEmpty validate: notEmpty
}) })
questions.push({
name: 'db.username',
message: 'DB user',
default: 'gancio',
when: answers => answers.db.dialect === 'postgres',
validate: notEmpty
})
questions.push({ questions.push({
name: 'db.username', name: 'db.password',
message: 'DB user', type: 'password',
default: 'gancio', message: 'DB password',
when: answers => (db && db==='postgresql') || (answers.db && answers.db.dialect === 'postgres'), default: 'gancio',
validate: notEmpty when: answers => answers.db.dialect === 'postgres',
}) validate: async (password, options) => {
questions.push({
name: 'db.password',
type: 'password',
message: 'DB password',
default: 'gancio',
when: answers => (db && db==='postgresql') || (answers.db && answers.db.dialect === 'postgres'),
validate: async (password, options) => {
try {
const db = new sequelize({ ...options.db, dialect: 'postgres' , password, logging: false })
return db.authenticate().then( () => {
db.close()
return true
})
} catch(e) {
consola.error(e)
return false
}
}
})
questions.push({
name: 'upload_path',
message: 'Where gancio has to store media?',
default: is_docker ? '/opt/gancio/uploads' : './uploads',
when: answers => !is_docker,
filter: p => path.resolve(cwd, p),
validate: async p => {
let exists = fs.existsSync(p)
if (!exists) {
consola.warn(`"${p}" does not exists, trying to create it`)
try { try {
mkdirp.sync(p) const db = new sequelize({ ...options.db, dialect: 'postgres' , password, logging: false })
return db.authenticate().then( () => {
db.close()
return true
})
} catch(e) { } catch(e) {
console.error(String(e)) consola.error(e)
return false return false
} }
} }
return true })
}
})
questions.push({
name: 'upload_path',
message: 'Where gancio has to store media?',
default: './uploads',
filter: p => path.resolve(cwd, p),
validate: async p => {
let exists = fs.existsSync(p)
if (!exists) {
consola.warn(`"${p}" does not exists, trying to create it`)
try {
mkdirp.sync(p)
} catch(e) {
console.error(String(e))
return false
}
}
return true
}
})
}
questions.push({ questions.push({
name: 'admin.email', name: 'admin.email',
message: `Admin email (a first user with this username will be created)`, message: `Admin email (a first user with this username will be created)`,
default: options => { default: options => {
return options.title.replace(' ', '').toLowerCase() + '@' + options.baseurl return options.title.replace(' ', '').toLowerCase() + '@' + url.parse(options.baseurl, true).hostname
}, },
validate: notEmpty validate: notEmpty
}) })
@@ -160,7 +160,7 @@ async function setupQuestionnaire(is_docker, db) {
name: 'smtp.auth.user', name: 'smtp.auth.user',
message: 'SMTP User', message: 'SMTP User',
validate: notEmpty, validate: notEmpty,
default: options => options.admin.email default: answers => answers.admin.email
}) })
questions.push({ questions.push({
@@ -171,6 +171,17 @@ async function setupQuestionnaire(is_docker, db) {
}) })
const answers = await inquirer.prompt(questions) const answers = await inquirer.prompt(questions)
if (is_docker) {
answers.server = { host: '0.0.0.0', port: 13120 }
answers.upload_path = '/opt/gancio/uploads'
if(db === 'sqlite') {
answers.db = { dialect: db, storage: '/opt/gancio/db.sqlite' }
} else {
answers.db = { dialect: db, host: 'db', database: 'gancio',
username: 'gancio', password: 'gancio'}
}
}
return answers return answers
} }
@@ -198,10 +209,10 @@ async function start (options) {
// is first run? // is first run?
if (firstrun.check(options.config)) { if (firstrun.check(options.config)) {
if (options.docker) { if (options.docker) {
consola.error('Something goes wrong, did you run "docker-compose run --rm gancio gancio setup"') consola.error(' Something goes wrong, did you run "docker-compose run --rm gancio gancio setup"')
process.exit(-1) process.exit(-1)
} }
consola.error(`Configuration file "${options.config}" not found! Use "--config <CONFIG_FILE.json>" to specify another path. consola.error(`Configuration file "${options.config}" not found! Use "--config <CONFIG_FILE.json>" to specify another path.
If this is your first run use 'gancio setup --config <CONFIG_FILE.json>' `) If this is your first run use 'gancio setup --config <CONFIG_FILE.json>' `)
process.exit(-1) process.exit(-1)
} }

View File

@@ -19,20 +19,24 @@ module.exports = {
config.admin_email = admin.email config.admin_email = admin.email
config.db.logging = false config.db.logging = false
consola.info(`Save configuration to ${config_path}`) consola.info(`Save configuration to ${config_path}`)
fs.writeFileSync(config_path, JSON.stringify(config, null, 2)) try {
fs.writeFileSync(config_path, JSON.stringify(config, null, 2))
} catch(e) {
consola.warn(` ⚠️ ${e}. You can specify configuration path using '--config'`)
}
// sync db // sync db
const db = require('./api/models') const db = require('./api/models')
try { try {
await db.user.findAll() await db.user.findAll()
consola.warn(`⚠️ Non empty db! Please move your current db elsewhere than retry.`) consola.warn(` Non empty db! Please move your current db elsewhere than retry.`)
return false return false
} catch (e) { } } catch (e) { }
consola.info(`Create tables schema`) consola.info(`Create tables schema`)
await db.sequelize.sync().catch(e => { await db.sequelize.sync().catch(e => {
consola.error('Error creating tables', e) consola.error(' Error creating tables', e)
return false return false
}) })
@@ -68,5 +72,7 @@ module.exports = {
// close db connection // close db connection
await db.sequelize.close() await db.sequelize.close()
return true
} }
} }