Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 12 additions & 0 deletions compose.yaml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ services:
{% if db_host_type == "container" -%}
depends_on:
- db
{%- elif db_host_type == "same_server" -%}
extra_hosts:
- "host.docker.internal:host-gateway"
{%- endif %}
env_file:
- $PWD/.envs/admin-legacy.env
Expand Down Expand Up @@ -95,6 +98,9 @@ services:
{% if db_host_type == "container" -%}
depends_on:
- db
{%- elif db_host_type == "same_server" -%}
extra_hosts:
- "host.docker.internal:host-gateway"
{%- endif %}
env_file:
$PWD/.envs/admin.env
Expand Down Expand Up @@ -129,6 +135,9 @@ services:
{% if db_host_type == "container" -%}
depends_on:
- db
{%- elif db_host_type == "same_server" -%}
extra_hosts:
- "host.docker.internal:host-gateway"
{%- endif %}
env_file:
- $PWD/.envs/listener.env
Expand Down Expand Up @@ -161,6 +170,9 @@ services:
{% if db_host_type == "container" -%}
depends_on:
- db
{%- elif db_host_type == "same_server" -%}
extra_hosts:
- "host.docker.internal:host-gateway"
{%- endif %}
environment:
- TZ=${TIMEZONE}
Expand Down
32 changes: 24 additions & 8 deletions copier.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extra_files:
type: str
help: Where are the extra files located (absolute path)?
when: "{{ run_setup }}"
# needs to be absolute because the tasks are run in the context of the target directory
validator: |
{% if run_setup %}
{% if extra_files | length < 1 %}Required{% endif %}
Expand Down Expand Up @@ -85,22 +86,28 @@ db_host:
type: str
help: What's the hostname of the DB server?
default: |-
{%- if db_host_type == "container" -%}
{% if db_host_type == "container" -%}
db
{%- elif db_host_type == "same_server" -%}
host.docker.internal
{%- else -%}
{#- Force the user to type a host -#}

{%- endif -%}
{{ UNSET }}
{%- endif %}
when: "{{ db_host_type == 'separate_server' }}"
validator: "{% if db_host | length < 1 %}Required{% endif %}"

db_port:
type: int
help: Port the DB server is listening at
when: "{{ db_host_type != 'container' }}"
default: 3306
default: |-
{% if db_host_type == 'container' -%}
3306
{%- else -%}
{{ UNSET }}
{%- endif %}
validator: "{% if db_port < 1 %}Invalid input{% endif %}"

db_user:
type: str
Expand All @@ -118,7 +125,12 @@ db_password:
db_root_user:
type: str
help: Username of a database root user (to create user and databases)
default: root
default: |-
{% if db_host_type == 'container' -%}
root
{%- else -%}
{{ UNSET }}
{%- endif %}
when: "{{ db_host_type != 'container'}}"
validator: "{% if db_root_user | length < 1 %}Required{% endif %}"

Expand All @@ -131,7 +143,7 @@ db_root_password:
{{ existing_secret('.env', 'DB_ROOT_PASSWORD') or random_password(32) }}
{%- else -%}
{#- Force the user to type a password -#}

{{ UNSET }}
{%- endif %}
validator: "{% if db_root_password | length < 1 %}Required{% endif %}"
when: "{{ db_host_type != 'container'}}"
Expand Down Expand Up @@ -283,9 +295,9 @@ _tasks:
exit 1
when: "{{ not is_port_available(https_port) }}"
- command: |
echo DB port {{ db_port }} on {{ db_host }} is not reachable
echo DB port {{ db_port }} on localhost is not reachable
exit 1
when: "{{ db_host_type != 'container' and is_port_available(db_port, db_host) }}"
when: "{{ db_host_type == 'same_server' and is_port_available(db_port | int, 'localhost') }}"
# validation succeeded
- "echo Copying extra files into project directory..."
- mkdir -p config/firebase
Expand Down Expand Up @@ -320,6 +332,10 @@ _tasks:
docker compose up -d db
echo "Waiting for DB container to be ready..."
when: "{{ db_host_type == 'container' }}"
# force the creation of the network so that the init_db script can find it
- command: |
docker compose create admin
when: "{{ db_host_type != 'container' }}"
# set up the databases
- command: |
echo "Running init_db script to initialize DB..."
Expand Down
8 changes: 5 additions & 3 deletions ctt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ _extra_tasks = [
]

# DB on same host
# [output.".ctt/db_same_host"]
# db_host_type = "separate_server"
# db_root_password = "root-password"
[output.".ctt/db_same_host"]
db_host_type = "same_server"
db_port = 3307
db_root_user = "root"
db_root_password = "root-password"
9 changes: 8 additions & 1 deletion extensions/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ def is_port_available(port: int, host: str = 'localhost') -> bool:
print(f'Checking availability of port {port} on host {host}...')

with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
return sock.connect_ex((host, port)) != 0
return_code = sock.connect_ex((host, port))

if return_code == 0:
print(f'Port {port} on host {host} is open (not available)')
else:
print(f'Port {port} on host {host} is closed (available)')

return return_code != 0


class NetworkExtension(Extension):
Expand Down
3 changes: 2 additions & 1 deletion scripts/init_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ALPINE_VERSION="3.23.2"
WAIT_FOR_IT_VERSION="latest"

echo "Waiting for DB container to be ready..."
docker run --rm -i --network opal-${ENVIRONMENT} chainguard/wait-for-it:${WAIT_FOR_IT_VERSION} --host="$DB_HOST" --port="$DB_PORT" --timeout=20
docker run --rm --interactive --network opal-${ENVIRONMENT} --add-host "host.docker.internal:host-gateway" chainguard/wait-for-it:${WAIT_FOR_IT_VERSION} --host="$DB_HOST" --port="$DB_PORT" --timeout=20

echo "Running container for mysql-client..."
docker run --rm --interactive \
Expand All @@ -17,6 +17,7 @@ docker run --rm --interactive \
--env DB_PASSWORD=${DB_PASSWORD} \
--env DB_NAME=${DB_NAME} \
--network opal-${ENVIRONMENT} \
--add-host "host.docker.internal:host-gateway" \
alpine:${ALPINE_VERSION} sh -s << EOF
set -euo pipefail
apk add --no-cache mysql-client
Expand Down
Loading