Skip to content

Commit

Permalink
Merge pull request #200 from rtCamp/bench-cli-operations
Browse files Browse the repository at this point in the history
Move bench creations operation from docker entrypoint/script to cli
  • Loading branch information
Xieyt authored Jun 21, 2024
2 parents bab8dc9 + a5d18d3 commit 830d601
Show file tree
Hide file tree
Showing 23 changed files with 865 additions and 405 deletions.
11 changes: 9 additions & 2 deletions Docker/frappe/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ update_uid_gid "${USERID}" "${USERGROUP}" "frappe" "frappe"

mkdir -p /opt/user/conf.d

start_time=$(date +%s.%N)
chown -R "$USERID":"$USERGROUP" /opt
end_time=$(date +%s.%N)
execution_time=$(awk "BEGIN {print $end_time - $start_time}")
echo "Time taken for chown /opt : $execution_time seconds"

if [[ ! -d "/workspace/.oh-my-zsh" ]]; then
cp -pr /opt/user/.oh-my-zsh /workspace/
Expand All @@ -40,9 +44,12 @@ if [[ ! -f "/workspace/.profile" ]]; then
cp -p /opt/user/.profile /workspace/
fi

chown "$USERID":"$USERGROUP" /workspace /workspace/frappe-bench

ls -pA /workspace | xargs -I{} chown -R "$USERID":"$USERGROUP" /workspace/{} &
start_time=$(date +%s.%N)
chown -R "$USERID":"$USERGROUP" /workspace
end_time=$(date +%s.%N)
execution_time=$(awk "BEGIN {print $end_time - $start_time}")
echo "Time taken for chown /workspace : $execution_time seconds"

if [ "$#" -gt 0 ]; then
gosu "$USERID":"$USERGROUP" "/scripts/$@" &
Expand Down
2 changes: 1 addition & 1 deletion Docker/frappe/frappe-dev.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ stopsignal=QUIT
[program:frappe-bench-frappe-watch]
command=/opt/user/bench-dev-watch.sh
priority=4
autostart=true
autostart=false
autorestart=false
stdout_logfile=/workspace/frappe-bench/logs/watch.dev.log
redirect_stderr=true
Expand Down
26 changes: 26 additions & 0 deletions Docker/frappe/helper-function.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
#!/usr/bin/bash

# Function: chown directory and files
# Parameters:
# - user
# - group
chown_directory_and_files(){
local user; user="$1"
local group; group="$2"
local dir; dir="$3"

user_not_owned_files=$(find "$dir" ! -user "$user" -type f -exec realpath {} + | sort -u)
group_not_owned_files=$(find "$dir" ! -group "$group" -type f -exec realpath {} + | sort -u)

user_not_owned_dirs=$(find "$dir" ! -user "$user" -type d -exec realpath {} + | sort -u)
group_not_owned_dirs=$(find "$dir" ! -group "$group" -type d -exec realpath {} + | sort -u)

# Concatenate both lists, sort, and remove duplicates
not_owned_files=$(echo -e "$user_not_owned_files\n$group_not_owned_files" | sort -u)
not_owned_dirs=$(echo -e "$user_not_owned_dirs\n$group_not_owned_dirs" | sort -u)

cpu_cores=$(nproc)

echo "$not_owned_files" | xargs -P "$cpu_cores" -I{} bash -c "if [ -f {} ]; then chown ${user}:${group} {};fi"

echo "$not_owned_dirs" | xargs -P "$cpu_cores" -I{} bash -c "if [ -d {} ]; then chown -R ${user}:${group} {};fi"
}

# Function: update_common_site_config
# Description: Updates the common site config file with the provided key-value pair.
# Parameters:
Expand Down
175 changes: 7 additions & 168 deletions Docker/frappe/user-script.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#!/bin/bash

source /scripts/helper-function.sh
source /prebake_info

set -e

