Skip to content

Commit

Permalink
Use parent directory as working directory for project duplication and…
Browse files Browse the repository at this point in the history
… snapshots. Fixes GNS3/gns3-gui#2909
  • Loading branch information
grossmj committed Jul 17, 2020
1 parent 99128e7 commit 37c7202
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
11 changes: 10 additions & 1 deletion gns3server/controller/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,16 @@ async def duplicate(self, name=None, location=None, reset_mac_addresses=True):
assert self._status != "closed"
try:
begin = time.time()
with tempfile.TemporaryDirectory() as tmpdir:

# use the parent directory of the project we are duplicating as a
# temporary directory to avoid no space left issues when '/tmp'
# is location on another partition.
if location:
working_dir = os.path.abspath(os.path.join(location, os.pardir))
else:
working_dir = os.path.abspath(os.path.join(self.path, os.pardir))

with tempfile.TemporaryDirectory(dir=working_dir) as tmpdir:
# Do not compress the exported project when duplicating
with aiozipstream.ZipFile(compression=zipfile.ZIP_STORED) as zstream:
await export_project(zstream, self, tmpdir, keep_compute_id=True, allow_all_nodes=True, reset_mac_addresses=reset_mac_addresses)
Expand Down
2 changes: 1 addition & 1 deletion gns3server/controller/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async def create(self):

try:
begin = time.time()
with tempfile.TemporaryDirectory() as tmpdir:
with tempfile.TemporaryDirectory(dir=snapshot_directory) as tmpdir:
# Do not compress the snapshots
with aiozipstream.ZipFile(compression=zipfile.ZIP_STORED) as zstream:
await export_project(zstream, self._project, tmpdir, keep_compute_id=True, allow_all_nodes=True)
Expand Down
10 changes: 7 additions & 3 deletions gns3server/handlers/api/controller/project_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,11 @@ async def export_project(request, response):

try:
begin = time.time()
with tempfile.TemporaryDirectory() as tmp_dir:
# use the parent directory as a temporary working dir
working_dir = os.path.abspath(os.path.join(project.path, os.pardir))
with tempfile.TemporaryDirectory(dir=working_dir) as tmpdir:
with aiozipstream.ZipFile(compression=compression) as zstream:
await export_project(zstream, project, tmp_dir, include_snapshots=include_snapshots, include_images=include_images, reset_mac_addresses=reset_mac_addresses)
await export_project(zstream, project, tmpdir, include_snapshots=include_snapshots, include_images=include_images, reset_mac_addresses=reset_mac_addresses)

# We need to do that now because export could failed and raise an HTTP error
# that why response start need to be the later possible
Expand Down Expand Up @@ -380,7 +382,9 @@ async def import_project(request, response):
# It could be more optimal to stream this but it is not implemented in Python.
try:
begin = time.time()
with tempfile.TemporaryDirectory() as tmpdir:
# use the parent directory as a temporary working dir
working_dir = os.path.abspath(os.path.join(path, os.pardir))
with tempfile.TemporaryDirectory(dir=working_dir) as tmpdir:
temp_project_path = os.path.join(tmpdir, "project.zip")
async with aiofiles.open(temp_project_path, 'wb') as f:
while True:
Expand Down

0 comments on commit 37c7202

Please sign in to comment.