initializer.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import subprocess, os
  2. from toolset.utils import setup_util
  3. DEVNULL = open(os.devnull, 'w')
  4. def initialize(args):
  5. fwroot = setup_util.get_fwroot()
  6. dbuser = args.database_user
  7. dbhost = args.database_host
  8. dbiden = args.database_identity_file
  9. cluser = args.client_user
  10. clhost = args.client_host
  11. cliden = args.client_identity_file
  12. aphost = args.server_host
  13. # test ssh connections to all the machines
  14. client_conn = __check_connection(cluser, clhost, cliden, aphost)
  15. database_conn = __check_connection(dbuser, dbhost, dbiden, aphost)
  16. conn_success = client_conn and database_conn
  17. if not conn_success and not args.quiet:
  18. return __print_failure()
  19. # set up client machine
  20. if not __init_client(fwroot, cluser, clhost, cliden,
  21. args.quiet) and not args.quiet:
  22. return __print_failure()
  23. # set up database software
  24. if not __init_database(fwroot, dbuser, dbhost, dbiden,
  25. args.quiet) and not args.quiet:
  26. return __print_failure()
  27. def __print_failure():
  28. print("""
  29. -------------------------------------------------------------------------------
  30. This wizard is intended to help configure the required software on all the
  31. machines in the ecosystem specified in benchmark.cfg.
  32. Note: It is expected that you have already set up passwordless-sudo on all
  33. of the machines (app, database, client) as well as identity file based
  34. authentication and hostname setup in your hosts file.
  35. More information on this required setup can be found at:
  36. frameworkbenchmarks.readthedocs.io/en/latest/Development/Installation-Guide/
  37. Please ensure that your benchmark.cfg is correctly configured as well as all
  38. of the machines (app, database, client).
  39. -------------------------------------------------------------------------------"""
  40. )
  41. def __ssh_string(user, host, identity_file):
  42. return [
  43. "ssh", "-T", "-o", "StrictHostKeyChecking=no",
  44. "%s@%s" % (user, host), "-i", identity_file
  45. ]
  46. def __check_connection(user, host, identity_file, app_host):
  47. '''
  48. Checks that the given user and host are accessible via ssh with the given
  49. identity file and have the the following permissions:
  50. 1. passwordless sudo
  51. 2. ability to ssh back to app machine
  52. '''
  53. client_conn = True
  54. try:
  55. p = subprocess.Popen(
  56. __ssh_string(user, host, identity_file),
  57. stdin=subprocess.PIPE,
  58. stdout=DEVNULL,
  59. stderr=DEVNULL)
  60. p.communicate("ssh -T -o StrictHostKeyChecking=no %s" % app_host)
  61. if p.returncode:
  62. client_conn = False
  63. except Exception:
  64. client_conn = False
  65. return client_conn
  66. def __init_client(fwroot, user, host, identity_file, quiet):
  67. '''
  68. Initializes and configures the software required to run the suite on the
  69. client machine.
  70. '''
  71. if not quiet:
  72. print("INSTALL: Installing client software")
  73. with open(
  74. os.path.join(fwroot, "toolset", "setup", "linux", "client.sh"),
  75. "r") as myfile:
  76. remote_script = myfile.read()
  77. if quiet:
  78. p = subprocess.Popen(
  79. __ssh_string(user, host, identity_file),
  80. stdin=subprocess.PIPE,
  81. stdout=DEVNULL,
  82. stderr=DEVNULL)
  83. else:
  84. p = subprocess.Popen(
  85. __ssh_string(user, host, identity_file), stdin=subprocess.PIPE)
  86. p.communicate(remote_script)
  87. return p.returncode == 0
  88. def __init_database(fwroot, user, host, identity_file, quiet):
  89. '''
  90. Initializes and configures the software required to run the suite on the
  91. database machine.
  92. '''
  93. if not quiet:
  94. print("INSTALL: Installing database software")
  95. with open(
  96. os.path.join(fwroot, "toolset", "setup", "linux", "database.sh"),
  97. "r") as myfile:
  98. remote_script = myfile.read()
  99. if quiet:
  100. p = subprocess.Popen(
  101. __ssh_string(user, host, identity_file),
  102. stdin=subprocess.PIPE,
  103. stdout=DEVNULL,
  104. stderr=DEVNULL)
  105. else:
  106. p = subprocess.Popen(
  107. __ssh_string(user, host, identity_file), stdin=subprocess.PIPE)
  108. p.communicate(remote_script)
  109. return p.returncode == 0