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

Simplify and improve docker setup. #215

Merged
merged 4 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
62 changes: 0 additions & 62 deletions Dockerfile

This file was deleted.

5 changes: 4 additions & 1 deletion .dockerignore → config/docker_demo/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
/*/log/*
/*/tmp/*

# Ignore spec files.
/*/spec/*

# Ignore dockerfile
Dockerfile
Dockerfile
2 changes: 1 addition & 1 deletion .env → config/docker_demo/.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
OPENSEARCH_VERSION=2.18.0
ENV=local
ELASTICGRAPH_PORT=9393
ELASTICGRAPH_PORT=9393
47 changes: 47 additions & 0 deletions config/docker_demo/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
ARG PORT=9393
ARG RUBY_VERSION=3.4

FROM ruby:${RUBY_VERSION}

WORKDIR /app

# Copy the minimal Gemfile and local ElasticGraph gems that we need.
COPY config/docker_demo/Gemfile .
COPY elasticgraph elasticgraph
COPY elasticgraph-admin elasticgraph-admin
COPY elasticgraph-datastore_core elasticgraph-datastore_core
COPY elasticgraph-graphql elasticgraph-graphql
COPY elasticgraph-indexer elasticgraph-indexer
COPY elasticgraph-json_schema elasticgraph-json_schema
COPY elasticgraph-local elasticgraph-local
COPY elasticgraph-opensearch elasticgraph-opensearch
COPY elasticgraph-query_registry elasticgraph-query_registry
COPY elasticgraph-rack elasticgraph-rack
COPY elasticgraph-schema_artifacts elasticgraph-schema_artifacts
COPY elasticgraph-schema_definition elasticgraph-schema_definition
COPY elasticgraph-support elasticgraph-support

# Change the `ElasticGraph::VERSION` constant to a version that will never match any of the released
# ElasticGraph gems. This ensures that when we `bundle install` in our demo project, it will not fall
# back to released ElasticGraph gems. If we've failed to copy over any of the ElasticGraph gems needed
# to boot the demo app, we want it to fail fast and tell us rather than pulling in released gems.
RUN sed -i 's/^\(\s*\)VERSION = ".*"/\1VERSION = "999.999.999"/' elasticgraph-support/lib/elastic_graph/version.rb

# Install dependencies
RUN bundle install

# Running the new command will commit to git. Setup defaults
RUN git config --global user.email "[email protected]"
RUN git config --global user.name "Demo User"

# Create demo app using the locally built elasticgraph project
RUN ELASTICGRAPH_GEMS_PATH=/app bundle exec elasticgraph new demo --datastore opensearch

# Change work directory into the demo app
WORKDIR /app/demo

# Reference OpenSearch from the docker container
RUN sed -i 's/localhost:9293/opensearch:9200/g' config/settings/local.yaml

# Generate fake data and boot the GraphiQL UI
CMD ["bundle", "exec", "rake", "index_fake_data:artists" ,"boot_graphiql[${PORT},--host=0.0.0.0,true]"]
16 changes: 16 additions & 0 deletions config/docker_demo/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2024 Block, Inc.
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
#
# frozen_string_literal: true

source "https://rubygems.org"

# This minimal Gemfile is used by the docker image and intentionally only contains
# what is needed to be able to run `bundle exec elasticgraph new`. The resulting
# demo project has its own `Gemfile` which will pull in the rest of the ElasticGraph
# gems.
gemspec path: "elasticgraph"
gemspec path: "elasticgraph-support"
22 changes: 5 additions & 17 deletions docker-compose.yaml → config/docker_demo/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ networks:
services:
opensearch:
build:
context: ./elasticgraph-local/lib/elastic_graph/local/opensearch/.
context: ../../elasticgraph-local/lib/elastic_graph/local/opensearch/.
dockerfile: Dockerfile
args:
VERSION: 2.18.0
container_name: opensearch-2.18.0-${ENV}
VERSION: ${OPENSEARCH_VERSION}
container_name: opensearch-${OPENSEARCH_VERSION}-${ENV}
healthcheck:
interval: 10s
retries: 80
Expand Down Expand Up @@ -38,22 +38,10 @@ services:
- opensearch:/usr/share/opensearch/data
ports:
- 9200:9200
dashboards:
build:
context: ./elasticgraph-local/lib/elastic_graph/local/opensearch/.
dockerfile: UI-Dockerfile
args:
VERSION: ${OPENSEARCH_VERSION}
container_name: dashboards-${OPENSEARCH_VERSION}-${ENV}
environment:
- OPENSEARCH_HOSTS=http://opensearch:9200
- DISABLE_SECURITY_DASHBOARDS_PLUGIN=true
ports:
- 5601:5601
elasticgraph:
build:
context: .
dockerfile: Dockerfile
context: ../..
dockerfile: config/docker_demo/Dockerfile
args:
container_name: elasticgraph-${ENV}
environment:
Expand Down
16 changes: 15 additions & 1 deletion elasticgraph/lib/elastic_graph/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,25 @@ def new(app_path)
raise ::Thor::Error, "Invalid datastore option: #{options[:datastore]}. Must be #{VALID_DATASTORES.join(" or ")}."
end

# This determines where the ElasticGraph gems are sourced from. By default, we source them from the
# released gems (using the current `VERSION`). However, we also need to be able to use the local
# unreleased gems in some specific situations:
#
# - From our cli acceptance spec -- we want to test against our local gems, not the released gems.
# - From our Dockerfile -- we want it to build the docker image from our local gems.
gemfile_elasticgraph_details_code_snippet = %(["#{VERSION}"])
if (eg_gems_path = ENV["ELASTICGRAPH_GEMS_PATH"])
gemfile_elasticgraph_details_code_snippet = %([path: "#{eg_gems_path}"])
# :nocov: -- our tests always override `gemfile_elasticgraph_details_code_snippet` using the ENV var.
else
# :nocov:
end

setup_env = SetupEnv.new(
app_name: app_name,
app_module: app_name.split("_").map(&:capitalize).join,
datastore: options.fetch(:datastore),
gemfile_elasticgraph_details_code_snippet: %(["#{VERSION}"])
gemfile_elasticgraph_details_code_snippet: gemfile_elasticgraph_details_code_snippet
)

say "Creating a new #{setup_env.datastore_name} ElasticGraph project called '#{app_name}' at: #{new_app_path}", :green
Expand Down
130 changes: 64 additions & 66 deletions elasticgraph/spec/acceptance/cli_new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,68 +12,68 @@
module ElasticGraph
::RSpec.describe CLI, "new command", :in_temp_dir do
it "initializes a new ElasticGraph project" do
override_gemfile_to_use_local_elasticgraph_gems

output = run_new("musical_artists1")

expect(output.lines.first(18).join).to eq <<~EOS
Creating a new OpenSearch ElasticGraph project called 'musical_artists1' at: #{::Dir.pwd}/musical_artists1
create musical_artists1
create musical_artists1/.gitignore
create musical_artists1/.standard.yml
create musical_artists1/Gemfile
create musical_artists1/README.md
create musical_artists1/Rakefile
create musical_artists1/config/queries/example_client/FindArtist.graphql
create musical_artists1/config/queries/example_client/ListArtistAlbums.graphql
create musical_artists1/config/schema.rb
create musical_artists1/config/schema/artists.rb
create musical_artists1/config/settings/local.yaml
create musical_artists1/spec/project_spec.rb
create musical_artists1/lib/musical_artists1
create musical_artists1/lib/musical_artists1/factories.rb
create musical_artists1/lib/musical_artists1/fake_data_batch_generator.rb
create musical_artists1/lib/musical_artists1/shared_factories.rb
run bundle install from "./musical_artists1"
EOS

bundle_exec_rake_line = output.lines.index { |l| l =~ /bundle exec rake/ }
expect(output.lines[bundle_exec_rake_line..(bundle_exec_rake_line + 16)].join).to eq <<~EOS
run bundle exec rake schema_artifacts:dump query_registry:dump_variables:all build from "./musical_artists1"
Dumped schema artifact to `config/schema/artifacts/datastore_config.yaml`.
Dumped schema artifact to `config/schema/artifacts/json_schemas.yaml`.
Dumped schema artifact to `config/schema/artifacts/json_schemas_by_version/v1.yaml`.
Dumped schema artifact to `config/schema/artifacts/runtime_metadata.yaml`.
Dumped schema artifact to `config/schema/artifacts/schema.graphql`.
- Dumped `config/queries/example_client/FindArtist.variables.yaml`.
- Dumped `config/queries/example_client/ListArtistAlbums.variables.yaml`.
Inspecting 8 files
........

8 files inspected, no offenses detected
For client `example_client`:
- FindArtist.graphql (1 operation):
- FindArtist: ✅
- ListArtistAlbums.graphql (1 operation):
- ListArtistAlbums: ✅
EOS

expect(output.lines.last(6).join).to eq <<~EOS
Successfully bootstrapped 'musical_artists1' as a new OpenSearch ElasticGraph project.
Next steps:
1. cd musical_artists1
2. Run `bundle exec rake boot_locally` to try it out in your browser.
3. Run `bundle exec rake -T` to view other available tasks.
4. Customize your new project as needed. (Search for `TODO` to find things that need updating.)
EOS

# Verify that all ERB templates rendered properly. If any files had ERB template tags (e.g. `<%= foo %>`)
# but were not named with the proper `.tt` file extension, then thor would copy them without rendering them
# as ERB. This would catch it.
expect(all_committed_code_in("musical_artists1")).to exclude("<%", "%>")

# Verify that the only TODO comments in the project comte from our template, not from our generated artifacts.
expect(todo_comments_in("musical_artists1").join("\n")).to eq(todo_comments_in(CLI.source_root).join("\n"))
override_gemfile_to_use_local_elasticgraph_gems do
output = run_new("musical_artists1")

expect(output.lines.first(18).join).to eq <<~EOS
Creating a new OpenSearch ElasticGraph project called 'musical_artists1' at: #{::Dir.pwd}/musical_artists1
create musical_artists1
create musical_artists1/.gitignore
create musical_artists1/.standard.yml
create musical_artists1/Gemfile
create musical_artists1/README.md
create musical_artists1/Rakefile
create musical_artists1/config/queries/example_client/FindArtist.graphql
create musical_artists1/config/queries/example_client/ListArtistAlbums.graphql
create musical_artists1/config/schema.rb
create musical_artists1/config/schema/artists.rb
create musical_artists1/config/settings/local.yaml
create musical_artists1/spec/project_spec.rb
create musical_artists1/lib/musical_artists1
create musical_artists1/lib/musical_artists1/factories.rb
create musical_artists1/lib/musical_artists1/fake_data_batch_generator.rb
create musical_artists1/lib/musical_artists1/shared_factories.rb
run bundle install from "./musical_artists1"
EOS

bundle_exec_rake_line = output.lines.index { |l| l =~ /bundle exec rake/ }
expect(output.lines[bundle_exec_rake_line..(bundle_exec_rake_line + 16)].join).to eq <<~EOS
run bundle exec rake schema_artifacts:dump query_registry:dump_variables:all build from "./musical_artists1"
Dumped schema artifact to `config/schema/artifacts/datastore_config.yaml`.
Dumped schema artifact to `config/schema/artifacts/json_schemas.yaml`.
Dumped schema artifact to `config/schema/artifacts/json_schemas_by_version/v1.yaml`.
Dumped schema artifact to `config/schema/artifacts/runtime_metadata.yaml`.
Dumped schema artifact to `config/schema/artifacts/schema.graphql`.
- Dumped `config/queries/example_client/FindArtist.variables.yaml`.
- Dumped `config/queries/example_client/ListArtistAlbums.variables.yaml`.
Inspecting 8 files
........

8 files inspected, no offenses detected
For client `example_client`:
- FindArtist.graphql (1 operation):
- FindArtist: ✅
- ListArtistAlbums.graphql (1 operation):
- ListArtistAlbums: ✅
EOS

expect(output.lines.last(6).join).to eq <<~EOS
Successfully bootstrapped 'musical_artists1' as a new OpenSearch ElasticGraph project.
Next steps:
1. cd musical_artists1
2. Run `bundle exec rake boot_locally` to try it out in your browser.
3. Run `bundle exec rake -T` to view other available tasks.
4. Customize your new project as needed. (Search for `TODO` to find things that need updating.)
EOS

# Verify that all ERB templates rendered properly. If any files had ERB template tags (e.g. `<%= foo %>`)
# but were not named with the proper `.tt` file extension, then thor would copy them without rendering them
# as ERB. This would catch it.
expect(all_committed_code_in("musical_artists1")).to exclude("<%", "%>")

# Verify that the only TODO comments in the project comte from our template, not from our generated artifacts.
expect(todo_comments_in("musical_artists1").join("\n")).to eq(todo_comments_in(CLI.source_root).join("\n"))
end
end

it "aborts if given an invalid datastore option" do
Expand Down Expand Up @@ -171,10 +171,8 @@ def run_new(*argv)
# Here we hook into the call to `ElasticGraph.setup_env` in order to override its
# `gemfile_elasticgraph_details_code_snippet`, to force it to use oru local gems.
def override_gemfile_to_use_local_elasticgraph_gems
allow(::ElasticGraph).to receive(:setup_env).and_wrap_original do |original|
original.call&.with(
gemfile_elasticgraph_details_code_snippet: %([path: "#{CommonSpecHelpers::REPO_ROOT}"])
)
with_env "ELASTICGRAPH_GEMS_PATH" => CommonSpecHelpers::REPO_ROOT do
yield
end
end

Expand Down