mirror of
https://github.com/GNS3/gns3-server
synced 2025-01-12 00:50:56 +00:00
Patch hostname in configuration file even if name is unsync
Ref https://github.com/GNS3/gns3-gui/issues/1889
This commit is contained in:
parent
8fd59c7967
commit
70e2b87ff0
@ -24,6 +24,7 @@ import asyncio
|
|||||||
import time
|
import time
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import glob
|
import glob
|
||||||
import shlex
|
import shlex
|
||||||
import base64
|
import base64
|
||||||
@ -1493,7 +1494,7 @@ class Router(BaseNode):
|
|||||||
try:
|
try:
|
||||||
with open(startup_config_path, "r+", encoding="utf-8", errors="replace") as f:
|
with open(startup_config_path, "r+", encoding="utf-8", errors="replace") as f:
|
||||||
old_config = f.read()
|
old_config = f.read()
|
||||||
new_config = old_config.replace(self.name, new_name)
|
new_config = re.sub(r"^hostname .+$", "hostname " + new_name, old_config, flags=re.MULTILINE)
|
||||||
f.seek(0)
|
f.seek(0)
|
||||||
self._startup_config_content = new_config
|
self._startup_config_content = new_config
|
||||||
f.write(new_config)
|
f.write(new_config)
|
||||||
|
@ -307,7 +307,7 @@ class IOUVM(BaseNode):
|
|||||||
|
|
||||||
if self.startup_config_file:
|
if self.startup_config_file:
|
||||||
content = self.startup_config_content
|
content = self.startup_config_content
|
||||||
content = content.replace(self._name, new_name)
|
content = re.sub(r"^hostname .+$", "hostname " + new_name, content, flags=re.MULTILINE)
|
||||||
self.startup_config_content = content
|
self.startup_config_content = content
|
||||||
|
|
||||||
super(IOUVM, IOUVM).name.__set__(self, new_name)
|
super(IOUVM, IOUVM).name.__set__(self, new_name)
|
||||||
|
@ -171,6 +171,7 @@ class VPCSVM(BaseNode):
|
|||||||
if self.script_file:
|
if self.script_file:
|
||||||
content = self.startup_script
|
content = self.startup_script
|
||||||
content = content.replace(self._name, new_name)
|
content = content.replace(self._name, new_name)
|
||||||
|
content = re.sub(r"^set pcname .+$", "set pcname " + new_name, content, flags=re.MULTILINE)
|
||||||
self.startup_script = content
|
self.startup_script = content
|
||||||
|
|
||||||
super(VPCSVM, VPCSVM).name.__set__(self, new_name)
|
super(VPCSVM, VPCSVM).name.__set__(self, new_name)
|
||||||
|
@ -298,6 +298,14 @@ def test_change_name(vm, tmpdir):
|
|||||||
assert vm.name == "hello"
|
assert vm.name == "hello"
|
||||||
with open(path) as f:
|
with open(path) as f:
|
||||||
assert f.read() == "hostname hello"
|
assert f.read() == "hostname hello"
|
||||||
|
# support hostname not sync
|
||||||
|
vm.name = "alpha"
|
||||||
|
with open(path, 'w+') as f:
|
||||||
|
f.write("no service password-encryption\nhostname beta\nno ip icmp rate-limit unreachable")
|
||||||
|
vm.name = "charlie"
|
||||||
|
assert vm.name == "charlie"
|
||||||
|
with open(path) as f:
|
||||||
|
assert f.read() == "no service password-encryption\nhostname charlie\nno ip icmp rate-limit unreachable"
|
||||||
|
|
||||||
|
|
||||||
def test_library_check(loop, vm):
|
def test_library_check(loop, vm):
|
||||||
|
@ -243,12 +243,12 @@ def test_update_startup_script(vm):
|
|||||||
|
|
||||||
|
|
||||||
def test_update_startup_script_h(vm):
|
def test_update_startup_script_h(vm):
|
||||||
content = "setname %h\n"
|
content = "set pcname %h\n"
|
||||||
vm.name = "pc1"
|
vm.name = "pc1"
|
||||||
vm.startup_script = content
|
vm.startup_script = content
|
||||||
assert os.path.exists(vm.script_file)
|
assert os.path.exists(vm.script_file)
|
||||||
with open(vm.script_file) as f:
|
with open(vm.script_file) as f:
|
||||||
assert f.read() == "setname pc1\n"
|
assert f.read() == "set pcname pc1\n"
|
||||||
|
|
||||||
|
|
||||||
def test_get_startup_script(vm):
|
def test_get_startup_script(vm):
|
||||||
@ -275,11 +275,18 @@ def test_change_name(vm, tmpdir):
|
|||||||
path = os.path.join(vm.working_dir, 'startup.vpc')
|
path = os.path.join(vm.working_dir, 'startup.vpc')
|
||||||
vm.name = "world"
|
vm.name = "world"
|
||||||
with open(path, 'w+') as f:
|
with open(path, 'w+') as f:
|
||||||
f.write("name world")
|
f.write("set pcname world")
|
||||||
vm.name = "hello"
|
vm.name = "hello"
|
||||||
assert vm.name == "hello"
|
assert vm.name == "hello"
|
||||||
with open(path) as f:
|
with open(path) as f:
|
||||||
assert f.read() == "name hello"
|
assert f.read() == "set pcname hello"
|
||||||
|
# Support when the name is not sync with config
|
||||||
|
with open(path, 'w+') as f:
|
||||||
|
f.write("set pcname alpha")
|
||||||
|
vm.name = "beta"
|
||||||
|
assert vm.name == "beta"
|
||||||
|
with open(path) as f:
|
||||||
|
assert f.read() == "set pcname beta"
|
||||||
|
|
||||||
|
|
||||||
def test_close(vm, port_manager, loop):
|
def test_close(vm, port_manager, loop):
|
||||||
|
Loading…
Reference in New Issue
Block a user