def download_jdk()

in ambari-server/src/main/python/ambari-server.py [0:0]


def download_jdk(args):
  properties = get_ambari_properties()
  if properties == -1:
    err = "Error getting ambari properties"
    raise FatalException(-1, err)
  conf_file = properties.fileName
  ok = False
  if get_JAVA_HOME() and not args.java_home:
    pass # do nothing
  elif args.java_home and os.path.exists(args.java_home):
    jce_policy_path = os.path.join(properties[RESOURCES_DIR_PROPERTY], JCE_POLICY_FILENAME)
    if os.path.exists(jce_policy_path):
      err = "Command failed to execute. Please remove or move " + jce_policy_path + " and retry again"
      raise FatalException(1, err)

    print_warning_msg("JAVA_HOME " + args.java_home
                    + " must be valid on ALL hosts")
    write_property(JAVA_HOME_PROPERTY, args.java_home)
    
    warn = "JCE Policy files are required for configuring Kerberos security. If you plan to use Kerberos," \
            "please make sure JCE Unlimited Strength Jurisdiction Policy Files are valid on all hosts."
    print_warning_msg(warn)

    return 0
  else:
    try:
      jdk_url = properties[JDK_URL_PROPERTY]
      resources_dir = properties[RESOURCES_DIR_PROPERTY]
    except (KeyError), e:
      err = 'Property ' + str(e) + ' is not defined at ' + conf_file
      raise FatalException(1, err)
    dest_file = resources_dir + os.sep + JDK_LOCAL_FILENAME
    if os.path.exists(dest_file):
      print "JDK already exists, using " + dest_file
    elif args.jdk_location and os.path.exists(args.jdk_location):
      print "Copying local JDK file {0} to {1}".format(args.jdk_location, dest_file)
      try:
        shutil.copyfile(args.jdk_location, dest_file)
      except Exception, e:
        err = "Can not copy file {0} to {1} due to: {2} . Please check file " \
              "permissions and free disk space.".format(args.jdk_location,
                                                        dest_file, e.message)
        raise FatalException(1, err)
    else:
      ok = get_YN_input("To download the Oracle JDK you must accept the "
                        "license terms found at "
                        "http://www.oracle.com/technetwork/java/javase/"
                        "terms/license/index.html and not accepting will "
                        "cancel the Ambari Server setup.\nDo you accept the "
                        "Oracle Binary Code License Agreement [y/n] (y)? ", True)
      if not ok:
        print 'Exiting...'
        sys.exit(1)

      print 'Downloading JDK from ' + jdk_url + ' to ' + dest_file
      jdk_download_fail_msg = " Failed to download JDK: {0}. Please check that Oracle " \
        "JDK is available at {1}. Also you may specify JDK file " \
        "location in local filesystem using --jdk-location command " \
        "line argument.".format("{0}", jdk_url)
      try:
        size_command = JDK_DOWNLOAD_SIZE_CMD.format(jdk_url);
        #Get Header from url,to get file size then
        retcode, out, err = run_os_command(size_command)
        if out.find("Content-Length") == -1:
          err = jdk_download_fail_msg.format("Request header doesn't contain Content-Length")
          raise FatalException(1, err)
        start_with = int(out.find("Content-Length") + len("Content-Length") + 2)
        end_with = out.find("\r\n", start_with)
        src_size = int(out[start_with:end_with])
        print 'JDK distribution size is ' + str(src_size) + ' bytes'
        file_exists = os.path.isfile(dest_file)
        file_size = -1
        if file_exists:
          file_size = os.stat(dest_file).st_size
        if file_exists and file_size == src_size:
          print_info_msg("File already exists")
        else:
          track_jdk(JDK_LOCAL_FILENAME, jdk_url, dest_file)
          print 'Successfully downloaded JDK distribution to ' + dest_file
      except FatalException:
        raise
      except Exception, e:
        err = jdk_download_fail_msg.format(str(e))
        raise FatalException(1, err)
      downloaded_size = os.stat(dest_file).st_size
      if downloaded_size != src_size or downloaded_size < JDK_MIN_FILESIZE:
        err = 'Size of downloaded JDK distribution file is ' \
                      + str(downloaded_size) + ' bytes, it is probably \
                      damaged or incomplete'
        raise FatalException(1, err)

    try:
       out = install_jdk(dest_file)
       jdk_version = re.search('Creating (jdk.*)/jre', out).group(1)
    except Exception, e:
       print "Installation of JDK has failed: %s\n" % e.message
       file_exists = os.path.isfile(dest_file)
       if file_exists:
          ok = get_YN_input("JDK found at "+dest_file+". "
                      "Would you like to re-download the JDK [y/n] (y)? ", True)
          if not ok:
             err = "Unable to install JDK. Please remove JDK file found at "+ \
                   dest_file +" and re-run Ambari Server setup"
             raise FatalException(1, err)
          else:
             track_jdk(JDK_LOCAL_FILENAME, jdk_url, dest_file)
             print 'Successfully re-downloaded JDK distribution to ' + dest_file
             try:
                 out = install_jdk(dest_file)
                 jdk_version = re.search('Creating (jdk.*)/jre', out).group(1)
             except Exception, e:
               print "Installation of JDK was failed: %s\n" % e.message
               err = "Unable to install JDK. Please remove JDK, file found at "+ \
                     dest_file +" and re-run Ambari Server setup"
               raise FatalException(1, err)

       else:
           err = "Unable to install JDK. File "+ dest_file +" does not exist, " \
                                        "please re-run Ambari Server setup"
           raise FatalException(1, err)

    print "Successfully installed JDK to {0}/{1}".\
        format(JDK_INSTALL_DIR, jdk_version)
    write_property(JAVA_HOME_PROPERTY, "{0}/{1}".
        format(JDK_INSTALL_DIR, jdk_version))

  try:
    download_jce_policy(properties, ok)
  except FatalException as e:
    print "JCE Policy files are required for secure HDP setup. Please ensure " \
            " all hosts have the JCE unlimited strength policy 6, files."
    print_error_msg("Failed to download JCE policy files:")
    if e.reason is not None:
      print_error_msg("Reason: {0}".format(e.reason))
    # TODO: We don't fail installation if download_jce_policy fails. Is it OK?
  return 0