Skip to content

Commit

Permalink
feat: update project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Michele Mondelli committed Aug 1, 2022
1 parent 05d7b3f commit b9a0fd1
Show file tree
Hide file tree
Showing 17 changed files with 346 additions and 59 deletions.
14 changes: 5 additions & 9 deletions .bmeme/bin/app
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#!/usr/bin/env bash

set -e
QUOTEDCMD=()
for token in "$@"; do
QUOTEDCMD+=($(printf "%q" "$token"))

for var in "$@"
do
PARAMS="$PARAMS ${var}"
done
CMD="${QUOTEDCMD[*]}"
if [[ -z "$CMD" ]]
then
CMD="sh"
fi

if [[ -t 1 ]] ; then OPTS=""; else OPTS="-T"; fi

docker-compose exec -u $(id -u):$(id -g) ${OPTS} app /bin/sh -c "${CMD}"
docker-compose exec -u $(id -u):$(id -g) ${OPTS} app /bin/bash -c "${PARAMS}"
27 changes: 27 additions & 0 deletions .bmeme/bin/c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

PWD=$(pwd)
while [[ "$PWD" != "" && ! -e "$PWD/.bmeme" ]]; do
PWD=${PWD%/*}
done

FOUND=$PWD

if [[ "${FOUND}" = "" ]]; then
echo "Unable to found parent bmeme project!"
exit -1
fi

ORIG_DIR=$(pwd)
REL_DIR=${ORIG_DIR#"$FOUND"}

set -e
QUOTEDCMD=()
for token in "$@"; do
QUOTEDCMD+=($(printf "%q" "$token"))
done
CMD="${QUOTEDCMD[*]}"

pushd ${FOUND} > /dev/null
.bmeme/bin/app cd ".${REL_DIR}" \; ${CMD}
popd > /dev/null
33 changes: 33 additions & 0 deletions .bmeme/build/commands/create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

set -e

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
source "${SCRIPT_DIR}/../common/lib.sh"

if [[ "$1" == "descr" ]]; then
echo -n "Create and initialize application"
exit 0
fi

if [[ "$1" == "run" ]]; then

echo ""
echo -e "Setting up ${CG}docker environment${CN} ..."
echo ""
echo "${LOGPREFIX} CMD docker-compose build --build-arg FIX_UID=$(id -u) --build-arg FIX_GID=$(id -g)" >> ${LOGFILE}
docker-compose build --build-arg FIX_UID=$(id -u) --build-arg FIX_GID=$(id -g) 2>&1 >> ${LOGFILE}
echo "${LOGPREFIX} CMD docker-compose up -d" >> ${LOGFILE}
docker-compose up -d 2>&1 >> ${LOGFILE}

echo ""
if [[ ":$PATH:" == *":$LOCAL_PATH:"* ]]; then
echo -e "Your ${CG}\$PATH${CN} is correctly set"
else
echo -e "Your ${CG}\$PATH${CN} is not configured."
echo ""
echo -e "Run: ${CY}eval \$(${0} env)${CN}"
echo ""
fi

fi
19 changes: 19 additions & 0 deletions .bmeme/build/commands/destroy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

set -e

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
source "${SCRIPT_DIR}/../common/lib.sh"

if [[ "$1" == "descr" ]]; then
echo -n "Clean docker environment"
exit 0
fi

if [[ "$1" == "run" ]]; then
echo ""
echo -e "Cleaning ${CG}docker environment${CN} ..."
echo ""
echo "${LOGPREFIX} CMD docker-compose down -v" >> ${LOGFILE}
docker-compose down -v 2>&1 >> ${LOGFILE}
fi
21 changes: 21 additions & 0 deletions .bmeme/build/commands/requirements.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

set -e

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
source "${SCRIPT_DIR}/../common/lib.sh"

if [[ "$1" == "descr" ]]; then
echo -n "Check system requirements"
exit 0
fi

if [[ "$1" == "run" ]]; then
echo ""
echo -e "Checking ${CG}system requirements${CN} ..."
echo ""
echo "${LOGPREFIX} CMD docker --version" >> ${LOGFILE}
docker --version 2>&1 >> ${LOGFILE}
echo "${LOGPREFIX} CMD docker-compose --version" >> ${LOGFILE}
docker-compose --version 2>&1 >> ${LOGFILE}
fi
19 changes: 19 additions & 0 deletions .bmeme/build/commands/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

set -e

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
source "${SCRIPT_DIR}/../common/lib.sh"

if [[ "$1" == "descr" ]]; then
echo -n "Start docker environment"
exit 0
fi

if [[ "$1" == "run" ]]; then
echo ""
echo -e "Starting ${CG}docker environment${CN} ..."
echo ""
echo "${LOGPREFIX} CMD docker-compose start" >> ${LOGFILE}
docker-compose start 2>&1 >> ${LOGFILE}
fi
19 changes: 19 additions & 0 deletions .bmeme/build/commands/status.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

set -e

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
source "${SCRIPT_DIR}/../common/lib.sh"

if [[ "$1" == "descr" ]]; then
echo -n "Show docker environment status"
exit 0
fi

if [[ "$1" == "run" ]]; then
echo ""
echo -e "Checking ${CG}docker environment${CN} status ..."
echo ""
echo "${LOGPREFIX} CMD docker-compose ps" >> ${LOGFILE}
docker-compose ps
fi
19 changes: 19 additions & 0 deletions .bmeme/build/commands/stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

set -e

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
source "${SCRIPT_DIR}/../common/lib.sh"

if [[ "$1" == "descr" ]]; then
echo -n "Stop docker environment"
exit 0
fi

if [[ "$1" == "run" ]]; then
echo ""
echo -e "Stopping your ${CG}docker environment${CN} ..."
echo ""
echo "${LOGPREFIX} CMD docker-compose stop" >> ${LOGFILE}
docker-compose stop 2>&1 >> ${LOGFILE}
fi
50 changes: 50 additions & 0 deletions .bmeme/build/common/lib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash

set -e

CN="\\033[0m"
CR="\\033[91m"
CG="\\033[92m"
CY="\\033[93m"
CB="\\033[94m"
CT="\\033[37m"
TB="\\033[1m"

LIB_SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
BASE_PRJ_DIR=$(realpath "${LIB_SCRIPT_DIR}/../../..")
if test -f "${BASE_PRJ_DIR}/.env"; then
source "${BASE_PRJ_DIR}/.env"
fi
APP_DIR="${BASE_PRJ_DIR}/app"

LOCAL_PATH="${BASE_PRJ_DIR}/.bmeme/bin"
C_BIN="${BASE_PRJ_DIR}/.bmeme/bin/c"
LOGDIR="${BASE_PRJ_DIR}/.bmeme/log"
DATE=$(date +%Y-%m-%d_%H-%M-%S)
LOGFILE="${LOGDIR}/configure.log"
LOGPREFIX=">>>> "

log_begin() {
echo "" >>${LOGFILE}
echo "${LOGPREFIX} SCRIPT EXECUTED" >>${LOGFILE}
echo "${LOGPREFIX} Bin: ${0}" >>${LOGFILE}
echo "${LOGPREFIX} Params: ${@}" >>${LOGFILE}
echo "${LOGPREFIX} Time: $(date +'%Y-%m-%d %H:%M:%S')" >>${LOGFILE}
echo "" >>${LOGFILE}
}

log_end() {
echo "" >>${LOGFILE}
echo "${LOGPREFIX} SCRIPT TERMINATED" >>${LOGFILE}
echo "${LOGPREFIX} Time: $(date +'%Y-%m-%d %H:%M:%S')" >>${LOGFILE}
echo "" >>${LOGFILE}
}

show_cmd_error() {
echo -e "${TB}${CR}ERROR${CN}"
echo ""
echo -e "See ${CB}logfile${CN} for details: "
echo -e "\t${CY}tail -f ${LOGFILE}${CN}"
echo ""
exit 1
}
9 changes: 9 additions & 0 deletions .bmeme/config/docker/dockerfiles/Dockerfile.app
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM maven:3.8-openjdk-11-slim

ARG FIX_UID
ARG FIX_GID

RUN groupadd -g $FIX_GID -o maven && \
useradd -m -u $FIX_UID -g $FIX_GID -d /usr/src/app -o -s /bin/bash maven

USER maven
9 changes: 7 additions & 2 deletions .bmeme/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
version: "3.4"
services:
app:
image: bmeme/java-dev:3.8.3-jdk-11-openj9
build:
context: config/docker
dockerfile: dockerfiles/Dockerfile.app
entrypoint: /bin/sleep infinity
working_dir: /usr/src/app/
volumes:
- "../:/usr/src/app:cached"
environment:
- TERM=xterm-256color
- MAVEN_CONFIG=/usr/src/app/.cache/.m2
- DNSDOCK_ALIAS=${PROJECT_NAME}.${PROJECT_VENDOR}.docker
- DNSDOCK_ALIAS=app.${PROJECT_NAME}.${PROJECT_VENDOR}.docker
- VIRTUAL_PORT=8090
- VIRTUAL_HOST=app.${PROJECT_NAME}.${PROJECT_VENDOR}.docker
6 changes: 3 additions & 3 deletions .env → .env.dist
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Custom variables.
PROJECT_NAME=__PROJECT__
PROJECT_VENDOR=__VENDOR__
PROJECT_NAME=@@@PROJECT@@@
PROJECT_VENDOR=@@@VENDOR@@@

# Compose CLI environment variables.
# @see https://docs.docker.com/compose/reference/envvars
COMPOSE_PROJECT_NAME=${PROJECT_NAME}_${PROJECT_VENDOR}
COMPOSE_PROJECT_NAME=@@@PROJECT@@@_@@@VENDOR@@@
COMPOSE_FILE=.bmeme/docker-compose.yml
56 changes: 20 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,49 @@ Example project to develop application with [Java](https://www.java.com/) using

The idea is that you can use docker to develop your java application, without having to install anything on your system.

If you use the wrappers `./mvn`, `./java`, `./jshell` you can run anything in the container like you use to do if you have maven installed on your system.
If you prefix your commands with the `c` wrapper you can run anything in the container like you use to do if you have maven and java installed on your system.

## TL;DR

Edit the configuration file `.env` to fit your needs.

A configured file will look something like:

```env
PROJECT_NAME=awesomeidea
PROJECT_VENDOR=mystartup
COMPOSE_PROJECT_NAME=awesomeidea_mystartup
COMPOSE_FILE=.bmeme/docker-compose.yml
Run the `configure` script.
```bash
./configure
```

Create and run the docker container
On the first run, it will ask some questions and set up for you the `.env` file.
Once this quick configuration is done, the help is shown.

To create the docker environment needed to work with your project, run:
```bash
$ docker-compose up -d
./configure create
```

You can check that everything is working

```
$ docker-compose ps
Name Command State Ports
-----------------------------------------------------------------
awesomeidea_mystartup_app_1 /bin/sleep infinity Up
When the docker environment is up and running, you need to set your PATH to let it find the `c` wrapper.
```bash
eval $(./configure env)
```

Run the `npm` or `npx` using the wrappers
All done!

Check that everything is working well by running some test commands:

```bash
$ ./mvn --version
Apache Maven 3.8.3
c mvn --version
```

If you need to run other commands inside the cointainer, you can run it with `.bmeme/bin/app`

```
$ .bmeme/bin/app java --version
openjdk 11.0.13 2021-10-19
```bash
c java -version
```

## Access the container

You can use the `.bmeme/bin/app` wrapper to exec commands in the container.
If you run the script without anything, you will access a shell inside the container itself.
You can use the `c` wrapper to exec commands in the container.

```
$ .bmeme/bin/app jshell
| Welcome to JShell -- Version 11.0.13
| For an introduction type: /help intro
jshell>
c bash
```

## References

- [BMEME Java Development Docker images](https://hub.docker.com/r/bmeme/java-dev)
- [BMEME](https://www.bmeme.com)

Loading

0 comments on commit b9a0fd1

Please sign in to comment.