Skip to content

Commit

Permalink
feat: import_file: load file to node from host
Browse files Browse the repository at this point in the history
  • Loading branch information
magicnat committed Dec 3, 2021
1 parent b9c8051 commit d9b3910
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
18 changes: 18 additions & 0 deletions seedemu/compiler/Docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from os import mkdir, chdir
from re import sub
from ipaddress import IPv4Network, IPv4Address
from shutil import copyfile

SEEDEMU_CLIENT_IMAGE='magicnat/seedemu-client'
ETH_SEEDEMU_CLIENT_IMAGE='rawisader/seedemu-eth-client'
Expand Down Expand Up @@ -669,6 +670,20 @@ def _addFile(self, path: str, content: str) -> str:
staged_path = md5(path.encode('utf-8')).hexdigest()
print(content, file=open(staged_path, 'w'))
return 'COPY {} {}\n'.format(staged_path, path)

def _importFile(self, path: str, hostpath: str) -> str:
"""!
@brief Stage file to local folder and return Dockerfile command.
@param path path to file. (in container)
@param hostpath path to file. (on host)
@returns COPY expression for dockerfile.
"""

staged_path = md5(path.encode('utf-8')).hexdigest()
copyfile(hostpath, staged_path)
return 'COPY {} {}\n'.format(staged_path, path)

def _compileNode(self, node: Node) -> str:
"""!
Expand Down Expand Up @@ -802,6 +817,9 @@ def _compileNode(self, node: Node) -> str:
(path, content) = file.get()
dockerfile += self._addFile(path, content)

for (cpath, hpath) in node.getImportedFiles().items():
dockerfile += self._importFile(cpath, hpath)

dockerfile += 'CMD ["/start.sh"]\n'
print(dockerfile, file=open('Dockerfile', 'w'))

Expand Down
25 changes: 25 additions & 0 deletions seedemu/core/Node.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ class Node(Printable, Registrable, Configurable, Vertex):
__role: NodeRole
__interfaces: List[Interface]
__files: Dict[str, File]
__imported_files: Dict[str, str]
__softwares: Set[str]
__build_commands: List[str]
__start_commands: List[Tuple[str, bool]]
Expand Down Expand Up @@ -237,6 +238,7 @@ def __init__(self, name: str, role: NodeRole, asn: int, scope: str = None):

self.__interfaces = []
self.__files = {}
self.__imported_files = {}
self.__asn = asn
self.__role = role
self.__name = name
Expand Down Expand Up @@ -540,6 +542,29 @@ def appendFile(self, path: str, content: str) -> Node:

return self

def getImportedFiles(self) -> Dict[str, str]:
"""!
@brief Get imported files.
@returns dict of imported files, where key = path of the file in the
container, value = path of the file on the host.
"""
return self.__imported_files

def importFile(self, hostpath: str, containerpath: str) -> Node:
"""!
@brief Import a file from the host to the container.
@param hostpath path of the file on the host. (should be absolute to
prevent issues)
@param containerpath path of the file in the container.
@returns self, for chaining API calls.
"""
self.__imported_files[containerpath] = hostpath

return self

def addSoftware(self, name: str) -> Node:
"""!
@brief Add new software to node.
Expand Down

0 comments on commit d9b3910

Please sign in to comment.