Tools for GNS3 GUI
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

0007-Tools-Passing-controller-parameters-via-parameter-fi.patch 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. From ec41c624782342df1607675689a42183ff0dd3c2 Mon Sep 17 00:00:00 2001
  2. From: Bernhard Ehlers <be@bernhard-ehlers.de>
  3. Date: Tue, 19 Nov 2019 14:18:55 +0100
  4. Subject: [PATCH 7/7] Tools - Passing controller parameters via parameter file
  5. ---
  6. gns3/tool.py | 40 ++++++++++++++++++++++++++++++++++++++--
  7. tools.md | 10 ++++++++--
  8. 2 files changed, 46 insertions(+), 4 deletions(-)
  9. diff --git a/gns3/tool.py b/gns3/tool.py
  10. index 90441612..55dcd562 100644
  11. --- a/gns3/tool.py
  12. +++ b/gns3/tool.py
  13. @@ -25,6 +25,7 @@ import logging
  14. import re
  15. import shutil
  16. import subprocess
  17. +import tempfile
  18. from enum import Enum
  19. from .version import __version__
  20. @@ -32,6 +33,7 @@ from .items.node_item import NodeItem
  21. from .items.link_item import LinkItem
  22. from .items.text_item import TextItem
  23. from .items.drawing_item import DrawingItem
  24. +from .controller import Controller
  25. log = logging.getLogger(__name__)
  26. @@ -101,6 +103,34 @@ class Tool:
  27. """ tool name """
  28. return self._name
  29. + def conn_param_file(self):
  30. + """ create file with connection setup parameter """
  31. +
  32. + http_client = Controller.instance().httpClient()
  33. + if not http_client:
  34. + log.error('Could not start tool %s: %s', self._name,
  35. + "Controller not set")
  36. + return None
  37. +
  38. + fname = None
  39. + try:
  40. + fd, fname = tempfile.mkstemp(prefix="GNS3_tool_", suffix=".tmp",
  41. + text=True)
  42. + data = "\n".join((http_client.url(), http_client.user() or '',
  43. + http_client.password() or '')) + "\n"
  44. + os.write(fd, data.encode("utf-8", errors='ignore'))
  45. + os.close(fd)
  46. + except OSError as err:
  47. + log.error("Can't create connection setup file: %s", err)
  48. + if fname is not None:
  49. + try:
  50. + os.close(fd)
  51. + os.remove(fname)
  52. + except OSError:
  53. + pass
  54. + return None
  55. + return fname
  56. +
  57. def run_in_terminal(self, argv):
  58. """ run command in terminal window """
  59. @@ -134,8 +164,11 @@ on run (args)
  60. return
  61. end run
  62. """
  63. + user_dir = os.getcwd()
  64. + if user_dir == "/":
  65. + user_dir = os.path.expanduser("~")
  66. cmd = "PATH=" + arg_quote(os.environ['PATH']) + "; " + \
  67. - "cd " + arg_quote(os.getcwd()) + "; " + cmd
  68. + "cd " + arg_quote(user_dir) + "; " + cmd
  69. if self.confirm_close == ConfirmCloseState.enable:
  70. cmd += "; read -p '\nClose...' var"
  71. elif self.confirm_close == ConfirmCloseState.on_error:
  72. @@ -166,7 +199,10 @@ end run
  73. if not project:
  74. return
  75. - args = [self._path, __version__, project.id()]
  76. + param_file = self.conn_param_file()
  77. + if not param_file:
  78. + return
  79. + args = [self._path, __version__, param_file, project.id()]
  80. for item in item_list:
  81. if isinstance(item, NodeItem):
  82. args.append("nodes/" + item.node().node_id())
  83. diff --git a/tools.md b/tools.md
  84. index f60652ac..c2ffcc2a 100644
  85. --- a/tools.md
  86. +++ b/tools.md
  87. @@ -11,8 +11,14 @@ called by the GUI with the following parameters:
  88. | No. | Parameter |
  89. |------|--------------------------------------------|
  90. | 1 | GNS3 version (for compatibility checks) |
  91. -| 2 | Project UUID |
  92. -| 3+ | List of selected items, can be empty |
  93. +| 2 | Filename of connection setup parameters |
  94. +| 3 | Project UUID |
  95. +| 4+ | List of selected items, can be empty |
  96. +
  97. +The file with the connection setup parameters contains
  98. +three lines with the URL, username and password for
  99. +connecting to the controller. If the filename has a
  100. +.tmp extension it may be deleted after use.
  101. An item consists of it's UUID prefixed by `nodes/`
  102. for node items, `text_drawings/` for text items
  103. --
  104. 2.15.1 (Apple Git-101)