Verified Commit 3577d762 authored by Aral Balkan's avatar Aral Balkan
Browse files

Add start and stop commands for the daemon; update readme

parent d23b39ab
......@@ -350,7 +350,7 @@ npm run deploy
site [command] [folder|:port] [@host[:port]] [--options]
```
- `command`: version | help | serve | enable | disable | logs | status
- `command`: serve | enable | disable | start | stop | logs | status | update | uninstall | version | help
- `folder|:port`: Path of folder to serve (defaults to current folder) or port on localhost to proxy.
- `@host[:port]`: Host (and, optionally port) to sync. Valid hosts are @localhost and @hostname.
- `--options`: Settings that alter command behaviour.
......@@ -368,20 +368,24 @@ __Key:__ `[]` = optional   `|` = or
If a port (e.g., `:1313`) is specified instead of my-folder, start an HTTP/WebSocket proxy.
- `version`: Display version and exit.
- `help`: Display help screen and exit.
- `uninstall`: Uninstall Site.js.
On Linux distributions with systemd, you can also use:
- `enable`: Start server as daemon with globally-trusted certificates and add to startup.
- `disable`: Stop server daemon and remove from startup.
- `start`: Start server as daemon with globally-trusted certificates.
- `stop`: Stop server daemon.
- `logs`: Display and tail server logs.
- `status`: Display detailed server information.
- `update`: Check for Site.js updates and update if new version is found.
- `uninstall`: Uninstall Site.js.
- `version`: Display version and exit.
- `help`: Display help screen and exit.
If `command` is omitted, behaviour defaults to `serve`.
### Options:
......
......@@ -8,9 +8,14 @@
//////////////////////////////////////////////////////////////////////
const _disable = require('../lib/disable')
const ensure = require('../lib/ensure')
const Site = require('../../index')
function disable () {
ensure.systemctl()
ensure.root('disable')
Site.logAppNameAndVersion()
try {
// Disable and stop the web server.
......
......@@ -47,6 +47,8 @@ function help () {
const commandEnable = command('enable')
const commandDisable = command('disable')
const commandStart = command('start')
const commandStop = command('stop')
const commandLogs = command('logs')
const commandStatus = command('status')
......@@ -81,7 +83,7 @@ function help () {
${prompt} ${clr(appName, 'bold')} [${usageCommand}] [${usageFolderOrPort}] [${usageHostAndPort}] [${usageOptions}]
${usageCommand}\t\t${commandServe}${systemdExists ? ` | ${commandEnable} | ${commandDisable} | ${commandLogs} | ${commandStatus}` : ''} | ${commandUpdate} | ${commandUninstall} | ${commandVersion} | ${commandHelp}
${usageCommand}\t\t${commandServe}${systemdExists ? ` | ${commandEnable} | ${commandDisable} | ${commandStart} | ${commandStop} | ${commandLogs} | ${commandStatus}` : ''} | ${commandUpdate} | ${commandUninstall} | ${commandVersion} | ${commandHelp}
${usageFolderOrPort}\tPath of folder to serve (defaults to current folder) or port on localhost to proxy.
${usageHostAndPort}\tHost (and, optionally port) to sync. Valid hosts are @localhost and @hostname.
${usageOptions}\t\tSettings that alter command behaviour.
......@@ -102,6 +104,8 @@ function help () {
`
${commandEnable}\tStart server as daemon with globally-trusted certificates and add to startup.
${commandDisable}\tStop server daemon and remove from startup.
${commandStart}\tStart server as daemon with globally-trusted certificates.
${commandStop}\tStop server daemon.
${commandLogs}\tDisplay and tail server logs.
${commandStatus}\tDisplay detailed server information.
` : ''}
......
//////////////////////////////////////////////////////////////////////
//
// Command: start
//
// Starts the web server daemon.
//
//////////////////////////////////////////////////////////////////////
const _start = require('../lib/start')
const ensure = require('../lib/ensure')
const Site = require('../../index')
function start () {
ensure.systemctl()
ensure.root('start')
Site.logAppNameAndVersion()
try {
// Start the web server.
_start()
} catch (error) {
process.exit(1)
}
}
module.exports = start
//////////////////////////////////////////////////////////////////////
//
// Command: stop
//
// Stops the web server daemon.
//
//////////////////////////////////////////////////////////////////////
const _stop = require('../lib/stop')
const ensure = require('../lib/ensure')
const Site = require('../../index')
function stop () {
ensure.systemctl()
ensure.root('stop')
Site.logAppNameAndVersion()
try {
// Stop the web server.
_stop()
} catch (error) {
process.exit(1)
}
}
module.exports = stop
......@@ -17,9 +17,10 @@ const concat = require('concat-stream')
const Site = require('../../index')
const ensure = require('../lib/ensure')
const getStatus = require('../lib/status')
async function update () {
ensure.root()
ensure.root('update')
Site.logAppNameAndVersion()
......@@ -95,11 +96,26 @@ async function update () {
await extract(latestRelease)
console.log(' 🎉 Done!\n')
//
// Check if the server daemon is running. If so, we must first stop
// it before we can install the binary otherwise we will get the
// error Error: ETXTBSY: text file is busy, open '/usr/local/bin/site'.
//
// We will restart the server using the latest version of Site.js
// after a successful install.
//
if (ensure.commandExists('systemctl')) {
const { isActive } = getStatus()
if (isActive) {
console.log(' 😈 Site.js daemon is active. Stopping it before installing latest version… ')
console.log('TODO')
} else {
// Debug
console.log('Server daemon is not active; no need to attempt restart.')
}
}
// TODO: Check if the server daemon is running. If so, restart it
// ===== so that it starts running the latest version.
// NOTE: Only do it if ensure.commandExists('systemctl')
console.log(' 🎉 Done!\n')
} else {
console.log(' 😁👍 You’re running the latest version of Site.js!\n')
......
......@@ -22,6 +22,8 @@ class CommandLineInterface {
['enable', arg0 === 'enable'],
['disable', arg0 === 'disable'],
['start', arg0 === 'start'],
['stop', arg0 === 'stop'],
['logs', arg0 === 'logs'],
['status', arg0 === 'status'],
......
......@@ -19,22 +19,14 @@ function throwError(errorMessage) {
throw new Error(errorMessage)
}
// Note: Ensure that systemctl exists and app is root before calling this function.
function disable () {
ensure.systemctl()
const { isActive, isEnabled } = status()
if (!isEnabled) {
Site.logAppNameAndVersion()
throwError('Site.js server is not enabled. Nothing to disable.')
}
ensure.root('disable')
Site.logAppNameAndVersion()
try {
// Disable and stop the web server.
childProcess.execSync('sudo systemctl disable site.js', {env: process.env, stdio: 'pipe'})
......
//////////////////////////////////////////////////////////////////////
//
// Function: start
//
// Starts the Site.js server daemon.
//
//////////////////////////////////////////////////////////////////////
const childProcess = require('child_process')
const status = require('../lib/status')
function throwError(errorMessage) {
console.log(` 👿 Error: ${errorMessage}\n`)
throw new Error(errorMessage)
}
function start () {
// Note: Ensure that systemctl exists and app is root before calling this function.
const { isActive, isEnabled } = status()
if (!isEnabled) {
throwError('Site.js daemon is not enabled. Please run site enable to enable it.')
}
if (isActive) {
throwError('Site.js server is already active. Nothing to start.')
}
try {
// Start the web server.
childProcess.execSync('sudo systemctl start site.js', {env: process.env, stdio: 'pipe'})
} catch (error) {
throwError(`Could not start Site.js server (${error}).`)
}
console.log(' 🎈 Server started.\n')
}
module.exports = start
//////////////////////////////////////////////////////////////////////
//
// Function: stop
//
// Stops the Site.js server daemon.
//
//////////////////////////////////////////////////////////////////////
const childProcess = require('child_process')
const status = require('../lib/status')
const ensure = require('../lib/ensure')
function throwError(errorMessage) {
console.log(` 👿 Error: ${errorMessage}\n`)
throw new Error(errorMessage)
}
// Note: Ensure that systemctl exists and app is root before calling this function.
function stop () {
const { isActive } = status()
if (!isActive) {
throwError('Site.js server is not active. Nothing to stop.')
}
try {
// Stop the web server.
childProcess.execSync('sudo systemctl stop site.js', {env: process.env, stdio: 'pipe'})
} catch (error) {
throwError(`Could not stop Site.js server (${error}).`)
}
console.log(' 🎈 Server stopped.\n')
}
module.exports = stop
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