From 8df38dbebe10629f3352f2d4221db0faad8a25fd Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Thu, 28 Nov 2024 09:43:22 -0800 Subject: [PATCH] server: add env control --- server/package-lock.json | 4 ++-- server/src/runtime.ts | 6 +++++- server/src/scrypted-cluster-main.ts | 6 ++++++ server/src/scrypted-main-exports.ts | 7 +++---- server/src/services/env.ts | 17 +++++++++++++++++ 5 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 server/src/services/env.ts diff --git a/server/package-lock.json b/server/package-lock.json index 1c3f02bae..c30829206 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -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": { diff --git a/server/src/runtime.ts b/server/src/runtime.ts index 9c3b0297f..c4e5a94cc 100644 --- a/server/src/runtime.ts +++ b/server/src/runtime.ts @@ -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 { 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 { } 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 { return this.backup; case 'cluster-fork': return this.clusterFork; + case 'env-control': + return this.envControl; } } diff --git a/server/src/scrypted-cluster-main.ts b/server/src/scrypted-cluster-main.ts index 234c780cb..a547df0b6 100644 --- a/server/src/scrypted-cluster-main.ts +++ b/server/src/scrypted-cluster-main.ts @@ -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', () => { diff --git a/server/src/scrypted-main-exports.ts b/server/src/scrypted-main-exports.ts index ac203db7c..d14022216 100644 --- a/server/src/scrypted-main-exports.ts +++ b/server/src/scrypted-main-exports.ts @@ -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(); diff --git a/server/src/services/env.ts b/server/src/services/env.ts new file mode 100644 index 000000000..9b692644f --- /dev/null +++ b/server/src/services/env.ts @@ -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'); + } +}