mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
Merge pull request #926 from thewilloftheshadow/feat/mongo-replica-sets
feat: mongo replica sets
This commit is contained in:
@@ -18,6 +18,7 @@ import { DropdownMenuItem } from "@/components/ui/dropdown-menu";
|
|||||||
import {
|
import {
|
||||||
Form,
|
Form,
|
||||||
FormControl,
|
FormControl,
|
||||||
|
FormDescription,
|
||||||
FormField,
|
FormField,
|
||||||
FormItem,
|
FormItem,
|
||||||
FormLabel,
|
FormLabel,
|
||||||
@@ -35,6 +36,7 @@ import {
|
|||||||
SelectTrigger,
|
SelectTrigger,
|
||||||
SelectValue,
|
SelectValue,
|
||||||
} from "@/components/ui/select";
|
} from "@/components/ui/select";
|
||||||
|
import { Switch } from "@/components/ui/switch";
|
||||||
import { Textarea } from "@/components/ui/textarea";
|
import { Textarea } from "@/components/ui/textarea";
|
||||||
import { slugify } from "@/lib/slug";
|
import { slugify } from "@/lib/slug";
|
||||||
import { api } from "@/utils/api";
|
import { api } from "@/utils/api";
|
||||||
@@ -95,6 +97,7 @@ const mySchema = z.discriminatedUnion("type", [
|
|||||||
.object({
|
.object({
|
||||||
type: z.literal("mongo"),
|
type: z.literal("mongo"),
|
||||||
databaseUser: z.string().default("mongo"),
|
databaseUser: z.string().default("mongo"),
|
||||||
|
replicaSets: z.boolean().default(false),
|
||||||
})
|
})
|
||||||
.merge(baseDatabaseSchema),
|
.merge(baseDatabaseSchema),
|
||||||
z
|
z
|
||||||
@@ -216,6 +219,7 @@ export const AddDatabase = ({ projectId, projectName }: Props) => {
|
|||||||
databaseUser:
|
databaseUser:
|
||||||
data.databaseUser || databasesUserDefaultPlaceholder[data.type],
|
data.databaseUser || databasesUserDefaultPlaceholder[data.type],
|
||||||
serverId: data.serverId,
|
serverId: data.serverId,
|
||||||
|
replicaSets: data.replicaSets,
|
||||||
});
|
});
|
||||||
} else if (data.type === "redis") {
|
} else if (data.type === "redis") {
|
||||||
promise = redisMutation.mutateAsync({
|
promise = redisMutation.mutateAsync({
|
||||||
@@ -542,6 +546,30 @@ export const AddDatabase = ({ projectId, projectName }: Props) => {
|
|||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
{type === "mongo" && (
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="replicaSets"
|
||||||
|
render={({ field }) => {
|
||||||
|
return (
|
||||||
|
<FormItem className="flex flex-row items-center justify-between p-3 mt-4 border rounded-lg shadow-sm">
|
||||||
|
<div className="space-y-0.5">
|
||||||
|
<FormLabel>Use Replica Sets</FormLabel>
|
||||||
|
</div>
|
||||||
|
<FormControl>
|
||||||
|
<Switch
|
||||||
|
checked={field.value}
|
||||||
|
onCheckedChange={field.onChange}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
1
apps/dokploy/drizzle/0053_broken_kulan_gath.sql
Normal file
1
apps/dokploy/drizzle/0053_broken_kulan_gath.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE "mongo" ADD COLUMN "replicaSets" boolean DEFAULT false;
|
||||||
4253
apps/dokploy/drizzle/meta/0053_snapshot.json
Normal file
4253
apps/dokploy/drizzle/meta/0053_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -372,6 +372,13 @@
|
|||||||
"when": 1734809337308,
|
"when": 1734809337308,
|
||||||
"tag": "0052_bumpy_luckman",
|
"tag": "0052_bumpy_luckman",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 53,
|
||||||
|
"version": "6",
|
||||||
|
"when": 1735118844878,
|
||||||
|
"tag": "0053_broken_kulan_gath",
|
||||||
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { relations } from "drizzle-orm";
|
import { relations } from "drizzle-orm";
|
||||||
import { integer, pgTable, text } from "drizzle-orm/pg-core";
|
import { boolean, integer, pgTable, text } from "drizzle-orm/pg-core";
|
||||||
import { createInsertSchema } from "drizzle-zod";
|
import { createInsertSchema } from "drizzle-zod";
|
||||||
import { nanoid } from "nanoid";
|
import { nanoid } from "nanoid";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
@@ -43,6 +43,7 @@ export const mongo = pgTable("mongo", {
|
|||||||
serverId: text("serverId").references(() => server.serverId, {
|
serverId: text("serverId").references(() => server.serverId, {
|
||||||
onDelete: "cascade",
|
onDelete: "cascade",
|
||||||
}),
|
}),
|
||||||
|
replicaSets: boolean("replicaSets").default(false),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const mongoRelations = relations(mongo, ({ one, many }) => ({
|
export const mongoRelations = relations(mongo, ({ one, many }) => ({
|
||||||
@@ -77,6 +78,7 @@ const createSchema = createInsertSchema(mongo, {
|
|||||||
externalPort: z.number(),
|
externalPort: z.number(),
|
||||||
description: z.string().optional(),
|
description: z.string().optional(),
|
||||||
serverId: z.string().optional(),
|
serverId: z.string().optional(),
|
||||||
|
replicaSets: z.boolean().default(false),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const apiCreateMongo = createSchema
|
export const apiCreateMongo = createSchema
|
||||||
@@ -89,6 +91,7 @@ export const apiCreateMongo = createSchema
|
|||||||
databaseUser: true,
|
databaseUser: true,
|
||||||
databasePassword: true,
|
databasePassword: true,
|
||||||
serverId: true,
|
serverId: true,
|
||||||
|
replicaSets: true,
|
||||||
})
|
})
|
||||||
.required();
|
.required();
|
||||||
|
|
||||||
|
|||||||
@@ -28,17 +28,66 @@ export const buildMongo = async (mongo: MongoNested) => {
|
|||||||
databasePassword,
|
databasePassword,
|
||||||
command,
|
command,
|
||||||
mounts,
|
mounts,
|
||||||
|
replicaSets,
|
||||||
} = mongo;
|
} = mongo;
|
||||||
|
|
||||||
const defaultMongoEnv = `MONGO_INITDB_ROOT_USERNAME=${databaseUser}\nMONGO_INITDB_ROOT_PASSWORD=${databasePassword}${
|
const startupScript = `
|
||||||
|
#!/bin/bash
|
||||||
|
${
|
||||||
|
replicaSets
|
||||||
|
? `
|
||||||
|
mongod --port 27017 --replSet rs0 --bind_ip_all &
|
||||||
|
MONGOD_PID=$!
|
||||||
|
|
||||||
|
# Wait for MongoDB to be ready
|
||||||
|
while ! mongosh --eval "db.adminCommand('ping')" > /dev/null 2>&1; do
|
||||||
|
sleep 2
|
||||||
|
done
|
||||||
|
|
||||||
|
# Check if replica set is already initialized
|
||||||
|
REPLICA_STATUS=$(mongosh --quiet --eval "rs.status().ok || 0")
|
||||||
|
|
||||||
|
if [ "$REPLICA_STATUS" != "1" ]; then
|
||||||
|
echo "Initializing replica set..."
|
||||||
|
mongosh --eval '
|
||||||
|
rs.initiate({
|
||||||
|
_id: "rs0",
|
||||||
|
members: [{ _id: 0, host: "localhost:27017", priority: 1 }]
|
||||||
|
});
|
||||||
|
|
||||||
|
// Wait for the replica set to initialize
|
||||||
|
while (!rs.isMaster().ismaster) {
|
||||||
|
sleep(1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create root user after replica set is initialized and we are primary
|
||||||
|
db.getSiblingDB("admin").createUser({
|
||||||
|
user: "${databaseUser}",
|
||||||
|
pwd: "${databasePassword}",
|
||||||
|
roles: ["root"]
|
||||||
|
});
|
||||||
|
'
|
||||||
|
|
||||||
|
else
|
||||||
|
echo "Replica set already initialized."
|
||||||
|
fi
|
||||||
|
`
|
||||||
|
: ""
|
||||||
|
}
|
||||||
|
|
||||||
|
${command ?? "wait $MONGOD_PID"}`;
|
||||||
|
|
||||||
|
const defaultMongoEnv = `MONGO_INITDB_ROOT_USERNAME=${databaseUser}\nMONGO_INITDB_ROOT_PASSWORD=${databasePassword}${replicaSets ? "\nMONGO_INITDB_DATABASE=admin" : ""}${
|
||||||
env ? `\n${env}` : ""
|
env ? `\n${env}` : ""
|
||||||
}`;
|
}`;
|
||||||
|
|
||||||
const resources = calculateResources({
|
const resources = calculateResources({
|
||||||
memoryLimit,
|
memoryLimit,
|
||||||
memoryReservation,
|
memoryReservation,
|
||||||
cpuLimit,
|
cpuLimit,
|
||||||
cpuReservation,
|
cpuReservation,
|
||||||
});
|
});
|
||||||
|
|
||||||
const envVariables = prepareEnvironmentVariables(
|
const envVariables = prepareEnvironmentVariables(
|
||||||
defaultMongoEnv,
|
defaultMongoEnv,
|
||||||
mongo.project.env,
|
mongo.project.env,
|
||||||
@@ -56,12 +105,17 @@ export const buildMongo = async (mongo: MongoNested) => {
|
|||||||
Image: dockerImage,
|
Image: dockerImage,
|
||||||
Env: envVariables,
|
Env: envVariables,
|
||||||
Mounts: [...volumesMount, ...bindsMount, ...filesMount],
|
Mounts: [...volumesMount, ...bindsMount, ...filesMount],
|
||||||
...(command
|
...(replicaSets
|
||||||
? {
|
? {
|
||||||
Command: ["/bin/sh"],
|
Command: ["/bin/bash"],
|
||||||
Args: ["-c", command],
|
Args: ["-c", startupScript],
|
||||||
}
|
}
|
||||||
: {}),
|
: {
|
||||||
|
...(command && {
|
||||||
|
Command: ["/bin/bash"],
|
||||||
|
Args: ["-c", command],
|
||||||
|
}),
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
Networks: [{ Target: "dokploy-network" }],
|
Networks: [{ Target: "dokploy-network" }],
|
||||||
Resources: {
|
Resources: {
|
||||||
@@ -90,6 +144,7 @@ export const buildMongo = async (mongo: MongoNested) => {
|
|||||||
: [],
|
: [],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const service = docker.getService(appName);
|
const service = docker.getService(appName);
|
||||||
const inspect = await service.inspect();
|
const inspect = await service.inspect();
|
||||||
|
|||||||
Reference in New Issue
Block a user