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

Convert to docker #1

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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,524 changes: 1,509 additions & 15 deletions README.md

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions app/default/.elasticsearch-http-requests/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

set -eu

SCRIPTPATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )";

"$SCRIPTPATH/request.sh" "$SCRIPTPATH/Index/Delete.http" || true
"$SCRIPTPATH/request.sh" "$SCRIPTPATH/Index/Create.http" && \
"$SCRIPTPATH/request.sh" "$SCRIPTPATH/Index/Bulk.http" && sleep 1 && \
"$SCRIPTPATH/request.sh" "$SCRIPTPATH/Index/Count.http"

printf "Done!\n"
84 changes: 84 additions & 0 deletions app/default/.elasticsearch-http-requests/request.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/bin/sh
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@212223 why this directory move from app to app/default here and in lots of places? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is good to have a web application served by nginx from a sub-directory app/default instead of a parent directory app because this allows to switch between applications instantaneously.

For example instead of having your application inside the app/default dir you may have it in app/bar dir and a symlink app/default -> app/bar that points to the dir of the application you want to serve. Demo commit.

Then if you want to change the application you serve you don't need to reconfigure nginx nor restarting any of the services is required but only changing the symlink target, for example:

cd app
unlink default
ln -sr foo default

like in the Demo commit

and you are ready to serve the new application.
Example use cases:

  • Live update of an application
  • Rollback to previous version of an app in case of troubles.
  • Presenting different applications or the same in different versions during a time constrained presentation.
  • Fallback to an application that helps debugging problems.
  • Creating a new version of an app just by copying it to a new dir and symlink to it.

Currently the web app is in the app/default dir. Would you like me to:

  1. Leave it as it is
  2. Move it to a different dir and create a symlink default that points to that dir
  3. Move that application to app dir from app/default and reconfigure docker-compose?


# Example usage:
# ./request.sh ./Index/Create.http
# ./request.sh /app/default/.elasticsearch-http-requests/Index/Delete.http

set -eu

http_request_path="${1:-}"

[ -n "$http_request_path" ] || \
{ printf "Usage: $1 <path to request file>\n" && exit 1; }

[ -n "${DOCKER_ELASTICSEARCH_HOST:-}" ] || \
{ printf "Set ENV varialbe DOCKER_ELASTICSEARCH_HOST first\n" && exit 1 ;}

RED='\033[31;5;7m'
GREEN='\033[1;0;42m'
NC='\033[0m' # No Color

SCRIPTPATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )";

# if request path is not absolute make it relative to the script
[ ! "$http_request_path" = "${http_request_path#/}" ] || \
http_request_path="$SCRIPTPATH/$http_request_path"

[ -f $http_request_path ] || \
{ printf "Path does not point to a file, path: $1\n" && exit 1; }

[ -r "$http_request_path" ] || \
{ printf "File is not readable, check permissions, path: $1\n" && exit 1; }

# read content of a file
content="$(cat "$http_request_path" || { 1>&2 printf "Could not read the file: $http_request_path\n" && exit 1; })"

# dissasemble request to its part
request="$(printf "$content" | head -n1)"
method="$(printf "$request" | grep -oE '^\w+')"
url=$(printf "$request" | sed 's/^\w* //')
header="$(printf "$content\n" | awk '/http/,/^$/' | tail -n+2 | sed '$d')"
body="$(printf "$content" | tail -n+"$(printf "$content\n" | awk '/http/,/^$/' | wc -l)" | tail -n+2)"

# convert localhost to elasticsearch service host
find='http://localhost:9200'
replace="$DOCKER_ELASTICSEARCH_HOST"
url="$(printf "$url\n" | awk -v r="$replace" -v f="$find" '{ gsub(f,r); print $0 }')"

# create header options for curl
curl_header=''
newline="$(printf "\nx")" && newline="${newline%x}"
IFS="$newline"
for head in $header; do
curl_header="${curl_header} -H '$head'"
done

