Repository navigation
feat(engines): IBM Db2 engine #48
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
Open
RambokDev
wants to merge
1
commit into
main
Choose a base branch
from
feat/db2-datasource
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| from typing import Any | ||
|
|
||
| from core.errors import ValidationError | ||
| from core.fields import Field | ||
| from core.specs import DatabaseSpec | ||
| from engines.base import DbEngine | ||
| from services.ports import PortAllocator | ||
|
|
||
| DB2_NAME = re.compile(r"[A-Za-z0-9_]+") | ||
|
|
||
|
|
||
| def db2_name(value: str) -> str: | ||
| name = value.strip() | ||
| if not DB2_NAME.fullmatch(name): | ||
| raise ValidationError( | ||
| f"'{value}' is not a Db2 name: use letters, digits and _ only.", | ||
| hint="e.g. TESTDB or db2inst1", | ||
| ) | ||
| return name | ||
|
|
||
|
|
||
| def optional_db2_name(value: str) -> str: | ||
| return db2_name(value) if value.strip() else "" | ||
|
|
||
|
|
||
| class Db2Engine(DbEngine): | ||
| key, display = "db2", "IBM Db2" | ||
| template = None | ||
| has_modes = False | ||
| label_default = "IBM Db2" | ||
| warning = ( | ||
| "The agent backs up Db2 with the db2 command line of the Db2 server: run " | ||
| "this agent on the Db2 server itself (native binary, as root or as the " | ||
| "instance owner). Requires agent 1.29.0 or later." | ||
| ) | ||
|
|
||
| def fields_existing(self) -> list[Field]: | ||
| return [ | ||
| Field( | ||
| "database", "Database alias (e.g. TESTDB)", "text", validator=db2_name | ||
| ), | ||
| Field( | ||
| "username", | ||
| "Username (empty: operating system authentication of the instance owner)", | ||
| "text", | ||
| default="", | ||
| validator=optional_db2_name, | ||
| ), | ||
| Field("password", "Password", "secret", default=""), | ||
| ] | ||
|
|
||
| def fields_new(self) -> list[Field]: | ||
| return self.fields_existing() | ||
|
|
||
| def option_fields(self) -> list[Field]: | ||
| return [ | ||
| Field( | ||
| "instance", | ||
| "Instance owner", | ||
| "text", | ||
| default="db2inst1", | ||
| validator=db2_name, | ||
| help="Operating system user that owns the Db2 instance.", | ||
| ) | ||
| ] | ||
|
|
||
| def generate( | ||
| self, *, auth: bool, ports: PortAllocator, answers: dict[str, Any] | ||
| ) -> DatabaseSpec: | ||
| return self.from_existing(answers) | ||
|
|
||
| def from_existing(self, answers: dict[str, Any]) -> DatabaseSpec: | ||
| return DatabaseSpec( | ||
| id=self.new_id(), | ||
| engine=self.key, | ||
| name=answers.get("label") or self.label_default, | ||
| managed=False, | ||
| database=answers["database"], | ||
| username=answers.get("username") or None, | ||
| password=answers.get("password") or None, | ||
| ) | ||
|
|
||
| def env_vars(self, spec: DatabaseSpec) -> dict[str, str]: | ||
| return {} | ||
|
|
||
| def agent_entry(self, spec: DatabaseSpec) -> dict[str, Any]: | ||
| entry: dict[str, Any] = { | ||
| "name": spec.name, | ||
| "type": self.key, | ||
| "database": spec.database or "", | ||
| "generated_id": spec.id, | ||
| } | ||
| if spec.username: | ||
| entry["username"] = spec.username | ||
| entry["password"] = spec.password or "" | ||
| options = self.non_default_options(spec) | ||
| if options: | ||
| entry["options"] = options | ||
| return entry | ||
|
|
||
| def describe(self, spec: DatabaseSpec) -> str: | ||
| return f"database: {spec.database}" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import pytest | ||
|
|
||
| from core.errors import ValidationError | ||
| from core.specs import DatabaseSpec | ||
| from engines import registry | ||
| from engines.db2 import Db2Engine, db2_name, optional_db2_name | ||
| from tests.support import agent_service, field_specs | ||
|
|
||
| DB2 = registry.get("db2") | ||
| ANSWERS = { | ||
| "label": "Orders", | ||
| "database": "TESTDB", | ||
| "username": "db2inst1", | ||
| "password": "secret", | ||
| } | ||
|
|
||
|
|
||
| def attributes(): | ||
| assert type(DB2) is Db2Engine | ||
| assert (DB2.key, DB2.display, DB2.default_port) == ("db2", "IBM Db2", None) | ||
| assert DB2.template is None | ||
| assert (DB2.auth_variants, DB2.has_modes) == (False, False) | ||
| assert "Db2 server itself" in (DB2.warning or "") | ||
|
|
||
|
|
||
| def generate(ports): | ||
| spec = DB2.generate(auth=True, ports=ports, answers=ANSWERS) | ||
| assert spec == DatabaseSpec( | ||
| id=spec.id, | ||
| engine="db2", | ||
| name="Orders", | ||
| managed=False, | ||
| database="TESTDB", | ||
| username="db2inst1", | ||
| password="secret", | ||
| ) | ||
| assert DB2.describe(spec) == "database: TESTDB" | ||
|
|
||
|
|
||
| def env_vars(ports): | ||
| spec = DB2.generate(auth=True, ports=ports, answers=ANSWERS) | ||
| assert DB2.env_vars(spec) == {} | ||
|
|
||
|
|
||
| def agent_entry(ports): | ||
| with_credentials = DB2.generate(auth=True, ports=ports, answers=ANSWERS) | ||
| assert DB2.agent_entry(with_credentials) == { | ||
| "name": "Orders", | ||
| "type": "db2", | ||
| "database": "TESTDB", | ||
| "generated_id": with_credentials.id, | ||
| "username": "db2inst1", | ||
| "password": "secret", | ||
| } | ||
| operating_system_authentication = DB2.from_existing( | ||
| {"database": "TESTDB", "username": "", "password": ""} | ||
| ).with_options({"instance": "db2prod"}) | ||
| assert DB2.agent_entry(operating_system_authentication) == { | ||
| "name": "IBM Db2", | ||
| "type": "db2", | ||
| "database": "TESTDB", | ||
| "generated_id": operating_system_authentication.id, | ||
| "options": {"instance": "db2prod"}, | ||
| } | ||
|
|
||
|
|
||
| def fields(): | ||
| assert field_specs(DB2.fields_existing()) == [ | ||
| ("database", "text", None), | ||
| ("username", "text", ""), | ||
| ("password", "secret", ""), | ||
| ] | ||
| assert DB2.fields_new() == DB2.fields_existing() | ||
| assert field_specs(DB2.option_fields()) == [("instance", "text", "db2inst1")] | ||
|
|
||
|
|
||
| def from_existing(): | ||
| spec = DB2.from_existing({"database": "TESTDB"}) | ||
| assert (spec.managed, spec.username, spec.password) == (False, None, None) | ||
|
|
||
|
|
||
| def compose_service(render_engine): | ||
| rendered = render_engine("db2", answers=ANSWERS) | ||
| assert rendered.doc["services"] == {"agent": agent_service()} | ||
| assert "volumes" not in rendered.doc | ||
| assert rendered.databases == [DB2.agent_entry(rendered.spec)] | ||
|
|
||
|
|
||
| def compose_service_inline(render_engine): | ||
| rendered = render_engine("db2", inline=True, answers=ANSWERS) | ||
| assert rendered.doc["services"] == {"agent": agent_service(inline=True)} | ||
|
|
||
|
|
||
| def names_reject_shell_characters(): | ||
| assert db2_name(" TESTDB ") == "TESTDB" | ||
| assert optional_db2_name(" ") == "" | ||
| for value in ["", "TEST DB", "x;rm -rf /", "$(id)", "db-2"]: | ||
| with pytest.raises(ValidationError): | ||
| db2_name(value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| "redis", | ||
| "valkey", | ||
| "mssql", | ||
| "db2", | ||
| "docker-volume", | ||
| "files", | ||
| ] | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: Portabase/cli
Length of output: 192
Script executed:
Repository: Portabase/cli
Length of output: 8314
Script executed:
Repository: Portabase/cli
Length of output: 5168
Db2 agents started through the create flow cannot reach the Db2 server.
After a user selects Db2, the create flow still offers "Start agent now?" and the
--startoption still runsdocker compose up -d. The generated stack runsportabase/agent:latestwith onlydatabases.jsonand any user-declared mounts. It does not provide the Db2 instance or thedb2command line, which the Db2 engine warning says backups require. The warning does not stop the flow, so the generated Docker agent can fail to back up Db2.Define the required behavior for a Db2 deployment. Either the create flow must not start a Docker agent for Db2 databases, or it must document and support the native-agent handoff from the generated configuration.