Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C deploy to flyio #68

Merged
merged 18 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
- [Telemetry](telemetry/README.md)
- [Prometheus](telemetry/prometheus.md)
- [Templates]() (to come)
- [Deploy on Fly.io](deploy-on-fly/README.md)
- [Launch a Corrosion Cluster](deploy-on-fly/launch.md)
- [Work with cluster data on Fly.io](deploy-on-fly/explore.md)
- [Run Corrosion commands on a remote node](deploy-on-fly/local-remote.md)

# Reference
- [API](api/README.md)
Expand Down
11 changes: 11 additions & 0 deletions doc/deploy-on-fly/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Deploy on Fly.io

The [Corrosion repository](https://github.com/superfly/corrosion) on GitHub includes example files to deploy Fly Machines running Corrosion in a cluster, communicating via [Fly private networking](https://fly.io/docs/reference/private-networking/).

- [Launch a Corrosion cluster](./launch.md)
- [Work with cluster data on Fly.io](./explore.md)
- [Run Corrosion commands on a remote node](./local-remote.md)

Corrosion is designed to run on the same node as any program that uses it. On Fly.io, that means deploying from a Docker image that runs both your code and Corrosion.

It's also possible for your other Machines on the same Fly private network to read from and write to their nearest Corrosion node via [API](../api/). This can be handy for occasional or development use.
154 changes: 154 additions & 0 deletions doc/deploy-on-fly/explore.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# Work with cluster data on Fly.io

With your Corrosion cluster deployed, you can work with the example database.

To get started, shell into each Corrosion node, in separate terminals.

```
fly ssh console --pty --app <your-app-name> --select
```

If you run the command from the directory containing your Corrosion app's `fly.toml` configuration file, you can omit the `--app` flag.

We'll call one Machine "Node A" and the other "Node B". Every node is read-write, so it doesn't matter which is which.

The example schema, `todo.sql`, specifies a single table called `todos`, with `id`, `title`, and `completed_at` columns.

```sql
-- /etc/corrosion/schemas/todo.sql

CREATE TABLE todos (
id BLOB NOT NULL PRIMARY KEY,
title TEXT NOT NULL DEFAULT '',
completed_at INTEGER
);
```

## Inserting and querying data

### Insert some data on Node A

From Node A's terminal session:

```bash
# corrosion exec --param 'some-id' --param 'Write some Corrosion docs!' 'INSERT INTO todos (id, title) VALUES (?, ?)'
INFO corrosion: Rows affected: 1
```
### Query data on Node A

Via SQLite directly:

```plain
# sqlite3 /var/lib/corrosion/state.db 'SELECT * FROM todos;'
some-id|Write some Corrosion docs!|
```

Using the API, via the CLI:

```bash
# corrosion query 'SELECT * FROM todos;' --columns
id|title|completed_at
some-id|Write some Corrosion docs!|
```

### Query data on Node B

From Node B's terminal:

```bash
# corrosion query 'SELECT * FROM todos;' --columns
id|title|completed_at
some-id|Write some Corrosion docs!|
```
Node A's contribution is already present in Node B's database.

### Insert data on Node B

```bash
# corrosion exec --param 'some-id-2' --param 'Show how broadcasts work' 'INSERT INTO todos (id, title) VALUES (?, ?)'
INFO corrosion: Rows affected: 1
```

### Check the data in Node A's database


```
# corrosion query 'SELECT * FROM todos;' --columns
id|title|completed_at
some-id|Write some Corrosion docs!|
some-id-2|Show how broadcasts work|
```

The second update has propagated back to Node A.

## Updating a file using a Corrosion template

The example template `todos.rhai` makes a checklist out of the rows in our `todos` table.

```js
/* /etc/corrosion/templates/todos.rhai */

<% for todo in sql("SELECT title, completed_at FROM todos") { %>
[<% if todo.completed_at.is_null() { %> <% } else { %>X<% } %>] <%= todo.title %>
<% } %>
```

### Start `corrosion template` and watch the output file

On Node A, start processing the template.

```
# corrosion template "/etc/corrosion/templates/todos.rhai:todos.txt" &
[1] 354
root@4d8964eb9d9487:/# INFO corrosion::command::tpl: Watching and rendering /etc/corrosion/templates/todos.rhai to todos.txt
```
Whenever there's an update to the results of the template's query (or queries), `corrosion template` re-renders the output file.

Start watching the output file.

```
# watch -n 0.5 cat todos.txt
Every 0.5s: cat todos.txt

[ ] Write some Corrosion docs!
[ ] Show how broadcasts work
```

### Add a todo item

On the other Machine (Node B), insert some data.

```
# corrosion exec --param 'some-id-3' --param 'Hello from a template!' 'INSERT INTO todos (id, title) VALUES (?, ?)'
INFO corrosion: Rows affected: 1
```

The new todo item gets propagated back to Node A, and your watch should look like this:

```
Every 0.5s: cat todos.txt

[ ] Write some Corrosion docs!
[ ] Show how broadcasts work
[ ] Hello from a template!
```

### Mark all items as done

Mark all tasks as completed, on either node:

```
# corrosion exec 'UPDATE todos SET completed_at = 1234567890'
INFO corrosion: Rows affected: 3
```

```
$ watch -n 0.5 cat todos.txt
Every 0.5s: cat todos.txt

[X] Write some Corrosion docs!
[X] Show how broadcasts work
[X] Hello from a template!
```

Now you have a distributed to-do list app! A front end is left as an exercise for the reader ;)
Loading
Loading