Skip to content

Commit

Permalink
Merge pull request #9 from steveiliop56/feat/refactor-and-start-stop
Browse files Browse the repository at this point in the history
a lot of new stuff
  • Loading branch information
steveiliop56 authored Apr 1, 2024
2 parents f495687 + 3412814 commit 2268474
Show file tree
Hide file tree
Showing 26 changed files with 444 additions and 210 deletions.
1 change: 1 addition & 0 deletions migrations/0001_running_status.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE shells ADD `running` text;
72 changes: 72 additions & 0 deletions migrations/meta/0001_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"version": "5",
"dialect": "sqlite",
"id": "4e855da6-f319-4b0c-aaf8-9177a674bf7e",
"prevId": "8bb98af3-fe24-4378-8a91-f420b7cccf82",
"tables": {
"shells": {
"name": "shells",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"distro": {
"name": "distro",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"port": {
"name": "port",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"password": {
"name": "password",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"extraArgs": {
"name": "extraArgs",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"running": {
"name": "running",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}
7 changes: 7 additions & 0 deletions migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"when": 1711053809691,
"tag": "0000_create_shells_table",
"breakpoints": false
},
{
"idx": 1,
"version": "5",
"when": 1711990928976,
"tag": "0001_running_status",
"breakpoints": false
}
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "getashell",
"version": "0.1.5",
"version": "0.1.6",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
"use server";

import { revalidatePath } from "next/cache";
import { containerData } from "../types/types";
import { changeShellPassword } from "../queries/queries";
import { changePassword } from "../utils/container-helpers";
import { containerData } from "../../types/types";
import { changeShellPassword } from "../../server/queries/queries";
import { containerHelpers } from "../../utils/container-helpers";

export const change = async (shell: containerData, newPassword: string) => {
shell.password = newPassword;

const { success, error } = await changePassword(shell);
const { success, error } = await new containerHelpers(shell).changePassword();

if (success) {
console.log("Password changed!");
await changeShellPassword(shell);
revalidatePath("/", "layout");
return { success: true };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import {
checkIfShellExists,
getShellIds,
portAvailable,
} from "../queries/queries";
import { createContainer } from "../utils/container-helpers";
} from "../../server/queries/queries";
import { containerHelpers } from "../../utils/container-helpers";
import { revalidatePath } from "next/cache";
import {
createRandomPassowrd,
createRandomPort,
} from "../utils/random-generator";
import { availablePortChecker } from "../utils/port-checker";
} from "../../utils/random-generator";
import { availablePortChecker } from "../../utils/port-checker";

export async function create(name: string, distro: string, extraArgs: string) {
console.log(
Expand All @@ -37,16 +37,17 @@ export async function create(name: string, distro: string, extraArgs: string) {
port: port,
password: createRandomPassowrd(),
extraArgs: extraArgs,
running: true,
};

const ok = await createContainer(data);
const { success, error } = await new containerHelpers(data).createContainer();

if (ok?.success) {
if (success) {
console.log("Server ready!");
await addShell(data);
revalidatePath("/", "layout");
return { success: true };
}
console.error(`Failed to bake server: ${ok?.error}`);
console.error(`Failed to bake server: ${error}`);
return { success: false };
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
"use server";

import { getShellFromId, deleteShell } from "../queries/queries";
import { removeContainer } from "../utils/container-helpers";
import { getShellFromId, deleteShell } from "../../server/queries/queries";
import { containerHelpers } from "../../utils/container-helpers";
import { revalidatePath } from "next/cache";

export async function remove(id: number) {
const shell = await getShellFromId(id);
const remove = await removeContainer(shell);
const remove = await new containerHelpers(shell).removeContainer();

if (remove?.success) {
if (remove.success) {
console.log("Container killed! Removing from db...");
await deleteShell(id);
revalidatePath("/", "layout");
return { success: true };
}
console.error(
`Cannot remove container, still removing from db, error: ${remove?.error}`,
`Cannot remove container, still removing from db, error: ${remove.error}`,
);
await deleteShell(id);
revalidatePath("/", "layout");
Expand Down
21 changes: 21 additions & 0 deletions src/app/actions/start-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use server";

import { changeShellRunningStatus } from "@/server/queries/queries";
import { containerData } from "@/types/types";
import { containerHelpers } from "@/utils/container-helpers";
import { revalidatePath } from "next/cache";

export const start = async (shell: containerData) => {
shell.running = true;
const { success, error } = await new containerHelpers(shell).startContainer();

if (success) {
console.log("Shell started!");
revalidatePath("/", "layout");
await changeShellRunningStatus(shell);
return { success: true };
}

console.error(`Failed to start ${shell.name}! Error: ${error}`);
return { success: false };
};
21 changes: 21 additions & 0 deletions src/app/actions/stop-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use server";

import { changeShellRunningStatus } from "@/server/queries/queries";
import { containerData } from "@/types/types";
import { containerHelpers } from "@/utils/container-helpers";
import { revalidatePath } from "next/cache";

export const stop = async (shell: containerData) => {
shell.running = false;
const { success, error } = await new containerHelpers(shell).stopContainer();

if (success) {
console.log("Shell stopped!");
revalidatePath("/", "layout");
await changeShellRunningStatus(shell);
return { success: true };
}

console.error(`Failed to stop ${shell.name}! Error: ${error}`);
return { success: false };
};
2 changes: 1 addition & 1 deletion src/app/components/create-shell-form/create-shell-form.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { create } from "@/server/actions/create-action";
import { create } from "../../actions/create-action";
import { Button, Flex, Select, TextField } from "@radix-ui/themes";
import { FormEvent } from "react";
import { toast } from "react-toastify";
Expand Down

This file was deleted.

47 changes: 28 additions & 19 deletions src/app/components/render-shell/render-shell.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
"use client";

import { remove } from "@/server/actions/remove-action";
import { containerData } from "@/server/types/types";
import { containerData } from "@/types/types";
import { Card, Flex, Text, Button } from "@radix-ui/themes";
import { SettingsDialog } from "../settings-dialog";
import { toast } from "react-toastify";
import { InfoDialog } from "./components/info-dialog";
import { stop } from "../../actions/stop-action";
import { start } from "../../actions/start-action";

export const renderShell = (shell: containerData) => {
const handleDelete = async (shell: containerData) => {
toast.info(`Deleting ${shell.name}...`);
const { success } = await remove(shell.id);
if (success) {
toast.success("Shell deleted!");
const handleStopStart = async () => {
if (shell.running) {
toast.info(`Stopping ${shell.name}...`);
const { success } = await stop(shell);
if (success) {
toast.success("Shell stopped!");
} else {
toast.error("Error in stopping shell! Please check logs!");
}
} else {
toast.error(
"Error in deleting shell, please check logs. Still removing from database...",
);
toast.info(`Starting ${shell.name}...`);
const { success } = await start(shell);
if (success) {
toast.success("Shell starrted");
} else {
toast.error("Error in starrting shell! Please check logs!");
}
}
};

Expand All @@ -26,14 +35,14 @@ export const renderShell = (shell: containerData) => {
<Text weight="medium">{shell.name}</Text>
</Flex>
<Flex className="flex-row gap-1 items-center">
<InfoDialog shell={shell} />
<Button
onClick={() => handleDelete(shell)}
color="orange"
variant="soft"
>
Delete
</Button>
<SettingsDialog shell={shell} />
{shell.running ? (
<Button onClick={() => handleStopStart()} color="red">
Stop
</Button>
) : (
<Button onClick={() => handleStopStart()}>Start</Button>
)}
</Flex>
</Flex>
</Card>
Expand Down
1 change: 1 addition & 0 deletions src/app/components/settings-dialog/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { SettingsDialog } from "./settings-dialog";
Loading

0 comments on commit 2268474

Please sign in to comment.