Verified Commit 97c19671 authored by Aral Balkan's avatar Aral Balkan
Browse files

Closes #36: status command now displays daemon details if available

parent f36d824a
......@@ -11,6 +11,7 @@
const Site = require('../../index')
const getStatus = require('../lib/status')
const clr = require('../../lib/clr')
const crossPlatformHostname = require('@small-tech/cross-platform-hostname')
const ensure = require('../lib/ensure')
function status () {
......@@ -20,14 +21,36 @@ function status () {
// We cannot check in the function itself as it would create
// a circular dependency.
ensure.systemctl()
const { isActive, isEnabled } = getStatus()
const { isActive, isEnabled, daemonDetails } = getStatus()
const activeState = isActive ? clr('active', 'green') : clr('inactive', 'red')
const enabledState = isEnabled ? clr('enabled', 'green') : clr('disabled', 'red')
const stateEmoji = (isActive && isEnabled) ? '💡' : '🛑'
console.log(` ${stateEmoji} ❨site.js❩ Server is ${activeState} and ${enabledState}.\n`)
console.log(` ${stateEmoji} ❨site.js❩ Server is ${activeState} and ${enabledState}.`)
if (daemonDetails !== null) {
const textColour = isActive ? 'green' : 'red'
console.log(`\n Path : ${clr(daemonDetails.pathBeingServed, textColour)}`)
console.log(` Domain : ${clr(daemonDetails.optionalOptions.domain, 'yellow') || clr(crossPlatformHostname, daemonDetails.optionalOptions.skipDomainReachabilityCheck && isActive ? 'yellow' : textColour)}`)
console.log(` Account: ${clr(daemonDetails.account, textColour)}`)
if (daemonDetails.optionalOptions.aliases !== null) {
const aliasesString = daemonDetails.optionalOptions.aliases.reduce(
(str, alias) => `${str}${clr(alias, textColour)}, `,
''
)
console.log(` Aliases: ${aliasesString.replace(/, $/, '')}`)
}
if (daemonDetails.optionalOptions.skipDomainReachabilityCheck) {
console.log(`\n ${clr('Domain reachability pre-flight check is disabled.', 'yellow')}`)
}
}
console.log('')
}
module.exports = status
......@@ -8,6 +8,9 @@
//
//////////////////////////////////////////////////////////////////////
const os = require('os')
const fs = require('fs')
const path = require('path')
const childProcess = require('child_process')
function status () {
......@@ -39,7 +42,40 @@ function status () {
isEnabled = false
}
return { isActive, isEnabled }
let daemonDetails = null
if (isEnabled) {
// Parse the systemd unit configuration file to retrieve daemon details.
const configuration = fs.readFileSync(path.join(path.sep, 'etc', 'systemd', 'system', 'site.js.service'), 'utf-8').trim().split('\n')
const account = configuration[8].trim().replace('User=', '')
const execStart = configuration[14].trim()
// Launch configuration.
const binaryAndPathBeingServed = /ExecStart=(.*?) (.*?) @hostname/.exec(execStart)
const siteJSBinary = binaryAndPathBeingServed[1]
const pathBeingServed = binaryAndPathBeingServed[2]
// Optional options.
let _domain, _aliases
const domain = (_domain = /--domain=(.*?)(\s|--|$)/.exec(execStart)) === null ? null : _domain[1]
const aliases = (_aliases = /--aliases=(.*?)(\s|--|$)/.exec(execStart)) === null ? null : _aliases[1].split(',')
const skipDomainReachabilityCheck = execStart.includes('--skip-domain-reachability-check')
const optionalOptions = {
domain,
aliases,
skipDomainReachabilityCheck
}
daemonDetails = {
account,
siteJSBinary,
pathBeingServed,
optionalOptions
}
}
return { isActive, isEnabled, daemonDetails }
}
module.exports = status
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