cleanup() {
Expand All @@ -24,170 +21,12 @@ emer() {
exit 1
}

# handle previous logs location symlink
if [[ -d 'logs' ]]; then
if [[ -f 'logs/bench-start.log' ]]; then
mv logs/bench-start.log logs/bench-start.log.bak
fi
ln -sfn ../frappe-bench/logs/web.dev.log logs/bench-start.log
fi

REDIS_SOCKETIO_PORT=80
WEB_PORT=80

if [[ ! "${MARIADB_HOST:-}" ]]; then
MARIADB_HOST='global-db'
fi

if [[ "${DEBUG:-}" ]]; then
set -x
fi

if [[ ! "${MARIADB_ROOT_PASS:-}" ]]; then
MARIADB_ROOT_PASS='root'
fi

BENCH_COMMAND='/opt/.pyenv/shims/bench'

[[ "${ENVIRONMENT:-}" ]] || emer "[ERROR] ENVIRONMENT env not found. Please provide ENVIRONMENT env."
[[ "${WEB_PORT:-}" ]] || emer "[ERROR] WEB_PORT env not found. Please provide WEB_PORT env."
[[ "${SITENAME:-}" ]] || emer "[ERROR] SITENAME env not found. Please provide SITENAME env."

configure_common_site_config() {
# start_time=$(date +%s.%N)

update_common_site_config db_host "$MARIADB_HOST"
update_common_site_config db_port 3307
update_common_site_config redis_cache "redis://${CONTAINER_NAME_PREFIX}-redis-cache:6379"
update_common_site_config redis_queue "redis://${CONTAINER_NAME_PREFIX}-redis-queue:6379"
update_common_site_config redis_socketio "redis://${CONTAINER_NAME_PREFIX}-redis-socketio:6379"
update_common_site_config webserver_port "$WEB_PORT"
update_common_site_config socketio_port "$REDIS_SOCKETIO_PORT"
update_common_site_config restart_supervisor_on_update 0
update_common_site_config developer_mode "$DEVELOPER_MODE"

# end_time=$(date +%s.%N)
# execution_time=$(awk "BEGIN {print $end_time - $start_time}")
# echo "Execution time for set-config : $execution_time seconds"
}

# check if the site is created
if [[ ! -d "/workspace/frappe-bench/sites/$SITENAME" ]]; then

[[ "${REDIS_SOCKETIO_PORT:-}" ]] || emer "[ERROR] REDIS_SOCKETIO_PORT env not found. Please provide REDIS_SOCKETIO_PORT env."
[[ "${DEVELOPER_MODE:-}" ]] || emer "[ERROR] DEVELOPER_MODE env not found. Please provide DEVELOPER_MODE env."
[[ "${MARIADB_ROOT_PASS:-}" ]] || emer "[ERROR] MARIADB_ROOT_PASS env not found. Please provide MARIADB_ROOT_PASS env."
[[ "${MARIADB_HOST:-}" ]] || emer "[ERROR] MARIADB_HOST env not found. Please provide MARIADB_HOST env."
[[ "${ADMIN_PASS:-}" ]] || emer "[ERROR] ADMIN_PASS env not found. Please provide ADMIN_PASS env."
[[ "${DB_NAME:-}" ]] || emer "[ERROR] DB_NAME env not found. Please provide DB_NAME env."
[[ "${CONTAINER_NAME_PREFIX:-}" ]] || emer "[ERROR] CONTAINER_NAME_PREFIX env not found. Please provide CONTAINER_NAME_PREFIX env."

# setting configuration
wait-for-it -t 120 "$MARIADB_HOST":3306
wait-for-it -t 120 "${CONTAINER_NAME_PREFIX}-redis-cache":6379
wait-for-it -t 120 "${CONTAINER_NAME_PREFIX}-redis-queue":6379
wait-for-it -t 120 "${CONTAINER_NAME_PREFIX}-redis-socketio":6379

cd frappe-bench

configure_common_site_config

# HANDLE Frappe
if [[ ! "${FRAPPE_BRANCH}" = "${PREBAKE_FRAPPE_BRANCH}" ]]; then
bench get-app --overwrite --branch "${FRAPPE_BRANCH}" frappe
fi

install_apps "$APPS_LIST" "$PREBAKE_APPS"

rm -rf archived

$BENCH_COMMAND setup supervisor --skip-redis --skip-supervisord --yes --user "$USER"
/scripts/divide-supervisor-conf.py config/supervisor.conf

echo "Environment: ${ENVIRONMENT}"
echo "Configuring frappe server"
bench_serve_help_output=$($BENCH_COMMAND serve --help)
host_changed=$(echo "$bench_serve_help_output" | grep -c 'host' || true)

# Addresses the introduction of the --host flag in bench serve command for compatibility with Frappe version updates.
if [[ "$host_changed" -ge 1 ]]; then
awk -v a="$WEB_PORT" '{sub(/--port [[:digit:]]+/,"--host 0.0.0.0 --port "a); print}' /opt/user/bench-dev-server >file.tmp && mv file.tmp /opt/user/bench-dev-server.sh
else
awk -v a="$WEB_PORT" '{sub(/--port [[:digit:]]+/,"--port "a); print}' /opt/user/bench-dev-server >file.tmp && mv file.tmp /opt/user/bench-dev-server.sh
fi

if [[ "$DEVELOPER_MODE" == "true" ]]; then
bench setup requirements --dev
fi

chmod +x /opt/user/bench-dev-server.sh

$BENCH_COMMAND build &
$BENCH_COMMAND new-site --db-root-password $(cat $MARIADB_ROOT_PASS) --db-name "$DB_NAME" --db-host "$MARIADB_HOST" --admin-password "$ADMIN_PASS" --db-port 3306 --verbose --no-mariadb-socket "$SITENAME"
$BENCH_COMMAND use "$SITENAME"
$BENCH_COMMAND --site "$SITENAME" scheduler enable

wait

if [[ "${ENVIRONMENT}" = "dev" ]]; then
cp /opt/user/frappe-dev.conf /opt/user/conf.d/frappe-dev.conf
else
ln -sfn /workspace/frappe-bench/config/frappe-bench-frappe-web.fm.supervisor.conf /opt/user/conf.d/frappe-bench-frappe-web.fm.supervisor.conf
fi

if [[ -n "$BENCH_START_OFF" ]]; then
tail -f /dev/null
else
supervisord -c /opt/user/supervisord.conf &
supervisord_pid=$!
wait $supervisord_pid
fi

# fi
if [[ -n "$BENCH_START_OFF" ]]; then
tail -f /dev/null
else
wait-for-it -t 120 "$MARIADB_HOST":3306
wait-for-it -t 120 "${CONTAINER_NAME_PREFIX}-redis-cache":6379
wait-for-it -t 120 "${CONTAINER_NAME_PREFIX}-redis-queue":6379
wait-for-it -t 120 "${CONTAINER_NAME_PREFIX}-redis-socketio":6379

cd frappe-bench

echo "Environment: ${ENVIRONMENT}"
echo "Configuring frappe dev server"
# Addresses the introduction of the --host flag in bench serve command for compatibility with Frappe version updates.
bench_serve_help_output=$($BENCH_COMMAND serve --help)

host_changed=$(echo "$bench_serve_help_output" | grep -c 'host' || true)

$BENCH_COMMAND setup supervisor --skip-redis --skip-supervisord --yes --user "$USER"
/scripts/divide-supervisor-conf.py config/supervisor.conf

# Addresses the introduction of the --host flag in bench serve command for compatibility with Frappe version updates.
if [[ "$host_changed" -ge 1 ]]; then
awk -v a="$WEB_PORT" '{sub(/--port [[:digit:]]+/,"--host 0.0.0.0 --port "a); print}' /opt/user/bench-dev-server >file.tmp && mv file.tmp /opt/user/bench-dev-server.sh
else
awk -v a="$WEB_PORT" '{sub(/--port [[:digit:]]+/,"--port "a); print}' /opt/user/bench-dev-server >file.tmp && mv file.tmp /opt/user/bench-dev-server.sh
fi

chmod +x /opt/user/bench-dev-server.sh

# if [[ "${ENVIRONMENT}" = "dev" ]]; then
# cp /opt/user/frappe-dev.conf /opt/user/conf.d/frappe-dev.conf
# else
# if [[ -f '/workspace/frappe-bench/config/frappe-bench-frappe-web.fm.supervisor.conf' ]]; then

# ln -sfn /workspace/frappe-bench/config/frappe-bench-frappe-web.fm.supervisor.conf /opt/user/conf.d/frappe-bench-frappe-web.fm.supervisor.conf
# else
# emer 'Not able to start the server. /workspace/frappe-bench/config/frappe-bench-frappe-web.fm.supervisor.conf not found.'
# fi
# fi

if [[ -n "$BENCH_START_OFF" ]]; then
tail -f /dev/null
else
echo "Starting supervisor.."
supervisord -c /opt/user/supervisord.conf &
supervisord_pid=$!
wait $supervisord_pid
fi

echo "Starting supervisor.."
supervisord -c /opt/user/supervisord.conf &
supervisord_pid=$!
wait $supervisord_pid
fi
2 changes: 1 addition & 1 deletion Docker/images-tag.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"frappe": "v0.13.0",
"frappe": "v0.15.0",
"nginx": "v0.13.0",
"mailhog": "v0.8.3"
}
1 change: 0 additions & 1 deletion frappe_manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
# save the function so that recurssion doesn't occur
rich_format_help_original = ut.rich_format_help


