Skip to content

Commit

Permalink
overwrite main with preview content (#96)
Browse files Browse the repository at this point in the history
* overwrite main with preview content

* deleted test folder

* deleted old part store
  • Loading branch information
Sebastian689 authored Feb 20, 2025
1 parent b341468 commit 8046f88
Show file tree
Hide file tree
Showing 126 changed files with 5,144 additions and 3,182 deletions.
5 changes: 3 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ DATABASE_URL=postgres://username:password@timescaledb:5432/database

## Stateless
INTERFACE_FQDN=http://localhost
MICROSOFT_TENANT_ID=common
MICROSOFT_TENANT_ID=
MICROSOFT_CLIENT_ID="d03453634..."
MICROSOFT_CLIENT_SECRET="jcmads...."
RESEND_CLIENT_SECRET=
PROD=true

## Monitor
Expand All @@ -23,7 +24,7 @@ HCLOUD_TOKEN=

## Nixos
SERVER_IP=
SSH_KEYS="ssh-rsa AAAAB3..."
SSH_PUBLIC_KEYS="ssh-rsa AAAAB3..."
PASSWORD_HASH=

## S3 Storage
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
POSTGRES_USER: ${{ secrets.POSTGRES_USER }}
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
DATABASE_URL: postgres://${{ secrets.POSTGRES_USER }}:${{ secrets.POSTGRES_PASSWORD }}@timescaledb:5432/${{ vars.POSTGRES_DB }}
RESEND_CLIENT_SECRET: ${{ secrets.RESEND_CLIENT_SECRET }}
PORTAINER_FQDN: ${{ vars.PORTAINER_FQDN }}

steps:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- preview
pull_request:

jobs:
check:
Expand Down
1,081 changes: 1,081 additions & 0 deletions bun.lock

Large diffs are not rendered by default.

Binary file modified bun.lockb
100755 → 100644
Binary file not shown.
29 changes: 29 additions & 0 deletions ci-cd/bun/src/backup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { $, env, file } from "bun";

// Validate required environment variables
console.log("🚀 Starting upload script");
const filePath = process.argv[2];
const force = process.argv[3] === "--force";
const POSTGRES_USER = env.POSTGRES_USER;
const POSTGRES_DB = env.POSTGRES_DB;

if (!filePath) throw new Error("❌ Missing file path argument. Please provide a file path when running the script.");
if (await file(filePath).exists() && !force) throw new Error(`❌ File found at: ${filePath}. Refusing to overwrite ${filePath}. use --force to overwrite the file`);
if (!POSTGRES_USER) throw new Error("❌ Missing environment variable: POSTGRES_USER")
if (!POSTGRES_DB ) throw new Error("❌ Missing environment variable: POSTGRES_DB")
if (force) console.warn(`⚠️ Using --force flag. will overwrite`);

console.info(`📁 File path argument received: ${filePath}`);

// Execute backup command using Bun's shell
const backup = $
`docker compose exec timescaledb pg_dump \
-U ${POSTGRES_USER} \
-d ${POSTGRES_DB} \
--format=custom > ${filePath}`;

console.log(`⏳ Starting Backup at: ${filePath}`);

await backup

console.log(`✅ Backup successfully created at: ${filePath}`);
40 changes: 40 additions & 0 deletions ci-cd/bun/src/download.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { S3Client, env, file } from "bun";

console.log("🚀 Starting S3 download script");

const S3_ACCESS_KEY_ID = env.S3_ACCESS_KEY_ID;
const S3_SECRET_ACCESS_KEY = env.S3_SECRET_ACCESS_KEY;
const S3_BUCKET = env.S3_BUCKET ?? "local";
const S3_ENDPOINT = env.S3_ENDPOINT ?? "http://localhost:9000";
const s3FilePath = process.argv[2];
const localFilePath = process.argv[3];

if (!s3FilePath) throw new Error("❌ Missing s3FilePath argument. Provide an s3FilePath key to download.");
if (!localFilePath) throw new Error("❌ Missing S3 key argument. Provide an S3 object key to download.");
if (!S3_ACCESS_KEY_ID) throw new Error("❌ Missing environment variable: S3_ACCESS_KEY_ID");
if (!S3_SECRET_ACCESS_KEY) throw new Error("❌ Missing environment variable: S3_SECRET_ACCESS_KEY");
if (!S3_BUCKET) console.warn("⚠️ S3_BUCKET environment variable not set, using default 'local'.");
if (!S3_ENDPOINT) console.warn("⚠️ S3_ENDPOINT environment variable not set, using default 'http://localhost:9000'.");

// Initialize clients
console.log(`⏳ Initializing S3 from bucket ${S3_BUCKET} and local file`)
const localFile = file(localFilePath);
const minio = new S3Client({
accessKeyId: S3_ACCESS_KEY_ID,
secretAccessKey: S3_SECRET_ACCESS_KEY,
bucket: S3_BUCKET,
endpoint: S3_ENDPOINT,
});

if (await localFile.exists()) throw new Error(`❌ Exsisting file found at path: ${localFilePath}. Not allowed to overwrite`);

const s3File = minio.file(s3FilePath);

if (!await s3File.exists()) throw new Error(`❌ File "${s3FilePath}" not found in S3 bucket`);


console.log(`📥 Downloading "${s3FilePath}" from bucket "${S3_BUCKET}"...`);
const content = await s3File.arrayBuffer();
await Bun.write(localFile, content);

console.log(`✅ Successfully saved to: ${localFilePath}`);
44 changes: 44 additions & 0 deletions ci-cd/bun/src/restore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { $, env, file } from "bun";

// Validate required environment variables
console.log("🚀 Starting upload script");
const filePath = process.argv[2];
const force = process.argv[3] === "--force";
const POSTGRES_USER = env.POSTGRES_USER;
const POSTGRES_DB = env.POSTGRES_DB;

if (!filePath) throw new Error("❌ Missing file path argument. Please provide a file path when running the script.");
if (await file(filePath).exists() && !force) throw new Error(`❌ File found at: ${filePath}. Refusing to overwrite ${filePath}. use --force to overwrite the file`);
if (!POSTGRES_USER) throw new Error("❌ Missing environment variable: POSTGRES_USER")
if (!POSTGRES_DB ) throw new Error("❌ Missing environment variable: POSTGRES_DB")
if (force) console.warn(`⚠️ Using --force flag. will overwrite`);

console.info(`📁 File path argument received: ${filePath}`);

// Execute backup command using Bun's shell

const preRestore = $
`docker compose exec timescaledb pg_dump \
-U ${POSTGRES_USER} \
-d ${POSTGRES_DB} \
--format=custom > ${filePath}`;

const restore = $
`docker compose exec timescaledb pg_dump \
-U ${POSTGRES_USER} \
-d ${POSTGRES_DB} \
--format=custom > ${filePath}`;

const postRestore = $
`docker compose exec timescaledb pg_dump \
-U ${POSTGRES_USER} \
-d ${POSTGRES_DB} \
--format=custom > ${filePath}`;


console.log(`⏳ Starting Backup at: ${filePath}`);
await preRestore
await restore
await postRestore

console.log(`✅ Backup successfully created at: ${filePath}`);
39 changes: 39 additions & 0 deletions ci-cd/bun/src/upload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { S3Client, write, env, file } from "bun";
import path from 'path';

console.log("🚀 Starting S3 upload script");

const S3_ACCESS_KEY_ID = env.S3_ACCESS_KEY_ID;
const S3_SECRET_ACCESS_KEY = env.S3_SECRET_ACCESS_KEY;
const S3_BUCKET = env.S3_BUCKET ?? "local";
const S3_ENDPOINT = env.S3_ENDPOINT ?? "http://localhost:9000";
const filePath = process.argv[2];
const inputFile = file(filePath);

if (!filePath) throw new Error("❌ Missing file path argument. Please provide a file path when running the script.");
if (!await inputFile.exists()) throw new Error(`❌ File not found at path: ${filePath}`);
if (!S3_ACCESS_KEY_ID) throw new Error("❌ Missing environment variable: S3_ACCESS_KEY_ID");
if (!S3_SECRET_ACCESS_KEY) throw new Error("❌ Missing environment variable: S3_SECRET_ACCESS_KEY");
if (!S3_BUCKET) console.warn("⚠️ S3_BUCKET environment variable is not set, using default 'local'.");
if (!S3_ENDPOINT) console.warn("⚠️ S3_ENDPOINT environment variable is not set, using default 'http://localhost:9000'.");


console.info(`📁 File path argument received: ${filePath}`);

const fileContent = await inputFile.arrayBuffer();
const fileName = path.basename(filePath);

const minio = new S3Client({
accessKeyId: S3_ACCESS_KEY_ID,
secretAccessKey: S3_SECRET_ACCESS_KEY,
bucket: S3_BUCKET,
endpoint: S3_ENDPOINT,
});

const metadata = minio.file(fileName);

console.log(`📤 Uploading "${fileName}" to S3 bucket "${S3_BUCKET}"...`);
await write(metadata, fileContent);

console.log(`🎉 Uploaded "${fileName}" to S3 successfully!`);

2 changes: 1 addition & 1 deletion ci-cd/nixos/create-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ end=$((SECONDS + 300))
while [ $SECONDS -lt $end ]; do
if ssh -o StrictHostKeyChecking=no "root@${SERVER_IP}" "systemctl is-system-running" >/dev/null 2>&1; then
echo "🔄 Updating NixOS configuration..."
export SSH_KEYS=$(hcloud ssh-key describe deploy-key -o format='{{.PublicKey}}')
export SSH_PUBLIC_KEYS=$(hcloud ssh-key describe deploy-key -o format='{{.PublicKey}}')
bash ./remote-update.sh
echo "🎉 NixOS ready! Connect with: ssh root@$SERVER_IP or admin@$SERVER_IP"
exit 0
Expand Down
6 changes: 3 additions & 3 deletions ci-cd/nixos/remote-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ USER="root"
FILE="configuration.nix"

# Check if the environment variables are set
if [ -z "${SSH_KEYS}" ] || [ -z "${PASSWORD_HASH}" ] || [ -z "${SERVER_IP}" ]; then
echo "❌ Error: SSH_KEYS, PASSWORD_HASH or SERVER_IP environment variables not defined"
if [ -z "${SSH_PUBLIC_KEYS}" ] || [ -z "${PASSWORD_HASH}" ] || [ -z "${SERVER_IP}" ]; then
echo "❌ Error: SSH_PUBLIC_KEYS, PASSWORD_HASH or SERVER_IP environment variables not defined"
exit 1
fi

Expand All @@ -19,7 +19,7 @@ echo "📝 Transferring password hash file..."
ssh "${USER}@${SERVER_IP}" "echo '${PASSWORD_HASH}' > ${DEST_DIR}/admin-password-hash"

echo "🔑 Transferring SSH keys file..."
ssh "${USER}@${SERVER_IP}" "echo '${SSH_KEYS}' > ${DEST_DIR}/ssh-keys"
ssh "${USER}@${SERVER_IP}" "echo '${SSH_PUBLIC_KEYS}' > ${DEST_DIR}/ssh-keys"

echo "🔒 Transferring file permissions..."
ssh "${USER}@${SERVER_IP}" "chmod 600 ${DEST_DIR}/admin-password-hash ${DEST_DIR}/ssh-keys"
Expand Down
1 change: 1 addition & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ services:
- MICROSOFT_TENANT_ID=${MICROSOFT_TENANT_ID}
- MICROSOFT_CLIENT_ID=${MICROSOFT_CLIENT_ID}
- MICROSOFT_CLIENT_SECRET=${MICROSOFT_CLIENT_SECRET}
- RESEND_CLIENT_SECRET=${RESEND_CLIENT_SECRET}
- PROD=${PROD}
labels:
caddy: "${INTERFACE_FQDN}"
Expand Down
2 changes: 1 addition & 1 deletion services/backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ FROM oven/bun:1.2 AS build
WORKDIR /app

# Copy necessary package files to install dependencies
COPY bun.lockb package.json /app/
COPY bun.lock package.json /app/
COPY /services/backend/package.json /app/services/backend/
COPY /services/frontend/package.json /app/services/frontend/

Expand Down
2 changes: 1 addition & 1 deletion services/backend/develop.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ FROM oven/bun:1.2 AS build
WORKDIR /app

# Copy necessary package files to install dependencies
COPY bun.lockb package.json /app/
COPY bun.lock package.json /app/
COPY /services/backend/package.json /app/services/backend/
COPY /services/frontend/package.json /app/services/frontend/

Expand Down
2 changes: 1 addition & 1 deletion services/backend/drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if (!process.env.DATABASE_URL) {

export default defineConfig({
out: "./src/db/migrations",
schema: "./src/db/collections/**/schema.ts",
schema: "./src/collections/**/schema.ts",
dialect: "postgresql",
dbCredentials: {
url: process.env.DATABASE_URL,
Expand Down
9 changes: 5 additions & 4 deletions services/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"compile": "bun build src/index.ts --compile --minify --sourcemap --target bun --outfile backend",
"check": "tsc --noEmit",
"preview": "NODE_ENV=production bun dist/index.js",
"format": "bunx @biomejs/biome format --write src",
"lint": "bunx @biomejs/biome check src",
"lint:fix": "bunx @biomejs/biome check src --write",
"format": "bun x @biomejs/biome format --write src",
"lint": "bun x @biomejs/biome check src",
"lint:fix": "bun x @biomejs/biome check src --write",
"db:generate": "drizzle-kit generate",
"db:push": "drizzle-kit push --verbose --strict",
"db:migrate": "drizzle-kit migrate",
Expand All @@ -30,7 +30,8 @@
"drizzle-typebox": "0.1.1",
"elysia": "^1.2.10",
"nanoid": "^5.0.9",
"postgres": "^3.4.5"
"postgres": "^3.4.5",
"resend": "^4.1.2"
},
"devDependencies": {
"@biomejs/biome": "^1.9.4",
Expand Down
Loading

0 comments on commit 8046f88

Please sign in to comment.