Skip to content

Commit

Permalink
added setup controller and router
Browse files Browse the repository at this point in the history
  • Loading branch information
jaenixlee committed Apr 17, 2023
1 parent f4380a9 commit ab7a11b
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"workbench.localHistory.enabled": true,
"editor.tabSize": 2,
"editor.formatOnSave": true,
"editor.formatOnSave": false,
"editor.formatOnPaste": true,
"editor.formatOnType": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"editor.acceptSuggestionOnEnter": "off"
}
}
9 changes: 8 additions & 1 deletion dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ ENV DOCKERVERSION=20.10.23

RUN curl -fsSLO https://download.docker.com/linux/static/stable/x86_64/docker-${DOCKERVERSION}.tgz \
&& tar xzvf docker-${DOCKERVERSION}.tgz --strip 1 -C /usr/local/bin docker/docker \
&& rm docker-${DOCKERVERSION}.tgz
&& rm docker-${DOCKERVERSION}.tgz \
&& curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 \
&& chmod 700 get_helm.sh \
&& ./get_helm.sh \
&& curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" \
&& chmod +x kubectl \
&& mkdir -p ~/.local/bin \
&& mv ./kubectl ~/.local/bin/kubectl

COPY package*.json ./

Expand Down
2 changes: 2 additions & 0 deletions server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import dbRouter from './routes/dbRouter';
import initRouter from './routes/initRouter';
import loginRouter from './routes/loginRouter';
import logoutRouter from './routes/logoutRouter';
import setupRouter from './routes/setupRouter';
import signupRouter from './routes/signupRouter';
// import userController from './controllers/userController';

Expand All @@ -61,6 +62,7 @@ app.use('/db', dbRouter);
app.use('/init', initRouter);
app.use('/login', loginRouter);
app.use('/logout', logoutRouter);
app.use('/setup', setupRouter);
app.use('/signup', signupRouter);

// Handling requests to unknown endpoints...
Expand Down
116 changes: 116 additions & 0 deletions server/controllers/setupController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { Request, Response, NextFunction } from 'express';
import { exec, execSync, spawn, spawnSync } from 'child_process';
import { SetupController } from '../../types';


const setupController: SetupController = {
promInstall: (req: Request, res: Response, next: NextFunction): void => {
// exec('curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash && helm repo add prometheus-community https://prometheus-community.github.io/helm-charts && helm install prometheus-operator prometheus-community/kube-prometheus-stack', (error, stdout, stderr) => {
// if (error) {
// console.error(`Error: ${error.message}`);
// res.send(`Error: ${error.message}`);
// return;
// }
// if (stderr) {
// console.error(`Stderr: ${stderr}`);
// res.send(`Stderr: ${stderr}`);
// return;
// }
// console.log(`stdout: ${stdout}`);
// res.send(`stdout: ${stdout}`);
// });

// synchronous functions need to be used in order to make sure these commands execute successively
// spawnSync(
// 'docker exec docketeer bash',
// {
// stdio: 'inherit',
// shell: true,
// }
// );
spawnSync(
'docker exec -t docketeer helm repo add prometheus-community https://prometheus-community.github.io/helm-charts',
{
stdio: 'inherit',
shell: true,
}
);
spawnSync('docker exec -t docketeer helm repo update', {
stdio: 'inherit',
shell: true,
});
spawnSync(
'docker exec -t docketeer helm install prometheus prometheus-community/kube-prometheus-stack',
{
stdio: 'inherit',
shell: true,
}
);
return next();
},


applyGraf: (req: Request, res: Response, next: NextFunction): void => {
let pod: string;
const getPods = exec('kubectl get pods', (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
const output = stdout.split('\n');
output.forEach((line) => {
if (line.includes('prometheus-grafana')) {
[pod] = line.split(' ');
}
});
});

getPods.once('close', () => {
spawnSync('kubectl apply -f prometheus-grafana.yml', {
stdio: 'inherit',
shell: true,
});
spawnSync(`kubectl delete pod ${pod}`, {
stdio: 'inherit',
shell: true,
});
return next();
});
},


portForward: (req: Request, res: Response, next: NextFunction): void => {
let pod: string;
let podStatus: string;
while (podStatus !== 'Running') {
const abc = execSync('kubectl get pods');
abc
.toString()
.split('\n')
.forEach((line) => {
if (line.includes('prometheus-grafana')) {
if (line.includes('Running')) podStatus = 'Running';
[pod] = line.split(' ');
}
});
}

const ports = spawn(
`kubectl port-forward deployment/${pod} 3000`,
{ shell: true }
);
ports.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ports.stderr.on('data', (data) => {
console.error(`prometheus-grafana port forwarding error: ${data}`);
});
return next();
}
};

