Skip to content

Commit

Permalink
feat: better logging and fix deployment (#150)
Browse files Browse the repository at this point in the history
* feat: better logging and fix deployment

* fix tests

* remove comment

---------

Co-authored-by: Natoandro <anatoandro@hotmail.com>
  • Loading branch information
zifeo and Natoandro authored Feb 24, 2023
1 parent 4c11692 commit c4b6b34
Show file tree
Hide file tree
Showing 45 changed files with 475 additions and 322 deletions.
128 changes: 59 additions & 69 deletions Cargo.lock

Large diffs are not rendered by default.

67 changes: 42 additions & 25 deletions dev/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,74 +1,91 @@
ARG RUST_VERSION=1.67.1
ARG DENO_VERSION=1.30.3
ARG DISTROLESS_TAG=nonroot

FROM lukemathwalker/cargo-chef:latest-rust-${RUST_VERSION} AS base

WORKDIR /
WORKDIR /app

#

FROM base as plan

COPY . .
RUN rm -rf meta-cli \
&& cargo new meta-cli \
&& cargo chef prepare --recipe-path recipe.json

RUN cargo chef prepare --recipe-path recipe.json

#

FROM denoland/deno:bin-${DENO_VERSION} AS deno-bin

#

FROM base AS builder

ARG DENO_VERSION=1.30.3
ARG DENO_BINDGEN_URL=https://github.com/denoland/deno_bindgen/raw/main/cli.ts

ENV DENO_DIR /deno-dir/
ENV DENO_INSTALL /root/.deno
ENV PATH "${DENO_INSTALL}/bin:${PATH}"
ENV OUT_DIR target

COPY --from=deno-bin /deno /bin/deno

RUN apt-get update \
&& apt-get install -y \
&& apt-get install -y --no-install-recommends \
protobuf-compiler \
libprotobuf-dev \
&& rm -rf /var/lib/apt/lists/* \
&& curl -fsSL https://deno.land/x/install/install.sh -o install.sh \
&& sh ./install.sh v${DENO_VERSION} \
&& deno install -Afq -n deno_bindgen $DENO_BINDGEN_URL

COPY --from=plan /recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
COPY --from=plan /app/recipe.json recipe.json

RUN cargo chef cook --recipe-path recipe.json --release --package native --features deno

COPY . .
RUN OUT_DIR=target deno_bindgen --release -- --locked --package native -F deno \
&& mv /target/release/libnative.so .

WORKDIR /typegate
RUN deno cache --config=deno.json --unstable src/main.ts \
&& mkdir tmp
RUN deno_bindgen --release -- --locked --package native --features deno \
&& mv /app/target/release/libnative.so .

WORKDIR /app/typegate

RUN deno cache --unstable src/main.ts \
&& mkdir -p tmp

#

FROM debian:stable-slim AS debian

ARG TINI_VERSION=v0.19.0

ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /tini

RUN chmod +x /tini

#

FROM denoland/deno:distroless-${DENO_VERSION} AS runner
FROM gcr.io/distroless/cc-debian11:${DISTROLESS_TAG}

ENV DENO_DIR /deno-dir/

WORKDIR /app

COPY --from=debian /tini /tini
COPY --from=deno-bin /deno /bin/deno
COPY --from=debian /lib/x86_64-linux-gnu/libz.so* /lib/x86_64-linux-gnu/
COPY LICENSE.md .
COPY --from=builder /bindings/bindings.ts /bindings/
COPY --from=builder /libnative.so /target/release/
COPY --from=builder /typegate/deno.json ./
COPY --from=builder /typegate/src ./src
COPY --from=builder /app/bindings/bindings.ts /bindings/
COPY --from=builder /app/libnative.so /target/release/
COPY --from=builder /app/typegate/deno.json ./
COPY --from=builder /app/typegate/src ./src

# writeable
COPY --from=builder --chown=nonroot:nonroot /deno-dir /deno-dir
COPY --from=builder --chown=nonroot:nonroot /typegate/deno.lock ./
COPY --from=builder --chown=nonroot:nonroot /typegate/tmp ./
COPY --from=builder --chown=nonroot:nonroot /app/typegate/deno.lock ./
COPY --from=builder --chown=nonroot:nonroot /app/typegate/tmp ./

USER nonroot
EXPOSE 7890

ENTRYPOINT ["/tini", "--"]

CMD ["deno", "run", "--config=deno.json", "--unstable", "--allow-run=hostname", "--allow-env", "--allow-hrtime", "--allow-write", "--allow-ffi", "--allow-read", "--allow-net", "src/main.ts"]
ENTRYPOINT ["/tini", "--", "/bin/deno"]
CMD ["run", "--unstable", "--allow-run=hostname", "--allow-env", "--allow-hrtime", "--allow-write", "--allow-ffi", "--allow-read", "--allow-net", "src/main.ts"]
1 change: 1 addition & 0 deletions dev/Dockerfile.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ website
bindings
examples
**/.*
tmp
4 changes: 3 additions & 1 deletion examples/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
TG_SECRET: "a4lNi0PbEItlFZbus1oeH/+wyIxi9uH6TpL8AIqIaMBNvp7SESmuUBbfUwC0prxhGhZqHw8vMDYZAGMhSZ4fLw=="
TG_ADMIN_PASSWORD: password
env_file:
- .env.example
- .env.sample
depends_on:
- redis

Expand All @@ -28,3 +28,5 @@ services:
POSTGRES_DB: db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
ports:
- "5432:5432"
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
-- CreateTable
CREATE TABLE "Post" (
CREATE TABLE "User" (
"id" INTEGER NOT NULL,
"content" TEXT NOT NULL,
"authorId" INTEGER NOT NULL,
"name" TEXT NOT NULL,

CONSTRAINT "Post_pkey" PRIMARY KEY ("id")
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "User" (
CREATE TABLE "Post" (
"id" INTEGER NOT NULL,
"name" TEXT NOT NULL,
"content" TEXT NOT NULL,
"authorId" INTEGER NOT NULL,

CONSTRAINT "User_pkey" PRIMARY KEY ("id")
CONSTRAINT "Post_pkey" PRIMARY KEY ("id")
);

-- CreateTable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@ pub fn archive<P: AsRef<Path>>(folder: P) -> Result<String> {
let mut tar = tar::Builder::new(encoder);
tar.append_dir_all(".", &folder)?;
let bytes = tar.into_inner()?.finish()?;

Ok(STANDARD.encode(bytes))
}
2 changes: 1 addition & 1 deletion libs/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright Metatype OÜ under the Elastic License 2.0 (ELv2). See LICENSE.md for usage.

pub mod migrations;
pub mod archive;
pub mod typegraph;

pub fn get_version() -> String {
Expand Down
39 changes: 28 additions & 11 deletions libs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,38 @@ use proc_macro::TokenStream;
use quote::quote;
use syn::{parse, DeriveInput, ItemFn};

#[proc_macro_attribute]
pub fn deno(_attr: TokenStream, input: TokenStream) -> TokenStream {
fn deno_attr(input: TokenStream, non_blocking: bool) -> TokenStream {
if let Ok(input) = parse::<ItemFn>(input.clone()) {
// fn
let deno = if non_blocking {
quote! { deno_bindgen::deno_bindgen(non_blocking) }
} else {
quote! { deno_bindgen::deno_bindgen }
};
let output = quote! {
#[cfg_attr(feature = "deno", deno_bindgen::deno_bindgen(non_blocking))]
#[cfg_attr(feature = "deno", #deno)]
#[cfg_attr(not(feature = "deno"), allow(dead_code))]
#input
};
return TokenStream::from(output);
TokenStream::from(output)
} else {
// struct
let input = parse::<DeriveInput>(input).unwrap();
let output = quote! {
#[derive(Debug)]
#[deno_bindgen::deno_bindgen]
#input
};
TokenStream::from(output)
}
let input = parse::<DeriveInput>(input).unwrap();
let output = quote! {
#[derive(Debug)]
#[deno_bindgen::deno_bindgen]
#input
};
TokenStream::from(output)
}

#[proc_macro_attribute]
pub fn deno(_attr: TokenStream, input: TokenStream) -> TokenStream {
deno_attr(input, true)
}

#[proc_macro_attribute]
pub fn deno_sync(_attr: TokenStream, input: TokenStream) -> TokenStream {
deno_attr(input, false)
}
14 changes: 4 additions & 10 deletions meta-cli/src/cli/prisma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ use crate::{
},
};
use anyhow::{bail, Context, Result};
use base64::{engine::general_purpose::STANDARD, Engine};
use clap::{Parser, Subcommand};
use colored::Colorize;
use flate2::{write::GzEncoder, Compression};
use common::archive::archive;
use indoc::indoc;
use prisma_models::psl;
use question::{Answer, Question};
Expand Down Expand Up @@ -156,12 +155,7 @@ impl Action for Deploy {
.join(&self.typegraph)
.join(&self.runtime);

let enc = GzEncoder::new(Vec::new(), Compression::default());
let mut tar = tar::Builder::new(enc);
tar.append_dir_all("migrations", migrations_path.as_path())?;
let enc = tar.into_inner()?;
let migrations = enc.finish()?;
let migrations = STANDARD.encode(migrations);
let migrations = archive(migrations_path.as_path())?;

let node_config = config.node("deploy");
let node_url = node_config.url(self.gate.clone());
Expand Down Expand Up @@ -351,7 +345,7 @@ impl PrismaMigrate {
runtime.as_deref(),
)?;
let migrations = if let Some(path) = migrations_path {
Some(common::migrations::archive(path)?)
Some(common::archive::archive(path)?)
} else {
None
};
Expand All @@ -375,7 +369,7 @@ impl PrismaMigrate {
.as_ref()
.expect("migrations should have been updated"); // migrations should have been set

common::migrations::unpack(migrations_path, self.migrations)?;
common::archive::unpack(migrations_path, self.migrations)?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion typegate/native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ connection-string = "0.1.14"
url = "2.3.1"
thiserror = "1.0.38"
tokio = { version = "1.25.0", features = ["full"] }
lazy_static = "1.4.0"
log = "0.4.17"
env_logger = "0.10.0"
serde_json = "1.0.93"
Expand All @@ -46,3 +45,4 @@ rust-s3 = { git = "https://github.com/zifeo/rust-s3", branch = "order" }
http = "0.2.9"
uuid = { version = "1.3.0", features = ["v4"] }
envconfig = "0.10.0"
once_cell = "1.17.1"
85 changes: 49 additions & 36 deletions typegate/native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,67 @@ mod config;
mod runtimes;
mod typegraph;

use config::Config;
use envconfig::Envconfig;
use lazy_static::lazy_static;
use log::info;
use macros::deno;
use log::{error, info};
use macros::deno_sync;
use once_cell::sync::Lazy;
use sentry::ClientInitGuard;
use std::io::Write;
use std::{borrow::Cow, env, fs, panic, path::PathBuf};
use tokio::runtime::Runtime;

lazy_static! {
pub static ref CONFIG: config::Config = config::Config::init_from_env().unwrap();
static ref RT: Runtime = {
info!("Runtime created");
Runtime::new().unwrap()
static RT: Lazy<Runtime> = Lazy::new(|| Runtime::new().expect("failed to create Tokio runtime"));
static CONFIG: Lazy<Config> =
Lazy::new(|| Config::init_from_env().expect("failed to parse config"));
static TMP_DIR: Lazy<PathBuf> = Lazy::new(|| {
let path = env::current_dir().expect("no current dir").join("tmp");
fs::create_dir_all(&path).expect("failed to create tmp dir");
path
});
static SENTRY_GUARD: Lazy<ClientInitGuard> = Lazy::new(|| {
let env = if CONFIG.debug {
"development".to_string()
} else {
"production".to_string()
};
static ref SENTRY_GUARD: sentry::ClientInitGuard = {
sentry::init((
CONFIG.sentry_dsn.clone(),
sentry::ClientOptions {
release: Some(Cow::from(common::get_version())),
environment: Some(Cow::from(if CONFIG.debug {
"development".to_string()
} else {
"production".to_string()
})),
sample_rate: CONFIG.sentry_sample_rate,
traces_sample_rate: CONFIG.sentry_traces_sample_rate,
..Default::default()
},
))
};
pub static ref TMP_DIR: PathBuf = {
let dir = env::current_dir().expect("no current dir").join("tmp");
fs::create_dir_all(&dir).expect("failed to create tmp dir");
dir
};
}
sentry::init((
CONFIG.sentry_dsn.clone(),
sentry::ClientOptions {
release: Some(Cow::from(common::get_version())),
environment: Some(Cow::from(env)),
sample_rate: CONFIG.sentry_sample_rate,
traces_sample_rate: CONFIG.sentry_traces_sample_rate,
..Default::default()
},
))
});

#[deno]
fn init() {
env_logger::init();
info!("init");
#[deno_sync]
fn init_native() {
SENTRY_GUARD.is_enabled();
env_logger::builder()
.format_timestamp_millis()
.format(|buf, record| {
writeln!(
buf,
"{} {: <5} {: <12} {}",
buf.timestamp_millis(),
record.level(),
record.target(),
record.args()
)
})
.init();
info!("init native");
let default_panic = std::panic::take_hook();
panic::set_hook(Box::new(move |panic_info| {
println!("Panic: {}", panic_info);
error!("Panic: {}", panic_info);
default_panic(panic_info);
}));
}

#[deno]
#[deno_sync]
fn get_version() -> String {
common::get_version()
}
Loading

0 comments on commit c4b6b34

Please sign in to comment.