feat(settings): Add runtime settings management, node & baremetal managers, and settings UI
This commit is contained in:
61
ts/manager.node/classes.clusternode.ts
Normal file
61
ts/manager.node/classes.clusternode.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import * as plugins from '../plugins.js';
|
||||
|
||||
/**
|
||||
* ClusterNode represents a logical node participating in a cluster
|
||||
*/
|
||||
@plugins.smartdata.Manager()
|
||||
export class ClusterNode extends plugins.smartdata.SmartDataDbDoc<
|
||||
ClusterNode,
|
||||
plugins.servezoneInterfaces.data.IClusterNode
|
||||
> {
|
||||
// STATIC
|
||||
public static async createFromHetznerServer(
|
||||
hetznerServerArg: plugins.hetznercloud.HetznerServer,
|
||||
clusterId: string,
|
||||
baremetalId: string,
|
||||
) {
|
||||
const newNode = new ClusterNode();
|
||||
newNode.id = plugins.smartunique.shortId(8);
|
||||
const data: plugins.servezoneInterfaces.data.IClusterNode['data'] = {
|
||||
clusterId: clusterId,
|
||||
baremetalId: baremetalId,
|
||||
nodeType: 'baremetal',
|
||||
status: 'initializing',
|
||||
role: 'worker',
|
||||
joinedAt: Date.now(),
|
||||
lastHealthCheck: Date.now(),
|
||||
sshKeys: [],
|
||||
requiredDebianPackages: [],
|
||||
};
|
||||
Object.assign(newNode, { data });
|
||||
await newNode.save();
|
||||
return newNode;
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
@plugins.smartdata.unI()
|
||||
public id: string;
|
||||
|
||||
@plugins.smartdata.svDb()
|
||||
public data: plugins.servezoneInterfaces.data.IClusterNode['data'];
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
public async getDeployments(): Promise<plugins.servezoneInterfaces.data.IDeployment[]> {
|
||||
// TODO: Implement getting deployments for this node
|
||||
return [];
|
||||
}
|
||||
|
||||
public async updateMetrics(metrics: plugins.servezoneInterfaces.data.IClusterNodeMetrics) {
|
||||
this.data.metrics = metrics;
|
||||
this.data.lastHealthCheck = Date.now();
|
||||
await this.save();
|
||||
}
|
||||
|
||||
public async updateStatus(status: plugins.servezoneInterfaces.data.IClusterNode['data']['status']) {
|
||||
this.data.status = status;
|
||||
await this.save();
|
||||
}
|
||||
}
|
90
ts/manager.node/classes.curlfresh.ts
Normal file
90
ts/manager.node/classes.curlfresh.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import { logger } from '../logger.js';
|
||||
import * as plugins from '../plugins.js';
|
||||
import type { CloudlyNodeManager } from './classes.nodemanager.js';
|
||||
|
||||
export class CurlFresh {
|
||||
public optionsArg = {
|
||||
npmRegistry: 'https://registry.npmjs.org',
|
||||
};
|
||||
public scripts = {
|
||||
'setup.sh': `#!/bin/bash
|
||||
|
||||
# lets update the system and install curl
|
||||
# might be installed already, but entrypoint could have been wget
|
||||
apt-get update
|
||||
apt-get install -y --force-yes curl
|
||||
|
||||
# Basic updating of the software lists
|
||||
echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections
|
||||
apt-get update
|
||||
apt-get upgrade -y --force-yes
|
||||
apt-get install -y --force-yes fail2ban curl git
|
||||
curl -sL https://deb.nodesource.com/setup_18.x | bash
|
||||
|
||||
# Install docker
|
||||
curl -sSL https://get.docker.com/ | sh
|
||||
|
||||
# Install default nodejs to run nodejs tools
|
||||
apt-get install -y nodejs zsh
|
||||
zsh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
|
||||
npm config set unsafe-perm true
|
||||
|
||||
# lets install pnpm
|
||||
curl -fsSL https://get.pnpm.io/install.sh | sh -
|
||||
|
||||
# lets make sure we use the correct npm registry
|
||||
bash -c "npm config set registry ${this.optionsArg.npmRegistry}"
|
||||
|
||||
# lets install spark
|
||||
bash -c "pnpm install -g @serve.zone/spark"
|
||||
|
||||
# lets install the spark daemon
|
||||
bash -c "spark installdaemon"
|
||||
|
||||
# TODO: start spark with jump code
|
||||
`,
|
||||
};
|
||||
|
||||
public nodeManagerRef: CloudlyNodeManager;
|
||||
public curlFreshRoute: plugins.typedserver.servertools.Route;
|
||||
public handler = new plugins.typedserver.servertools.Handler('ALL', async (req, res) => {
|
||||
logger.log('info', 'curlfresh handler called. a server might be coming online soon :)');
|
||||
const scriptname = req.params.scriptname;
|
||||
switch (scriptname) {
|
||||
case 'setup.sh':
|
||||
logger.log('info', 'sending setup.sh');
|
||||
res.type('application/x-sh');
|
||||
res.send(this.scripts['setup.sh']);
|
||||
break;
|
||||
default:
|
||||
res.send('no script found');
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
constructor(nodeManagerRefArg: CloudlyNodeManager) {
|
||||
this.nodeManagerRef = nodeManagerRefArg;
|
||||
}
|
||||
public async getServerUserData(): Promise<string> {
|
||||
const sslMode =
|
||||
await this.nodeManagerRef.cloudlyRef.config.appData.waitForAndGetKey('sslMode');
|
||||
let protocol: 'http' | 'https';
|
||||
if (sslMode === 'none') {
|
||||
protocol = 'http';
|
||||
} else {
|
||||
protocol = 'https';
|
||||
}
|
||||
|
||||
const domain =
|
||||
await this.nodeManagerRef.cloudlyRef.config.appData.waitForAndGetKey('publicUrl');
|
||||
const port =
|
||||
await this.nodeManagerRef.cloudlyRef.config.appData.waitForAndGetKey('publicPort');
|
||||
|
||||
const serverUserData = `#cloud-config
|
||||
runcmd:
|
||||
- curl -o- ${protocol}://${domain}:${port}/curlfresh/setup.sh | sh
|
||||
`;
|
||||
console.log(serverUserData);
|
||||
return serverUserData;
|
||||
}
|
||||
}
|
131
ts/manager.node/classes.nodemanager.ts
Normal file
131
ts/manager.node/classes.nodemanager.ts
Normal file
@@ -0,0 +1,131 @@
|
||||
import * as plugins from '../plugins.js';
|
||||
import { Cloudly } from '../classes.cloudly.js';
|
||||
import { Cluster } from '../manager.cluster/classes.cluster.js';
|
||||
import { ClusterNode } from './classes.clusternode.js';
|
||||
import { CurlFresh } from './classes.curlfresh.js';
|
||||
|
||||
export class CloudlyNodeManager {
|
||||
public cloudlyRef: Cloudly;
|
||||
public typedRouter = new plugins.typedrequest.TypedRouter();
|
||||
public curlfreshInstance = new CurlFresh(this);
|
||||
|
||||
public hetznerAccount: plugins.hetznercloud.HetznerAccount;
|
||||
|
||||
public get db() {
|
||||
return this.cloudlyRef.mongodbConnector.smartdataDb;
|
||||
}
|
||||
public CClusterNode = plugins.smartdata.setDefaultManagerForDoc(this, ClusterNode);
|
||||
|
||||
constructor(cloudlyRefArg: Cloudly) {
|
||||
this.cloudlyRef = cloudlyRefArg;
|
||||
|
||||
/**
|
||||
* is used be serverconfig module on the node to get the actual node config
|
||||
*/
|
||||
this.typedRouter.addTypedHandler(
|
||||
new plugins.typedrequest.TypedHandler<plugins.servezoneInterfaces.requests.config.IRequest_Any_Cloudly_GetNodeConfig>(
|
||||
'getNodeConfig',
|
||||
async (requestData) => {
|
||||
const nodeId = requestData.nodeId;
|
||||
const node = await this.CClusterNode.getInstance({
|
||||
id: nodeId,
|
||||
});
|
||||
return {
|
||||
configData: await node.createSavableObject(),
|
||||
};
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public async start() {
|
||||
const hetznerToken = await this.cloudlyRef.settingsManager.getSetting('hetznerToken');
|
||||
|
||||
if (!hetznerToken) {
|
||||
console.log('warn', 'No Hetzner token configured in settings. Hetzner features will be disabled.');
|
||||
return;
|
||||
}
|
||||
|
||||
this.hetznerAccount = new plugins.hetznercloud.HetznerAccount(hetznerToken);
|
||||
}
|
||||
|
||||
public async stop() {}
|
||||
|
||||
/**
|
||||
* creates the node infrastructure on hetzner
|
||||
* ensures that there are exactly the resources that are needed
|
||||
* no more, no less
|
||||
*/
|
||||
public async ensureNodeInfrastructure() {
|
||||
// get all clusters
|
||||
const allClusters = await this.cloudlyRef.clusterManager.getAllClusters();
|
||||
for (const cluster of allClusters) {
|
||||
// Skip clusters that are not set up for Hetzner auto-provisioning
|
||||
if (cluster.data.setupMode !== 'hetzner') {
|
||||
console.log(`Skipping node provisioning for cluster ${cluster.id} - setupMode is ${cluster.data.setupMode || 'manual'}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// get existing nodes
|
||||
const nodes = await this.getNodesByCluster(cluster);
|
||||
|
||||
// if there is no node, create one
|
||||
if (nodes.length === 0) {
|
||||
const hetznerServer = await this.hetznerAccount.createServer({
|
||||
name: plugins.smartunique.uniSimple('node'),
|
||||
location: 'nbg1',
|
||||
type: 'cpx41',
|
||||
labels: {
|
||||
clusterId: cluster.id,
|
||||
priority: '1',
|
||||
},
|
||||
userData: await this.curlfreshInstance.getServerUserData(),
|
||||
});
|
||||
|
||||
// First create BareMetal record
|
||||
const baremetal = await this.cloudlyRef.baremetalManager.createBaremetalFromHetznerServer(hetznerServer);
|
||||
|
||||
const newNode = await ClusterNode.createFromHetznerServer(hetznerServer, cluster.id, baremetal.id);
|
||||
await baremetal.assignNode(newNode.id);
|
||||
console.log(`cluster created new node for cluster ${cluster.id}`);
|
||||
} else {
|
||||
console.log(
|
||||
`cluster ${cluster.id} already has nodes. Making sure that they actually exist in the real world...`,
|
||||
);
|
||||
// if there is a node, make sure that it exists
|
||||
for (const node of nodes) {
|
||||
const hetznerServers = await this.hetznerAccount.getServersByLabel({
|
||||
clusterId: cluster.id,
|
||||
});
|
||||
if (!hetznerServers || hetznerServers.length === 0) {
|
||||
console.log(`node ${node.id} does not exist in the real world. Creating it now...`);
|
||||
const hetznerServer = await this.hetznerAccount.createServer({
|
||||
name: plugins.smartunique.uniSimple('node'),
|
||||
location: 'nbg1',
|
||||
type: 'cpx41',
|
||||
labels: {
|
||||
clusterId: cluster.id,
|
||||
priority: '1',
|
||||
},
|
||||
});
|
||||
|
||||
// First create BareMetal record
|
||||
const baremetal = await this.cloudlyRef.baremetalManager.createBaremetalFromHetznerServer(hetznerServer);
|
||||
|
||||
const newNode = await ClusterNode.createFromHetznerServer(hetznerServer, cluster.id, baremetal.id);
|
||||
await baremetal.assignNode(newNode.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async getNodesByCluster(clusterArg: Cluster) {
|
||||
const results = await this.CClusterNode.getInstances({
|
||||
data: {
|
||||
clusterId: clusterArg.id,
|
||||
},
|
||||
});
|
||||
return results;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user