Collection of GNS3 GUI Tools
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.

97 lines
2.9 KiB

  1. #!/usr/local/bin/python3
  2. """
  3. adapter_count.py - change the number of adapters
  4. """
  5. import os
  6. import sys
  7. import gns3api
  8. from qt_widgets import SimpleWidgets
  9. def die(text):
  10. """ terminate program with error message """
  11. SimpleWidgets().alert(None, text)
  12. sys.exit(text)
  13. # get command line parameter
  14. if len(sys.argv) < 4:
  15. die("usage:\ncadapter_count version parameter-file project-id [sel-item ...]")
  16. try:
  17. with open(sys.argv[2], "r") as file:
  18. cntl_url, cntl_user, cntl_passwd, *_ = file.read(512).splitlines()
  19. if sys.argv[2].endswith(".tmp"):
  20. os.remove(sys.argv[2])
  21. except (OSError, ValueError) as err:
  22. sys.exit("Can't get controller connection params: {}".format(err))
  23. project_id = sys.argv[3]
  24. sel_items = sys.argv[4:]
  25. # get node id from command line
  26. sel_nodes = [item[6:] for item in sel_items
  27. if item.startswith("nodes/")]
  28. if len(sel_nodes) != 1:
  29. die("Exactly one node must be selected")
  30. node_id = sel_nodes[0]
  31. # connect to GNS3 controller
  32. try:
  33. api = gns3api.GNS3Api(cntl_url, cntl_user, cntl_passwd)
  34. except gns3api.GNS3ApiException as err:
  35. die("Can't connect to GNS3 controller: {}".format(err))
  36. # get node information
  37. try:
  38. node = api.request('GET', ('/v2/projects', project_id, 'nodes', node_id))
  39. except gns3api.GNS3ApiException as err:
  40. die("Can't get node information: {}".format(err))
  41. if node['node_type'] not in ("docker", "iou", "qemu"):
  42. die("Only the node types Docker, IOU and QEMU are supported")
  43. if node['status'] != "stopped":
  44. die("Node must be stopped")
  45. # New number of adapters
  46. adapter_property = 'adapters'
  47. if node['node_type'] == "iou":
  48. adapter_property = 'ethernet_adapters'
  49. cur_adapters = node['properties'][adapter_property]
  50. adapters = SimpleWidgets().get_int(None, "Adapters", cur_adapters, 1)
  51. if adapters is None or adapters == cur_adapters:
  52. sys.exit(0) # Nothing to do
  53. # Get links of the node
  54. try:
  55. links = api.request('GET', ('/v2/projects', project_id, 'nodes', node_id, 'links'))
  56. except gns3api.GNS3ApiException as err:
  57. die("Can't get link information: {}".format(err))
  58. # delete links of the node
  59. try:
  60. for link in links:
  61. api.request('DELETE', ('/v2/projects', project_id, 'links', link['link_id']))
  62. except gns3api.GNS3ApiException as err:
  63. die("Can't delete link: {}".format(err))
  64. # update adapter count
  65. update_err = None
  66. try:
  67. api.request('PUT', ('/v2/projects', project_id, 'nodes', node_id),
  68. {'properties': {adapter_property: adapters}})
  69. except gns3api.GNS3ApiException as err:
  70. update_err = "Can't update adapter count: {}".format(err)
  71. # restore the links
  72. for link in links:
  73. del link['link_id']
  74. try:
  75. api.request('POST', ('/v2/projects', project_id, 'links'), link)
  76. except gns3api.GNS3ApiException as err:
  77. if not isinstance(err, gns3api.HTTPError) or err.args[0] != 404:
  78. die("Can't restore link: {}".format(err))
  79. if update_err:
  80. die(update_err)