Commit 43c6d72e authored by Aral Balkan's avatar Aral Balkan
Browse files

Fixes #253, 254: fix sync-related regressions

These were introduced when the pull and push commands were added and only manifest when there is generated content (i.e., Hugo)
parent d680f295
...@@ -4,11 +4,13 @@ All notable changes to this project will be documented in this file. ...@@ -4,11 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [16.0.2] - unreleased ## [16.0.2] - 2020-12-11
### Fixed ### Fixed
- No longer crashes on enable command when running under Node.js on older systemd versions that require an absolute path for the binary in the service unit description. (Bug encountered on elementary OS Hera 5.1.7). - Regression: `site disable` crashes
- Regression: `--sync-to` without `--sync-from` fails (#254)
- No longer crashes on enable command when running under Node.js on older systemd versions that require an absolute path for the binary in the service unit description. (Bug encountered on elementary OS Hera 5.1.7). (#255)
## [16.0.1] - 2020-11-07 ## [16.0.1] - 2020-11-07
......
...@@ -27,6 +27,7 @@ const minimist = require('minimist') ...@@ -27,6 +27,7 @@ const minimist = require('minimist')
const package = require('../package.json') const package = require('../package.json')
const moment = require('moment') const moment = require('moment')
const Hugo = require('@small-tech/node-hugo') const Hugo = require('@small-tech/node-hugo')
const Util = require('../lib/Util')
const cpuArchitecture = os.arch() const cpuArchitecture = os.arch()
// Parse the command-line arguments. // Parse the command-line arguments.
...@@ -515,7 +516,7 @@ async function buildBinary () { ...@@ -515,7 +516,7 @@ async function buildBinary () {
// At this point, the new Nexe binary will be at ~/.nexe/<node version>/out/Release/node and // At this point, the new Nexe binary will be at ~/.nexe/<node version>/out/Release/node and
// needs to be copied to ~/.nexe/<platform>-<cpu architecture>-<node version> // needs to be copied to ~/.nexe/<platform>-<cpu architecture>-<node version>
const nexeBaseBinariesPath = path.join(os.homedir(), '.nexe') const nexeBaseBinariesPath = path.join(Util.unprivilegedHomeDirectory(), '.nexe')
const compiledNexeBaseBinaryPath = path.join(nexeBaseBinariesPath, nodeVersion, 'out', 'Release', 'node') const compiledNexeBaseBinaryPath = path.join(nexeBaseBinariesPath, nodeVersion, 'out', 'Release', 'node')
const nexeBaseBinaryNewName = `${platform}-${cpuArchitecture}-${nodeVersion}` const nexeBaseBinaryNewName = `${platform}-${cpuArchitecture}-${nodeVersion}`
const nexeBaseBinaryNewPath = path.join(nexeBaseBinariesPath, nexeBaseBinaryNewName) const nexeBaseBinaryNewPath = path.join(nexeBaseBinariesPath, nexeBaseBinaryNewName)
......
...@@ -116,14 +116,7 @@ function enable (args) { ...@@ -116,14 +116,7 @@ function enable (args) {
const launchCommand = `${executable} ${absolutePathToServe} @hostname ${domain} ${aliases} ${skipDomainReachabilityCheck} ${accessLogErrorsOnly} ${accessLogDisable}` const launchCommand = `${executable} ${absolutePathToServe} @hostname ${domain} ${aliases} ${skipDomainReachabilityCheck} ${accessLogErrorsOnly} ${accessLogDisable}`
let accountName const accountName = Util.unprivilegedAccountName()
try {
// Courtesy: https://www.unix.com/302402784-post4.html
accountName = childProcess.execSync(`awk -v val=${accountUID} -F ":" '$3==val{print $1}' /etc/passwd`, {env: process.env, stdio: 'pipe'}).toString().replace('\n', '')
} catch (error) {
console.log(`\n ❌ ${clr('❨site.js❩ Error:', 'red')} Could not get account name \n${error}.\n`)
process.exit(1)
}
const unit = `[Unit] const unit = `[Unit]
Description=Site.js Description=Site.js
......
...@@ -168,7 +168,7 @@ function serve (args) { ...@@ -168,7 +168,7 @@ function serve (args) {
;(async () => { ;(async () => {
// Generate any content that needs to be generated (e.g., Hugo content). // Generate any content that needs to be generated (e.g., Hugo content).
await generateContent(path, syncOptions.host, Site) await generateContent(syncOptions.from, syncOptions.host, Site)
// Any content that needs to be generated has been generated. Ready to sync. // Any content that needs to be generated has been generated. Ready to sync.
sync(syncOptions) sync(syncOptions)
...@@ -318,8 +318,8 @@ function localFolder (args) { ...@@ -318,8 +318,8 @@ function localFolder (args) {
let localFolder = null let localFolder = null
// If --sync-from is not specified, we default to the path to be served (or default path). // If --sync-from is not specified, we default to the path to be served (or use a default path).
const syncFrom = args.named[SYNC_FROM] || path const syncFrom = args.named[SYNC_FROM] || path || '.'
const syncFromEndsWithPathSeparator = syncFrom.endsWith(pathModule.sep) const syncFromEndsWithPathSeparator = syncFrom.endsWith(pathModule.sep)
// Handle the sync-folder-and-contents flag or its lack // Handle the sync-folder-and-contents flag or its lack
......
...@@ -11,6 +11,7 @@ const fs = require('fs-extra') ...@@ -11,6 +11,7 @@ const fs = require('fs-extra')
const path = require('path') const path = require('path')
const Site = require('../../index') const Site = require('../../index')
const Util = require('../../lib/Util')
const runtime = require('./runtime') const runtime = require('./runtime')
const getStatus = require('./status') const getStatus = require('./status')
const clr = require('../../lib/clr') const clr = require('../../lib/clr')
...@@ -141,7 +142,7 @@ class Ensure { ...@@ -141,7 +142,7 @@ class Ensure {
// (This will install it automatically if a supported package manager exists.) // (This will install it automatically if a supported package manager exists.)
rsyncExists() { rsyncExists() {
const rsyncOnWindowsPath = path.join(os.homedir(), '.small-tech.org', 'site.js', 'portable-rsync-with-ssh-for-windows') const rsyncOnWindowsPath = path.join(Util.unprivilegedHomeDirectory(), '.small-tech.org', 'site.js', 'portable-rsync-with-ssh-for-windows')
if (os.platform() === 'win32') { if (os.platform() === 'win32') {
if (fs.existsSync(rsyncOnWindowsPath)) return // Already installed. if (fs.existsSync(rsyncOnWindowsPath)) return // Already installed.
......
...@@ -20,6 +20,7 @@ const Graceful = require('node-graceful') ...@@ -20,6 +20,7 @@ const Graceful = require('node-graceful')
const RsyncWatcher = require('./RsyncWatcher') const RsyncWatcher = require('./RsyncWatcher')
const ensure = require('./ensure') const ensure = require('./ensure')
const Util = require('../../lib/Util')
const clr = require('../../lib/clr') const clr = require('../../lib/clr')
...@@ -146,7 +147,7 @@ function sync (options) { ...@@ -146,7 +147,7 @@ function sync (options) {
console.log(' 💫 ❨site.js❩ Sync will include the database as requested.') console.log(' 💫 ❨site.js❩ Sync will include the database as requested.')
} }
const sshDirectory = path.join(os.homedir(), '.ssh') const sshDirectory = path.join(Util.unprivilegedHomeDirectory(), '.ssh')
const folderToSyncPathSegments = path.resolve(rsyncOptions.sync.from).split(path.sep) const folderToSyncPathSegments = path.resolve(rsyncOptions.sync.from).split(path.sep)
const folderToSyncName = folderToSyncPathSegments[folderToSyncPathSegments.length - 1] const folderToSyncName = folderToSyncPathSegments[folderToSyncPathSegments.length - 1]
...@@ -190,7 +191,7 @@ function sync (options) { ...@@ -190,7 +191,7 @@ function sync (options) {
} }
// Configure the rsync library to use our bundled rsync executable instead of the system one. // Configure the rsync library to use our bundled rsync executable instead of the system one.
const externalRsyncBundleDirectory = path.join(os.homedir(), '.small-tech.org', 'site.js', 'portable-rsync-with-ssh-for-windows') const externalRsyncBundleDirectory = path.join(Util.unprivilegedHomeDirectory(), '.small-tech.org', 'site.js', 'portable-rsync-with-ssh-for-windows')
const externalRsyncBundleBinDirectory = path.join(externalRsyncBundleDirectory, 'bin') const externalRsyncBundleBinDirectory = path.join(externalRsyncBundleDirectory, 'bin')
const rsyncExecutable = path.join(externalRsyncBundleBinDirectory, 'rsync.exe') const rsyncExecutable = path.join(externalRsyncBundleBinDirectory, 'rsync.exe')
const sshExecutable = path.join(externalRsyncBundleBinDirectory, 'ssh.exe') const sshExecutable = path.join(externalRsyncBundleBinDirectory, 'ssh.exe')
......
...@@ -187,7 +187,7 @@ class Site { ...@@ -187,7 +187,7 @@ class Site {
static set hostname (domain) { this._hostname = domain } static set hostname (domain) { this._hostname = domain }
// This is the directory that settings and other persistent data is stored for Site.js. // This is the directory that settings and other persistent data is stored for Site.js.
static get settingsDirectory () { return path.join(os.homedir(), '.small-tech.org', 'site.js') } static get settingsDirectory () { return path.join(Util.unprivilegedHomeDirectory(), '.small-tech.org', 'site.js') }
// Logs a nicely-formatted version string based on // Logs a nicely-formatted version string based on
// the version set in the package.json file to console. // the version set in the package.json file to console.
...@@ -883,7 +883,7 @@ class Site { ...@@ -883,7 +883,7 @@ class Site {
} }
// Specify custom certificate directory for Site.js. // Specify custom certificate directory for Site.js.
options.settingsPath = path.join(os.homedir(), '.small-tech.org', 'site.js', 'tls') options.settingsPath = path.join(Util.unprivilegedHomeDirectory(), '.small-tech.org', 'site.js', 'tls')
// Create and return the HTTPS server. // Create and return the HTTPS server.
return https.createServer(options, requestListener) return https.createServer(options, requestListener)
......
...@@ -12,6 +12,23 @@ const process = require('process') ...@@ -12,6 +12,23 @@ const process = require('process')
const clr = require('../lib/clr') const clr = require('../lib/clr')
class Util { class Util {
static unprivilegedAccountName () {
if (process.platform === 'win32' || process.env.USER !== 'root') {
// Does not apply to Windows as there is no sudo.
return os.userInfo().username
} else {
return process.env.SUDO_USER
}
}
static unprivilegedHomeDirectory () {
if (process.platform === 'win32') {
return os.homedir()
} else {
// Linuxesque: return non-root home folder even if invoked via sudo.
return `/home/${Util.unprivilegedAccountName()}`
}
}
static refuseToRunAsRoot () { static refuseToRunAsRoot () {
// Refuse to run if this is the root account. // Refuse to run if this is the root account.
......
{ {
"name": "@small-tech/site.js", "name": "@small-tech/site.js",
"version": "16.0.1", "version": "16.0.2",
"description": "Small Web construction set.", "description": "Small Web construction set.",
"keywords": [ "keywords": [
"web server", "web server",
......
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