initializer.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import subprocess, os
  2. from setup.linux 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, args.quiet) and not args.quiet:
  21. return __print_failure()
  22. # set up database software
  23. if not __init_database(fwroot, dbuser, dbhost, dbiden, args.quiet) and not args.quiet:
  24. return __print_failure()
  25. else:
  26. # set up database docker images
  27. if not __build_database_docker_images(fwroot, dbuser, dbhost, dbiden, args.quiet) and not args.quiet:
  28. return __print_failure()
  29. def __print_failure():
  30. print("""
  31. -------------------------------------------------------------------------------
  32. This wizard is intended to help configure the required software on all the
  33. machines in the ecosystem specified in benchmark.cfg.
  34. Note: It is expected that you have already set up passwordless-sudo on all
  35. of the machines (app, database, client) as well as identity file based
  36. authentication and hostname setup in your hosts file.
  37. More information on this required setup can be found at:
  38. frameworkbenchmarks.readthedocs.io/en/latest/Development/Installation-Guide/
  39. Please ensure that your benchmark.cfg is correctly configured as well as all
  40. of the machines (app, database, client).
  41. -------------------------------------------------------------------------------""")
  42. def __ssh_string(user, host, identity_file):
  43. return ["ssh", "-T", "-o", "StrictHostKeyChecking=no", "%s@%s" % (user, host), "-i", identity_file]
  44. def __scp_string(user, host, identity_file, files):
  45. scpstr = ["scp", "-i", identity_file]
  46. for file in files:
  47. scpstr.append(file)
  48. scpstr.append("%s@%s:~/" % (user, host))
  49. return scpstr
  50. def __check_connection(user, host, identity_file, app_host):
  51. '''
  52. Checks that the given user and host are accessible via ssh with the given
  53. identity file and have the the following permissions:
  54. 1. passwordless sudo
  55. 2. ability to ssh back to app machine
  56. '''
  57. client_conn = True
  58. try:
  59. p = subprocess.Popen(__ssh_string(user, host, identity_file),
  60. stdin=subprocess.PIPE, stdout=DEVNULL, stderr=DEVNULL)
  61. p.communicate("ssh -T -o StrictHostKeyChecking=no %s" % app_host)
  62. if p.returncode:
  63. client_conn = False
  64. except Exception as e:
  65. client_conn = False
  66. return client_conn
  67. def __init_client(fwroot, user, host, identity_file, quiet):
  68. '''
  69. Initializes and configures the software required to run the suite on the
  70. client machine.
  71. '''
  72. if not quiet:
  73. print("INSTALL: Installing client software")
  74. with open (os.path.join(fwroot, "toolset", "setup", "linux", "client.sh"), "r") as myfile:
  75. remote_script=myfile.read()
  76. if quiet:
  77. p = subprocess.Popen(__ssh_string(user, host, identity_file),
  78. stdin=subprocess.PIPE, stdout=DEVNULL, stderr=DEVNULL)
  79. else:
  80. p = subprocess.Popen(__ssh_string(user, host, identity_file),
  81. stdin=subprocess.PIPE)
  82. p.communicate(remote_script)
  83. return p.returncode == 0
  84. def __init_database(fwroot, user, host, identity_file, quiet):
  85. '''
  86. Initializes and configures the software required to run the suite on the
  87. database machine.
  88. '''
  89. if not quiet:
  90. print("INSTALL: Installing database software")
  91. with open(os.path.join(fwroot, "toolset", "setup", "linux", "database.sh"), "r") as myfile:
  92. remote_script=myfile.read()
  93. if quiet:
  94. p = subprocess.Popen(__ssh_string(user, host, identity_file),
  95. stdin=subprocess.PIPE, stdout=DEVNULL, stderr=DEVNULL)
  96. else:
  97. p = subprocess.Popen(__ssh_string(user, host, identity_file),
  98. stdin=subprocess.PIPE)
  99. p.communicate(remote_script)
  100. return p.returncode == 0
  101. def __build_database_docker_images(fwroot, user, host, identity_file, quiet):
  102. '''
  103. Transfers all the files required by each database to the database machine and
  104. builds the docker image for each on the database machine.
  105. '''
  106. if not quiet:
  107. print("INSTALL: Building database docker images")
  108. returncode = 0
  109. databases_path = os.path.join(fwroot, "toolset", "setup", "linux", "docker", "databases")
  110. for database in os.listdir(databases_path):
  111. dbpath = os.path.join(databases_path, database)
  112. dbfiles = ""
  113. for dbfile in os.listdir(dbpath):
  114. dbfiles += "%s " % os.path.join(dbpath,dbfile)
  115. p = subprocess.Popen(__scp_string(user, host, identity_file, dbfiles.split()),
  116. stdin=subprocess.PIPE)
  117. p.communicate()
  118. returncode += p.returncode
  119. if p.returncode == 0:
  120. p = subprocess.Popen(__ssh_string(user, host, identity_file),
  121. stdin=subprocess.PIPE)
  122. p.communicate("docker build -f ~/%s.dockerfile -t %s ~/" % (database, database))
  123. returncode += p.returncode
  124. return returncode == 0