Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: Bill Maxwell <[email protected]>
  • Loading branch information
cloudnautique committed Dec 20, 2023
0 parents commit 188bd8e
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Publish Acorn image
on:
push:
tags:
- "v[0-9]*"

jobs:
publish:
name: Publish
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: acorn-io/actions-setup@v2
with:
acorn-version: "main"
- name: Login to GHCR
uses: acorn-io/actions-login@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and Publish Acorn image
run: |
TAG=${GITHUB_REF#refs/*/}
USER=${{ github.repository_owner }}
REPO=${{ github.event.repository.name }}
acorn build --platform linux/amd64 --push -t ghcr.io/$USER/$REPO:$TAG .
acorn build --platform linux/amd64 --push -t ghcr.io/$USER/$REPO/examples:$TAG examples
52 changes: 52 additions & 0 deletions Acornfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
services: postgres: {
generated: job: "create"
default: true
}

jobs: create: {
image: "alpine"
env: {
"secret://config": ""
}
dirs: {
"/acorn/scripts": "./scripts"
}
entrypoint: ["/acorn/scripts/render.sh"]
}

secrets: admin: {
type: "generated"
params: job: "create"
}

secrets: config: {
type: "credential.acorn.io/postgres"
params: {
instructions: localData.instructions
promptOrder: ["proto", "address", "port", "adminUsername", "adminPassword", "dbName"]
}
data: {
address: ""
port: ""
adminUsername: ""
adminPassword: ""
proto: ""
dbName: ""
}
}

localData: instructions: """
## Overview

This will create the service from an existing Postgres server.

## Instructions

fill in:

- address: the address of the Postgres server endpoint (redis.example.com)
- port: the port of the Postgres server (5432)
- adminPassword: the main password to use to connect to the Postgres server as an admin
- proto: the protocol to use to connect to the Postgres server.
- dbName: the name of the database to use
"""
112 changes: 112 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Postgres External

This repo is an Acorn for connecting external Postgres databases to your Acorn applications through the service interface.

## Usage

The Acorn will prompt the user for the following information:

- Address: The address of the Postgres database, no port.
- Port: The port of the Postgres database.
- adminUsername: The username to connect to the Postgres database.
- adminPassword: The password to connect to the Postgres database.
- databaseName: The name of the database to connect to.
- proto: If any special protocol information is needed.

This information will be rendered into an Acorn service object.

if you run:

```bash
acorn run -n postgres-external ghcr.io/acorn-io/postgres-external:v#.#.#
```

It will require you to login to the Acorn to fill in the information.

```bash
acorn login [APP_NAME]
```

In the above example, `acorn login postgres-external` will prompt you for the information.

## How to use with Neon Tech Postgres

Lets walk through using the this Acorn to connect to a Neon Tech Postgres database.

First, you will need an account on [Neon](https://neon.tech) to create a database. We will be using the free tier for this example.

Once we have signed in, we will create a project called demo, and go with the default "neondb" for the database name. We will also use US East Ohio for the region.

## Launch our example Acorn

Now that we have a database created, we can launch our Acorn.

```bash
acorn run -n postgres-demo ghcr.io/acorn-io/postgres-external/examples:v#.#.#
```

This will prompt us for the following information:

```bash
postgres-demo
STATUS: ENDPOINTS[] HEALTHY[] UPTODATE[]
STATUS: ENDPOINTS[] HEALTHY[0] UPTODATE[0] waiting for quota allocation; (container: nextcloud): pending; (service: db): pending
STATUS: ENDPOINTS[] HEALTHY[0] UPTODATE[0] (container: nextcloud): waiting for service to be created [db], waiting for service to be ready [db]; (service: db): pending
STATUS: ENDPOINTS[https://postgres-demo-1b8cb199.zvgz4d.on-acorn.io] HEALTHY[0] UPTODATE[0] (container: nextcloud): waiting for service to be created [db], waiting for service to be ready [db]; (service: db): acorn [postgres-demo.db] is not ready

## Overview

This will create the service from an existing Postgres server.

## Instructions

fill in:

• address: the address of the Postgres server endpoint (redis.example.com)
• port: the port of the Postgres server (5432)
• adminPassword: the main password to use to connect to the Postgres server
as an admin
• proto: the protocol to use to connect to the Postgres server.
• dbName: the name of the database to use


? address ***********************************************
? adminPassword ************
? adminUsername ****
? dbName ******
? port ****
? proto
STATUS: ENDPOINTS[https://postgres-demo-1b8cb199.zvgz4d.on-acorn.io] HEALTHY[0] UPTODATE[0] "acorn login postgres-demo" required
STATUS: ENDPOINTS[https://postgres-demo-1b8cb199.zvgz4d.on-acorn.io] HEALTHY[0] UPTODATE[0] (container: nextcloud): waiting for service to be created [db], waiting for service to be ready [db]; (service: db): acorn [postgres-demo.db] is not ready
STATUS: ENDPOINTS[https://postgres-demo-1b8cb199.zvgz4d.on-acorn.io] HEALTHY[0] UPTODATE[0] (container: nextcloud): waiting for service to be ready [db]; (service: db): acorn [postgres-demo.db] is not ready
STATUS: ENDPOINTS[https://postgres-demo-1b8cb199.zvgz4d.on-acorn.io] HEALTHY[0] UPTODATE[0] (container: nextcloud): waiting for service to be ready [db]
STATUS: ENDPOINTS[https://postgres-demo-1b8cb199.zvgz4d.on-acorn.io] HEALTHY[0] UPTODATE[0] (container: nextcloud): pending
STATUS: ENDPOINTS[https://postgres-demo-1b8cb199.zvgz4d.on-acorn.io] HEALTHY[0/1] UPTODATE[0] (container: nextcloud): not ready
STATUS: ENDPOINTS[https://postgres-demo-1b8cb199.zvgz4d.on-acorn.io] HEALTHY[0/1] UPTODATE[1] (container: nextcloud): ContainerCreating
STATUS: ENDPOINTS[https://postgres-demo-1b8cb199.zvgz4d.on-acorn.io] HEALTHY[0/1] UPTODATE[1] (container: nextcloud): not ready
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
| STATUS: ENDPOINTS[https://postgres-demo-1b8cb199.zvgz4d.on-acorn.io] HEALTHY[1] UPTODATE[1] OK |
└────────────────────────────────────────────────────────────────────────────────────────────────┘
```

Where Address is the address of the Postgres server, obtained from the connection string in Neon dashboard. It does not include the `postgres://` or `:5432` port information.
The port is `5432` unless otherwise specified.
The adminUsername is obtained from the connection string in Neon dashboard.
The adminPassword is obtained from the connection string in Neon dashboard, it requires clicking the "show password" eye icon.
The proto was left blank in this case.

Once the application comes up and you see the URL, you can click on it to see the application. You will need to set the admin username and password for the Next Cloud instance on first login.

## Cleaning up

Once you have launched the app and are finished with it, you can delete it with:

```bash
acorn rm -af postgres-demo
acorn secrets
NAME TYPE KEYS CREATED
config-lsc6n credential.acorn.io/postgres [address adminPassword adminUsername dbName port proto] 15m ago
acorn secret rm config-lsc6n
```

Then you can remove the database instance from the Neon dashboard.
18 changes: 18 additions & 0 deletions examples/Acornfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
services: db: {
build: {
context: "../"
acornfile: "../Acornfile"
}
}

containers: nextcloud: {
image: "nextcloud"
ports: publish: "80/http"
consumes: ["db"]
env: {
POSTGRES_HOST: "@{service.db.address}"
POSTGRES_DB: "@{service.db.data.dbName}"
POSTGRES_USER: "@{service.db.secrets.admin.username}"
POSTGRES_PASSWORD: "@{service.db.secrets.admin.password}"
}
}
22 changes: 22 additions & 0 deletions scripts/render.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh

cat > /run/secrets/output<<EOF
services: postgres: {
address: "${address}"
default: true
secrets: ["admin"]
ports: "5432:${port}"
data: {
proto: "${proto}"
dbName: "${dbName}"
}
}
secrets: admin: {
type: "basic"
data: {
username: "${adminUsername}"
password: "${adminPassword}"
}
}
EOF

0 comments on commit 188bd8e

Please sign in to comment.