Files
cloudly/ts/manager.node/classes.curlfresh.ts
T

102 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-06-13 09:36:02 +02:00
import { logger } from '../logger.js';
import * as plugins from '../plugins.js';
import type { CloudlyNodeManager } from './classes.nodemanager.js';
2026-05-08 13:56:20 +00:00
import type { Cluster } from '../manager.cluster/classes.cluster.js';
2024-06-13 09:36:02 +02:00
export class CurlFresh {
public optionsArg = {
npmRegistry: 'https://registry.npmjs.org',
};
2024-06-13 09:36:02 +02:00
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
2024-06-20 19:00:58 +02:00
# lets install pnpm
curl -fsSL https://get.pnpm.io/install.sh | sh -
2024-06-13 09:36:02 +02:00
# lets make sure we use the correct npm registry
bash -c "npm config set registry ${this.optionsArg.npmRegistry}"
# lets install spark
2024-06-20 19:00:58 +02:00
bash -c "pnpm install -g @serve.zone/spark"
2024-06-13 09:36:02 +02:00
# lets install the spark daemon
2026-05-08 13:56:20 +00:00
bash -c "spark installdaemon --mode=coreflow-node --cloudlyUrl='__CLOUDLY_URL__' --jumpcode='__JUMPCODE__'"
2024-06-13 09:36:02 +02:00
`,
};
public nodeManagerRef: CloudlyNodeManager;
2026-05-08 13:56:20 +00:00
public async handleRequest(ctx: plugins.typedserver.IRequestContext): Promise<Response> {
2024-06-13 09:36:02 +02:00
logger.log('info', 'curlfresh handler called. a server might be coming online soon :)');
2026-05-08 13:56:20 +00:00
const scriptname = ctx.params.scriptname;
switch (scriptname) {
2024-06-13 09:36:02 +02:00
case 'setup.sh':
logger.log('info', 'sending setup.sh');
2026-05-08 13:56:20 +00:00
return new Response(this.scripts['setup.sh']
.replaceAll('__CLOUDLY_URL__', ctx.url.searchParams.get('cloudlyUrl') || '')
.replaceAll('__JUMPCODE__', ctx.url.searchParams.get('jumpcode') || ''), {
headers: {
'Content-Type': 'application/x-sh',
},
});
2024-06-13 09:36:02 +02:00
default:
2026-05-08 13:56:20 +00:00
return new Response('no script found', { status: 404 });
2024-06-13 09:36:02 +02:00
}
2026-05-08 13:56:20 +00:00
}
2024-06-13 09:36:02 +02:00
constructor(nodeManagerRefArg: CloudlyNodeManager) {
this.nodeManagerRef = nodeManagerRefArg;
2024-06-13 09:36:02 +02:00
}
2026-05-08 13:56:20 +00:00
public async getServerUserData(clusterArg?: Cluster): Promise<string> {
const sslMode =
await this.nodeManagerRef.cloudlyRef.config.appData.waitForAndGetKey('sslMode');
2024-06-13 09:36:02 +02:00
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');
2026-05-08 13:56:20 +00:00
let cloudlyUrl = `${protocol}://${domain}:${port}/`;
let jumpcode = '';
if (clusterArg?.data.userId) {
const clusterUser = await this.nodeManagerRef.cloudlyRef.authManager.CUser.getInstance({
id: clusterArg.data.userId,
});
jumpcode = clusterUser?.data.tokens?.[0]?.token || '';
cloudlyUrl = clusterArg.data.cloudlyUrl || cloudlyUrl;
}
2024-06-13 09:36:02 +02:00
const serverUserData = `#cloud-config
runcmd:
2026-05-08 13:56:20 +00:00
- curl -o- '${protocol}://${domain}:${port}/curlfresh/setup.sh?cloudlyUrl=${encodeURIComponent(cloudlyUrl)}&jumpcode=${encodeURIComponent(jumpcode)}' | sh
`;
2024-06-13 09:36:02 +02:00
console.log(serverUserData);
return serverUserData;
}
2024-06-13 09:36:02 +02:00
}