Watch for dynamips rom & nvram change

This monitor for change the file from dynamips by computing a
hash of the watched file.

The way dynamips work prevent the update of the modification time.

We can improve that by using native system for watching file but:
* it's require dependencies specific for each OS
* dependencies use C extensions
* this is only a backup if your router is cleanly shutdown we export
stuff
This commit is contained in:
Julien Duponchelle committed 2016-06-13 15:57:36 +02:00
1 parent 2bde02d459
commit 67c04a7855
5 files changed
+97 -39

No files matched your search

-17
View File
@@ -239,23 +239,6 @@ class Dynamips(BaseManager):
if device.project.id == project.id:
yield from device.hypervisor.set_working_dir(project.module_working_directory(self.module_name.lower()))
@asyncio.coroutine
def project_committed(self, project):
"""
Called when a project has been committed.
:param project: Project instance
"""
# save the configs when the project is committed
for node in self._nodes.copy().values():
if node.project.id == project.id:
try:
yield from node.save_configs()
except DynamipsError as e:
log.warning(e)
continue
@property
def dynamips_path(self):
"""
@@ -35,6 +35,8 @@ from ...base_node import BaseNode
from ..dynamips_error import DynamipsError
from ..nios.nio_udp import NIOUDP
from gns3server.utils.file_watcher import FileWatcher
from gns3server.utils.asyncio import wait_run_in_executor, monitor_process
from gns3server.utils.images import md5sum
@@ -92,6 +94,7 @@ class Router(BaseNode):
self._system_id = "FTX0945W0MY" # processor board ID in IOS
self._slots = []
self._ghost_flag = ghost_flag
self._memory_watcher = None
if not ghost_flag:
if not dynamips_id:
@@ -160,6 +163,12 @@ class Router(BaseNode):
return router_info
def _memory_changed(self, path):
"""
Called when the NVRAM file has changed
"""
asyncio.async(self.save_configs())
@property
def dynamips_id(self):
"""
@@ -248,6 +257,8 @@ class Router(BaseNode):
yield from self._hypervisor.send('vm start "{name}"'.format(name=self._name))
self.status = "started"
log.info('router "{name}" [{id}] has been started'.format(name=self._name, id=self._id))
self._memory_watcher = FileWatcher(self._memory_files(), self._memory_changed, strategy='hash', delay=30)
monitor_process(self._hypervisor.process, self._termination_callback)
@asyncio.coroutine
@@ -278,6 +289,9 @@ class Router(BaseNode):
log.warn("Could not stop {}: {}".format(self._name, e))
self.status = "stopped"
log.info('Router "{name}" [{id}] has been stopped'.format(name=self._name, id=self._id))
if self._memory_watcher:
self._memory_watcher.close()
self._memory_watcher = None
yield from self.save_configs()
@asyncio.coroutine
@@ -1599,3 +1613,10 @@ class Router(BaseNode):
yield from self._hypervisor.send('vm clean_delete "{}"'.format(self._name))
self._hypervisor.devices.remove(self)
log.info('Router "{name}" [{id}] has been deleted (including associated files)'.format(name=self._name, id=self._id))
def _memory_files(self):
project_dir = os.path.join(self.project.module_working_directory(self.manager.module_name.lower()))
return [
os.path.join(project_dir, "{}_i{}_rom".format(self.platform, self.dynamips_id)),
os.path.join(project_dir, "{}_i{}_nvram".format(self.platform, self.dynamips_id))
]