Fix MAC address support for Docker containers

This commit is contained in:
grossmj 2024-09-18 18:02:35 +07:00
parent 3214f52e9e
commit a1666dd247
No known key found for this signature in database
GPG Key ID: 0A2D76AC45EA25CD
6 changed files with 35 additions and 2 deletions

View File

@ -69,6 +69,7 @@ async def create_docker_node(project_id: UUID, node_data: schemas.DockerCreate)
start_command=node_data.get("start_command"), start_command=node_data.get("start_command"),
environment=node_data.get("environment"), environment=node_data.get("environment"),
adapters=node_data.get("adapters"), adapters=node_data.get("adapters"),
mac_address=node_data.get("mac_address"),
console=node_data.get("console"), console=node_data.get("console"),
console_type=node_data.get("console_type"), console_type=node_data.get("console_type"),
console_resolution=node_data.get("console_resolution", "1024x768"), 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", "start_command",
"environment", "environment",
"adapters", "adapters",
"mac_address",
"custom_adapters",
"extra_hosts", "extra_hosts",
"extra_volumes", "extra_volumes",
"memory", "memory",

View File

@ -79,6 +79,7 @@ class DockerVM(BaseNode):
aux=None, aux=None,
start_command=None, start_command=None,
adapters=None, adapters=None,
mac_address="",
environment=None, environment=None,
console_type="telnet", console_type="telnet",
aux_type="none", aux_type="none",
@ -106,7 +107,6 @@ class DockerVM(BaseNode):
self._environment = environment self._environment = environment
self._cid = None self._cid = None
self._ethernet_adapters = [] self._ethernet_adapters = []
self._mac_address = ""
self._temporary_directory = None self._temporary_directory = None
self._telnet_servers = [] self._telnet_servers = []
self._vnc_process = None self._vnc_process = None
@ -132,7 +132,7 @@ class DockerVM(BaseNode):
else: else:
self.adapters = adapters self.adapters = adapters
self.mac_address = "" # this will generate a MAC address self.mac_address = mac_address
log.debug( log.debug(
"{module}: {name} [{image}] initialized.".format( "{module}: {name} [{image}] initialized.".format(

View File

@ -66,6 +66,7 @@ class DockerTemplate(Template):
template_id = Column(GUID, ForeignKey("templates.template_id", ondelete="CASCADE"), primary_key=True) template_id = Column(GUID, ForeignKey("templates.template_id", ondelete="CASCADE"), primary_key=True)
image = Column(String) image = Column(String)
adapters = Column(Integer) adapters = Column(Integer)
mac_address = Column(String)
start_command = Column(String) start_command = Column(String)
environment = Column(String) environment = Column(String)
console_type = Column(String) console_type = Column(String)

View File

@ -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')

View File

@ -39,6 +39,7 @@ class DockerBase(BaseModel):
usage: Optional[str] = Field(None, description="How to use the Docker container") usage: Optional[str] = Field(None, description="How to use the Docker container")
start_command: Optional[str] = Field(None, description="Docker CMD entry") start_command: Optional[str] = Field(None, description="Docker CMD entry")
adapters: Optional[int] = Field(None, ge=0, le=99, description="Number of adapters") 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") environment: Optional[str] = Field(None, description="Docker environment variables")
extra_hosts: Optional[str] = Field(None, description="Docker extra hosts (added to /etc/hosts)") 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") extra_volumes: Optional[List[str]] = Field(None, description="Additional directories to make persistent")

View File

@ -29,6 +29,7 @@ class DockerTemplate(TemplateBase):
symbol: Optional[str] = "docker_guest" symbol: Optional[str] = "docker_guest"
image: str = Field(..., description="Docker image name") image: str = Field(..., description="Docker image name")
adapters: Optional[int] = Field(1, ge=0, le=100, description="Number of adapters") 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") start_command: Optional[str] = Field("", description="Docker CMD entry")
environment: Optional[str] = Field("", description="Docker environment variables") environment: Optional[str] = Field("", description="Docker environment variables")
console_type: Optional[ConsoleType] = Field("telnet", description="Console type") console_type: Optional[ConsoleType] = Field("telnet", description="Console type")