server: add env control

This commit is contained in:
Koushik Dutta
2024-11-28 09:43:22 -08:00
parent 229dcd3174
commit 8df38dbebe
5 changed files with 33 additions and 7 deletions

View File

@@ -1,12 +1,12 @@
{
"name": "@scrypted/server",
"version": "0.123.41",
"version": "0.123.42",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@scrypted/server",
"version": "0.123.41",
"version": "0.123.42",
"hasInstallScript": true,
"license": "ISC",
"dependencies": {

View File

@@ -46,6 +46,7 @@ import { UsersService } from './services/users';
import { getState, ScryptedStateManager, setState } from './state';
import { isClusterAddress } from './cluster/cluster-setup';
import { RunningClusterWorker } from './scrypted-cluster-main';
import { EnvControl } from './services/env';
interface DeviceProxyPair {
handler: PluginDeviceProxyHandler;
@@ -90,6 +91,7 @@ export class ScryptedRuntime extends PluginHttp<HttpPluginData> {
addressSettings = new AddressSettings(this);
usersService = new UsersService(this);
clusterFork = new ClusterForkService(this);
envControl = new EnvControl();
info = new Info();
backup = new Backup(this);
pluginHosts = getBuiltinRuntimeHosts();
@@ -125,7 +127,7 @@ export class ScryptedRuntime extends PluginHttp<HttpPluginData> {
}
let address = clusterObject.address;
if (isClusterAddress(address))
if (isClusterAddress(address))
address = '127.0.0.1';
const socket = net.connect({
port: clusterObject.port,
@@ -376,6 +378,8 @@ export class ScryptedRuntime extends PluginHttp<HttpPluginData> {
return this.backup;
case 'cluster-fork':
return this.clusterFork;
case 'env-control':
return this.envControl;
}
}

View File

@@ -22,6 +22,7 @@ import type { ScryptedRuntime } from './runtime';
import type { ClusterForkService } from './services/cluster-fork';
import { sleep } from './sleep';
import type { ServiceControl } from './services/service-control';
import { EnvControl } from './services/env';
installSourceMapSupport({
environment: 'node',
@@ -200,6 +201,9 @@ function createClusterForkParam(mainFilename: string, clusterId: string, cluster
export function startClusterClient(mainFilename: string, serviceControl?: ServiceControl) {
console.log('Cluster client starting.');
const envControl = new EnvControl();
const originalClusterAddress = process.env.SCRYPTED_CLUSTER_ADDRESS;
const labels = getClusterLabels();
@@ -248,6 +252,8 @@ export function startClusterClient(mainFilename: string, serviceControl?: Servic
process.env.SCRYPTED_CLUSTER_ADDRESS = socket.localAddress;
const peer = preparePeer(socket, 'client');
peer.params['service-control'] = serviceControl;
peer.params['env-control'] = envControl;
const { localAddress, localPort } = socket;
console.log('Cluster server connected.', localAddress, localPort);
socket.on('close', () => {

View File

@@ -1,17 +1,16 @@
import dns from 'dns';
import dotenv from 'dotenv';
import fs from 'fs';
import path from 'path';
import process from 'process';
import semver from 'semver';
import v8 from 'v8';
import vm from 'vm';
import { getScryptedClusterMode } from './cluster/cluster-setup';
import { PluginError } from './plugin/plugin-error';
import { getScryptedVolume } from './plugin/plugin-volume';
import { isNodePluginWorkerProcess } from './plugin/runtime/node-fork-worker';
import { RPCResultError, startPeriodicGarbageCollection } from './rpc';
import type { Runtime } from './scrypted-server-main';
import { getScryptedClusterMode } from './cluster/cluster-setup';
import { getDotEnvPath } from './services/env';
import type { ServiceControl } from './services/service-control';
function start(mainFilename: string, options?: {
@@ -66,7 +65,7 @@ function start(mainFilename: string, options?: {
});
dotenv.config({
path: path.join(getScryptedVolume(), '.env'),
path: getDotEnvPath(),
});
const clusterMode = getScryptedClusterMode();

View File

@@ -0,0 +1,17 @@
import fs from 'fs';
import path from 'path';
import { getScryptedVolume } from '../plugin/plugin-volume';
export function getDotEnvPath() {
return path.join(getScryptedVolume(), '.env')
}
export class EnvControl {
async setDotEnv(env: string) {
await fs.promises.writeFile(getDotEnvPath(), env, 'utf8');
}
getDotEnv() {
return fs.promises.readFile(getDotEnvPath(), 'utf8');
}
}