Skip to content

Commit

Permalink
feat: timescale
Browse files Browse the repository at this point in the history
  • Loading branch information
Fyko committed Apr 29, 2024
1 parent 01c272a commit 92d67e5
Show file tree
Hide file tree
Showing 23 changed files with 578 additions and 68 deletions.
78 changes: 78 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
[target.'cfg(all())']
rustflags = [
"-Funsafe_code",
"-Dclippy::all",
"-Wclippy::await_holding_lock",
"-Wclippy::char_lit_as_u8",
"-Wclippy::checked_conversions",
"-Wclippy::dbg_macro",
"-Wclippy::debug_assert_with_mut_call",
"-Wclippy::disallowed_methods",
"-Wclippy::disallowed_types",
"-Wclippy::empty_enum",
"-Wclippy::enum_glob_use",
"-Wclippy::exit",
"-Wclippy::expl_impl_clone_on_copy",
"-Wclippy::explicit_deref_methods",
"-Wclippy::explicit_into_iter_loop",
"-Wclippy::fallible_impl_from",
"-Wclippy::filter_map_next",
"-Wclippy::flat_map_option",
"-Wclippy::float_cmp_const",
"-Wclippy::fn_params_excessive_bools",
"-Wclippy::from_iter_instead_of_collect",
"-Wclippy::if_let_mutex",
"-Wclippy::implicit_clone",
"-Wclippy::imprecise_flops",
"-Dclippy::inefficient_to_string",
"-Wclippy::invalid_upcast_comparisons",
"-Wclippy::large_digit_groups",
"-Wclippy::large_stack_arrays",
"-Wclippy::large_types_passed_by_value",
"-Wclippy::let_unit_value",
"-Wclippy::linkedlist",
"-Wclippy::lossy_float_literal",
"-Wclippy::macro_use_imports",
"-Wclippy::manual_ok_or",
"-Wclippy::map_err_ignore",
"-Wclippy::map_flatten",
"-Wclippy::map_unwrap_or",
"-Wclippy::match_on_vec_items",
"-Wclippy::match_same_arms",
"-Wclippy::match_wild_err_arm",
"-Dclippy::match_wildcard_for_single_variants",
"-Wclippy::mem_forget",
"-Wclippy::mismatched_target_os",
"-Wclippy::missing_enforced_import_renames",
"-Wclippy::mut_mut",
"-Wclippy::mutex_integer",
"-Wclippy::needless_borrow",
"-Wclippy::needless_continue",
"-Wclippy::needless_for_each",
"-Wclippy::option_option",
"-Wclippy::path_buf_push_overwrite",
"-Wclippy::ptr_as_ptr",
"-Wclippy::rc_mutex",
"-Wclippy::ref_option_ref",
"-Wclippy::rest_pat_in_fully_bound_structs",
"-Wclippy::same_functions_in_if_condition",
"-Wclippy::semicolon_if_nothing_returned",
"-Wclippy::single_match_else",
"-Wclippy::string_add_assign",
"-Wclippy::string_add",
"-Wclippy::string_lit_as_bytes",
"-Wclippy::string_to_string",
"-Wclippy::todo",
"-Wclippy::trait_duplication_in_bounds",
"-Wclippy::unimplemented",
"-Wclippy::unnested_or_patterns",
"-Wclippy::unused_self",
"-Wclippy::useless_transmute",
"-Wclippy::verbose_file_reads",
"-Wclippy::zero_sized_map_values",
"-Wfuture_incompatible",
# TODO: reenable
# "-Wmissing_docs",
"-Wnonstandard_style",
"-Wrust_2018_idioms",
]
82 changes: 82 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Deploy

on:
push:
branches:
- main
workflow_dispatch:

env:
slug: ghcr.io/fyko/ambient-weather-collector
dockerfile: ./Dockerfile

