Skip to content

Commit b1fdf60

Browse files
authored
Merge pull request #9 from Portabase/feat/firebird
add: firebird
2 parents d1d8db9 + 122057c commit b1fdf60

4 files changed

Lines changed: 132 additions & 11 deletions

File tree

commands/agent.py

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
validate_edge_key,
2222
)
2323
from templates.compose import (
24+
AGENT_FIREBIRD_SNIPPET,
2425
AGENT_MARIADB_SNIPPET,
2526
AGENT_MONGODB_AUTH_SNIPPET,
2627
AGENT_MONGODB_SNIPPET,
@@ -77,6 +78,22 @@ def agent(
7778

7879
raw_template = fetch_template("agent.yml")
7980

81+
if "{{EXTRA_SERVICES}}" not in raw_template:
82+
if "\nnetworks:" in raw_template:
83+
raw_template = raw_template.replace(
84+
"\nnetworks:", "\n\n{{EXTRA_SERVICES}}\n\nnetworks:"
85+
)
86+
else:
87+
raw_template += "\n\n{{EXTRA_SERVICES}}\n"
88+
89+
if "{{EXTRA_VOLUMES}}" not in raw_template:
90+
if "\nnetworks:" in raw_template:
91+
raw_template = raw_template.replace(
92+
"\nnetworks:", "\n\n{{EXTRA_VOLUMES}}\n\nnetworks:"
93+
)
94+
else:
95+
raw_template += "\n\n{{EXTRA_VOLUMES}}\n"
96+
8097
env_vars = {
8198
"EDGE_KEY": key,
8299
"PROJECT_NAME": project_name,
@@ -114,7 +131,7 @@ def agent(
114131
if category == "SQL":
115132
db_type = Prompt.ask(
116133
"Type",
117-
choices=["postgresql", "mysql", "mariadb", "sqlite"],
134+
choices=["postgresql", "mysql", "mariadb", "sqlite", "firebird"],
118135
default="postgresql",
119136
)
120137
else:
@@ -150,7 +167,11 @@ def agent(
150167
"Port",
151168
default=5432
152169
if db_type == "postgresql"
153-
else (3306 if db_type in ["mysql", "mariadb"] else 27017),
170+
else (
171+
3050
172+
if db_type == "firebird"
173+
else (3306 if db_type in ["mysql", "mariadb"] else 27017)
174+
),
154175
)
155176
user = Prompt.ask("Username")
156177
password = Prompt.ask("Password", password=True)
@@ -177,7 +198,7 @@ def agent(
177198
if category == "SQL":
178199
db_engine = Prompt.ask(
179200
"Engine",
180-
choices=["postgresql", "mysql", "mariadb", "sqlite"],
201+
choices=["postgresql", "mysql", "mariadb", "sqlite", "firebird"],
181202
default="postgresql",
182203
)
183204
db_variant = "standard"
@@ -372,6 +393,48 @@ def agent(
372393
f"[success]✔ Added MongoDB container (Port {mongo_port})[/success]"
373394
)
374395

396+
elif db_engine == "firebird":
397+
fb_port = get_free_port()
398+
db_user = "alice"
399+
db_pass = secrets.token_hex(8)
400+
db_name = "mirror.fdb"
401+
service_name = f"db-firebird-{secrets.token_hex(2)}"
402+
403+
var_prefix = service_name.upper().replace("-", "_")
404+
env_vars[f"{var_prefix}_PORT"] = str(fb_port)
405+
env_vars[f"{var_prefix}_DB"] = db_name
406+
env_vars[f"{var_prefix}_USER"] = db_user
407+
env_vars[f"{var_prefix}_PASS"] = db_pass
408+
409+
snippet = (
410+
AGENT_FIREBIRD_SNIPPET.replace("${SERVICE_NAME}", service_name)
411+
.replace("${PORT}", f"${{{var_prefix}_PORT}}")
412+
.replace("${VOL_NAME}", f"{service_name}-data")
413+
.replace("${DB_NAME}", f"${{{var_prefix}_DB}}")
414+
.replace("${USER}", f"${{{var_prefix}_USER}}")
415+
.replace("${PASSWORD}", f"${{{var_prefix}_PASS}}")
416+
)
417+
418+
extra_services += snippet
419+
volumes_list.append(f"{service_name}-data")
420+
421+
add_db_to_json(
422+
path,
423+
{
424+
"name": db_name,
425+
"database": db_name,
426+
"type": "firebird",
427+
"username": db_user,
428+
"password": db_pass,
429+
"port": fb_port,
430+
"host": "localhost",
431+
"generated_id": str(uuid.uuid4()),
432+
},
433+
)
434+
console.print(
435+
f"[success]✔ Added Firebird container (Port {fb_port})[/success]"
436+
)
437+
375438
if volumes_list:
376439
for vol in volumes_list:
377440
extra_volumes += f" {vol}:\n"

commands/db.py

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from core.config import add_db_to_json, load_db_config, save_db_config, write_env_file
1111
from core.utils import console, get_free_port, validate_work_dir
1212
from templates.compose import (
13+
AGENT_FIREBIRD_SNIPPET,
1314
AGENT_MARIADB_SNIPPET,
1415
AGENT_MONGODB_AUTH_SNIPPET,
1516
AGENT_MONGODB_SNIPPET,
@@ -75,7 +76,7 @@ def add_db(name: str = typer.Argument(..., help="Name of the agent")):
7576
if category == "SQL":
7677
db_type = Prompt.ask(
7778
"Type",
78-
choices=["postgresql", "mysql", "mariadb", "sqlite"],
79+
choices=["postgresql", "mysql", "mariadb", "sqlite", "firebird"],
7980
default="postgresql",
8081
)
8182
else:
@@ -98,7 +99,11 @@ def add_db(name: str = typer.Argument(..., help="Name of the agent")):
9899
"Port",
99100
default=5432
100101
if db_type == "postgresql"
101-
else (3306 if db_type in ["mysql", "mariadb"] else 27017),
102+
else (
103+
3050
104+
if db_type == "firebird"
105+
else (3306 if db_type in ["mysql", "mariadb"] else 27017)
106+
),
102107
)
103108
user = Prompt.ask("Username")
104109
password = Prompt.ask("Password", password=True)
@@ -119,7 +124,7 @@ def add_db(name: str = typer.Argument(..., help="Name of the agent")):
119124
if category == "SQL":
120125
db_engine = Prompt.ask(
121126
"Engine",
122-
choices=["postgresql", "mysql", "mariadb", "sqlite"],
127+
choices=["postgresql", "mysql", "mariadb", "sqlite", "firebird"],
123128
default="postgresql",
124129
)
125130
else:
@@ -246,23 +251,55 @@ def add_db(name: str = typer.Argument(..., help="Name of the agent")):
246251
.replace("${DB_NAME}", f"${{{var_prefix}_DB}}")
247252
)
248253

254+
elif db_engine == "firebird":
255+
db_port = get_free_port()
256+
db_user = "alice"
257+
db_pass = secrets.token_hex(8)
258+
db_name = "mirror.fdb"
259+
service_name = f"db-firebird-{secrets.token_hex(2)}"
260+
var_prefix = service_name.upper().replace("-", "_")
261+
env_vars[f"{var_prefix}_PORT"] = str(db_port)
262+
env_vars[f"{var_prefix}_DB"] = db_name
263+
env_vars[f"{var_prefix}_USER"] = db_user
264+
env_vars[f"{var_prefix}_PASS"] = db_pass
265+
snippet = (
266+
AGENT_FIREBIRD_SNIPPET.replace("${SERVICE_NAME}", service_name)
267+
.replace("${PORT}", f"${{{var_prefix}_PORT}}")
268+
.replace("${VOL_NAME}", f"{service_name}-data")
269+
.replace("${DB_NAME}", f"${{{var_prefix}_DB}}")
270+
.replace("${USER}", f"${{{var_prefix}_USER}}")
271+
.replace("${PASSWORD}", f"${{{var_prefix}_PASS}}")
272+
)
273+
249274
compose_path = path / "docker-compose.yml"
250275
if compose_path.exists():
251276
with open(compose_path, "r") as f:
252277
content = f.read()
253278

254-
insert_pos = content.find("networks:")
255-
if insert_pos == -1:
279+
if "\nnetworks:" in content:
280+
insert_pos = content.find("\nnetworks:") + 1
281+
elif content.startswith("networks:"):
282+
insert_pos = 0
283+
else:
256284
insert_pos = len(content)
257285

258286
new_content = content[:insert_pos] + snippet + "\n" + content[insert_pos:]
259287

260288
vol_snippet = f" {service_name}-data:\n"
261-
vol_pos = new_content.find("volumes:")
289+
290+
vol_pos = -1
291+
if "\nvolumes:" in new_content:
292+
vol_pos = new_content.find("\nvolumes:") + 1
293+
elif new_content.startswith("volumes:"):
294+
vol_pos = 0
295+
262296
if vol_pos != -1:
263-
end_of_volumes = new_content.find("networks:", vol_pos)
297+
end_of_volumes = new_content.find("\nnetworks:", vol_pos)
264298
if end_of_volumes == -1:
265299
end_of_volumes = len(new_content)
300+
else:
301+
end_of_volumes += 1
302+
266303
new_content = (
267304
new_content[:end_of_volumes]
268305
+ vol_snippet

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ dependencies = [
88
"rich>=14.2.0",
99
"typer>=0.20.0",
1010
"pyinstaller>=6.17.0",
11-
"requests>=2.32.5"
11+
"requests>=2.32.5",
12+
"pyyaml >=6.0.0",
1213
]

templates/compose.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,25 @@
5757
- ${VOL_NAME}:/data/db
5858
"""
5959

60+
AGENT_FIREBIRD_SNIPPET = """
61+
${SERVICE_NAME}:
62+
container_name: ${PROJECT_NAME}-${SERVICE_NAME}
63+
image: firebirdsql/firebird
64+
restart: always
65+
networks:
66+
- portabase
67+
- default
68+
ports:
69+
- "${PORT}:3050"
70+
volumes:
71+
- ${VOL_NAME}:/var/lib/firebird/data
72+
environment:
73+
- FIREBIRD_DATABASE=${DB_NAME}
74+
- FIREBIRD_USER=${USER}
75+
- FIREBIRD_PASSWORD=${PASSWORD}
76+
- FIREBIRD_ROOT_PASSWORD=${PASSWORD}
77+
- FIREBIRD_DATABASE_DEFAULT_CHARSET=UTF8
78+
"""
79+
6080

6181

0 commit comments

Comments
 (0)