import { logger } from '../logger.js'; import * as plugins from '../plugins.js'; import type { CloudlyNodeManager } from './classes.nodemanager.js'; import type { Cluster } from '../manager.cluster/classes.cluster.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 --mode=coreflow-node --cloudlyUrl='__CLOUDLY_URL__' --jumpcode='__JUMPCODE__'" `, }; public nodeManagerRef: CloudlyNodeManager; public async handleRequest(ctx: plugins.typedserver.IRequestContext): Promise { logger.log('info', 'curlfresh handler called. a server might be coming online soon :)'); const scriptname = ctx.params.scriptname; switch (scriptname) { case 'setup.sh': logger.log('info', 'sending setup.sh'); 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', }, }); default: return new Response('no script found', { status: 404 }); } } constructor(nodeManagerRefArg: CloudlyNodeManager) { this.nodeManagerRef = nodeManagerRefArg; } public async getServerUserData(clusterArg?: Cluster): Promise { 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'); 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; } const serverUserData = `#cloud-config runcmd: - curl -o- '${protocol}://${domain}:${port}/curlfresh/setup.sh?cloudlyUrl=${encodeURIComponent(cloudlyUrl)}&jumpcode=${encodeURIComponent(jumpcode)}' | sh `; console.log(serverUserData); return serverUserData; } }