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 6 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,523 changes: 1,509 additions & 14 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"
lrynek marked this conversation as resolved.
Show resolved Hide resolved
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.
3 changes: 1 addition & 2 deletions composer.json → app/default/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"symfony/runtime": "5.4.*",
"symfony/twig-bundle": "5.4.*",
"symfony/yaml": "5.4.*",
"twig/extra-bundle": "^3.0",
"twig/twig": "^3.0"
"twig/extra-bundle": "^3.0"
},
"require-dev": {
},
Expand Down
Loading