fix(systemdmanager): migrate systemd file operations to smartfs and tighten TypeScript safety

This commit is contained in:
2026-05-01 15:24:08 +00:00
parent a8b6ecb1a6
commit ddb77fd37d
12 changed files with 2569 additions and 3270 deletions
+1 -1
View File
@@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartdaemon',
version: '2.1.0',
version: '2.1.1',
description: 'Start scripts as long running daemons and manage them.'
}
+8 -9
View File
@@ -21,20 +21,19 @@ export class SmartDaemonService implements ISmartDaemonServiceConstructorOptions
optionsArg: ISmartDaemonServiceConstructorOptions
) {
const service = new SmartDaemonService(smartdaemonRef);
for (const key of Object.keys(optionsArg)) {
service[key] = optionsArg[key];
}
Object.assign(service, optionsArg);
service.options = optionsArg;
return service;
}
public options: ISmartDaemonServiceConstructorOptions;
public options!: ISmartDaemonServiceConstructorOptions;
public alreadyExists = false;
public name: string;
public version: string;
public command: string;
public workingDir: string;
public description: string;
public name!: string;
public version!: string;
public command!: string;
public workingDir!: string;
public description!: string;
public user?: string;
public group?: string;
+9 -9
View File
@@ -29,6 +29,7 @@ export class SmartDaemonSystemdManager {
public smartdaemonRef: SmartDaemon;
public smartshellInstance: plugins.smartshell.Smartshell;
public smartsystem: plugins.smartsystem.Smartsystem;
public smartFs: plugins.smartfs.SmartFs;
public shouldExecute: boolean = false;
public isRoot: boolean = false;
@@ -40,6 +41,7 @@ export class SmartDaemonSystemdManager {
executor: 'bash',
});
this.smartsystem = new plugins.smartsystem.Smartsystem();
this.smartFs = new plugins.smartfs.SmartFs(new plugins.smartfs.SmartFsProviderNode());
this.sudoPassword = sudoPasswordArg;
// Check if we're running as root
@@ -85,7 +87,7 @@ export class SmartDaemonSystemdManager {
try {
await this.smartshellInstance.exec(commandArg);
} catch (error) {
if (!this.isRoot && error.message && error.message.includes('authentication')) {
if (!this.isRoot && error instanceof Error && error.message.includes('authentication')) {
throw new Error('Sudo authentication failed. Please configure passwordless sudo or provide a sudo password.');
}
throw error;
@@ -102,12 +104,10 @@ export class SmartDaemonSystemdManager {
const smartfmInstance = new plugins.smartfm.Smartfm({
fmType: 'yaml',
});
const availableServices = await plugins.smartfile.fs.fileTreeToObject(
paths.systemdDir,
'smartdaemon_*.service'
);
const availableServices = await this.smartFs.directory(paths.systemdDir).filter('smartdaemon_*.service').list();
for (const serviceFile of availableServices) {
const data = smartfmInstance.parseFromComments('# ', serviceFile.contentBuffer.toString())
const serviceFileContent = await this.smartFs.file(serviceFile.path).encoding('utf8').read();
const data = smartfmInstance.parseFromComments('# ', serviceFileContent.toString())
.data as ISmartDaemonServiceConstructorOptions;
const service = await SmartDaemonService.createFromOptions(this.smartdaemonRef, data);
service.alreadyExists = true;
@@ -143,11 +143,11 @@ export class SmartDaemonSystemdManager {
if (this.isRoot) {
// Direct write when running as root
await plugins.smartfile.memory.toFs(content, targetPath);
await this.smartFs.file(targetPath).encoding('utf8').mode(0o644).write(content);
} else {
// Use sudo to write when not root
const tempPath = `/tmp/smartdaemon_${serviceArg.name}.service`;
await plugins.smartfile.memory.toFs(content, tempPath);
await this.smartFs.file(tempPath).encoding('utf8').mode(0o644).write(content);
await this.execute(`mv ${tempPath} ${targetPath}`); // execute() will add sudo
await this.execute(`chmod 644 ${targetPath}`); // execute() will add sudo
}
@@ -159,7 +159,7 @@ export class SmartDaemonSystemdManager {
const filePath = SmartDaemonSystemdManager.createFilePathFromServiceName(serviceArg.name);
if (this.isRoot) {
await plugins.smartfile.fs.remove(filePath);
await this.smartFs.file(filePath).delete();
} else {
await this.execute(`rm ${filePath}`); // execute() will add sudo
}
+2 -2
View File
@@ -5,8 +5,8 @@ export { path };
// @pushrocks scope
import * as lik from '@push.rocks/lik';
import * as smartfile from '@push.rocks/smartfile';
import * as smartfm from '@push.rocks/smartfm';
import * as smartfs from '@push.rocks/smartfs';
import * as smartlog from '@push.rocks/smartlog';
import * as smartlogDestinationLocal from '@push.rocks/smartlog-destination-local';
import * as smartpath from '@push.rocks/smartpath';
@@ -15,8 +15,8 @@ import * as smartsystem from '@push.rocks/smartsystem';
export {
lik,
smartfile,
smartfm,
smartfs,
smartlog,
smartlogDestinationLocal,
smartpath,