diff --git a/.vscode/fastify.code-snippets b/.vscode/fastify.code-snippets index c8f4771cc..866416314 100644 --- a/.vscode/fastify.code-snippets +++ b/.vscode/fastify.code-snippets @@ -13,5 +13,27 @@ "", "export default ${1:$TM_FILENAME_BASE};" ] + }, + + "schema": { + "scope": "typescript", + "prefix": "schema", + "body": [ + "import { FastifyInstance } from \"fastify\";", + "import fp from \"fastify-plugin\";", + "import { Schema } from \"mongoose\";", + "", + "const ${2:$TM_FILENAME_BASE} = fp((app: FastifyInstance, _: {}, done: () => void) => {", + "\tconst schema = new Schema({", + "\t\t${0}", + "\t});", + "\t", + "\tapp.decorate(\"${2:$TM_FILENAME_BASE}\", app.mongoose.model(\"${1}\", schema));", + "\t", + "\tdone();", + "});", + "", + "export default ${2:$TM_FILENAME_BASE};" + ] } } \ No newline at end of file diff --git a/core/src/db/index.ts b/core/src/db/index.ts deleted file mode 100644 index 3ab94e963..000000000 --- a/core/src/db/index.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { FastifyInstance } from "fastify"; -import fp from "fastify-plugin"; -import mongoose, { Model } from "mongoose"; -import { MONGO_URL } from "../config"; -import { User } from "@monitor/types"; - -declare module "fastify" { - interface FastifyInstance { - mongoose: typeof mongoose; - users: Model; - } -} - -const db = fp((app: FastifyInstance, _: {}, done: () => void) => { - mongoose.connect(MONGO_URL); - - app.decorate("mongoose", mongoose); - - done(); -}); - -export default db; \ No newline at end of file diff --git a/core/src/main.ts b/core/src/main.ts index 5d75a6af3..eae1c205d 100644 --- a/core/src/main.ts +++ b/core/src/main.ts @@ -1,9 +1,9 @@ import fastify from "fastify"; import fastifyCors from "fastify-cors"; -import auth from "./auth"; +import auth from "./util/auth"; import { LOG, PORT } from "./config"; -import db from "./db"; -import ws from "./ws"; +import db from "./util/db"; +import ws from "./util/ws"; const app = fastify({ logger: LOG }) .register(fastifyCors) diff --git a/core/src/auth/github.ts b/core/src/util/auth/github.ts similarity index 96% rename from core/src/auth/github.ts rename to core/src/util/auth/github.ts index 874cf8a1e..74cfc69b4 100644 --- a/core/src/auth/github.ts +++ b/core/src/util/auth/github.ts @@ -2,7 +2,7 @@ import axios from "axios"; import { FastifyInstance } from "fastify"; import fastifyOauth2, { OAuth2Namespace } from "fastify-oauth2"; import fp from "fastify-plugin"; -import { HOST, SECRETS, TOKEN_EXPIRES_IN } from "../config"; +import { HOST, SECRETS, TOKEN_EXPIRES_IN } from "../../config"; declare module "fastify" { interface FastifyInstance { diff --git a/core/src/auth/index.ts b/core/src/util/auth/index.ts similarity index 100% rename from core/src/auth/index.ts rename to core/src/util/auth/index.ts diff --git a/core/src/auth/jwt.ts b/core/src/util/auth/jwt.ts similarity index 95% rename from core/src/auth/jwt.ts rename to core/src/util/auth/jwt.ts index eb6a4dded..68461f2b7 100644 --- a/core/src/auth/jwt.ts +++ b/core/src/util/auth/jwt.ts @@ -2,7 +2,7 @@ import { FastifyReply, FastifyRequest } from "fastify"; import { FastifyInstance } from "fastify"; import fastifyJwt from "fastify-jwt"; import fp from "fastify-plugin"; -import { SECRETS } from "../config"; +import { SECRETS } from "../../config"; declare module "fastify" { interface FastifyInstance { diff --git a/core/src/auth/local.ts b/core/src/util/auth/local.ts similarity index 96% rename from core/src/auth/local.ts rename to core/src/util/auth/local.ts index 478a7803b..e54b7009a 100644 --- a/core/src/auth/local.ts +++ b/core/src/util/auth/local.ts @@ -2,7 +2,7 @@ import { User } from "@monitor/types"; import { compare, hash } from "bcrypt"; import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify"; import fp from "fastify-plugin"; -import { PASSWORD_SALT_ROUNDS, TOKEN_EXPIRES_IN } from "../config"; +import { PASSWORD_SALT_ROUNDS, TOKEN_EXPIRES_IN } from "../../config"; const local = fp((app: FastifyInstance, _: {}, done: () => void) => { app.post( diff --git a/core/src/util/db/builds.ts b/core/src/util/db/builds.ts new file mode 100644 index 000000000..2eebe570b --- /dev/null +++ b/core/src/util/db/builds.ts @@ -0,0 +1,25 @@ +import { FastifyInstance } from "fastify"; +import fp from "fastify-plugin"; +import { Schema } from "mongoose"; + +const builds = fp((app: FastifyInstance, _: {}, done: () => void) => { + const schema = new Schema({ + name: { type: String, unique: true, index: true }, + /* repo related */ + repo: String, + branch: String, + accessToken: String, // to gain access to private repos + /* build related */ + buildPath: String, // build folder relative to repo root + dockerfilePath: String, // relative to buildPath + pullName: { type: String, index: true }, // derived on build creation + imageName: String, // derived on build creation + owner: { type: String, index: true }, // userID / username + }); + + app.decorate("builds", app.mongoose.model("Build", schema)); + + done(); +}); + +export default builds; diff --git a/core/src/util/db/deployments.ts b/core/src/util/db/deployments.ts new file mode 100644 index 000000000..73694ef49 --- /dev/null +++ b/core/src/util/db/deployments.ts @@ -0,0 +1,34 @@ +import { FastifyInstance } from "fastify"; +import fp from "fastify-plugin"; +import { Schema } from "mongoose"; +import { Conversion, EnvironmentVar, Volume } from "./misc"; + +const deployments = fp((app: FastifyInstance, _: {}, done: () => void) => { + const schema = new Schema({ + name: { type: String, unique: true, index: true }, + owner: { type: String, index: true }, + serverID: { type: String, index: true }, + buildID: { type: String, index: true }, // if deploying a monitor build + /* to create docker run command */ + image: String, // used if deploying an external image (from docker hub) + latest: Boolean, // if custom image, use this to add :latest + ports: [Conversion], + volumes: [Volume], + environment: [EnvironmentVar], + network: String, + restart: String, + postImage: String, // interpolated into run command after the image String + containerUser: String, // after -u in the run command + /* to manage repo for static frontend, mounted as a volume */ + repo: String, + accessToken: String, + pullName: { type: String, index: true }, // for auto pull of repo + containerMount: String, // the file path to mount repo on inside the container + }); + + app.decorate("deployments", app.mongoose.model("Deployment", schema)); + + done(); +}); + +export default deployments; \ No newline at end of file diff --git a/core/src/util/db/index.ts b/core/src/util/db/index.ts new file mode 100644 index 000000000..594bbdcb5 --- /dev/null +++ b/core/src/util/db/index.ts @@ -0,0 +1,35 @@ +import { FastifyInstance } from "fastify"; +import fp from "fastify-plugin"; +import mongoose, { Model } from "mongoose"; +import { MONGO_URL } from "../../config"; +import { Build, Deployment, Update, User } from "@monitor/types"; +import users from "./users"; +import updates from "./updates"; +import deployments from "./deployments"; +import builds from "./builds"; + +declare module "fastify" { + interface FastifyInstance { + mongoose: typeof mongoose; + users: Model; + deployments: Model; + builds: Model; + updates: Model; + } +} + +const db = fp((app: FastifyInstance, _: {}, done: () => void) => { + mongoose.connect(MONGO_URL); + + app.decorate("mongoose", mongoose); + + app + .register(users) + .register(deployments) + .register(builds) + .register(updates); + + done(); +}); + +export default db; \ No newline at end of file diff --git a/core/src/util/db/misc.ts b/core/src/util/db/misc.ts new file mode 100644 index 000000000..1551c0a8c --- /dev/null +++ b/core/src/util/db/misc.ts @@ -0,0 +1,21 @@ +import { Schema } from "mongoose"; + +export const Conversion = new Schema({ + local: String, + container: String, +}); + +export const Volume = new Schema({ + variable: String, + value: String, +}); + +export const EnvironmentVar = new Schema({ + variable: String, + value: String, +}); + +export const Log = new Schema({ + stdout: String, + stderr: String, +}); \ No newline at end of file diff --git a/core/src/util/db/updates.ts b/core/src/util/db/updates.ts new file mode 100644 index 000000000..f7c41a807 --- /dev/null +++ b/core/src/util/db/updates.ts @@ -0,0 +1,25 @@ +import { FastifyInstance } from "fastify"; +import fp from "fastify-plugin"; +import { Schema } from "mongoose"; +import { Log } from "./misc"; + +const updates = fp((app: FastifyInstance, _: {}, done: () => void) => { + const schema = new Schema({ + buildID: { type: String, index: true }, + deploymentID: { type: String, index: true }, + serverID: { type: String, index: true }, + operation: { type: String, index: true }, + command: String, + log: Log, + timestamp: Number, + note: String, + isError: Boolean, + operator: { type: String, index: true }, // the userID or username + }); + + app.decorate("updates", app.mongoose.model("Update", schema)); + + done(); +}); + +export default updates; \ No newline at end of file diff --git a/core/src/db/users.ts b/core/src/util/db/users.ts similarity index 100% rename from core/src/db/users.ts rename to core/src/util/db/users.ts diff --git a/core/src/util/updates.ts b/core/src/util/updates.ts new file mode 100644 index 000000000..e69de29bb diff --git a/core/src/ws/index.ts b/core/src/util/ws/index.ts similarity index 100% rename from core/src/ws/index.ts rename to core/src/util/ws/index.ts diff --git a/core/src/ws/messages/index.ts b/core/src/util/ws/messages/index.ts similarity index 100% rename from core/src/ws/messages/index.ts rename to core/src/util/ws/messages/index.ts