Skip to content

Commit

Permalink
litestream option
Browse files Browse the repository at this point in the history
  • Loading branch information
rubys committed Dec 22, 2024
1 parent 3a05a0d commit fe518ff
Show file tree
Hide file tree
Showing 14 changed files with 1,908 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Options are saved between runs into `package.json`. To invert a boolean options,
* `--force` - overwrite existing files
* `--legacy-peer-deps` - [ignore peer dependencies](https://docs.npmjs.com/cli/v7/using-npm/config#legacy-peer-deps).
* `--litefs` - configure and enable [litefs](https://fly.io/docs/litefs/).
* `--litestream` - configure and enable [litestream](https://litestream.io/).
* `--nginxRoot=DIR` - serve static files from given directory via [nginx](https://www.nginx.com/).
* `--link` - Add [--link](https://docs.docker.com/engine/reference/builder/#copy---link) to COPY statements. Some tools, including [buildah](https://www.redhat.com/en/topics/containers/what-is-buildah)) or [Buildkit](https://docs.docker.com/build/buildkit/) don't properly support this feature.
* `--port=n` - expose port (default may vary based on framework, but otherwise is `3000`)
Expand Down
11 changes: 11 additions & 0 deletions gdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const defaults = {
legacyPeerDeps: false,
link: false,
litefs: false,
litestream: false,
nginxRoot: '',
port: 0,
swap: '',
Expand Down Expand Up @@ -264,6 +265,10 @@ export class GDF {
!!this.#pj.dependencies?.['litefs-js']
}

get litestream() {
return this.options.litestream
}

// Does this application use puppeteer?
get puppeteer() {
return !!this.#pj.dependencies?.puppeteer ||
Expand Down Expand Up @@ -331,6 +336,7 @@ export class GDF {
const packages = [...this.options.packages.deploy]

if (this.litefs) packages.push('ca-certificates', 'fuse3')
if (this.litestream) packages.push('ca-certificates', 'wget')
if (this.remix && this.sqlite3) packages.push('sqlite3')
if (this.prisma) packages.push('openssl')
if (this.options.nginxRoot) packages.push('nginx')
Expand Down Expand Up @@ -937,6 +943,11 @@ export class GDF {
templates['litefs.yml.ejs'] = `${this.configDir}litefs.yml`
}

if (this.litestream && this.prismaFile) {
templates['litestream.yml.ejs'] = `${this.configDir}litestream.yml`
this.path = path
}

if (this.options.nginxRoot) {
this.options.nginxRoot = path.join('/app', this.options.nginxRoot)
}
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ const options = yargs((hideBin(process.argv)))
describe: 'configure and enable litefs',
type: 'boolean'
})
.option('litestream', {
describe: 'configure and enable litestream',
type: 'boolean'
})
.option('link', {
describe: 'use COPY --link whenever possible',
type: 'boolean'
Expand Down
6 changes: 6 additions & 0 deletions templates/Dockerfile.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ RUN <% if (options.cache) { %><%= pkg_cache %>
<%= pkg_install %> <%= deployPackages.join(' ')%><% if (!options.cache) { %> && \
rm -rf <%= pkg_cleanup %><% } %>
<% } -%>
<% if (litestream) { -%>
RUN wget https://github.com/benbjohnson/litestream/releases/download/v0.3.13/litestream-v0.3.13-linux-amd64.deb && \
dpkg -i litestream-v0.3.13-linux-amd64.deb && \
rm litestream-v0.3.13-linux-amd64.deb
<% } -%>
# Copy built application
<% if (vite || astroStatic) { -%>
Expand Down
8 changes: 8 additions & 0 deletions templates/docker-entrypoint.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,15 @@ if (process.env.DATABASE_URL) {

<% } -%>
<%= tab(1) %>// launch application
<% if (nextjsGeneration) { -%>
<%= tab(1) %>if (process.env.BUCKET_NAME) {
<%= tab(2) %>await exec(`litestream replicate -config litestream.yml -exec ${JSON.stringify(process.argv.slice(2).join(' '))}`)
<%= tab(1) %>} else {
<%= tab(2) %>await exec(process.argv.slice(2).join(' '))
<%= tab(1) %>}
<% } else { -%>
<%= tab(1) %>await exec(process.argv.slice(2).join(' '))
<% } -%>
})()

<%= tab(0) %>function exec(command) {
Expand Down
13 changes: 13 additions & 0 deletions templates/litestream.yml.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This is the configuration file for litestream.
#
# For more details, see: https://litestream.io/reference/config/
#
dbs:
- path: /data/<%= path.basename(prismaFile) %>
replicas:
- type: s3
endpoint: $AWS_ENDPOINT_URL_S3
bucket: $BUCKET_NAME
path: litestream/<%= path.basename(prismaFile) %>
access-key-id: $AWS_ACCESS_KEY_ID
secret-access-key: $AWS_SECRET_ACCESS_KEY
6 changes: 6 additions & 0 deletions test/base/litestream/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.git
/node_modules
.dockerignore
.env
Dockerfile
fly.toml
60 changes: 60 additions & 0 deletions test/base/litestream/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# syntax = docker/dockerfile:1

# Adjust NODE_VERSION as desired
ARG NODE_VERSION=xxx
FROM node:${NODE_VERSION}-slim AS base

LABEL fly_launch_runtime="Node.js/Prisma"

# Node.js/Prisma app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV="production"


# Throw-away build stage to reduce size of final image
FROM base AS build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential node-gyp openssl pkg-config python-is-python3

# Install node modules
COPY package-lock.json package.json ./
RUN npm ci

# Generate Prisma Client
COPY prisma .
RUN npx prisma generate

# Copy application code
COPY . .


# Final stage for app image
FROM base

# Install packages needed for deployment
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y ca-certificates openssl wget && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

RUN wget https://github.com/benbjohnson/litestream/releases/download/v0.3.13/litestream-v0.3.13-linux-amd64.deb && \
dpkg -i litestream-v0.3.13-linux-amd64.deb && \
rm litestream-v0.3.13-linux-amd64.deb

# Copy built application
COPY --from=build /app /app

# Setup sqlite3 on a separate volume
RUN mkdir -p /data
VOLUME /data

# Entrypoint prepares the database.
ENTRYPOINT [ "/app/docker-entrypoint.js" ]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
ENV DATABASE_URL="file:///data/sqlite.db"
CMD [ "npm", "run", "start" ]
7 changes: 7 additions & 0 deletions test/base/litestream/fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

[mounts]
source = "data"
destination="/data"
auto_extend_size_threshold = 80
auto_extend_size_increment = "1GB"
auto_extend_size_limit = "10GB"
13 changes: 13 additions & 0 deletions test/base/litestream/litestream.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This is the configuration file for litestream.
#
# For more details, see: https://litestream.io/reference/config/
#
dbs:
- path: /data/dev.db
replicas:
- type: s3
endpoint: $AWS_ENDPOINT_URL_S3
bucket: $BUCKET_NAME
path: litestream/dev.db
access-key-id: $AWS_ACCESS_KEY_ID
secret-access-key: $AWS_SECRET_ACCESS_KEY
Loading

0 comments on commit fe518ff

Please sign in to comment.