Skip to content

Commit 1446d7e

Browse files
committed
feat: Add local-init, local-run, local-update and local-reset tasks
1 parent 1e6cb23 commit 1446d7e

13 files changed

+284
-17
lines changed

.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ ADRs
44
.vscode
55
.github
66
scripts
7+
tutorials
78
database*
89
Prose.toml
910
Rocket.toml

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
target
99

1010
# Local execution files
11+
*.env
1112
*.sqlite
1213
*.log

CONTRIBUTING.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ After release 1.0, contributions will be more than welcome though!
1010

1111
### `task`
1212

13-
Instead of using [GNU Make], we are using [Task] for its simplicity and flexibility. You can find installation instructions on [taskfile.dev/installation](https://taskfile.dev/installation/), or just run the folowing on macOS:
13+
Instead of using [GNU Make], we are using [Task] for its simplicity and flexibility.
14+
You can find installation instructions on [taskfile.dev/installation],
15+
or just run the folowing on macOS:
1416

1517
```bash
1618
brew install go-task
@@ -24,7 +26,9 @@ task -a
2426

2527
### `cross`
2628

27-
To build the Docker image locally, we need to cross-compile Rust code for a different architecture. To avoid cluttering your local environment, we use [`cross`] which handles everything transparently. You can find installation instructions on [github.com/cross-rs/cross#installation](https://github.com/cross-rs/cross?tab=readme-ov-file#installation), or just run the folowing:
29+
To build the Docker image locally, we need to cross-compile Rust code for a different architecture.
30+
To avoid cluttering your local environment, we use [`cross`] which handles everything transparently.
31+
You can find installation instructions on [github.com/cross-rs/cross#installation], or just run the following:
2832

2933
```bash
3034
cargo install cross --git https://github.com/cross-rs/cross
@@ -125,3 +129,5 @@ task build-image -- [TARGET_ARCH] --debug
125129
[Task]: https://stepci.com/ "Task"
126130
[GNU Make]: https://www.gnu.org/software/make/ "Make - GNU Project - Free Software Foundation"
127131
[`cross`]: https://github.com/cross-rs/cross "cross-rs/cross: “Zero setup” cross compilation and “cross testing” of Rust crates"
132+
[github.com/cross-rs/cross#installation]: https://github.com/cross-rs/cross?tab=readme-ov-file#installation "cross-rs/cross: “Zero setup” cross compilation and “cross testing” of Rust crates"
133+
[taskfile.dev/installation]: https://taskfile.dev/installation/ "Installation | Task"

Taskfile.yaml

+21-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ tasks:
5353
- rustup upgrade
5454
- cargo update
5555
check-for-outdated-dependencies:
56-
desc: Check for outdated dependencies.
56+
desc: Checks for outdated dependencies.
5757
cmds:
5858
# Check for outdated dependencies
5959
- "if cargo install --list | grep -q '^cargo-edit v'; then \
@@ -76,3 +76,23 @@ tasks:
7676
SCRIPTS_ROOT: "{{.ROOT_DIR}}/scripts"
7777
cmds:
7878
- "{{.ROOT_DIR}}/scripts/build-image {{.CLI_ARGS}}"
79+
local-init:
80+
desc: Initializes a local environment allowing one to run a Prose Pod API in one command (`task local-run`).
81+
dotenv: ["paths.env"]
82+
cmds:
83+
- "{{.ROOT_DIR}}/scripts/run-locally/init {{.CLI_ARGS}}"
84+
local-run:
85+
desc: Runs a Prose Pod API.
86+
dotenv: ["paths.env"]
87+
cmds:
88+
- "{{.ROOT_DIR}}/scripts/run-locally/run {{.CLI_ARGS}}"
89+
local-update:
90+
desc: Updates the Prose Pod API and the repositories it depends on to get the latest updates.
91+
dotenv: ["paths.env"]
92+
cmds:
93+
- "{{.ROOT_DIR}}/scripts/run-locally/update {{.CLI_ARGS}}"
94+
local-reset:
95+
desc: Starts fresh with a Prose Pod API that has no data.
96+
dotenv: ["paths.env"]
97+
cmds:
98+
- "{{.ROOT_DIR}}/scripts/run-locally/reset {{.CLI_ARGS}}"

scripts/integration-test

+1-12
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,7 @@ on-exit() {
1919
}
2020
trap 'on-exit' EXIT
2121

22-
# Redirect users to <CONTRIBUTING.md> if env vars are not set.
23-
# NOTE: We still use `${X:?}` in the rest of the script, this is just to improve the dev UX.
24-
# COPYRIGHT: <https://stackoverflow.com/a/65396324/10967642>.
25-
test-env-vars() {
26-
var_names=("$@")
27-
for var_name in "${var_names[@]}"; do
28-
[ -z "${!var_name}" ] && echo "${var_name} isn't set. Check <CONTRIBUTING.md> for more information." && var_unset=true
29-
done
30-
[ -n "$var_unset" ] && exit 1
31-
return 0
32-
}
33-
test-env-vars \
22+
test-env-vars 'CONTRIBUTING.md' \
3423
PROSE_POD_API_DIR \
3524
PROSE_POD_SYSTEM_DIR
3625

scripts/open-api-docs-ui

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
: ${SCRIPTS_ROOT:="$(dirname $0)"}
4-
: ${BASH_TOOLBOX:="${SCRIPTS_ROOT:?}"/bash-toolbox}
4+
: ${BASH_TOOLBOX:="${SCRIPTS_ROOT:?}/bash-toolbox"}
55
# NOTE: `log.sh` provides utility for logging at different levels.
66
source "${BASH_TOOLBOX:?}"/log.sh
77

scripts/run-locally/build-images

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
# Configure the script to exit when a command fails.
4+
set -e
5+
6+
: ${SCRIPTS_ROOT:="$(dirname $0)/.."}
7+
export SCRIPTS_ROOT
8+
source "${SCRIPTS_ROOT:?}"/util.sh
9+
10+
test-env-vars 'tutorials/run-locally.md' \
11+
PROSE_POD_SERVER_DIR \
12+
PROSE_POD_API_DIR \
13+
PROSE_POD_SYSTEM_DIR
14+
15+
# Build the Prose Pod API image.
16+
edo task build-image -- --debug
17+
18+
# Build the Prose Pod Server image.
19+
edo docker build -t proseim/prose-pod-server:latest "${PROSE_POD_SERVER_DIR:?}"

scripts/run-locally/init

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
3+
# Configure the script to exit when a command fails.
4+
set -e
5+
6+
: ${SCRIPTS_ROOT:="$(dirname $0)/.."}
7+
export SCRIPTS_ROOT
8+
source "${SCRIPTS_ROOT:?}"/util.sh
9+
10+
test-env-vars 'tutorials/run-locally.md' \
11+
PROSE_POD_SERVER_DIR \
12+
PROSE_POD_API_DIR \
13+
PROSE_POD_SYSTEM_DIR
14+
15+
# Clone prose-pod-server.
16+
[ -d "${PROSE_POD_SERVER_DIR:?}" ] || edo git clone https://github.com/prose-im/prose-pod-server.git "${PROSE_POD_SERVER_DIR:?}"
17+
edo git -C "${PROSE_POD_SERVER_DIR:?}" submodule update --init
18+
19+
# Clone prose-pod-api.
20+
[ -d "${PROSE_POD_API_DIR:?}" ] || edo git clone https://github.com/prose-im/prose-pod-api.git "${PROSE_POD_API_DIR:?}"
21+
edo git -C "${PROSE_POD_API_DIR:?}" submodule update --init
22+
23+
# Clone prose-pod-system.
24+
[ -d "${PROSE_POD_SYSTEM_DIR:?}" ] || edo git clone https://github.com/prose-im/prose-pod-system.git "${PROSE_POD_SYSTEM_DIR:?}"
25+
26+
# Create a `.env` file which we'll pass to prose-pod-api when it runs.
27+
edo echo "export ROCKET_DATABASES='{data={url=\"sqlite://database.sqlite?mode=rwc\"}}'
28+
export JWT_SIGNING_KEY='626290f2d56e43e872527a2fc40468a2ea0f075b51d4da00209f73be0ae40ed64dd164030035692c9ab07673caee8e8ccb0f87ca5572f977f6714f75640d5aeee98d76d514d60d3976b1073b179b314bf49d53913bfbab2bd82d0901e7c4b8f94aa766d914506c1643fa5e942518bc0a727f978f28d26464b116ab070bf0697881b991202814c4d8e2d630e11510777fd9abcf9d7fedfce9f886e970ec6dd670454feeb3be51af8cc04c19fa7cec7ebe623741dc7021562d08d243eac6299449023072c5fa9c0206733387432497b9e93b32988c0cf18762f1ec9fb634fc22bd89b6fe21efd03cb585cb2125b96386346f91e8ff02918209203383884429c4f9'
29+
export PROSE_BOOTSTRAP__PROSE_POD_API_XMPP_PASSWORD='jSq_fbnUZWBJ#eNK6Yt&dz%Vvr)RsA\`x~}p3^?>LE(-8@\"u.'
30+
export RUST_LOG='info,sqlx=warn,hyper=warn,hyper_util=warn,sea_orm_migration=warn'" \
31+
> "${PROSE_POD_SYSTEM_DIR:?}"/local-run.env
32+
33+
# Initialize an empty `.sqlite` file because otherwise Compose creates a directory and the API crashes.
34+
DATABASE_PATH="${PROSE_POD_SYSTEM_DIR}"/local-run.sqlite
35+
[ -f "${DATABASE_PATH:?}" ] || echo '' > "${DATABASE_PATH:?}"
36+
37+
traced "${SCRIPTS_ROOT:?}"/run-locally/build-images

scripts/run-locally/reset

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
# Configure the script to exit when a command fails.
4+
set -e
5+
6+
: ${SCRIPTS_ROOT:="$(dirname $0)/.."}
7+
export SCRIPTS_ROOT
8+
source "${SCRIPTS_ROOT:?}"/util.sh
9+
10+
test-env-vars 'tutorials/run-locally.md' \
11+
PROSE_POD_SYSTEM_DIR
12+
13+
export SERVER_ROOT="${PROSE_POD_SYSTEM_DIR:?}"/server/pod
14+
export DATABASE_PATH="${PROSE_POD_SYSTEM_DIR:?}"/local-run.sqlite
15+
16+
edo "${PROSE_POD_SYSTEM_DIR:?}"/tools/cleanup

scripts/run-locally/run

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
# Configure the script to exit when a command fails.
4+
set -e
5+
6+
: ${SCRIPTS_ROOT:="$(dirname $0)/.."}
7+
export SCRIPTS_ROOT
8+
source "${SCRIPTS_ROOT:?}"/util.sh
9+
10+
test-env-vars 'tutorials/run-locally.md' \
11+
PROSE_POD_SYSTEM_DIR
12+
13+
export ENV_FILE="${PROSE_POD_SYSTEM_DIR:?}"/local-run.env
14+
export SERVER_ROOT="${PROSE_POD_SYSTEM_DIR:?}"/server/pod
15+
export DATABASE_PATH="${PROSE_POD_SYSTEM_DIR:?}"/local-run.sqlite
16+
export PROSE_CONFIG_FILE="${PROSE_POD_SYSTEM_DIR:?}"/Prose-example.toml
17+
COMPOSE_FILE="${PROSE_POD_SYSTEM_DIR:?}"/compose.yaml
18+
19+
edo docker compose -f "${COMPOSE_FILE:?}" up

scripts/run-locally/update

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
# Configure the script to exit when a command fails.
4+
set -e
5+
6+
: ${SCRIPTS_ROOT:="$(dirname $0)/.."}
7+
export SCRIPTS_ROOT
8+
source "${SCRIPTS_ROOT:?}"/util.sh
9+
10+
test-env-vars 'tutorials/run-locally.md' \
11+
PROSE_POD_SERVER_DIR \
12+
PROSE_POD_API_DIR \
13+
PROSE_POD_SYSTEM_DIR
14+
15+
info 'Updating prose-pod-server…'
16+
edo git -C "${PROSE_POD_SERVER_DIR:?}" pull || :
17+
edo git -C "${PROSE_POD_SERVER_DIR:?}" submodule update
18+
19+
info 'Updating prose-pod-api…'
20+
edo git -C "${PROSE_POD_API_DIR:?}" pull || :
21+
edo git -C "${PROSE_POD_API_DIR:?}" submodule update
22+
23+
info 'Updating prose-pod-system…'
24+
edo git -C "${PROSE_POD_SYSTEM_DIR:?}" pull || :
25+
26+
traced "${SCRIPTS_ROOT:?}"/run-locally/build-images

scripts/util.sh

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
: ${BASH_TOOLBOX:="${SCRIPTS_ROOT:?}"/bash-toolbox}
1+
: ${BASH_TOOLBOX:="${SCRIPTS_ROOT:?}/bash-toolbox"}
22
# NOTE: `die` logs an error then exits.
33
source "${BASH_TOOLBOX:?}"/die.sh
44
# NOTE: `edo` supports dry mode via `DRY_MODE`. When not in dry mode,
@@ -48,3 +48,18 @@ change-build-target() {
4848
: ${PREFIXED_PROSE_POD_SERVER_IMAGE:="${DOCKER_ARCH_PREFIX+"${DOCKER_ARCH_PREFIX%/}/"}${PROSE_POD_SERVER_IMAGE:?}"}
4949
PROSE_POD_SERVER_IMAGE="${PREFIXED_PROSE_POD_SERVER_IMAGE}"
5050
}
51+
52+
# Redirect users to the docs if env vars are not set.
53+
# NOTE: We still use `${X:?}` in the rest of the script, this is just to improve the dev UX.
54+
# COPYRIGHT: <https://stackoverflow.com/a/65396324/10967642>.
55+
test-env-vars() {
56+
local doc_file="$1"
57+
shift 1
58+
59+
local var_names=("$@") var_name
60+
for var_name in "${var_names[@]}"; do
61+
[ -z "${!var_name}" ] && echo "${var_name} isn't set. Check $(format_url "${doc_file}") for more information." && var_unset=true
62+
done
63+
[ -n "$var_unset" ] && exit 1
64+
return 0
65+
}

tutorials/run-locally.md

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# How to run a Prose Pod API locally?
2+
3+
When developing the [Prose Pod Dashboard], one needs to easily start a Prose Pod API locally.
4+
This document explains how to do just this.
5+
6+
## First time setup
7+
8+
### Intall the tools you will need
9+
10+
#### `git`
11+
12+
We use Git as version control system so you will need to install it.
13+
You probably have it installed already, but if you don't, have a look at
14+
[Git - Installing Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git).
15+
16+
#### `task`
17+
18+
Instead of using [GNU Make], we are using [Task] for its simplicity and flexibility.
19+
You can find installation instructions on [taskfile.dev/installation],
20+
or just run the folowing on macOS:
21+
22+
```bash
23+
brew install go-task
24+
```
25+
26+
To list all available commands, use:
27+
28+
```bash
29+
task -a
30+
```
31+
32+
#### Docker
33+
34+
We need to build and run Docker container so you must have Docker installed.
35+
See [Install | Docker Docs](https://docs.docker.com/engine/install/).
36+
37+
#### `cross`
38+
39+
To build the Docker image locally, we need to cross-compile Rust code for a different architecture.
40+
To avoid cluttering your local environment, we use [`cross`] which handles everything transparently.
41+
You can find installation instructions on [github.com/cross-rs/cross#installation], or just run the following:
42+
43+
```bash
44+
cargo install cross --git https://github.com/cross-rs/cross
45+
```
46+
47+
### Initialize your environment
48+
49+
To avoid you having to copy-paste tons of commands, we hid all of the logic behind helper scripts
50+
which you can invoke using `task`. The first task to run is `local-init`.
51+
52+
But before that, you must declare where you want the repositories to be
53+
(replace `???` by the desired location):
54+
55+
```sh
56+
export PROSE_POD_SERVER_DIR=???
57+
export PROSE_POD_API_DIR=???
58+
export PROSE_POD_SYSTEM_DIR=???
59+
```
60+
61+
Then, run:
62+
63+
```sh
64+
git clone https://github.com/prose-im/prose-pod-api.git "${PROSE_POD_API_DIR:?}"
65+
git -C "${PROSE_POD_API_DIR:?}" submodule update --init
66+
task --taskfile "${PROSE_POD_API_DIR:?}"/Taskfile.yaml local-init
67+
```
68+
69+
> [!TIP]
70+
> To avoid having to export the environment variables every time, you can add them to a local `.env` file
71+
> which will be sourced automatically when you run the next commands:
72+
>
73+
> ```sh
74+
> echo "export PROSE_POD_SERVER_DIR='${PROSE_POD_SERVER_DIR:?}'
75+
> export PROSE_POD_API_DIR='${PROSE_POD_API_DIR:?}'
76+
> export PROSE_POD_SYSTEM_DIR='${PROSE_POD_SYSTEM_DIR:?}'" \
77+
> >> "${PROSE_POD_API_DIR:?}"/paths.env
78+
> ```
79+
80+
> [!TIP]
81+
> To run the `task` commands, you will have to `cd` into the Prose Pod API repository,
82+
> or invoke `task` telling it where to find the Taskfile. If you prefer the latter scenario,
83+
> you can export `PROSE_POD_API_DIR` in your `~/.zprofile` (or equivalent) and call
84+
> `task --taskfile "${PROSE_POD_API_DIR:?}"/Taskfile.yaml <task>` instead of `task <task>`.
85+
86+
## Run the API
87+
88+
At the root of the `prose-pod-api` repository, run:
89+
90+
```sh
91+
task local-update
92+
```
93+
94+
## When you want to update one of the repositories
95+
96+
At the root of the `prose-pod-api` repository, run:
97+
98+
```sh
99+
task local-update
100+
```
101+
102+
## When you want to start fresh with a Prose Pod API that has no data
103+
104+
At the root of the `prose-pod-api` repository, run:
105+
106+
```sh
107+
task local-reset
108+
```
109+
110+
OR do the "Initialize your environment" phase again but with different directory paths.
111+
This way you can have multiple instances of the API with different states.
112+
113+
[Prose Pod Dashboard]: https://github.com/prose-im/prose-pod-dashboard "prose-im/prose-pod-dashboard: Prose Pod dashboard. Static Web application used to interact with the Prose Pod API."
114+
[Task]: https://stepci.com/ "Task"
115+
[GNU Make]: https://www.gnu.org/software/make/ "Make - GNU Project - Free Software Foundation"
116+
[`cross`]: https://github.com/cross-rs/cross "cross-rs/cross: “Zero setup” cross compilation and “cross testing” of Rust crates"
117+
[github.com/cross-rs/cross#installation]: https://github.com/cross-rs/cross?tab=readme-ov-file#installation "cross-rs/cross: “Zero setup” cross compilation and “cross testing” of Rust crates"
118+
[taskfile.dev/installation]: https://taskfile.dev/installation/ "Installation | Task"

0 commit comments

Comments
 (0)