Skip to content

Commit

Permalink
feat: add update registry and fix the docker url markup
Browse files Browse the repository at this point in the history
  • Loading branch information
Siumauricio committed May 14, 2024
1 parent d19dec8 commit 08517d6
Show file tree
Hide file tree
Showing 11 changed files with 366 additions and 67 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ FROM node:18-slim AS production
# Install dependencies only for production
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable && apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
RUN corepack enable && apt-get update && apt-get install -y curl && apt-get install -y apache2-utils && rm -rf /var/lib/apt/lists/*

WORKDIR /app

Expand All @@ -47,7 +47,6 @@ RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-l
# Install docker
RUN curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh && rm get-docker.sh


# Install Nixpacks and tsx
# | VERBOSE=1 VERSION=1.21.0 bash
RUN curl -sSL https://nixpacks.com/install.sh -o install.sh \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,24 +217,24 @@ export const AddRegistry = () => {
variant={"secondary"}
isLoading={isLoading}
onClick={async () => {
if (!form.formState.isValid) {
toast.error("Please fill all the fields");
return;
}
await testRegistry({
username: username,
password: password,
registryUrl: registryUrl,
registryName: registryName,
registryType: "cloud",
imagePrefix: imagePrefix,
}).then((data) => {
if (data) {
toast.success("Registry Tested Successfully");
} else {
toast.error("Registry Test Failed");
}
});
})
.then((data) => {
if (data) {
toast.success("Registry Tested Successfully");
} else {
toast.error("Registry Test Failed");
}
})
.catch(() => {
toast.error("Error to test the registry");
});
}}
>
Test Registry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Server } from "lucide-react";
import { AddRegistry } from "./add-docker-registry";
import { AddSelfHostedRegistry } from "./add-self-docker-registry";
import { DeleteRegistry } from "./delete-registry";
import { UpdateDockerRegistry } from "./update-docker-registry";

export const ShowRegistry = () => {
const { data } = api.registry.all.useQuery();
Expand Down Expand Up @@ -49,8 +50,6 @@ export const ShowRegistry = () => {
<AddSelfHostedRegistry />
<AddRegistry />
</div>

{/* <AddCertificate /> */}
</div>
) : (
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
Expand All @@ -63,12 +62,11 @@ export const ShowRegistry = () => {
{index + 1}. {registry.registryName}
</span>
<div className="flex flex-row gap-3">
<UpdateDockerRegistry registryId={registry.registryId} />
<DeleteRegistry registryId={registry.registryId} />
</div>
</div>
))}

<div>{/* <AddCertificate /> */}</div>
</div>
)}
</CardContent>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { cn } from "@/lib/utils";
import { api } from "@/utils/api";
import { zodResolver } from "@hookform/resolvers/zod";
import { AlertTriangle, PenBoxIcon } from "lucide-react";
import { useEffect } from "react";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
import { z } from "zod";

const updateRegistry = z.object({
registryName: z.string().min(1, {
message: "Registry name is required",
}),
username: z.string().min(1, {
message: "Username is required",
}),
password: z.string(),
registryUrl: z.string().min(1, {
message: "Registry URL is required",
}),
imagePrefix: z.string(),
});

type UpdateRegistry = z.infer<typeof updateRegistry>;

interface Props {
registryId: string;
}