export default setupController;
26 changes: 26 additions & 0 deletions server/routes/setupRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @module Setup Router
* @description Routes all requests to set up cluster
*/
import { Router, Request, Response } from 'express';
import setupController from '../controllers/setupController';

const router = Router();

router.get('/promInstall', setupController.promInstall,
(req: Request, res: Response): Response => {
return res.sendStatus(200);
});

router.get('/applyGraf', setupController.applyGraf,
(req: Request, res: Response): Response => {
return res.sendStatus(200);
});

router.get('/portForward', setupController.portForward,
(req: Request, res: Response): Response => {
return res.sendStatus(200);
});


export default router;
10 changes: 7 additions & 3 deletions src/components/Setup/Setup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ const Setup = (): JSX.Element => {
<div className={styles.container}>
<h3>Configure Kubernetes</h3>
<div className={styles.buttonContainer}>
<button className={globalStyles.button1} style={{ alignSelf: 'center' }} onClick={() => fetch('http://localhost:3000/')}>Install Prometheus Operator</button>
<button className={globalStyles.button1} style={{ alignSelf: 'center' }} onClick={() => fetch('http://localhost:3000/')}>Get Metrics</button>
<button className={globalStyles.button1} style={{ alignSelf: 'center' }} onClick={() => fetch('http://localhost:3000/')}>Launch Port Forwarding</button>
<button className={globalStyles.button1} style={{ alignSelf: 'center' }}
onClick={() =>
fetch('/api/setup/promInstall')
.then(() => document.querySelector('span').style.display = 'inline-block')}>Install Prometheus Operator</button>
<span style={{display: 'none'}}>complete!&#10004;</span>
<button className={globalStyles.button1} style={{ alignSelf: 'center' }} onClick={() => fetch('/api/setup/applyGraf')}>Get Metrics</button>
<button className={globalStyles.button1} style={{ alignSelf: 'center' }} onClick={() => fetch('/api/setup/portForward')}>Launch Port Forwarding</button>
</div>
</div>
);
Expand Down
34 changes: 27 additions & 7 deletions types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Request, Response, NextFunction, RequestHandler } from 'express';
import { Request, Response, NextFunction, RequestHandler } from 'express';

// * BH: I think we should have a separate file for each interface, and then import them into this file. That way, we can keep the interfaces organized and not have to scroll through a huge file to find the interface we need.

Expand Down Expand Up @@ -231,12 +231,12 @@ export interface notificationList {
export interface AlertStateType {
alertList: (string | null)[];
promptList:
| [
prompt: string | null,
handleAccept: (() => void) | null,
handleDeny: (() => void) | null
]
| null[];
| [
prompt: string | null,
handleAccept: (() => void) | null,
handleDeny: (() => void) | null
]
| null[];
}

export interface notificationStateType {
Expand Down Expand Up @@ -543,6 +543,26 @@ export interface SettingsController {
addGitLinks: (req: Request, res: Response, next: NextFunction) => void;
}

export interface SetupController {
/**
* @description Installs Prometheus Operator on user's cluster
* @note Only needs to install once
*/
promInstall: MiddleWareFunction;

/**
* @description Applys prometheus-grafana.yml on cluster
* @note
*/
applyGraf: MiddleWareFunction;

/**
* @description Port forwards prometheus-grafana pod to 3000
* @note Must check for prometheus-grafana deployment
*/
portForward: MiddleWareFunction;
}

export interface SignupController {
/**
* @description Checks if username already exists in the database
Expand Down

0 comments on commit ab7a11b

Please sign in to comment.