Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit 480d5ae

Browse files
authored
Clean up spk dashboard (#99)
1 parent df56d88 commit 480d5ae

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

docs/service-introspection.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ Launch the service introspection dashboard
101101
102102
Options:
103103
-p, --port <port> Port to launch the dashboard on (default: 4040)
104+
-r, --remove-all Removes previously launched instances of the dashboard (default: false)
104105
-h, --help output usage information
105106
```
106107

src/commands/deployment/dashboard.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ afterAll(() => {
2525
describe("Validate dashboard container pull", () => {
2626
test("Pull dashboard container if docker is installed", async () => {
2727
try {
28-
const dashboardContainerId = await launchDashboard(2020);
28+
const dashboardContainerId = await launchDashboard(2020, false);
2929
const dockerInstalled = await validatePrereqs(["docker"], false);
3030
if (dockerInstalled) {
3131
const dockerId = await exec("docker", [
@@ -45,3 +45,30 @@ describe("Validate dashboard container pull", () => {
4545
}
4646
}, 30000);
4747
});
48+
49+
describe("Validate dashboard clean up", () => {
50+
test("Launch the dashboard two times", async () => {
51+
try {
52+
const dashboardContainerId = await launchDashboard(2020, true);
53+
const dockerInstalled = await validatePrereqs(["docker"], false);
54+
if (dockerInstalled) {
55+
const dockerId = await exec("docker", [
56+
"images",
57+
"-q",
58+
Config().introspection!.dashboard!.image!
59+
]);
60+
61+
expect(dockerId).toBeDefined();
62+
expect(dashboardContainerId).not.toBe("");
63+
logger.info("Verified that docker image has been pulled.");
64+
const dashboardContainerId2 = await launchDashboard(2020, true);
65+
expect(dashboardContainerId).not.toBe(dashboardContainerId2);
66+
await exec("docker", ["container", "stop", dashboardContainerId2]);
67+
} else {
68+
expect(dashboardContainerId).toBe("");
69+
}
70+
} catch (err) {
71+
logger.error(err);
72+
}
73+
}, 30000);
74+
});

src/commands/deployment/dashboard.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ export const dashboardCommandDecorator = (command: commander.Command): void => {
1515
.alias("d")
1616
.description("Launch the service introspection dashboard")
1717
.option("-p, --port <port>", "Port to launch the dashboard on", 4040)
18+
.option(
19+
"-r, --remove-all",
20+
"Removes previously launched instances of the dashboard",
21+
false
22+
)
1823
.action(async opts => {
1924
const config = Config();
2025
if (
@@ -33,17 +38,53 @@ export const dashboardCommandDecorator = (command: commander.Command): void => {
3338
);
3439
return;
3540
}
36-
if (await launchDashboard(opts.port)) {
41+
if (await launchDashboard(opts.port, opts.removeAll)) {
3742
await open("http://localhost:" + opts.port);
3843
}
3944
});
4045
};
4146

42-
export const launchDashboard = async (port: number): Promise<string> => {
47+
/**
48+
* Cleans previously launched spk dashboard docker containers
49+
*/
50+
export const cleanDashboarContainers = async () => {
51+
const config = Config();
52+
53+
let dockerOutput = await exec("docker", [
54+
"ps",
55+
"-a",
56+
"-q",
57+
"--filter",
58+
"ancestor=" + config.introspection!.dashboard!.image!,
59+
'--format="{{.ID}}"'
60+
]);
61+
if (dockerOutput.length > 0) {
62+
dockerOutput = dockerOutput.replace(/\n/g, " ");
63+
dockerOutput = dockerOutput.replace(/"/g, "");
64+
const containerIds = dockerOutput.split(" ");
65+
const args = ["kill", ...containerIds];
66+
67+
await exec("docker", args);
68+
}
69+
};
70+
71+
/**
72+
* Launches an instance of the spk dashboard
73+
* @param port the port number to launch the dashboard
74+
*/
75+
export const launchDashboard = async (
76+
port: number,
77+
removeAll: boolean
78+
): Promise<string> => {
4379
try {
4480
if (!(await validatePrereqs(["docker"], false))) {
4581
return "";
4682
}
83+
// Clean previous dashboard containers
84+
if (removeAll) {
85+
await cleanDashboarContainers();
86+
}
87+
4788
const config = Config();
4889
const dockerRepository = config.introspection!.dashboard!.image!;
4990
logger.info("Pulling dashboard docker image");

0 commit comments

Comments
 (0)