Skip to content

Commit

Permalink
add jobs and scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
a-type committed Mar 7, 2024
1 parent fc18b61 commit 4e96518
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/build-and-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Create and publish a Docker image

on:
push:
branches: [main]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
19 changes: 19 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Production Deploy
on:
release:
types: [published]

env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}

jobs:
deploy:
runs-on: ubuntu-latest
name: Deploy to production
steps:
- uses: actions/checkout@v2
- uses: superfly/flyctl-actions/setup-flyctl@master

- name: Deploy API
# deploy the image from ghcr.io for the target sha
run: flyctl deploy --image ghcr.io/a-type/biscuits:${{ github.sha }} --app biscuits
41 changes: 41 additions & 0 deletions .github/workflows/prod-backup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Prod Backup

on:
schedule:
- cron: '0 0 * * *'

jobs:
backup:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- uses: superfly/flyctl-actions/setup-flyctl@master

- name: Get current date
id: date
run: echo "::set-output name=date::$(date +'%Y-%m-%d')"

- name: Copy db file from server
run: |
mkdir ./backup
flyctl ssh sftp get /data/verdant.sqlite ./backup/verdant.sqlite
flyctl ssh sftp get /data/database.sqlite ./backup/database.sqlite
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
FLY_APP_NAME: aglio

- name: Upload to S3 in directory by date
uses: jakejarvis/s3-sync-action@master
with:
args: --follow-symlinks
env:
AWS_S3_BUCKET: prod-db-backups.gnocchi.club
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: us-east-1
SOURCE_DIR: ./backup
# date string
DEST_DIR: ${{ steps.date.outputs.date }}
20 changes: 20 additions & 0 deletions scripts/backup-prod.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as child from 'child_process';
import * as path from 'path';
import * as url from 'url';
import * as fs from 'fs';
import { promisify } from 'util';

// esm dirname
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));

const nowString = new Date().toISOString().replace(/:/g, '-');
const backupDirName = path.resolve(process.cwd(), `backups/${nowString}`);
fs.mkdirSync(backupDirName, { recursive: true });

const backupFileName = path.resolve(backupDirName, 'database.sqlite');
const backupVerdantFileName = path.resolve(backupDirName, 'verdant.sqlite');

// pull the files from Fly
const exec = promisify(child.exec);
await exec(`flyctl ssh sftp get /data/database.sqlite ${backupFileName}`);
await exec(`flyctl ssh sftp get /data/verdant.sqlite ${backupVerdantFileName}`);
56 changes: 56 additions & 0 deletions scripts/mirror-prod.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as prompt from '@clack/prompts';
import * as fs from 'fs';

prompt.intro('Gnocchi data mirroring');

// read directories in ./backups
const backups = fs.readdirSync('./backups').filter((f) => f !== '.gitkeep');

// prompt for which backup to use
const backup = await prompt.select({
message: 'Which backup?',
options: [
...backups
.sort((a, b) => -a.localeCompare(b))
.map((b) => ({
value: b,
label: b,
})),
{ value: 'restore', label: 'Restore before last backup' },
],
maxItems: 10,
});

if (prompt.isCancel(backup)) {
console.log('Cool.');
process.exit(0);
}

if (backup === 'restore') {
// restore the backup
console.log('Restoring backup...');
const lastBackup = backups[backups.length - 1];
fs.copyFileSync(`./server/verdant.sqlite.bak`, './server/verdant.sqlite');
fs.copyFileSync(`./server/database.sqlite.bak`, './server/database.sqlite');
fs.rmSync(`./server/verdant.sqlite.bak`);
fs.rmSync(`./server/database.sqlite.bak`);
console.log('Done.');
process.exit(0);
}

// make a backup of the current databases
if (fs.existsSync('./server/verdant.sqlite')) {
fs.copyFileSync('./server/verdant.sqlite', './server/verdant.sqlite.bak');
}
if (fs.existsSync('./server/database.sqlite')) {
fs.copyFileSync('./server/database.sqlite', './server/database.sqlite.bak');
}
fs.copyFileSync(
`./backups/${backup}/verdant.sqlite`,
'./server/verdant.sqlite',
);
fs.copyFileSync(
`./backups/${backup}/database.sqlite`,
'./server/database.sqlite',
);
prompt.outro('Done.');

0 comments on commit 4e96518

Please sign in to comment.