fix: add registry url and use spawnAsync

This commit is contained in:
Mauricio Siu 2024-11-28 21:54:20 -06:00
parent 34ea7ad8c9
commit 88f7cf2546
10 changed files with 4278 additions and 276 deletions

View File

@ -21,6 +21,7 @@ const DockerProviderSchema = z.object({
}),
username: z.string().optional(),
password: z.string().optional(),
registryURL: z.string().optional(),
});
type DockerProvider = z.infer<typeof DockerProviderSchema>;
@ -39,6 +40,7 @@ export const SaveDockerProvider = ({ applicationId }: Props) => {
dockerImage: "",
password: "",
username: "",
registryURL: "",
},
resolver: zodResolver(DockerProviderSchema),
});
@ -49,6 +51,7 @@ export const SaveDockerProvider = ({ applicationId }: Props) => {
dockerImage: data.dockerImage || "",
password: data.password || "",
username: data.username || "",
registryURL: data.registryUrl || "",
});
}
}, [form.reset, data, form]);
@ -59,6 +62,7 @@ export const SaveDockerProvider = ({ applicationId }: Props) => {
password: values.password || null,
applicationId,
username: values.username || null,
registryUrl: values.registryURL || null,
})
.then(async () => {
toast.success("Docker Provider Saved");
@ -76,7 +80,7 @@ export const SaveDockerProvider = ({ applicationId }: Props) => {
className="flex flex-col gap-4"
>
<div className="grid md:grid-cols-2 gap-4 ">
<div className="md:col-span-2 space-y-4">
<div className="space-y-4">
<FormField
control={form.control}
name="dockerImage"
@ -91,6 +95,19 @@ export const SaveDockerProvider = ({ applicationId }: Props) => {
)}
/>
</div>
<FormField
control={form.control}
name="registryURL"
render={({ field }) => (
<FormItem>
<FormLabel>Registry URL</FormLabel>
<FormControl>
<Input placeholder="Registry URL" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<div className="space-y-4">
<FormField
control={form.control}

View File

@ -0,0 +1 @@
ALTER TABLE "application" ADD COLUMN "registryUrl" text;

File diff suppressed because it is too large Load Diff

View File

@ -323,6 +323,13 @@
"when": 1732644181718,
"tag": "0045_smiling_blur",
"breakpoints": true
},
{
"idx": 46,
"version": "6",
"when": 1732851191048,
"tag": "0046_purple_sleeper",
"breakpoints": true
}
]
}

View File

@ -384,6 +384,7 @@ export const applicationRouter = createTRPCRouter({
password: input.password,
sourceType: "docker",
applicationStatus: "idle",
registryUrl: input.registryUrl,
});
return true;

View File

@ -147,6 +147,7 @@ export const applications = pgTable("application", {
username: text("username"),
password: text("password"),
dockerImage: text("dockerImage"),
registryUrl: text("registryUrl"),
// Git
customGitUrl: text("customGitUrl"),
customGitBranch: text("customGitBranch"),
@ -348,6 +349,7 @@ const createSchema = createInsertSchema(applications, {
dockerImage: z.string().optional(),
username: z.string().optional(),
password: z.string().optional(),
registryUrl: z.string().optional(),
customGitSSHKeyId: z.string().optional(),
repository: z.string().optional(),
dockerfile: z.string().optional(),
@ -451,6 +453,7 @@ export const apiSaveDockerProvider = createSchema
applicationId: true,
username: true,
password: true,
registryUrl: true,
})
.required();

View File

@ -208,14 +208,6 @@ export const deployApplication = async ({
adminId: application.project.adminId,
});
console.log(
"Error on ",
application.buildType,
"/",
application.sourceType,
error,
);
throw error;
}

View File

@ -11,12 +11,13 @@ import type { MysqlNested } from "../databases/mysql";
import type { PostgresNested } from "../databases/postgres";
import type { RedisNested } from "../databases/redis";
import { execAsync, execAsyncRemote } from "../process/execAsync";
import { spawnAsync } from "../process/spawnAsync";
import { getRemoteDocker } from "../servers/remote-docker";
interface RegistryAuth {
username: string;
password: string;
serveraddress: string;
registryUrl: string;
}
export const pullImage = async (
@ -29,29 +30,21 @@ export const pullImage = async (
throw new Error("Docker image not found");
}
await new Promise((resolve, reject) => {
docker.pull(dockerImage, { authconfig: authConfig }, (err, stream) => {
if (err) {
reject(err);
return;
}
docker.modem.followProgress(
stream as Readable,
(err: Error | null, res) => {
if (!err) {
resolve(res);
}
if (err) {
reject(err);
}
},
(event) => {
onData?.(event);
},
if (authConfig?.username && authConfig?.password) {
await spawnAsync(
"docker",
[
"login",
authConfig.registryUrl || "",
"-u",
authConfig.username,
"-p",
authConfig.password,
],
onData,
);
});
});
}
await spawnAsync("docker", ["pull", dockerImage], onData);
} catch (error) {
throw error;
}

View File

@ -5,7 +5,7 @@ import { pullImage } from "../docker/utils";
interface RegistryAuth {
username: string;
password: string;
serveraddress: string;
registryUrl: string;
}
export const buildDocker = async (
@ -16,6 +16,7 @@ export const buildDocker = async (
const authConfig: Partial<RegistryAuth> = {
username: username || "",
password: password || "",
registryUrl: application.registryUrl || "",
};
const writeStream = createWriteStream(logPath, { flags: "a" });
@ -33,7 +34,7 @@ export const buildDocker = async (
dockerImage,
(data) => {
if (writeStream.writable) {
writeStream.write(`${data.status}\n`);
writeStream.write(`${data}\n`);
}
},
authConfig,
@ -41,7 +42,7 @@ export const buildDocker = async (
await mechanizeDockerContainer(application);
writeStream.write("\nDocker Deployed: ✅\n");
} catch (error) {
writeStream.write(`ERROR: ${error}: ❌`);
writeStream.write("❌ Error");
throw error;
} finally {
writeStream.end();

View File

@ -34,7 +34,7 @@ export const authGithub = (githubProvider: Github): Octokit => {
};
export const getGithubToken = async (
octokit: ReturnType<typeof authGithub>,
octokit: ReturnType<typeof authGithub>
) => {
const installation = (await octokit.auth({
type: "installation",
@ -77,7 +77,7 @@ export type ComposeWithGithub = InferResultType<"compose", { github: true }>;
export const cloneGithubRepository = async (
entity: ApplicationWithGithub | ComposeWithGithub,
logPath: string,
isCompose = false,
isCompose = false
) => {
const { APPLICATIONS_PATH, COMPOSE_PATH } = paths();
const writeStream = createWriteStream(logPath, { flags: "a" });
@ -95,7 +95,7 @@ export const cloneGithubRepository = async (
// Check if requirements are met
if (requirements.length > 0) {
writeStream.write(
`\nGitHub Repository configuration failed for application: ${appName}\n`,
`\nGitHub Repository configuration failed for application: ${appName}\n`
);
writeStream.write("Reasons:\n");
writeStream.write(requirements.join("\n"));
@ -134,7 +134,7 @@ export const cloneGithubRepository = async (
if (writeStream.writable) {
writeStream.write(data);
}
},
}
);
writeStream.write(`\nCloned ${repoclone}: ✅\n`);
} catch (error) {
@ -148,7 +148,7 @@ export const cloneGithubRepository = async (
export const getGithubCloneCommand = async (
entity: ApplicationWithGithub | ComposeWithGithub,
logPath: string,
isCompose = false,
isCompose = false
) => {
const { appName, repository, owner, branch, githubId, serverId } = entity;
@ -206,7 +206,7 @@ export const getGithubCloneCommand = async (
rm -rf ${outputPath};
mkdir -p ${outputPath};
if ! git clone --branch ${branch} --depth 1 --recurse-submodules --progress ${cloneUrl} ${outputPath} >> ${logPath} 2>&1; then
echo "❌ [ERROR] Fallo al clonar el repositorio ${repoclone}" >> ${logPath};
echo "❌ [ERROR] Fail to clone repository ${repoclone}" >> ${logPath};
exit 1;
fi
echo "Cloned ${repoclone} to ${outputPath}: ✅" >> ${logPath};
@ -302,7 +302,7 @@ export const getGithubRepositories = async (githubId?: string) => {
});
const repositories = (await octokit.paginate(
octokit.rest.apps.listReposAccessibleToInstallation,
octokit.rest.apps.listReposAccessibleToInstallation
)) as unknown as Awaited<
ReturnType<typeof octokit.rest.apps.listReposAccessibleToInstallation>
>["data"]["repositories"];
@ -311,7 +311,7 @@ export const getGithubRepositories = async (githubId?: string) => {
};
export const getGithubBranches = async (
input: typeof apiFindGithubBranches._type,
input: typeof apiFindGithubBranches._type
) => {
if (!input.githubId) {
return [];