Skip to content

Commit

Permalink
Merge pull request #83 from NatLibFi/Automate-backend-setup-build
Browse files Browse the repository at this point in the history
Automate backend setup build
  • Loading branch information
natlibfi-kaisa authored Jul 17, 2024
2 parents 8eb6ab5 + 391e72d commit a519597
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 3 deletions.
99 changes: 99 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Makefile for setting up a virtual enviroment and running the application.

.PHONY: install dependencies install_libxmlsec1

# SECTION 1: Install dependencies

DEPENDENCIES := pkg-config libffi libjpeg poetry pyenv pyenv-virtualenv

install: dependencies install_libxmlsec1

dependencies: $(DEPENDENCIES)

$(DEPENDENCIES):
@if ! brew list $@ >/dev/null 2>&1; then \
echo "Installing $@..."; \
brew install $@; \
else \
echo "$@ is already installed."; \
fi

install_libxmlsec1:
@if ! brew list [email protected] >/dev/null 2>&1; then \
echo "Installing [email protected]..."; \
brew tap tvuotila/libxmlsec1; \
brew install tvuotila/libxmlsec1/[email protected]; \
else \
echo "[email protected] is already installed."; \
fi

# SECTION 2: Set up the virtual environment with Python packages

.PHONY: venv

# The Python version and virtual environment name
PYTHON_VERSION=3.11.1
VENV_NAME=circ-311

venv:
@echo "Installing Python $(PYTHON_VERSION)..."
pyenv install -s $(PYTHON_VERSION)
@echo "Creating virtual environment $(VENV_NAME)..."
pyenv virtualenv $(PYTHON_VERSION) $(VENV_NAME)

# SECTION 3: Run the python application and the containers in development mode.

.PHONY: run setup_env_variables run_python_app start_docker stop_docker rebuild_docker clean

# Define environment variables, change them to whatever you need
export ADMIN_EKIRJASTO_AUTHENTICATION_URL=https://localhost # This should be changed to the actual one!
export PALACE_SEARCH_URL=http://localhost:9200
export SIMPLIFIED_PRODUCTION_DATABASE=postgresql://palace:test@localhost:5432/circ

PYTHON_APP = app.py

# Docker compose file, change filename to whatever you need
DOCKER_COMPOSE_FILE = docker-compose-dev.yml

run: setup_env_variables rebuild_docker run_python_app

# Target to set up environment (exporting environment variables)
setup_env_variables:
@echo "Setting up environment..."
@export ADMIN_EKIRJASTO_AUTHENTICATION_URL=${ADMIN_EKIRJASTO_AUTHENTICATION_URL}
@export PALACE_SEARCH_URL=${PALACE_SEARCH_URL}
@export SIMPLIFIED_PRODUCTION_DATABASE=${SIMPLIFIED_PRODUCTION_DATABASE}
@echo "Environment variables set: ADMIN_EKIRJASTO_AUTHENTICATION_URL=${ADMIN_EKIRJASTO_AUTHENTICATION_URL}, \
PALACE_SEARCH_URL=${PALACE_SEARCH_URL}, SIMPLIFIED_PRODUCTION_DATABASE=${SIMPLIFIED_PRODUCTION_DATABASE}"

# Target to run the Python application instead in a Docker container
run_python_app:
@echo "Starting Python application..."
@poetry run python ${PYTHON_APP}

start_docker:
@echo "Starting Docker containers..."
@docker-compose -f ${DOCKER_COMPOSE_FILE} up -d

stop_docker:
@echo "Stopping Docker containers..."
@docker-compose -f ${DOCKER_COMPOSE_FILE} down

rebuild_docker:
@echo "Rebuilding Docker images ..."
@docker-compose -f ${DOCKER_COMPOSE_FILE} build --no-cache
@docker-compose -f ${DOCKER_COMPOSE_FILE} up -d

# Define the path where the PostgreSQL data is stored: Check your docker-compose-dev.yml file to see the location.
POSTGRES_DATA = ../circ-postgres-postrelease

# Clean target to remove any generated files or containers (if needed)
clean: stop_docker
@echo "Cleaning up..."
@if [ -d "${POSTGRES_DATA}" ]; then \
echo "Deleting directory: ${POSTGRES_DATA}"; \
rm -rf ${POSTGRES_DATA}; \
echo "Deleted directory: ${POSTGRES_DATA}"; \
else \
echo "Directory ${POSTGRES_DATA} does not exist."; \
fi
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,68 @@ features.

## Set Up

### Using Makefile to set up everything for development

Before running the commands in the Makefile, you should have `docker-compose-dev.yml` file in your directory. Also, you
should change the url of `EKIRJASTO_AUTHENTICATION_URL` in the Makefile's environment variables section to the real one.

To install dependencies and packages, run:

```shell
make install
```

For pyenv to work, add the following to your `.zshrc`:

```sh
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
```
and source it with:

```shell
source ~/.zshrc
```

Create a virtual environment with Python 3.11.1, run:

```shell
make venv
```

Activate the virtual environment and install dependencies with Poetry:

```shell
pyenv activate circ-311
poetry install
```

If all went well, you should see the activated virtual environment `(circ-311)` in your shell and all packages
installed. You can verify this by checking the Python version:

```shell
python3 --version
```

Now, all you need to do to start docker containers and run the application is:

```shell
make run
```

When you need to stop the application and containers and delete everything to start fresh, first check the path of the
postgres docker container volume in `docker-compose-dev.yml`. Update the `POSTGRES_DATA` in the Makefile to match it
and then run:

```shell
make clean
```

You can modify the Makefile to also create a virtual enviroment for Python 3.10.1. You can switch between the virtual
environments by activating and deactivating them with e.g. `pyenv activate circ-311` and `pyenv deactivate circ-311`.

### Docker Compose

In order to help quickly set up a development environment, we include a [docker-compose.yml](./docker-compose.yml)
Expand Down
4 changes: 2 additions & 2 deletions api/odl2.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def _checkout(
patron.loans,
)
)
if len(loans) > self.loan_limit: #Changed operator >= to >
if len(loans) > self.loan_limit: # Changed operator >= to >
raise PatronLoanLimitReached(limit=self.loan_limit)
return super()._checkout(patron, licensepool, hold)

Expand All @@ -125,7 +125,7 @@ def _place_hold(self, patron: Patron, licensepool: LicensePool) -> HoldInfo:
patron.holds,
)
)
if len(holds) > self.hold_limit: #Changed operator >= to >
if len(holds) > self.hold_limit: # Changed operator >= to >
raise PatronHoldLimitReached(limit=self.hold_limit)
return super()._place_hold(patron, licensepool)

Expand Down
2 changes: 1 addition & 1 deletion core/resources/bisac.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4584,4 +4584,4 @@
"YAN056010","Young Adult NonFiction / Transportation / Aviation"
"YAN056020","Young Adult NonFiction / Transportation / Boats, Ships & Underwater Craft"
"YAN056030","Young Adult NonFiction / Transportation / Cars & Trucks"
"YAN057000","Young Adult NonFiction / Travel"
"YAN057000","Young Adult NonFiction / Travel"

0 comments on commit a519597

Please sign in to comment.