From a1666dd247291bd216cef9b142751bc02cd64c3a Mon Sep 17 00:00:00 2001 From: grossmj Date: Wed, 18 Sep 2024 18:02:35 +0700 Subject: [PATCH] Fix MAC address support for Docker containers --- gns3server/api/routes/compute/docker_nodes.py | 3 +++ gns3server/compute/docker/docker_vm.py | 4 +-- gns3server/db/models/templates.py | 1 + ...aa4389_add_mac_address_field_in_docker_.py | 27 +++++++++++++++++++ gns3server/schemas/compute/docker_nodes.py | 1 + .../controller/templates/docker_templates.py | 1 + 6 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 gns3server/db_migrations/versions/9a5292aa4389_add_mac_address_field_in_docker_.py diff --git a/gns3server/api/routes/compute/docker_nodes.py b/gns3server/api/routes/compute/docker_nodes.py index 11a2cf658..bf1bb072c 100644 --- a/gns3server/api/routes/compute/docker_nodes.py +++ b/gns3server/api/routes/compute/docker_nodes.py @@ -69,6 +69,7 @@ async def create_docker_node(project_id: UUID, node_data: schemas.DockerCreate) start_command=node_data.get("start_command"), environment=node_data.get("environment"), adapters=node_data.get("adapters"), + mac_address=node_data.get("mac_address"), console=node_data.get("console"), console_type=node_data.get("console_type"), console_resolution=node_data.get("console_resolution", "1024x768"), @@ -124,6 +125,8 @@ async def update_docker_node(node_data: schemas.DockerUpdate, node: DockerVM = D "start_command", "environment", "adapters", + "mac_address", + "custom_adapters", "extra_hosts", "extra_volumes", "memory", diff --git a/gns3server/compute/docker/docker_vm.py b/gns3server/compute/docker/docker_vm.py index c88600ea6..20a8a7165 100644 --- a/gns3server/compute/docker/docker_vm.py +++ b/gns3server/compute/docker/docker_vm.py @@ -79,6 +79,7 @@ def __init__( aux=None, start_command=None, adapters=None, + mac_address="", environment=None, console_type="telnet", aux_type="none", @@ -106,7 +107,6 @@ def __init__( self._environment = environment self._cid = None self._ethernet_adapters = [] - self._mac_address = "" self._temporary_directory = None self._telnet_servers = [] self._vnc_process = None @@ -132,7 +132,7 @@ def __init__( else: self.adapters = adapters - self.mac_address = "" # this will generate a MAC address + self.mac_address = mac_address log.debug( "{module}: {name} [{image}] initialized.".format( diff --git a/gns3server/db/models/templates.py b/gns3server/db/models/templates.py index a24c59df0..c43642295 100644 --- a/gns3server/db/models/templates.py +++ b/gns3server/db/models/templates.py @@ -66,6 +66,7 @@ class DockerTemplate(Template): template_id = Column(GUID, ForeignKey("templates.template_id", ondelete="CASCADE"), primary_key=True) image = Column(String) adapters = Column(Integer) + mac_address = Column(String) start_command = Column(String) environment = Column(String) console_type = Column(String) diff --git a/gns3server/db_migrations/versions/9a5292aa4389_add_mac_address_field_in_docker_.py b/gns3server/db_migrations/versions/9a5292aa4389_add_mac_address_field_in_docker_.py new file mode 100644 index 000000000..5fb1597a5 --- /dev/null +++ b/gns3server/db_migrations/versions/9a5292aa4389_add_mac_address_field_in_docker_.py @@ -0,0 +1,27 @@ +"""add mac_address field in Docker templates table + +Revision ID: 9a5292aa4389 +Revises: 7ceeddd9c9a8 +Create Date: 2024-09-18 17:52:53.429522 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '9a5292aa4389' +down_revision = '7ceeddd9c9a8' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + + op.add_column('docker_templates', sa.Column('mac_address', sa.String())) + + +def downgrade() -> None: + + op.drop_column('docker_templates', 'mac_address') + diff --git a/gns3server/schemas/compute/docker_nodes.py b/gns3server/schemas/compute/docker_nodes.py index 63bc60af5..2d6d7d47a 100644 --- a/gns3server/schemas/compute/docker_nodes.py +++ b/gns3server/schemas/compute/docker_nodes.py @@ -39,6 +39,7 @@ class DockerBase(BaseModel): usage: Optional[str] = Field(None, description="How to use the Docker container") start_command: Optional[str] = Field(None, description="Docker CMD entry") adapters: Optional[int] = Field(None, ge=0, le=99, description="Number of adapters") + mac_address: Optional[str] = Field(None, description="Base MAC address", pattern="^([0-9a-fA-F]{2}[:]){5}([0-9a-fA-F]{2})$") environment: Optional[str] = Field(None, description="Docker environment variables") extra_hosts: Optional[str] = Field(None, description="Docker extra hosts (added to /etc/hosts)") extra_volumes: Optional[List[str]] = Field(None, description="Additional directories to make persistent") diff --git a/gns3server/schemas/controller/templates/docker_templates.py b/gns3server/schemas/controller/templates/docker_templates.py index 1a7992368..cd8010c2a 100644 --- a/gns3server/schemas/controller/templates/docker_templates.py +++ b/gns3server/schemas/controller/templates/docker_templates.py @@ -29,6 +29,7 @@ class DockerTemplate(TemplateBase): symbol: Optional[str] = "docker_guest" image: str = Field(..., description="Docker image name") adapters: Optional[int] = Field(1, ge=0, le=100, description="Number of adapters") + mac_address: Optional[str] = Field("", description="Base MAC address", pattern="^([0-9a-fA-F]{2}[:]){5}([0-9a-fA-F]{2})$|^$") start_command: Optional[str] = Field("", description="Docker CMD entry") environment: Optional[str] = Field("", description="Docker environment variables") console_type: Optional[ConsoleType] = Field("telnet", description="Console type")