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

Feature: add ontoportal bash script #59

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
API_URL=http://localhost:9393
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ on:
- development
- stage
- test

release:
types: [ published ]
jobs:
push_to_registry:
name: Push Docker branch image to Docker Hub
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ruby-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: copy-env-config
run: cp .env.sample .env
- name: Build docker-compose
run: docker-compose --profile 4store build #profile flag is set in order to build all containers in this step
- name: Run unit tests
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@ test/data/ontology_files/catalog-v001.xml
create_permissions.log

ontologies_api.iml

.env
62 changes: 54 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,53 @@

ontologies_api provides a RESTful interface for accessing [BioPortal](https://bioportal.bioontology.org/) (an open repository of biomedical ontologies). Supported services include downloads, search, access to terms and concepts, text annotation, and much more.

## Prerequisites
# Run ontologies_api

## Using OntoPortal api utilities script
### See help

```bash
bin/ontoportal help
```

```
Usage: bin/ontoportal {dev|test|run|help} [--reset-cache] [--api-url API_URL] [--api-key API_KEY]
dev : Start the Ontoportal API development server.
Example: bin/ontoportal dev --api-url http://localhost:9393
Use --reset-cache to remove volumes: bin/ontoportal dev --reset-cache
test : Run tests.
run : Run a command in the Ontoportal API Docker container.
help : Show this help message.
Description:
This script provides convenient commands for managing an Ontoportal API
application using Docker Compose. It includes options for starting the development server,
running tests, and executing commands within the Ontoportal API Docker container.
Goals:
- Simplify common tasks related to Ontoportal API development using Docker.
- Provide a consistent and easy-to-use interface for common actions.
```
### Configuration
```
cp .env.sample .env
```

### Run dev
```bash
bin/ontoportal dev
```

### Run test with a local OntoPortal API
```bash
bin/ontoportal test
```


## Manually
### Prerequisites

- [Ruby 2.x](http://www.ruby-lang.org/en/downloads/) (most recent patch level)
- [rbenv](https://github.com/sstephenson/rbenv) and [ruby-build](https://github.com/sstephenson/ruby-build) (optional)
Expand All @@ -19,7 +65,7 @@ ontologies_api provides a RESTful interface for accessing [BioPortal](https://bi
- [Solr](http://lucene.apache.org/solr/)
- BioPortal indexes ontology class and property content using Solr (a Lucene-based server)

## Configuring Solr
### Configuring Solr

To configure Solr for ontologies_api usage, modify the example project included with Solr by doing the following:

Expand All @@ -46,22 +92,22 @@ To configure Solr for ontologies_api usage, modify the example project included
# Edit the ontologieS_api/config/environments/{env}.rb file to point to your running instance:
# http://localhost:8983/solr/NCBO1

## Installing
### Installing

### Clone the repository
#### Clone the repository

```
$ git clone [email protected]:ncbo/ontologies_api.git
$ cd ontologies_api
```

### Install the dependencies
#### Install the dependencies

```
$ bundle install
```

### Create an environment configuration file
#### Create an environment configuration file

```
$ cp config/environments/config.rb.sample config/environments/development.rb
Expand All @@ -73,7 +119,7 @@ production.rb<br />
development.rb<br />
test.rb

### Run the unit tests (optional)
#### Run the unit tests (optional)

Requires a configuration file for the test environment:

Expand All @@ -87,7 +133,7 @@ Execute the suite of tests from the command line:
$ bundle exec rake test
```

### Run the application
#### Run the application

```
$ bundle exec rackup --port 9393
Expand Down
166 changes: 166 additions & 0 deletions bin/ontoportal
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#!/usr/bin/env bash

# Function to display script usage information
show_help() {
echo "Usage: $0 {dev|test|run|help} [--reset-cache] [--api-url API_URL] [--api-key API_KEY]"
echo " dev : Start the Ontoportal API development server."
echo " Example: $0 dev --api-url http://localhost:9393"
echo " Use --reset-cache to remove volumes: $0 dev --reset-cache"
echo " test : Run tests."
echo " run : Run a command in the Ontoportal API Docker container."
echo " help : Show this help message."
echo
echo "Description:"
echo " This script provides convenient commands for managing an Ontoportal API"
echo " application using Docker Compose. It includes options for starting the development server,"
echo " running tests, and executing commands within the Ontoportal API Docker container."
echo
echo "Goals:"
echo " - Simplify common tasks related to Ontoportal API development using Docker."
echo " - Provide a consistent and easy-to-use interface for common actions."
}
# Function to update or create the .env file with API_URL and API_KEY
update_env_file() {
local api_url="$1"

# Update the .env file with the provided values
file_content=$(<.env)

# Make changes to the variable
while IFS= read -r line; do
if [[ "$line" == "API_URL="* ]]; then
echo "API_URL=$api_url"
else
echo "$line"
fi
done <<< "$file_content" > .env
}

# Function to create configuration files if they don't exist
create_config_files() {
if [ ! -f ".env" ]; then
echo "Creating .env file from env.sample"
cp .env.sample .env
fi

if [ ! -f "config/environments/development.rb" ]; then
echo "Creating config/environments/development.rb file from config/environments/config.rb.sample"
cp config/bioportal_config_env.rb.sample config/bioportal_config_development.rb
fi
}

# Function to handle the "dev" option
dev() {
echo "Starting Ontoportal API development server..."

create_config_files
local reset_cache=false
local api_url=""


# Check for command line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--reset-cache)
reset_cache=true
shift
;;
--api-url)
api_url="$2"
shift 2
;;
*)
echo "Unknown option: $1"
show_help
exit 1
;;
esac
done



# Check if arguments are provided
if [ -n "$api_url" ] ; then
# If arguments are provided, update the .env file
update_env_file "$api_url"
else
# If no arguments, fetch values from the .env file
source .env
api_url="$API_URL"
fi

if [ -z "$api_url" ] ; then
echo "Error: Missing required arguments. Please provide both --api-url or update them in your .env"
exit 1
fi

# Check if --reset-cache is present and execute docker compose down --volumes
if [ "$reset_cache" = true ]; then
echo "Resetting cache. Running: docker compose down --volumes"
docker compose down --volumes
fi

echo "Run: bundle exec api s -b 0.0.0.0 -p 3000"
docker compose run --rm -it --service-ports api bash -c "(bundle check || bundle install) && bundle exec rackup -o 0.0.0.0 --port 9393"
}

# Function to handle the "test" option
test() {


local api_url=""
local test_path=""
local test_options=""

# Check for command line arguments
while [ "$#" -gt 0 ]; do
case "$1" in
--api-url)
shift
api_url="$1"
;;
*)
if [ -z "$test_path" ]; then
test_path="$1"
else
test_options="$test_options $1"
fi
;;
esac
shift
done



script="API_URL=$api_url bundle exec rake test TEST=\"$test_path\" TESTOPTS=\"$test_options\""
echo "Running tests..."
echo "Run: $script"

docker compose run --rm -it api bash -c "(bundle check || bundle install) && $script"
}

# Function to handle the "run" option
run() {
echo "Run: $*"
docker compose run --rm -it api bash -c "$*"
}

# Main script logic
case "$1" in
"run")
run "${@:2}"
;;
"dev")
dev "${@:2}"
;;
"test")
test "${@:2}"
;;
"help")
show_help
;;
*)
show_help
exit 1
;;
esac
2 changes: 1 addition & 1 deletion config/environments/config.rb.sample
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ REDIS_PERSISTENT_HOST = ENV.include?("REDIS_PERSISTENT_HOST") ? ENV["REDIS_PERSI
REDIS_PORT = ENV.include?("REDIS_PORT") ? ENV["REDIS_PORT"] : 6379
REPORT_PATH = ENV.include?("REPORT_PATH") ? ENV["REPORT_PATH"] : "./test/ontologies_report.json"
REPOSITORY_FOLDER = ENV.include?("REPOSITORY_FOLDER") ? ENV["REPOSITORY_FOLDER"] : "./test/data/ontology_files/repo"
REST_URL_PREFIX = ENV.include?("REST_URL_PREFIX") ? ENV["REST_URL_PREFIX"] : "http://localhost:9393"
REST_URL_PREFIX = ENV.include?("REST_URL_PREFIX") ? ENV["REST_URL_PREFIX"] : ENV["API_URL"] || "http://localhost:9393"
SOLR_PROP_SEARCH_URL = ENV.include?("SOLR_PROP_SEARCH_URL") ? ENV["SOLR_PROP_SEARCH_URL"] : "http://localhost:8983/solr/prop_search_core1"
SOLR_TERM_SEARCH_URL = ENV.include?("SOLR_TERM_SEARCH_URL") ? ENV["SOLR_TERM_SEARCH_URL"] : "http://localhost:8983/solr/term_search_core1"

Expand Down
9 changes: 3 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
x-app: &app
build:
context: .
args:
RUBY_VERSION: '2.7'
# Increase the version number in the image tag every time Dockerfile or its arguments is changed
image: ontologies_api:0.0.1
image: agroportal/ontologies_api:development
environment: &env
BUNDLE_PATH: /srv/ontoportal/bundle
# default bundle config resolves to /usr/local/bundle/config inside of the container
Expand Down Expand Up @@ -39,6 +34,8 @@ x-app: &app
services:
api:
<<: *app
env_file:
.env
environment:
<<: *env
GOO_BACKEND_NAME: 4store
Expand Down