def print_fm_examples(*, obj, ctx, markup_mode):
# utilising the original saved function
rich_format_help_original(obj=obj, ctx=ctx, markup_mode=markup_mode)
Expand Down
21 changes: 8 additions & 13 deletions frappe_manager/commands.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pathlib import Path
from frappe_manager.site_manager import bench_operations
from frappe_manager.site_manager.site_exceptions import BenchNotRunning
from frappe_manager.utils.site import pull_docker_images
import typer
Expand All @@ -24,7 +25,6 @@
from frappe_manager.services_manager.services import ServicesManager
from frappe_manager.migration_manager.migration_executor import MigrationExecutor
from frappe_manager.site_manager.site import Bench
from frappe_manager.site_manager.workers_manager.SiteWorker import BenchWorkers
from frappe_manager.ssl_manager import LETSENCRYPT_PREFERRED_CHALLENGE, SUPPORTED_SSL_TYPES
from frappe_manager.ssl_manager.certificate import SSLCertificate
from frappe_manager.ssl_manager.letsencrypt_certificate import LetsencryptSSLCertificate
Expand Down Expand Up @@ -75,7 +75,7 @@ def app_callback(
if not help_called:
first_time_install = False

richprint.start(f"Working")
richprint.start("Working")

if not CLI_DIR.exists():
# creating the sites dir
Expand Down Expand Up @@ -105,16 +105,16 @@ def app_callback(

fm_config_manager: FMConfigManager = FMConfigManager.import_from_toml()


# docker pull
if first_time_install:
if not fm_config_manager.root_path.exists():
richprint.print("It seems like the first installation. Pulling docker images...️", "🔍")

completed_status = pull_docker_images()

if not completed_status:
shutil.rmtree(CLI_DIR)
richprint.exit("Aborting. [bold][blue]fm[/blue][/bold] will not be able to work without images. 🖼️")
richprint.exit("Aborting. Not able to pull all required Docker images.")

current_version = Version(get_current_fm_version())
fm_config_manager.version = current_version
Expand All @@ -141,6 +141,7 @@ def app_callback(
ctx.obj["verbose"] = verbose
ctx.obj['fm_config_manager'] = fm_config_manager


@app.command(no_args_is_help=True)
def create(
ctx: typer.Context,
Expand Down Expand Up @@ -246,19 +247,16 @@ def create(
admin_tools=True if environment == FMBenchEnvType.dev else False,
admin_pass=admin_pass,
# TODO get this info from services, maybe ?
mariadb_host=services_manager.database_manager.database_server_info.host,
mariadb_root_pass='/run/secrets/db_root_password',
environment_type=environment,
root_path=bench_config_path,
ssl=ssl_certificate,
)

compose_path = bench_path / 'docker-compose.yml'
bench_workers = BenchWorkers(benchname, bench_path)
compose_file_manager = ComposeFile(compose_path)
compose_project = ComposeProject(compose_file_manager, verbose)

bench: Bench = Bench(bench_path, benchname, bench_config, compose_project, bench_workers, services_manager)
bench: Bench = Bench(bench_path, benchname, bench_config, compose_project, services_manager)
benches.add_bench(bench)
benches.create_benches(is_template_bench=template)

Expand Down Expand Up @@ -286,7 +284,6 @@ def delete(
bench_compose_path = bench_path / 'docker-compose.yml'
compose_file_manager = ComposeFile(bench_compose_path)
bench_compose_project = ComposeProject(compose_file_manager)
bench_workers = BenchWorkers(benchname, bench_path)

bench_config_path = bench_path / CLI_BENCH_CONFIG_FILE_NAME
# try using bench object if not then create bench
Expand All @@ -306,24 +303,22 @@ def delete(
# TODO do something about this forcefully delete maybe
admin_tools=False,
admin_pass='pass',
mariadb_host='global-db',
mariadb_root_pass="/run/secrets/db_root_password",
environment_type=FMBenchEnvType.dev,
ssl=SSLCertificate(domain=benchname, ssl_type=SUPPORTED_SSL_TYPES.none),
root_path=bench_config_path,
)

bench = Bench(
bench_path,
benchname,
fake_config,
bench_compose_project,
bench_workers,
services=services_manager,
workers_check=False,
)

else:
bench = Bench.get_object(benchname, services_manager, workers_check=False, admin_tools_check=False)
bench = Bench.get_object(benchname, services=services_manager, workers_check=False, admin_tools_check=False)

benches.add_bench(bench)
benches.remove_benches()
Expand Down
4 changes: 2 additions & 2 deletions frappe_manager/display_manager/DisplayManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def exit(self, text: str, emoji_code: str = ":x:", os_exit=False, error_msg=None

raise typer.Exit(1)

def print(self, text: str, emoji_code: str = ":white_check_mark:", prefix: Optional[str] = None):
def print(self, text: str, emoji_code: str = ":white_check_mark:", prefix: Optional[str] = None, **kwargs):
"""
Prints the given text with an optional emoji code.
Expand All @@ -102,7 +102,7 @@ def print(self, text: str, emoji_code: str = ":white_check_mark:", prefix: Optio
if prefix:
msg = f"{emoji_code} {prefix} {text}"

self.stdout.print(msg)
self.stdout.print(msg, **kwargs)

def update_head(self, text: str):
"""
Expand Down
Loading

0 comments on commit 830d601

Please sign in to comment.