From 3f0b45ac7f9b2987b191ca2054829fa51eac1d45 Mon Sep 17 00:00:00 2001 From: Stavros Date: Mon, 1 Apr 2024 20:40:56 +0300 Subject: [PATCH 1/6] a lot of new stuff --- migrations/0001_running_status.sql | 1 + migrations/meta/0001_snapshot.json | 72 +++++++ migrations/meta/_journal.json | 7 + .../actions/change-password-action.ts | 9 +- src/{server => app}/actions/create-action.ts | 15 +- src/{server => app}/actions/remove-action.ts | 10 +- src/app/actions/start-action.ts | 21 ++ src/app/actions/stop-action.ts | 21 ++ .../create-shell-form/create-shell-form.tsx | 2 +- .../components/info-dialog/index.ts | 1 - .../components/info-dialog/info-dialog.tsx | 145 ------------- .../components/render-shell/render-shell.tsx | 47 ++-- .../components/password-edit/index.ts | 1 + .../password-edit/password-edit.tsx | 83 +++++++ src/app/components/settings-dialog/index.ts | 1 + .../settings-dialog/settings-dialog.tsx | 105 +++++++++ .../shell-actions/shell-actions.tsx | 2 +- src/{server => }/config/config.ts | 0 src/server/db/db.ts | 2 +- src/server/db/migrator.ts | 2 +- src/server/db/schema.ts | 1 + src/server/queries/queries.ts | 11 +- src/server/schemas/container-schmea.ts | 10 - src/server/utils/container-helpers.ts | 138 ------------ src/{server => }/types/types.ts | 1 + src/utils/container-helpers.ts | 204 ++++++++++++++++++ src/{server => }/utils/port-checker.ts | 0 src/{server => }/utils/random-generator.ts | 0 28 files changed, 578 insertions(+), 334 deletions(-) create mode 100644 migrations/0001_running_status.sql create mode 100644 migrations/meta/0001_snapshot.json rename src/{server => app}/actions/change-password-action.ts (56%) rename src/{server => app}/actions/create-action.ts (72%) rename src/{server => app}/actions/remove-action.ts (65%) create mode 100644 src/app/actions/start-action.ts create mode 100644 src/app/actions/stop-action.ts delete mode 100644 src/app/components/render-shell/components/info-dialog/index.ts delete mode 100644 src/app/components/render-shell/components/info-dialog/info-dialog.tsx create mode 100644 src/app/components/settings-dialog/components/password-edit/index.ts create mode 100644 src/app/components/settings-dialog/components/password-edit/password-edit.tsx create mode 100644 src/app/components/settings-dialog/index.ts create mode 100644 src/app/components/settings-dialog/settings-dialog.tsx rename src/{server => }/config/config.ts (100%) delete mode 100644 src/server/schemas/container-schmea.ts delete mode 100644 src/server/utils/container-helpers.ts rename src/{server => }/types/types.ts (84%) create mode 100644 src/utils/container-helpers.ts rename src/{server => }/utils/port-checker.ts (100%) rename src/{server => }/utils/random-generator.ts (100%) diff --git a/migrations/0001_running_status.sql b/migrations/0001_running_status.sql new file mode 100644 index 0000000..55c5143 --- /dev/null +++ b/migrations/0001_running_status.sql @@ -0,0 +1 @@ +ALTER TABLE shells ADD `running` text; \ No newline at end of file diff --git a/migrations/meta/0001_snapshot.json b/migrations/meta/0001_snapshot.json new file mode 100644 index 0000000..d6572d1 --- /dev/null +++ b/migrations/meta/0001_snapshot.json @@ -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": {} + } +} \ No newline at end of file diff --git a/migrations/meta/_journal.json b/migrations/meta/_journal.json index 3ecfcab..a691aa0 100644 --- a/migrations/meta/_journal.json +++ b/migrations/meta/_journal.json @@ -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 } ] } diff --git a/src/server/actions/change-password-action.ts b/src/app/actions/change-password-action.ts similarity index 56% rename from src/server/actions/change-password-action.ts rename to src/app/actions/change-password-action.ts index b5b7833..794ef15 100644 --- a/src/server/actions/change-password-action.ts +++ b/src/app/actions/change-password-action.ts @@ -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 }; diff --git a/src/server/actions/create-action.ts b/src/app/actions/create-action.ts similarity index 72% rename from src/server/actions/create-action.ts rename to src/app/actions/create-action.ts index 11fb6ba..efd11c9 100644 --- a/src/server/actions/create-action.ts +++ b/src/app/actions/create-action.ts @@ -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( @@ -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 }; } diff --git a/src/server/actions/remove-action.ts b/src/app/actions/remove-action.ts similarity index 65% rename from src/server/actions/remove-action.ts rename to src/app/actions/remove-action.ts index c13d81e..dda39d8 100644 --- a/src/server/actions/remove-action.ts +++ b/src/app/actions/remove-action.ts @@ -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"); diff --git a/src/app/actions/start-action.ts b/src/app/actions/start-action.ts new file mode 100644 index 0000000..aabbd65 --- /dev/null +++ b/src/app/actions/start-action.ts @@ -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 }; +}; diff --git a/src/app/actions/stop-action.ts b/src/app/actions/stop-action.ts new file mode 100644 index 0000000..f204b5d --- /dev/null +++ b/src/app/actions/stop-action.ts @@ -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 }; +}; diff --git a/src/app/components/create-shell-form/create-shell-form.tsx b/src/app/components/create-shell-form/create-shell-form.tsx index b8cee24..fd183ea 100644 --- a/src/app/components/create-shell-form/create-shell-form.tsx +++ b/src/app/components/create-shell-form/create-shell-form.tsx @@ -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"; diff --git a/src/app/components/render-shell/components/info-dialog/index.ts b/src/app/components/render-shell/components/info-dialog/index.ts deleted file mode 100644 index 2519fcb..0000000 --- a/src/app/components/render-shell/components/info-dialog/index.ts +++ /dev/null @@ -1 +0,0 @@ -export { InfoDialog } from "./info-dialog"; diff --git a/src/app/components/render-shell/components/info-dialog/info-dialog.tsx b/src/app/components/render-shell/components/info-dialog/info-dialog.tsx deleted file mode 100644 index 2df7d7e..0000000 --- a/src/app/components/render-shell/components/info-dialog/info-dialog.tsx +++ /dev/null @@ -1,145 +0,0 @@ -"use client"; - -import { change } from "@/server/actions/change-password-action"; -import { containerData } from "@/server/types/types"; -import { InfoCircledIcon, Pencil1Icon } from "@radix-ui/react-icons"; -import { - Button, - Dialog, - Flex, - Text, - Code, - Popover, - IconButton, - TextArea, -} from "@radix-ui/themes"; -import React, { FormEvent } from "react"; -import { toast } from "react-toastify"; - -interface shellData { - shell: containerData; -} - -export const InfoDialog: React.FC = ({ shell }) => { - return ( - - - - - - - Shell info - View your shell information - - - - Name:{" "} - - {shell.name} - - - - Distro:{" "} - - {shell.distro} - - - - Username:{" "} - - {shell.distro} - - - - - Port:{" "} - - {shell.port} - - - {shell.extraArgs && ( - - Extra Arguments:{" "} - - {shell.extraArgs} - - - )} - - - - - - - - - - ); -}; - -const PasswordEdit: React.FC = ({ shell }) => { - const [isOpen, setIsOpen] = React.useState(false); - const [isRequired, setIsRequired] = React.useState(true); - - const handleSubmit = async (e: FormEvent) => { - e.preventDefault(); - setIsOpen(false); - const newPassword = new FormData(e.currentTarget).get( - "new-password", - ) as string; - const { success } = await change(shell, newPassword); - if (success) { - toast.success("Password changed successfully!"); - } else { - toast.error("Error in changing password! Please check logs."); - } - }; - return ( - - - Password:{" "} - - {shell.password} - - - - - - setIsOpen(true)} className="size-5"> - - - - - -
handleSubmit(e)}> - -