From f3b56a63e382914c8ef3878f8c9ef17a7cf78ad8 Mon Sep 17 00:00:00 2001 From: Hironori Yamamoto Date: Tue, 3 Jan 2023 11:55:26 +0900 Subject: [PATCH] Add docker compose configurations (#11) --- .env.sample | 5 +++ .gitignore | 1 + README.md | 17 ++++++++ docker-compose.yml | 29 ++++++++++++- redash-search-sync/Dockerfile | 35 --------------- redash-search-web/next-env.d.ts | 5 --- .../.dockerignore | 0 .../Cargo.lock | 2 +- .../Cargo.toml | 2 +- redash-searcher-sync/Dockerfile | 41 ++++++++++++++++++ .../src/app.rs | 0 .../src/configs.rs | 4 +- .../src/lib.rs | 0 .../src/main.rs | 21 ++++----- .../src/redash.rs | 0 .../.dockerignore | 0 .../.eslintrc.json | 0 .../.gitignore | 0 .../.prettier | 0 .../.prettierignore | 0 .../Dockerfile | 24 ++++++---- .../README.md | 0 .../components/HighlightedQuery.tsx | 0 .../components/HitList.tsx | 12 ++--- .../components/Search.tsx | 0 redash-searcher-web/entrypoint.sh | 10 +++++ .../lib/apolloClient.ts | 0 .../next.config.js | 5 --- .../package.json | 2 +- .../pages/[...index].tsx | 0 .../pages/_app.tsx | 0 .../pages/api/graphql.ts | 17 +++++++- .../pages/api/models.ts | 0 .../pages/index.tsx | 0 .../public/favicon.ico | Bin .../public/next.svg | 0 .../public/thirteen.svg | 0 .../public/vercel.svg | 0 .../tsconfig.json | 0 .../yarn.lock | 0 40 files changed, 154 insertions(+), 78 deletions(-) delete mode 100644 redash-search-sync/Dockerfile delete mode 100644 redash-search-web/next-env.d.ts rename {redash-search-sync => redash-searcher-sync}/.dockerignore (100%) rename {redash-search-sync => redash-searcher-sync}/Cargo.lock (99%) rename {redash-search-sync => redash-searcher-sync}/Cargo.toml (95%) create mode 100644 redash-searcher-sync/Dockerfile rename {redash-search-sync => redash-searcher-sync}/src/app.rs (100%) rename {redash-search-sync => redash-searcher-sync}/src/configs.rs (92%) rename {redash-search-sync => redash-searcher-sync}/src/lib.rs (100%) rename {redash-search-sync => redash-searcher-sync}/src/main.rs (64%) rename {redash-search-sync => redash-searcher-sync}/src/redash.rs (100%) rename {redash-search-web => redash-searcher-web}/.dockerignore (100%) rename {redash-search-web => redash-searcher-web}/.eslintrc.json (100%) rename {redash-search-web => redash-searcher-web}/.gitignore (100%) rename {redash-search-web => redash-searcher-web}/.prettier (100%) rename {redash-search-web => redash-searcher-web}/.prettierignore (100%) rename {redash-search-web => redash-searcher-web}/Dockerfile (64%) rename {redash-search-web => redash-searcher-web}/README.md (100%) rename {redash-search-web => redash-searcher-web}/components/HighlightedQuery.tsx (100%) rename {redash-search-web => redash-searcher-web}/components/HitList.tsx (91%) rename {redash-search-web => redash-searcher-web}/components/Search.tsx (100%) create mode 100755 redash-searcher-web/entrypoint.sh rename {redash-search-web => redash-searcher-web}/lib/apolloClient.ts (100%) rename {redash-search-web => redash-searcher-web}/next.config.js (56%) rename {redash-search-web => redash-searcher-web}/package.json (97%) rename {redash-search-web => redash-searcher-web}/pages/[...index].tsx (100%) rename {redash-search-web => redash-searcher-web}/pages/_app.tsx (100%) rename {redash-search-web => redash-searcher-web}/pages/api/graphql.ts (86%) rename {redash-search-web => redash-searcher-web}/pages/api/models.ts (100%) rename {redash-search-web => redash-searcher-web}/pages/index.tsx (100%) rename {redash-search-web => redash-searcher-web}/public/favicon.ico (100%) rename {redash-search-web => redash-searcher-web}/public/next.svg (100%) rename {redash-search-web => redash-searcher-web}/public/thirteen.svg (100%) rename {redash-search-web => redash-searcher-web}/public/vercel.svg (100%) rename {redash-search-web => redash-searcher-web}/tsconfig.json (100%) rename {redash-search-web => redash-searcher-web}/yarn.lock (100%) diff --git a/.env.sample b/.env.sample index 7b4d7a9..abb5253 100644 --- a/.env.sample +++ b/.env.sample @@ -1,2 +1,7 @@ +LOG_LEVEL=INFO REDASH_API_BASE_URL= REDASH_API_KEY= +NEXT_PUBLIC_REDASH__URL=${REDASH__URL} +OPEN_SEARCH__URL=http://localhost:9200 +OPEN_SEARCH__USERNAME=admin +OPEN_SEARCH__PASSWORD=admin diff --git a/.gitignore b/.gitignore index fe6508d..899de1a 100644 --- a/.gitignore +++ b/.gitignore @@ -89,6 +89,7 @@ web_modules/ .env.test.local .env.production.local .env.local +.env.docker # parcel-bundler cache (https://parceljs.org/) .cache diff --git a/README.md b/README.md index 71aa8f0..4dd87e8 100644 --- a/README.md +++ b/README.md @@ -1 +1,18 @@ # Redash Searcher + +Web application for searching Redash queries. +Queries are stored in OpenSearch and synchronized with Redash periodically. + +## Start Up + +:pencil2: Copy `.env.sample` to `.env.docker` and edit it. + +```console +cp .env.sample .env.docker +``` + +:rocket: Start up containers. + +```console +docker compose up +``` diff --git a/docker-compose.yml b/docker-compose.yml index 928e648..5643ea4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -version: '3' +version: "3" services: opensearch-dashboards: @@ -20,7 +20,7 @@ services: - cluster.initial_master_nodes=main - bootstrap.memory_lock=true - http.host=0.0.0.0 - - plugins.security.disabled=true + - plugins.security.ssl.http.enabled=false - transport.host=127.0.0.1 - "OPENSEARCH_JAVA_OPTS=-Xms512m -Xmx512m" ulimits: @@ -37,6 +37,31 @@ services: networks: - redash-searcher + redash-searcher-sync: + restart: on-failure + build: + context: ./redash-searcher-sync + dockerfile: Dockerfile + env_file: + - .env.docker + depends_on: + - opensearch-node-main + networks: + - redash-searcher + + redash-searcher-web: + restart: on-failure + build: + context: ./redash-searcher-web + dockerfile: Dockerfile + env_file: + - .env.docker + ports: + - 3000:3000 + depends_on: + - redash-searcher-sync + networks: + - redash-searcher volumes: opensearch-node-main-data: diff --git a/redash-search-sync/Dockerfile b/redash-search-sync/Dockerfile deleted file mode 100644 index 21b5a75..0000000 --- a/redash-search-sync/Dockerfile +++ /dev/null @@ -1,35 +0,0 @@ -# ref: https://dev.to/rogertorres/first-steps-with-docker-rust-30oi -FROM rust:1.66.0-bullseye as build - -RUN USER=root cargo new --bin redash-search-sync -WORKDIR /redash-search-sync - -# copy over your manifests -COPY ./Cargo.lock ./Cargo.lock -COPY ./Cargo.toml ./Cargo.toml - -# this build step will cache your dependencies -RUN cargo build --release -RUN rm src/*.rs - -# copy your source tree -COPY ./src ./src - -# build for release -RUN rm ./target/release/deps/redash_search_sync* -RUN cargo build --release - -# our final base -FROM debian:bullseye-slim - - -RUN addgroup --system --gid 1001 redash-search -RUN adduser --system --uid 1001 redash-search-sync - -# copy the build artifact from the build stage -COPY --from=build --chown=redash-search-sync:redash-search /redash-search-sync/target/release/redash-search-sync . - -USER redash-search-sync - -# set the startup command to run your binary -ENTRYPOINT ["./redash-search-sync"] diff --git a/redash-search-web/next-env.d.ts b/redash-search-web/next-env.d.ts deleted file mode 100644 index 4f11a03..0000000 --- a/redash-search-web/next-env.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -/// -/// - -// NOTE: This file should not be edited -// see https://nextjs.org/docs/basic-features/typescript for more information. diff --git a/redash-search-sync/.dockerignore b/redash-searcher-sync/.dockerignore similarity index 100% rename from redash-search-sync/.dockerignore rename to redash-searcher-sync/.dockerignore diff --git a/redash-search-sync/Cargo.lock b/redash-searcher-sync/Cargo.lock similarity index 99% rename from redash-search-sync/Cargo.lock rename to redash-searcher-sync/Cargo.lock index eaeb10d..f2af8fb 100644 --- a/redash-search-sync/Cargo.lock +++ b/redash-searcher-sync/Cargo.lock @@ -1213,7 +1213,7 @@ dependencies = [ ] [[package]] -name = "redash-search-sync" +name = "redash-searcher-sync" version = "0.1.0" dependencies = [ "anyhow", diff --git a/redash-search-sync/Cargo.toml b/redash-searcher-sync/Cargo.toml similarity index 95% rename from redash-search-sync/Cargo.toml rename to redash-searcher-sync/Cargo.toml index 451bf45..db63e14 100644 --- a/redash-search-sync/Cargo.toml +++ b/redash-searcher-sync/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "redash-search-sync" +name = "redash-searcher-sync" edition = "2021" version = "0.1.0" diff --git a/redash-searcher-sync/Dockerfile b/redash-searcher-sync/Dockerfile new file mode 100644 index 0000000..be4f921 --- /dev/null +++ b/redash-searcher-sync/Dockerfile @@ -0,0 +1,41 @@ +# ref: https://dev.to/rogertorres/first-steps-with-docker-rust-30oi +FROM rust:1.66.0-bullseye as build + +RUN USER=root cargo new --bin redash-searcher-sync +WORKDIR /redash-searcher-sync + +# copy over your manifests +COPY ./Cargo.lock ./Cargo.lock +COPY ./Cargo.toml ./Cargo.toml + +# this build step will cache your dependencies +RUN cargo build --release +RUN rm src/*.rs + +# copy your source tree +COPY ./src ./src + +# build for release +RUN rm ./target/release/deps/redash_searcher_sync* +RUN cargo build --release + +# our final base +FROM debian:bullseye-slim + + +RUN addgroup --system --gid 1001 redash-searcher +RUN adduser --system --uid 1001 redash-searcher-sync + +RUN apt-get update \ + && apt-get install -y ca-certificates \ + && update-ca-certificates \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +# copy the build artifact from the build stage +COPY --from=build --chown=redash-searcher-sync:redash-searcher /redash-searcher-sync/target/release/redash-searcher-sync . + +USER redash-searcher-sync + +# set the startup command to run your binary +ENTRYPOINT ["./redash-searcher-sync"] diff --git a/redash-search-sync/src/app.rs b/redash-searcher-sync/src/app.rs similarity index 100% rename from redash-search-sync/src/app.rs rename to redash-searcher-sync/src/app.rs diff --git a/redash-search-sync/src/configs.rs b/redash-searcher-sync/src/configs.rs similarity index 92% rename from redash-search-sync/src/configs.rs rename to redash-searcher-sync/src/configs.rs index a56ce60..f363cb2 100644 --- a/redash-search-sync/src/configs.rs +++ b/redash-searcher-sync/src/configs.rs @@ -10,8 +10,8 @@ pub struct RedashConfig { #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] pub struct OpenSearchConfig { - pub username: String, - pub password: String, + pub username: Option, + pub password: Option, pub url: String, } diff --git a/redash-search-sync/src/lib.rs b/redash-searcher-sync/src/lib.rs similarity index 100% rename from redash-search-sync/src/lib.rs rename to redash-searcher-sync/src/lib.rs diff --git a/redash-search-sync/src/main.rs b/redash-searcher-sync/src/main.rs similarity index 64% rename from redash-search-sync/src/main.rs rename to redash-searcher-sync/src/main.rs index 50549a2..3c9bee2 100644 --- a/redash-search-sync/src/main.rs +++ b/redash-searcher-sync/src/main.rs @@ -2,9 +2,9 @@ use std::str::FromStr; use opensearch::http::transport::{SingleNodeConnectionPool, TransportBuilder}; use opensearch::OpenSearch; -use redash_search_sync::app::App; -use redash_search_sync::configs::Configs; -use redash_search_sync::redash::DefaultRedashClient; +use redash_searcher_sync::app::App; +use redash_searcher_sync::configs::Configs; +use redash_searcher_sync::redash::DefaultRedashClient; use tracing::Level; #[tokio::main] @@ -21,13 +21,14 @@ async fn main() { let redash_client = Box::new(DefaultRedashClient::new(&config.redash.url, &config.redash.api_key).unwrap()); let conn_pool = SingleNodeConnectionPool::new((&config.open_search.url).parse().unwrap()); - let transport = TransportBuilder::new(conn_pool) - .auth(opensearch::auth::Credentials::Basic( - (&config.open_search.username).clone(), - (&config.open_search.password).clone(), - )) - .build() - .expect("failed to build transport"); + let mut builder = TransportBuilder::new(conn_pool); + if !(config.open_search.username.is_none() || config.open_search.password.is_none()) { + builder = builder.auth(opensearch::auth::Credentials::Basic( + config.open_search.username.clone().unwrap(), + config.open_search.password.clone().unwrap(), + )); + } + let transport = builder.build().expect("failed to build transport"); let client = OpenSearch::new(transport); let app = App::new(redash_client, client); diff --git a/redash-search-sync/src/redash.rs b/redash-searcher-sync/src/redash.rs similarity index 100% rename from redash-search-sync/src/redash.rs rename to redash-searcher-sync/src/redash.rs diff --git a/redash-search-web/.dockerignore b/redash-searcher-web/.dockerignore similarity index 100% rename from redash-search-web/.dockerignore rename to redash-searcher-web/.dockerignore diff --git a/redash-search-web/.eslintrc.json b/redash-searcher-web/.eslintrc.json similarity index 100% rename from redash-search-web/.eslintrc.json rename to redash-searcher-web/.eslintrc.json diff --git a/redash-search-web/.gitignore b/redash-searcher-web/.gitignore similarity index 100% rename from redash-search-web/.gitignore rename to redash-searcher-web/.gitignore diff --git a/redash-search-web/.prettier b/redash-searcher-web/.prettier similarity index 100% rename from redash-search-web/.prettier rename to redash-searcher-web/.prettier diff --git a/redash-search-web/.prettierignore b/redash-searcher-web/.prettierignore similarity index 100% rename from redash-search-web/.prettierignore rename to redash-searcher-web/.prettierignore diff --git a/redash-search-web/Dockerfile b/redash-searcher-web/Dockerfile similarity index 64% rename from redash-search-web/Dockerfile rename to redash-searcher-web/Dockerfile index d6ff833..0209ef4 100644 --- a/redash-search-web/Dockerfile +++ b/redash-searcher-web/Dockerfile @@ -22,10 +22,12 @@ COPY --from=deps /app/node_modules ./node_modules COPY . . # Next.js collects completely anonymous telemetry data about general usage. -# Learn more here: https://nextjs.org/telemetry +# Learn more here: https://redash-searcher-web.org/telemetry # Uncomment the following line in case you want to disable telemetry during the build. # ENV NEXT_TELEMETRY_DISABLED 1 +# set public env vars to be replaced at runtime +ENV NEXT_PUBLIC_REDASH__URL=APP_NEXT_PUBLIC_REDASH__URL RUN yarn build # If using npm comment out above and use below instead @@ -39,21 +41,25 @@ ENV NODE_ENV production # Uncomment the following line in case you want to disable telemetry during runtime. # ENV NEXT_TELEMETRY_DISABLED 1 -RUN addgroup --system --gid 1001 nodejs -RUN adduser --system --uid 1001 nextjs +RUN addgroup --system --gid 1001 redash-searcher +RUN adduser --system --uid 1001 redash-searcher-web COPY --from=builder /app/public ./public # Automatically leverage output traces to reduce image size -# https://nextjs.org/docs/advanced-features/output-file-tracing -COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ -COPY --from=builder --chown=nextjs:nodejs /app/.next/server ./.next/server -COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static +# https://nexjs.org/docs/advanced-features/output-file-tracing +COPY --from=builder --chown=redash-searcher-web:redash-searcher /app/.next/standalone ./ +COPY --from=builder --chown=redash-searcher-web:redash-searcher /app/.next/server ./.next/server +COPY --from=builder --chown=redash-searcher-web:redash-searcher /app/.next/static ./.next/static -USER nextjs +USER redash-searcher-web EXPOSE 3000 ENV PORT 3000 -ENTRYPOINT ["node", "server.js"] +COPY --chown=redash-searcher-web:redash-searcher entrypoint.sh ./ + +ENTRYPOINT [ "bash", "entrypoint.sh" ] + +CMD ["node", "server.js"] diff --git a/redash-search-web/README.md b/redash-searcher-web/README.md similarity index 100% rename from redash-search-web/README.md rename to redash-searcher-web/README.md diff --git a/redash-search-web/components/HighlightedQuery.tsx b/redash-searcher-web/components/HighlightedQuery.tsx similarity index 100% rename from redash-search-web/components/HighlightedQuery.tsx rename to redash-searcher-web/components/HighlightedQuery.tsx diff --git a/redash-search-web/components/HitList.tsx b/redash-searcher-web/components/HitList.tsx similarity index 91% rename from redash-search-web/components/HitList.tsx rename to redash-searcher-web/components/HitList.tsx index 38aa4c0..5b79672 100644 --- a/redash-search-web/components/HitList.tsx +++ b/redash-searcher-web/components/HitList.tsx @@ -10,14 +10,16 @@ import { } from "@elastic/eui"; import { IResultHitItem } from "../pages/api/models"; import { HighlightedQuery } from "./HighlightedQuery"; -import getConfig from "next/config"; - -const { publicRuntimeConfig } = getConfig(); export interface HitListProps { hitItems: IResultHitItem[]; } +const REDASH_URL = (process.env.NEXT_PUBLIC_REDASH__URL || "").replace( + /\/$/, + "" +); + const HitsList: React.FC = ({ hitItems }) => { return ( @@ -29,12 +31,12 @@ const HitsList: React.FC = ({ hitItems }) => { } title={hit.fields.name} - href={`${publicRuntimeConfig.redashURL}/queries/${hit.id}`} + href={`${REDASH_URL}/queries/${hit.id}`} description={hit.fields.description} > diff --git a/redash-search-web/components/Search.tsx b/redash-searcher-web/components/Search.tsx similarity index 100% rename from redash-search-web/components/Search.tsx rename to redash-searcher-web/components/Search.tsx diff --git a/redash-searcher-web/entrypoint.sh b/redash-searcher-web/entrypoint.sh new file mode 100755 index 0000000..f8bf7ab --- /dev/null +++ b/redash-searcher-web/entrypoint.sh @@ -0,0 +1,10 @@ +#!/bin/sh +# This is workaround to set public environment variables from runtime +# ref: https://dev.to/itsrennyman/manage-nextpublic-environment-variables-at-runtime-with-docker-53dl + +echo "Check that we have NEXT_PUBLIC_REDASH__URL vars" +test -n "$NEXT_PUBLIC_REDASH__URL" + +find /app/.next \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#APP_NEXT_PUBLIC_REDASH__URL#$NEXT_PUBLIC_REDASH__URL#g" + +exec "$@" diff --git a/redash-search-web/lib/apolloClient.ts b/redash-searcher-web/lib/apolloClient.ts similarity index 100% rename from redash-search-web/lib/apolloClient.ts rename to redash-searcher-web/lib/apolloClient.ts diff --git a/redash-search-web/next.config.js b/redash-searcher-web/next.config.js similarity index 56% rename from redash-search-web/next.config.js rename to redash-searcher-web/next.config.js index 3725085..6003305 100644 --- a/redash-search-web/next.config.js +++ b/redash-searcher-web/next.config.js @@ -1,14 +1,9 @@ -const { processEnv } = require("@next/env"); - /** @type {import('next').NextConfig} */ const nextConfig = { reactStrictMode: true, compiler: { emotion: true, }, - publicRuntimeConfig: { - redashURL: (processEnv.REDASH__URL || "").replace(/\/$/, ""), - }, output: "standalone", }; diff --git a/redash-search-web/package.json b/redash-searcher-web/package.json similarity index 97% rename from redash-search-web/package.json rename to redash-searcher-web/package.json index 1ef0e72..12cc780 100644 --- a/redash-search-web/package.json +++ b/redash-searcher-web/package.json @@ -1,5 +1,5 @@ { - "name": "redash-search-web", + "name": "redash-searcher-web", "version": "0.1.0", "private": true, "scripts": { diff --git a/redash-search-web/pages/[...index].tsx b/redash-searcher-web/pages/[...index].tsx similarity index 100% rename from redash-search-web/pages/[...index].tsx rename to redash-searcher-web/pages/[...index].tsx diff --git a/redash-search-web/pages/_app.tsx b/redash-searcher-web/pages/_app.tsx similarity index 100% rename from redash-search-web/pages/_app.tsx rename to redash-searcher-web/pages/_app.tsx diff --git a/redash-search-web/pages/api/graphql.ts b/redash-searcher-web/pages/api/graphql.ts similarity index 86% rename from redash-search-web/pages/api/graphql.ts rename to redash-searcher-web/pages/api/graphql.ts index 2cf5738..573e110 100644 --- a/redash-search-web/pages/api/graphql.ts +++ b/redash-searcher-web/pages/api/graphql.ts @@ -7,8 +7,21 @@ import { SearchkitSchema, } from "@searchkit/schema"; -const searchkitConfig= { - host: "http://localhost:9200", +const getOpenSearchURI = () => { + const openSearchURL = process.env.OPEN_SEARCH__URL || "http://localhost:9200"; + const openSearchUsername = process.env.OPEN_SEARCH__USERNAME; + const openSearchPassword = process.env.OPEN_SEARCH__PASSWORD; + if (!(openSearchUsername && openSearchPassword)) { + return openSearchURL; + } + return openSearchURL.replace( + "://", + `://${openSearchUsername}:${openSearchPassword}@` + ); +}; + +const searchkitConfig = { + host: getOpenSearchURI(), credential: {}, index: "redash", hits: { diff --git a/redash-search-web/pages/api/models.ts b/redash-searcher-web/pages/api/models.ts similarity index 100% rename from redash-search-web/pages/api/models.ts rename to redash-searcher-web/pages/api/models.ts diff --git a/redash-search-web/pages/index.tsx b/redash-searcher-web/pages/index.tsx similarity index 100% rename from redash-search-web/pages/index.tsx rename to redash-searcher-web/pages/index.tsx diff --git a/redash-search-web/public/favicon.ico b/redash-searcher-web/public/favicon.ico similarity index 100% rename from redash-search-web/public/favicon.ico rename to redash-searcher-web/public/favicon.ico diff --git a/redash-search-web/public/next.svg b/redash-searcher-web/public/next.svg similarity index 100% rename from redash-search-web/public/next.svg rename to redash-searcher-web/public/next.svg diff --git a/redash-search-web/public/thirteen.svg b/redash-searcher-web/public/thirteen.svg similarity index 100% rename from redash-search-web/public/thirteen.svg rename to redash-searcher-web/public/thirteen.svg diff --git a/redash-search-web/public/vercel.svg b/redash-searcher-web/public/vercel.svg similarity index 100% rename from redash-search-web/public/vercel.svg rename to redash-searcher-web/public/vercel.svg diff --git a/redash-search-web/tsconfig.json b/redash-searcher-web/tsconfig.json similarity index 100% rename from redash-search-web/tsconfig.json rename to redash-searcher-web/tsconfig.json diff --git a/redash-search-web/yarn.lock b/redash-searcher-web/yarn.lock similarity index 100% rename from redash-search-web/yarn.lock rename to redash-searcher-web/yarn.lock