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.
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
- 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
......
......@@ -27,6 +27,7 @@ const minimist = require('minimist')
const package = require('../package.json')
const moment = require('moment')
const Hugo = require('@small-tech/node-hugo')
const Util = require('../lib/Util')
const cpuArchitecture = os.arch()
// Parse the command-line arguments.
......@@ -515,7 +516,7 @@ async function buildBinary () {
// 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>
const nexeBaseBinariesPath = path.join(os.homedir(), '.nexe')
const nexeBaseBinariesPath = path.join(Util.unprivilegedHomeDirectory(), '.nexe')
const compiledNexeBaseBinaryPath = path.join(nexeBaseBinariesPath, nodeVersion, 'out', 'Release', 'node')
const nexeBaseBinaryNewName = `${platform}-${cpuArchitecture}-${nodeVersion}`
const nexeBaseBinaryNewPath = path.join(nexeBaseBinariesPath, nexeBaseBinaryNewName)
......
......@@ -116,14 +116,7 @@ function enable (args) {
const launchCommand = `${executable} ${absolutePathToServe} @hostname ${domain} ${aliases} ${skipDomainReachabilityCheck} ${accessLogErrorsOnly} ${accessLogDisable}`
let accountName
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 accountName = Util.unprivilegedAccountName()
const unit = `[Unit]
Description=Site.js
......
......@@ -168,7 +168,7 @@ function serve (args) {
;(async () => {
// 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.
sync(syncOptions)
......@@ -318,8 +318,8 @@ function localFolder (args) {
let localFolder = null
// If --sync-from is not specified, we default to the path to be served (or default path).
const syncFrom = args.named[SYNC_FROM] || 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 syncFromEndsWithPathSeparator = syncFrom.endsWith(pathModule.sep)
// Handle the sync-folder-and-contents flag or its lack
......
......@@ -11,6 +11,7 @@ const fs = require('fs-extra')
const path = require('path')
const Site = require('../../index')
const Util = require('../../lib/Util')
const runtime = require('./runtime')
const getStatus = require('./status')
const clr = require('../../lib/clr')
......@@ -141,7 +142,7 @@ class Ensure {
// (This will install it automatically if a supported package manager exists.)
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 (fs.existsSync(rsyncOnWindowsPath)) return // Already installed.
......
......@@ -20,6 +20,7 @@ const Graceful = require('node-graceful')
const RsyncWatcher = require('./RsyncWatcher')
const ensure = require('./ensure')
const Util = require('../../lib/Util')
const clr = require('../../lib/clr')
......@@ -146,7 +147,7 @@ function sync (options) {
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 folderToSyncName = folderToSyncPathSegments[folderToSyncPathSegments.length - 1]
......@@ -190,7 +191,7 @@ function sync (options) {
}
// 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 rsyncExecutable = path.join(externalRsyncBundleBinDirectory, 'rsync.exe')
const sshExecutable = path.join(externalRsyncBundleBinDirectory, 'ssh.exe')
......
......@@ -187,7 +187,7 @@ class Site {
static set hostname (domain) { this._hostname = domain }
// 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
// the version set in the package.json file to console.
......@@ -883,7 +883,7 @@ class Site {
}
// 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.
return https.createServer(options, requestListener)
......
......@@ -12,6 +12,23 @@ const process = require('process')
const clr = require('../lib/clr')
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 () {
// Refuse to run if this is the root account.
......
{
"name": "@small-tech/site.js",
"version": "16.0.1",
"version": "16.0.2",
"description": "Small Web construction set.",
"keywords": [
"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