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.

78 lines
2.3KB

  1. #!/usr/local/bin/python3
  2. """
  3. start_nodes - start nodes of a project one by one
  4. """
  5. import os
  6. import sys
  7. import time
  8. import gns3api
  9. def start_nodes(argv):
  10. """ parse command line, retrieve nodes and start nodes one by one """
  11. # get arguments
  12. if len(argv) < 4:
  13. sys.exit("usage:\nstart_nodes version parameter-file project-id [sel-item ...]")
  14. try:
  15. with open(argv[2], "r") as file:
  16. cntl_url, cntl_user, cntl_passwd, *_ = file.read(512).splitlines()
  17. if argv[2].endswith(".tmp"):
  18. os.remove(argv[2])
  19. except (OSError, ValueError) as err:
  20. sys.exit("Can't get controller connection params: {}".format(err))
  21. project_id = argv[3]
  22. sel_items = argv[4:]
  23. # connect to GNS3 controller
  24. try:
  25. api = gns3api.GNS3Api(cntl_url, cntl_user, cntl_passwd)
  26. except gns3api.GNS3ApiException as err:
  27. sys.exit("Can't connect to GNS3 controller: {}".format(err))
  28. # get node information
  29. nodes = {}
  30. try:
  31. for node in api.request('GET', ('/v2/projects', project_id, 'nodes')):
  32. nodes[node['node_id']] = node
  33. except gns3api.GNS3ApiException as err:
  34. sys.exit("Can't get node information: {}".format(err))
  35. if not nodes:
  36. sys.exit("No node in project")
  37. # get selected nodes
  38. if not sel_items:
  39. sel_nodes = list(nodes.keys())
  40. else:
  41. sel_nodes = [item[6:] for item in sel_items
  42. if item.startswith("nodes/")]
  43. if not sel_nodes:
  44. sys.exit("No node selected")
  45. sel_nodes.sort(key=lambda k: nodes[k]['name'].lower())
  46. # start nodes
  47. print("Starting nodes one by one")
  48. for node_id in sel_nodes:
  49. node = nodes[node_id]
  50. if node['status'] == 'started':
  51. continue
  52. time.sleep(3)
  53. while True:
  54. compute = api.request('GET', ('/v2/computes', node['compute_id']))
  55. if compute['cpu_usage_percent'] < 60.0:
  56. break
  57. time.sleep(10)
  58. print("Starting '{}'".format(node['name']))
  59. try:
  60. api.request("POST", ("/v2/projects", project_id, 'nodes', node['node_id'], 'start'))
  61. except gns3api.GNS3ApiException as err:
  62. sys.exit(str(err))
  63. try:
  64. start_nodes(sys.argv)
  65. except KeyboardInterrupt:
  66. print()
  67. sys.exit("Aborted\n")