Skip to content

Commit

Permalink
Merge pull request #80 from AmpersandTarski/feature/79-stop-prototype…
Browse files Browse the repository at this point in the history
…-when-already-running

Feature/79 stop prototype when already running
  • Loading branch information
FranSlot authored Feb 15, 2024
2 parents 99f9055 + 1620269 commit abd4371
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 50 deletions.
11 changes: 0 additions & 11 deletions assets/kubernetes.sh

This file was deleted.

27 changes: 26 additions & 1 deletion src/commands/generatePrototypeCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as vscode from 'vscode';
import * as path from 'path';

export class generatePrototypeCommand {
private static portForwardTerminalPID: vscode.Terminal | undefined;

static GeneratePrototypeCommand(context: vscode.ExtensionContext)
{
//Get extension path
Expand All @@ -22,6 +24,8 @@ export class generatePrototypeCommand {

const manifestFileUri: vscode.Uri = vscode.Uri.file(manifestFileName);

tryKillPortForwardedProcessAndTerminal(this.portForwardTerminalPID);

vscode.workspace.fs.readFile(templateFileUri).then((data: Uint8Array) =>{
const newData: Uint8Array = fileUtils.replaceMarkers(data, new Map<string, string>(
[
Expand All @@ -36,11 +40,32 @@ export class generatePrototypeCommand {
const deployment: string = 'prototype';
const service: string = 'prototype';

terminalUtils.RunCommandsInNewTerminal("Run prototype in minikube",
this.portForwardTerminalPID = terminalUtils.RunCommandsInNewTerminal("Run prototype in minikube",
[`kubectl apply -f ${manifestFileUri.fsPath}`,
`kubectl rollout status deployment/${deployment} --timeout=300s`,
`kubectl port-forward svc/${service} -n default 8000:80`,]);
});
});

async function tryKillPortForwardedProcessAndTerminal(terminalToKill : vscode.Terminal | undefined)
{
if(terminalToKill === undefined)
return;

//get the processID from the terminal that needs to be killed
let terminalPID = await terminalToKill.processId.then();

//kill the portforward process and then kill the terminal that was hosting it.
let killerTerminal = terminalUtils.RunCommandsInNewTerminal("Kill processes",
[`PID=$(ps -ef | grep 'kubectl port-forward' | grep -v grep | awk '{print $2}')`,
`kill $PID`,
(`kill -9 ${terminalPID}`)]);

//Get own terminal PID to kill it later
let killerTerminalPID = await killerTerminal.processId.then();

//Kill self to cleanup
terminalUtils.RunCommandsInExistingTerminal(killerTerminal,[(`kill -9 ${killerTerminalPID}`)])
}
}
}
20 changes: 16 additions & 4 deletions src/utils/terminalUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import vscode from 'vscode';
import vscode, { Terminal } from 'vscode';
import { fileUtils } from './fileUtils';

export class terminalUtils{
Expand All @@ -10,18 +10,30 @@ export class terminalUtils{
}

let terminal = vscode.window.createTerminal({name:terminalName,cwd:fileUtils.generateWorkspacePath(workingDir)});
terminal.sendText(runAmpersandCommand)
terminal.show();
this.RunCommandsInExistingTerminal(terminal,[runAmpersandCommand])
}

static RunCommandsInNewTerminal(terminalName : string, runAmpersandCommands : string[], workingDir? : string[])
static RunCommandsInNewTerminal(terminalName : string, runAmpersandCommands : string[], workingDir? : string[]) : Terminal
{
if(workingDir === undefined)
{
workingDir = [''];
}

let terminal = vscode.window.createTerminal({name:terminalName,cwd:fileUtils.generateWorkspacePath(workingDir)});

this.RunCommandsInExistingTerminal(terminal,runAmpersandCommands);

return terminal;
}

static RunCommandsInExistingTerminal(terminal : Terminal, runAmpersandCommands : string[], workingDir? : string[])
{
if(workingDir === undefined)
{
workingDir = [''];
}

terminal.show();
runAmpersandCommands.forEach(command => {
terminal.sendText(command)
Expand Down
34 changes: 0 additions & 34 deletions src/utils/zipUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,4 @@ export class zipUtils{

return encodedZipContent;
}

static replaceMarkers(templateFileUri : vscode.Uri, encodedZipContent: string, encodedMainScript: string): Uint8Array | undefined
{
let newData = undefined;

vscode.workspace.fs.readFile(templateFileUri).then((data: Uint8Array) =>{
newData = fileUtils.replaceMarkers(data, new Map<string, string>(
[
['{{zipFileContent}}', encodedZipContent],
['{{mainScript}}', encodedMainScript]
]
));
});

return newData;
}

static writeMarkerFile(manifestFileName : string, newData: Uint8Array)
{
const manifestFileUri: vscode.Uri = vscode.Uri.file(manifestFileName);

vscode.workspace.fs.writeFile(manifestFileUri, newData).then(() => {

vscode.window.showInformationMessage(`Manifest saved, running in minikube.`);

const deployment: string = 'prototype';
const service: string = 'prototype';

terminalUtils.RunCommandsInNewTerminal("Run prototype in minikube",
[`kubectl apply -f ${manifestFileUri.fsPath}`,
`kubectl rollout status deployment/${deployment} --timeout=300s`,
`kubectl port-forward svc/${service} -n default 8000:80`,]);
});
}
}

0 comments on commit abd4371

Please sign in to comment.