jobs:
publish:
name: Publish Image
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Login to Google Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.slug }}
tags: |
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }}
type=sha
type=sha,format=long
#type=ref,enable=true,priority=600,prefix=,suffix=,event=pr
#type=ref,enable=true,priority=600,prefix=,suffix=,event=push
- name: Build
uses: docker/build-push-action@v5
with:
context: .
file: ${{ env.dockerfile }}
push: true
tags: ${{ steps.meta.outputs.tags }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max

# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
- name: Trigger Portainer Webhook
if: github.repository == 'Fyko/ambient-weather-collector'
shell: bash
run: |
webhook_url=$PORTAINER_DEPLOY_WEBHOOK
if [ -z "$webhook_url" ]; then
echo "PORTAINER_DEPLOY_WEBHOOK secret is not set"
exit 0
fi
curl -X POST ${{ secrets.PORTAINER_DEPLOY_WEBHOOK }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ Cargo.lock
*.pdb

.envrc
.env
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ rust_socketio = { version = "0.6.0", features = ["async"] }
secrecy = { version = "0.8.0", features = ["serde"] }
serde = { version = "1.0.199", features = ["derive"] }
serde_json = "1.0.116"
sqlx = { version = "0.7", features = [
"runtime-tokio-rustls",
"postgres",
"uuid",
"time",
"json",
] }
tokio = { version = "1.37.0", features = [
"rt",
"rt-multi-thread",
Expand Down
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM clux/muslrust:stable AS chef
USER root
RUN cargo install cargo-chef
WORKDIR /app

FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

FROM chef AS builder
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --target x86_64-unknown-linux-musl --recipe-path recipe.json
COPY . .
RUN cargo build --release --target x86_64-unknown-linux-musl --bin ambient_weather_collector --no-default-features

FROM alpine AS runtime
WORKDIR /app
COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/ambient_weather_collector .

RUN ls -la
CMD ["/app/ambient_weather_collector"]
70 changes: 70 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
[tasks.setup]
script = '''
echo installing git hooks
pre-commit --version || pip install pre-commit
pre-commit install || echo "failed to install git hooks!" 1>&2
echo installing stuff for tests
cargo binstall -y cargo-nextest
rustup component add clippy-preview
'''

[tasks.dev]
command = "cargo"
env_files = ["./.env"]
args = ["run"]

[tasks.lint]
install_crate = "clippy"
command = "cargo"
args = [
"+nightly",
"clippy",
"--tests",
"--examples",
"--all-targets",
"--all-features",
]

[tasks.fmt]
install_crate = "rustfmt"
command = "cargo"
args = ["fmt", "--all"]

[tasks.lint-ci]
install_crate = "clippy"
command = "cargo"
args = [
"clippy",
"--tests",
"--examples",
"--all-targets",
"--all-features",
"--workspace",
"--",
"-D",
"warnings",
]

[tasks.fmt-ci]
install_crate = "rustfmt"
command = "cargo"
args = ["fmt", "--all", "--", "--check"]

# runs tests
[tasks.test]
env = { "RUN_MODE" = "test", "RUST_LOG" = "info", "RUST_BACKTRACE" = 0 }
command = "cargo"
args = [
"nextest",
"run",
"--examples",
"--all-targets",
"--all-features",
"${@}",
]

[tasks.test-ci]
env = { "RUN_MODE" = "ci", "RUST_LOG" = "info" }
command = "cargo"
args = ["nextest", "run", "--examples", "--all-targets", "--all-features"]
6 changes: 6 additions & 0 deletions datagrip/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions datagrip/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions datagrip/.idea/ambient-weather-collector.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions datagrip/.idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions datagrip/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions datagrip/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions docker-compose.portainer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
services:
collector:
image: ghcr.io/fyko/ambient-weather-collector:latest
restart: unless-stopped
environment:
- AMBIENT_WEATHER_API_KEY=${AMBIENT_WEATHER_API_KEY:?ambient weather api key required}
- AMBIENT_WEATHER_APPLICATION_KEY=${AMBIENT_WEATHER_APPLICATION_KEY:?ambient weather application key required}
- DATABASE_URL=${DATABASE_URL:?database name required}

timescale:
image: timescale/timescaledb-ha:pg16
restart: unless-stopped
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?database password required}
volumes:
- timescale-storage:/home/postgres/pgdata/data
- ./docker/postgres/migrations:/docker-entrypoint-initdb.d
- ./docker/postgres/postgresql.conf/:/etc/postgresql/postgresql.conf
healthcheck:
test:
[
"CMD-SHELL",
"pg_isready -d $${POSTGRES_DATABASE} -U $${POSTGRES_USER}",
]
start_period: 20s
interval: 30s
retries: 5
timeout: 5s
deploy:
resources:
limits:
cpus: "4"
memory: 2G
ports:
- 8432:8432

volumes:
timescale-storage:
30 changes: 30 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
services:
timescale:
image: timescale/timescaledb-ha:pg16
restart: unless-stopped
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?database password required}
volumes:
- timescale-storage:/home/postgres/pgdata/data
- ./docker/postgres/migrations:/docker-entrypoint-initdb.d
- ./docker/postgres/postgresql.conf/:/etc/postgresql/postgresql.conf
healthcheck:
test:
[
"CMD-SHELL",
"pg_isready -d $${POSTGRES_DATABASE} -U $${POSTGRES_USER}",
]
start_period: 20s
interval: 30s
retries: 5
timeout: 5s
deploy:
resources:
limits:
cpus: "4"
memory: 2G
ports:
- 5432:5432

volumes:
timescale-storage:
1 change: 1 addition & 0 deletions docker/postgres/migrations/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
create database ambient_weather;
Loading

0 comments on commit 92d67e5

Please sign in to comment.