Skip to content

Commit

Permalink
server: add env control
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Nov 28, 2024
1 parent 229dcd3 commit 8df38db
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
4 changes: 2 additions & 2 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion server/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}

Expand Down
6 changes: 6 additions & 0 deletions server/src/scrypted-cluster-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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', () => {
Expand Down
7 changes: 3 additions & 4 deletions server/src/scrypted-main-exports.ts
Original file line number Diff line number Diff line change
@@ -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?: {
Expand Down Expand Up @@ -66,7 +65,7 @@ function start(mainFilename: string, options?: {
});

dotenv.config({
path: path.join(getScryptedVolume(), '.env'),
path: getDotEnvPath(),
});

const clusterMode = getScryptedClusterMode();
Expand Down
17 changes: 17 additions & 0 deletions server/src/services/env.ts
Original file line number Diff line number Diff line change
@@ -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');
}
}

0 comments on commit 8df38db

Please sign in to comment.