option_data=''
[ -z "$body" ] || option_data=" --data-raw \\${newline}'$body${newline}'"

# escape body's single quote '
body="$(printf "$body" | sed "s/'/\\\'/g")"

from_newline=''
[ -z "$option_data" ] || from_newline="\\${newline}"

#Bugfix for: elasticsearch 7 deprec. use of _doc: '{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"The mapping definition cannot be nested under a type [_doc] unless include_type_name is set to true."}],"type":"illegal_argument_exception","reason":"The mapping definition cannot be nested under a type [_doc] unless include_type_name is set to true."},"status":400}'
[ ! "$method" = 'PUT' ] || url="${url}?include_type_name=true"

command_request="curl -sSL -X ${method:-GET}${curl_header}${option_data} ${from_newline}'$url'"
printf "Performing cURL request:\n${command_request}\n"

# example success responses
# expected_pattern='\{"count":0,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0}}'
# expected_pattern='^\{"acknowledged":true' # Delete,

# check for error response
expected_pattern='^\s*\{\s*"error"' # Delete,
response="$(printf "$command_request\n" | sh || ec=$? )"

# validate response
printf "$response" | grep -vE "$expected_pattern" > /dev/null || \
{ printf "${RED}FAIL${NC}\nThere was an error with cURL exit code: ${ec:-0}, and response: $response\n" && exit ${ec:-1}; }

printf "$response\n"
printf "${GREEN}Success.${NC}\n"
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions app/default/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Łukasz Rynek

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions config/services.yaml → app/default/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
app.elasticsearch.host: '%env(DOCKER_ELASTICSEARCH_HOST)%'

services:
# default configuration for services in *this* file
Expand Down Expand Up @@ -35,3 +36,7 @@ services:
resource: '../src/Twig/Extension/*'
tags:
- { name: twig.extension }

App\Elasticsearch\Service\ApiClient:
arguments:
$elasticsearchHost: '%app.elasticsearch.host%'
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

final class ApiClient implements ApiClientInterface
{
private const ELASTICSEARCH_HOST = 'http://localhost:9200';
private $elasticsearchHost;

private const ENDPOINT_SEARCH = '_search';

public function __construct(private ClientInterface $client)
public function __construct(private ClientInterface $client, string $elasticsearchHost)
{
$this->elasticsearchHost = $elasticsearchHost;
}

public function search(Index $index, Query $query): Response
Expand All @@ -29,7 +30,7 @@ public function search(Index $index, Query $query): Response

private function createEndpointUrl(string $index, string $endpoint): string
{
return implode(DIRECTORY_SEPARATOR, [self::ELASTICSEARCH_HOST, $index, $endpoint]);
return implode(DIRECTORY_SEPARATOR, [$this->elasticsearchHost, $index, $endpoint]);
}

private function convertResponseToArray(ResponseInterface $response): array
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions docker/service/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# name of the project, if blank then docker sets it to a name of a dir that docker-compose.yaml is inside
COMPOSE_PROJECT_NAME=carrental

# profile to run with docker-compose up (may be multiple separated by comma) makes docker-compose --profile flag not having any effect
# COMPOSE_PROFILES=

# list of files to combine the total docker-compose.yaml (a kind of include files)
COMPOSE_FILE=_network.yaml:_restart-policy.yaml:_limit-memory.yaml:_service-dependency.yaml:_profiles.yaml:docker-compose.yaml:php-composer-v2.yaml:elasticsearch-v7.yaml:php-fpm-v8.yaml:nginx-v1.yaml

# application path relative to the docker-compose.yaml file.
app_path=./../../app

# Path where different service images, their files, Dockerfiles are stored (relative to the docker-compose.yaml)
service_path=.

# path inside service_path where is Dockerfile and .env files stored
image_path=image

# path inside service_path where all image files are stored ${service_path}/${image_files_path}/usr/share => /usr/share (path inside a container)
image_files_path=image/files
1 change: 1 addition & 0 deletions docker/service/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker-compose.override.yaml
Loading