export const UpdateDockerRegistry = ({ registryId }: Props) => {
const utils = api.useUtils();
const { mutateAsync: testRegistry, isLoading } =
api.registry.testRegistry.useMutation();
const { data, refetch } = api.registry.one.useQuery(
{
registryId,
},
{
enabled: !!registryId,
},
);

const isCloud = data?.registryType === "cloud";
const { mutateAsync, isError, error } = api.registry.update.useMutation();

const form = useForm<UpdateRegistry>({
defaultValues: {
imagePrefix: "",
registryName: "",
username: "",
password: "",
registryUrl: "",
},
resolver: zodResolver(updateRegistry),
});

const password = form.watch("password");
const username = form.watch("username");
const registryUrl = form.watch("registryUrl");
const registryName = form.watch("registryName");
const imagePrefix = form.watch("imagePrefix");

useEffect(() => {
if (data) {
form.reset({
imagePrefix: data.imagePrefix || "",
registryName: data.registryName || "",
username: data.username || "",
password: "",
registryUrl: data.registryUrl || "",
});
}
}, [form, form.reset, data]);

const onSubmit = async (data: UpdateRegistry) => {
await mutateAsync({
registryId,
...(data.password ? { password: data.password } : {}),
registryName: data.registryName,
username: data.username,
registryUrl: data.registryUrl,
imagePrefix: data.imagePrefix,
})
.then(async (data) => {
toast.success("Registry Updated");
await refetch();
await utils.registry.all.invalidate();
})
.catch(() => {
toast.error("Error to update the registry");
});
};
return (
<Dialog>
<DialogTrigger className="" asChild>
<Button variant="ghost">
<PenBoxIcon className="size-4 text-muted-foreground" />
</Button>
</DialogTrigger>
<DialogContent className="max-h-screen overflow-y-auto sm:max-w-2xl">
<DialogHeader>
<DialogTitle>Registry</DialogTitle>
<DialogDescription>Update the registry information</DialogDescription>
</DialogHeader>
{isError && (
<div className="flex flex-row gap-4 rounded-lg bg-red-50 p-2 dark:bg-red-950">
<AlertTriangle className="text-red-600 dark:text-red-400" />
<span className="text-sm text-red-600 dark:text-red-400">
{error?.message}
</span>
</div>
)}

<Form {...form}>
<form
id="hook-form"
onSubmit={form.handleSubmit(onSubmit)}
className="grid w-full gap-8 "
>
<div className="flex flex-col gap-4">
<div className="flex flex-col gap-2">
<FormField
control={form.control}
name="registryName"
render={({ field }) => (
<FormItem>
<FormLabel>Registry Name</FormLabel>
<FormControl>
<Input placeholder="Registry Name" {...field} />
</FormControl>

<FormMessage />
</FormItem>
)}
/>

<FormField
control={form.control}
name="username"
render={({ field }) => (
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl>
<Input placeholder="Username" {...field} />
</FormControl>

<FormMessage />
</FormItem>
)}
/>

<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<FormLabel>Password</FormLabel>
<FormControl>
<Input
placeholder="Password"
{...field}
type="password"
/>
</FormControl>

<FormMessage />
</FormItem>
)}
/>
{isCloud && (
<FormField
control={form.control}
name="imagePrefix"
render={({ field }) => (
<FormItem>
<FormLabel>Image Prefix</FormLabel>
<FormControl>
<Input {...field} placeholder="Image Prefix" />
</FormControl>

<FormMessage />
</FormItem>
)}
/>
)}

<FormField
control={form.control}
name="registryUrl"
render={({ field }) => (
<FormItem>
<FormLabel>Registry URL</FormLabel>
<FormControl>
<Input
placeholder="https://aws_account_id.dkr.ecr.us-west-2.amazonaws.com"
{...field}
/>
</FormControl>

<FormMessage />
</FormItem>
)}
/>
</div>
</div>
</form>

<DialogFooter
className={cn(
isCloud ? "sm:justify-between " : "",
"flex flex-row w-full gap-4 flex-wrap",
)}
>
{isCloud && (
<Button
type="button"
variant={"secondary"}
isLoading={isLoading}
onClick={async () => {
await testRegistry({
username: username,
password: password,
registryUrl: registryUrl,
registryName: registryName,
registryType: "cloud",
imagePrefix: imagePrefix,
})
.then((data) => {
if (data) {
toast.success("Registry Tested Successfully");
} else {
toast.error("Registry Test Failed");
}
})
.catch(() => {
toast.error("Error to test the registry");
});
}}
>
Test Registry
</Button>
)}

<Button
isLoading={form.formState.isSubmitting}
form="hook-form"
type="submit"
>
Update
</Button>
</DialogFooter>
</Form>
</DialogContent>
</Dialog>
);
};
Loading

0 comments on commit 08517d6

Please sign in to comment.