Ejemplo n.º 1
0
    def ensure_jdbc_driver_installed(self, properties):
        # check default driver is already in classpath, will be passed for postgres only, because its driver is built-in
        is_driver_in_classpath = self._is_jdbc_driver_installed(properties)
        if is_driver_in_classpath == 0:
            return True

        # check driver is available via driver path property
        server_jdbc_path = properties.get_property(JDBC_DRIVER_PATH_PROPERTY)
        if server_jdbc_path and os.path.isfile(server_jdbc_path):
            return True

        # check driver is present by default driver path
        default_driver_path = self._get_default_driver_path(properties)
        if default_driver_path and os.path.isfile(default_driver_path):
            ambari_should_use_existing_default_jdbc = get_YN_input(
                "Should ambari use existing default jdbc {0} [y/n] (y)? ".
                format(default_driver_path), True)
            if ambari_should_use_existing_default_jdbc:
                properties.process_pair(JDBC_DRIVER_PATH_PROPERTY,
                                        default_driver_path)
                update_properties(properties)
                return True

        if get_silent():
            print_error_msg(self.JDBC_DRIVER_INSTALL_MSG)
            return False
        path_to_custom_jdbc_driver = get_validated_string_input(
            "Enter full path to custom jdbc driver: ", None, None, None, False,
            False)
        if path_to_custom_jdbc_driver and os.path.isfile(
                path_to_custom_jdbc_driver):
            try:
                custom_jdbc_name = os.path.basename(path_to_custom_jdbc_driver)
                if not path_to_custom_jdbc_driver == os.path.join(
                        configDefaults.JAVA_SHARE_PATH, custom_jdbc_name):
                    if os.path.isfile(
                            os.path.join(configDefaults.JAVA_SHARE_PATH,
                                         custom_jdbc_name)):
                        replace_jdbc_in_share_dir = get_YN_input(
                            "You already have file {0} in /usr/share/java/. Should it be replaced? [y/n] (y)? "
                            .format(custom_jdbc_name), True)
                        if replace_jdbc_in_share_dir:
                            try:
                                os.remove(
                                    os.path.join(
                                        configDefaults.JAVA_SHARE_PATH,
                                        custom_jdbc_name))
                            except Exception, ee:
                                err = 'ERROR: Could not remove jdbc file. %s' % os.path.join(
                                    configDefaults.JAVA_SHARE_PATH,
                                    custom_jdbc_name)
                                raise FatalException(1, err)
                    shutil.copy(path_to_custom_jdbc_driver,
                                configDefaults.JAVA_SHARE_PATH)
                    print "Copying {0} to {1}".format(
                        path_to_custom_jdbc_driver,
                        configDefaults.JAVA_SHARE_PATH)
            except Exception, e:
                err = "Can not copy file {0} to {1} due to: {2} . Please check file " \
                  "permissions and free disk space.".format(path_to_custom_jdbc_driver, configDefaults.JAVA_SHARE_PATH, str(e))
                raise FatalException(1, err)

            properties.process_pair(JDBC_DRIVER_PATH_PROPERTY,
                                    path_to_custom_jdbc_driver)
            update_properties(properties)
            return True
Ejemplo n.º 2
0
def run_component_https_cmd(cmd):
  retcode, out, err = run_os_command(cmd)

  if not retcode == 0:
    err = 'Error occured during truststore setup ! :' + out + " : " + err
    raise FatalException(1, err)
Ejemplo n.º 3
0
def upgrade(args):
    print_info_msg("Upgrade Ambari Server", True)
    if not is_root():
        err = configDefaults.MESSAGE_ERROR_UPGRADE_NOT_ROOT
        raise FatalException(4, err)
    print_info_msg(
        'Updating Ambari Server properties in {0} ...'.format(
            AMBARI_PROPERTIES_FILE), True)
    retcode = update_ambari_properties()
    if not retcode == 0:
        err = AMBARI_PROPERTIES_FILE + ' file can\'t be updated. Exiting'
        raise FatalException(retcode, err)

    print_info_msg(
        'Updating Ambari Server properties in {0} ...'.format(AMBARI_ENV_FILE),
        True)
    retcode = update_ambari_env()
    if not retcode == 0:
        err = AMBARI_ENV_FILE + ' file can\'t be updated. Exiting'
        raise FatalException(retcode, err)

    retcode = update_krb_jaas_login_properties()
    if retcode == -2:
        pass  # no changes done, let's be silent
    elif retcode == 0:
        print_info_msg("File {0} updated.".format(AMBARI_KRB_JAAS_LOGIN_FILE),
                       True)
    elif not retcode == 0:
        err = AMBARI_KRB_JAAS_LOGIN_FILE + ' file can\'t be updated. Exiting'
        raise FatalException(retcode, err)

    restore_custom_services()
    replay_mpack_logs()
    try:
        update_database_name_property(upgrade=True)
    except FatalException:
        return -1

    # Ignore the server version & database options passed via command-line arguments
    parse_properties_file(args)

    #TODO check database version
    change_objects_owner(args)

    retcode = run_schema_upgrade(args)
    if not retcode == 0:
        print_error_msg(
            "Ambari server upgrade failed. Please look at {0}, for more details."
            .format(configDefaults.SERVER_LOG_FILE))
        raise FatalException(11, 'Schema upgrade failed.')

    user = read_ambari_user()
    if user is None:
        warn = "Can not determine custom ambari user.\n" + SETUP_OR_UPGRADE_MSG
        print_warning_msg(warn)
    else:
        adjust_directory_permissions(user)

    # create jdbc symlinks if jdbc drivers are available in resources
    check_jdbc_drivers(args)

    properties = get_ambari_properties()
    if properties == -1:
        err = "Error getting ambari properties"
        print_error_msg(err)
        raise FatalException(-1, err)

    # Move *.py files from custom_actions to custom_actions/scripts
    # This code exists for historic reasons in which custom action python scripts location changed from Ambari 1.7.0 to 2.0.0
    ambari_version = get_ambari_version(properties)
    if ambari_version is None:
        args.warnings.append(
            "*.py files were not moved from custom_actions to custom_actions/scripts."
        )
    elif compare_versions(ambari_version, "2.0.0") == 0:
        move_user_custom_actions()

    # Move files installed by package to default views directory to a custom one
    for views_dir in get_views_dir(properties):
        root_views_dir = views_dir + "/../"

        if os.path.samefile(root_views_dir, get_default_views_dir()):
            continue

        for file in glob.glob(get_default_views_dir() + '/*'):
            shutil.move(file, root_views_dir)

    # Remove ADMIN_VIEW directory for upgrading Admin View on Ambari upgrade from 1.7.0 to 2.0.0
    admin_views_dirs = get_admin_views_dir(properties)
    for admin_views_dir in admin_views_dirs:
        shutil.rmtree(admin_views_dir)

    # Modify timestamp of views jars to current time
    views_jars = get_views_jars(properties)
    for views_jar in views_jars:
        os.utime(views_jar, None)

    # check if ambari is configured to use LDAP authentication
    if properties.get_property(CLIENT_SECURITY) == "ldap":
        args.warnings.append(
            "LDAP authentication is detected. You must run the \"ambari-server setup-ldap\" command to adjust existing LDAP configuration."
        )

    # adding custom jdbc name and previous custom jdbc properties
    # we need that to support new dynamic jdbc names for upgraded ambari
    add_jdbc_properties(properties)
Ejemplo n.º 4
0
def main(options, args, parser):
    init_logging()

    # set silent
    set_silent(options.silent)

    # debug mode
    set_debug_mode_from_options(options)
    init_debug(options)

    #perform checks

    options.warnings = []

    if are_cmd_line_db_args_blank(options):
        options.must_set_database_options = True
    elif not are_cmd_line_db_args_valid(options):
        parser.error(
            'All database options should be set. Please see help for the options.'
        )
    else:
        options.must_set_database_options = False

    #correct database
    fix_database_options(options, parser)

    if len(args) == 0:
        print parser.print_help()
        parser.error("No action entered")

    action_map = create_user_action_map(args, options)

    action = args[0]

    try:
        action_obj = action_map[action]
    except KeyError:
        parser.error("Invalid action: " + action)

    matches = 0
    for args_number_required in action_obj.possible_args_numbers:
        matches += int(len(args) == args_number_required)

    if matches == 0:
        print parser.print_help()
        possible_args = ' or '.join(
            str(x) for x in action_obj.possible_args_numbers)
        parser.error("Invalid number of arguments. Entered: " +
                     str(len(args)) + ", required: " + possible_args)

    options.exit_message = "Ambari Server '%s' completed successfully." % action
    options.exit_code = None

    try:
        if action in _action_option_dependence_map:
            required, optional = _action_option_dependence_map[action]
            for opt_str, opt_dest in required:
                if hasattr(options,
                           opt_dest) and getattr(options, opt_dest) is None:
                    print "Missing option {0} for action {1}".format(
                        opt_str, action)
                    print_action_arguments_help(action)
                    print "Run ambari-server.py --help to see detailed description of each option"
                    raise FatalException(1, "Missing option")
        action_obj.execute()

        if action_obj.need_restart:
            pstatus, pid = is_server_runing()
            if pstatus:
                print 'NOTE: Restart Ambari Server to apply changes' + \
                      ' ("ambari-server restart|stop+start")'

        if options.warnings:
            for warning in options.warnings:
                print_warning_msg(warning)
                pass
            options.exit_message = "Ambari Server '%s' completed with warnings." % action
            pass
    except FatalException as e:
        if e.reason is not None:
            print_error_msg("Exiting with exit code {0}. \nREASON: {1}".format(
                e.code, e.reason))
            logger.exception(str(e))
        sys.exit(e.code)
    except NonFatalException as e:
        options.exit_message = "Ambari Server '%s' completed with warnings." % action
        if e.reason is not None:
            print_warning_msg(e.reason)

    if options.exit_message is not None:
        print options.exit_message

    if options.exit_code is not None:  # not all actions may return a system exit code
        sys.exit(options.exit_code)
Ejemplo n.º 5
0
def setup(options):
    if options.only_silent:
        if check_setup_already_done():
            print "Nothing was done. Ambari Setup already performed and cannot re-run setup in silent mode. Use \"ambari-server setup\" command without -s option to change Ambari setup."
            sys.exit(0)

    retcode = verify_setup_allowed(options)
    if not retcode == 0:
        raise FatalException(1, None)

    if not is_root():
        err = configDefaults.MESSAGE_ERROR_SETUP_NOT_ROOT
        raise FatalException(4, err)

    # proceed jdbc properties if they were set
    if _check_jdbc_options(options):
        proceedJDBCProperties(options)
        return

    (retcode, err) = disable_security_enhancements()
    if not retcode == 0:
        raise FatalException(retcode, err)

    #Create ambari user, if needed
    (retcode, register_service, svc_user,
     svc_password) = check_ambari_user(options)
    if not retcode == 0:
        err = 'Failed to create user. Exiting.'
        raise FatalException(retcode, err)

    print configDefaults.MESSAGE_CHECK_FIREWALL
    check_firewall()

    # proceed jdbc properties if they were set
    if _check_jdbc_options(options):
        proceedJDBCProperties(options)

    print 'Checking JDK...'
    try:
        download_and_install_jdk(options)
    except FatalException as e:
        err = 'Downloading or installing JDK failed: {0}. Exiting.'.format(e)
        raise FatalException(e.code, err)

    print 'Completing setup...'
    retcode = configure_os_settings()
    if not retcode == 0:
        err = 'Configure of OS settings in ambari.properties failed. Exiting.'
        raise FatalException(retcode, err)

    print 'Configuring database...'
    prompt_db_properties(options)

    #DB setup should be done last after doing any setup.

    _setup_database(options)

    check_jdbc_drivers(options)

    print 'Extracting system views...'
    retcode = extract_views(options)
    if not retcode == 0:
        err = 'Error while extracting system views. Exiting'
        raise FatalException(retcode, err)

    # we've already done this, but new files were created so run it one time.
    adjust_directory_permissions(svc_user)

    service_setup(register_service, svc_user, svc_password)
Ejemplo n.º 6
0
def server_process_main(options, scmStatus=None):
    properties = get_ambari_properties()
    if properties == -1:
        err = "Error getting ambari properties"
        raise FatalException(-1, err)

    properties_for_print = []
    logger.info("Ambari server properties config:")
    for key, value in properties.getPropertyDict().items():
        if "passwd" not in key and "password" not in key:
            properties_for_print.append(key + "=" + value)

    logger.info(properties_for_print)

    # debug mode, including stop Java process at startup
    try:
        set_debug_mode_from_options(options)
    except AttributeError:
        pass

    if not check_reverse_lookup():
        print_warning_msg(
            "The hostname was not found in the reverse DNS lookup. "
            "This may result in incorrect behavior. "
            "Please check the DNS setup and fix the issue.")

    check_database_name_property()
    parse_properties_file(options)

    is_active_instance = get_is_active_instance()
    if not is_active_instance:
        print_warning_msg(
            "This instance of ambari server is not designated as active. Cannot start ambari server."
        )
        err = "This is not an active instance. Shutting down..."
        raise FatalException(1, err)

    ambari_user = read_ambari_user()
    current_user = ensure_can_start_under_current_user(ambari_user)

    print_info_msg("Ambari Server is not running...")

    jdk_path = find_jdk()
    if jdk_path is None:
        err = "No JDK found, please run the \"ambari-server setup\" " \
              "command to install a JDK automatically or install any " \
              "JDK manually to " + configDefaults.JDK_INSTALL_DIR
        raise FatalException(1, err)

    if not options.skip_properties_validation:
        missing_properties = get_missing_properties(properties)
        if missing_properties:
            err = "Required properties are not found: " + str(missing_properties) + ". To skip properties validation " \
                  "use \"--skip-properties-validation\""
            raise FatalException(1, err)

    # Preparations
    if is_root():
        print configDefaults.MESSAGE_SERVER_RUNNING_AS_ROOT

    ensure_jdbc_driver_is_installed(options, properties)

    ensure_dbms_is_running(options, properties, scmStatus)

    if scmStatus is not None:
        scmStatus.reportStartPending()

    refresh_stack_hash(properties)

    if scmStatus is not None:
        scmStatus.reportStartPending()

    ensure_server_security_is_configured()

    if scmStatus is not None:
        scmStatus.reportStartPending()

    java_exe = get_java_exe_path()

    serverClassPath = ServerClassPath(properties, options)

    debug_mode = get_debug_mode()
    debug_start = (debug_mode & 1) or SERVER_START_DEBUG
    suspend_start = (debug_mode & 2) or SUSPEND_START_MODE
    suspend_mode = 'y' if suspend_start else 'n'

    environ = generate_env(options, ambari_user, current_user)
    class_path = serverClassPath.get_full_ambari_classpath_escaped_for_shell(
        validate_classpath=True)

    if options.skip_database_check:
        global jvm_args
        jvm_args += " -DskipDatabaseConsistencyCheck"
        print "Ambari Server is starting with the database consistency check skipped. Do not make any changes to your cluster " \
              "topology or perform a cluster upgrade until you correct the database consistency issues. See \"" \
              + configDefaults.DB_CHECK_LOG + "\" for more details on the consistency issues."
        properties.process_pair(CHECK_DATABASE_SKIPPED_PROPERTY, "true")
    else:
        print "Ambari database consistency check started..."
        properties.process_pair(CHECK_DATABASE_SKIPPED_PROPERTY, "false")
        command = CHECK_DATABASE_HELPER_CMD.format(java_exe, class_path)

        (retcode, stdout, stderr) = run_os_command(command, env=environ)

        if retcode > 0:
            print str(stdout)
            raise FatalException(
                1, 'Database check failed to complete. Please check ' +
                configDefaults.SERVER_LOG_FILE + ' and ' +
                configDefaults.DB_CHECK_LOG + ' for more information.')
        else:
            print str(stdout)
            print "Ambari database consistency check finished"

            if not stdout.startswith("No errors"):
                sys.exit(1)

    update_properties(properties)
    param_list = generate_child_process_param_list(ambari_user, java_exe,
                                                   class_path, debug_start,
                                                   suspend_mode)

    # The launched shell process and sub-processes should have a group id that
    # is different from the parent.
    def make_process_independent():
        if IS_FOREGROUND:  # upstart script is not able to track process from different pgid.
            return

        processId = os.getpid()
        if processId > 0:
            try:
                os.setpgid(processId, processId)
            except OSError, e:
                print_warning_msg('setpgid({0}, {0}) failed - {1}'.format(
                    pidJava, str(e)))
                pass
Ejemplo n.º 7
0
def setup_master_key(options):
  if not is_root():
    err = 'Ambari-server setup should be run with ' \
          'root-level privileges'
    raise FatalException(4, err)

  properties = get_ambari_properties()
  if properties == -1:
    raise FatalException(1, "Failed to read properties file.")

  db_windows_auth_prop = properties.get_property(JDBC_USE_INTEGRATED_AUTH_PROPERTY)
  db_sql_auth = False if db_windows_auth_prop and db_windows_auth_prop.lower() == 'true' else True
  db_password = properties.get_property(JDBC_PASSWORD_PROPERTY)
  # Encrypt passwords cannot be called before setup
  if db_sql_auth and not db_password:
    print 'Please call "setup" before "encrypt-passwords". Exiting...'
    return 1

  # Check configuration for location of master key
  isSecure = get_is_secure(properties)
  (isPersisted, masterKeyFile) = get_is_persisted(properties)

  # Read clear text DB password from file
  if db_sql_auth and not is_alias_string(db_password) and os.path.isfile(db_password):
    with open(db_password, 'r') as passwdfile:
      db_password = passwdfile.read()

  ldap_password = properties.get_property(LDAP_MGR_PASSWORD_PROPERTY)
  if ldap_password:
    # Read clear text LDAP password from file
    if not is_alias_string(ldap_password) and os.path.isfile(ldap_password):
      with open(ldap_password, 'r') as passwdfile:
        ldap_password = passwdfile.read()

  ts_password = properties.get_property(SSL_TRUSTSTORE_PASSWORD_PROPERTY)
  resetKey = False
  masterKey = None

  if isSecure:
    print "Password encryption is enabled."
    resetKey = get_YN_input("Do you want to reset Master Key? [y/n] (n): ", False)

  # For encrypting of only unencrypted passwords without resetting the key ask
  # for master key if not persisted.
  if isSecure and not isPersisted and not resetKey:
    print "Master Key not persisted."
    masterKey = get_original_master_key(properties)
  pass

  # Make sure both passwords are clear-text if master key is lost
  if resetKey:
    if not isPersisted:
      print "Master Key not persisted."
      masterKey = get_original_master_key(properties)
      # Unable get the right master key or skipped question <enter>
      if not masterKey:
        print "To disable encryption, do the following:"
        print "- Edit " + find_properties_file() + \
              " and set " + SECURITY_IS_ENCRYPTION_ENABLED + " = " + "false."
        err = "{0} is already encrypted. Please call {1} to store unencrypted" \
              " password and call 'encrypt-passwords' again."
        if db_sql_auth and db_password and is_alias_string(db_password):
          print err.format('- Database password', "'" + SETUP_ACTION + "'")
        if ldap_password and is_alias_string(ldap_password):
          print err.format('- LDAP manager password', "'" + LDAP_SETUP_ACTION + "'")
        if ts_password and is_alias_string(ts_password):
          print err.format('TrustStore password', "'" + LDAP_SETUP_ACTION + "'")

        return 1
      pass
    pass
  pass

  # Read back any encrypted passwords
  if db_sql_auth  and db_password and is_alias_string(db_password):
    db_password = read_passwd_for_alias(JDBC_RCA_PASSWORD_ALIAS, masterKey)
  if ldap_password and is_alias_string(ldap_password):
    ldap_password = read_passwd_for_alias(LDAP_MGR_PASSWORD_ALIAS, masterKey)
  if ts_password and is_alias_string(ts_password):
    ts_password = read_passwd_for_alias(SSL_TRUSTSTORE_PASSWORD_ALIAS, masterKey)
  # Read master key, if non-secure or reset is true
  if resetKey or not isSecure:
    masterKey = read_master_key(resetKey)
    persist = get_YN_input("Do you want to persist master key. If you choose " \
                           "not to persist, you need to provide the Master " \
                           "Key while starting the ambari server as an env " \
                           "variable named " + SECURITY_KEY_ENV_VAR_NAME + \
                           " or the start will prompt for the master key."
                           " Persist [y/n] (y)? ", True)
    if persist:
      save_master_key(options, masterKey, get_master_key_location(properties) + os.sep +
                      SECURITY_MASTER_KEY_FILENAME, persist)
    elif not persist and masterKeyFile:
      try:
        os.remove(masterKeyFile)
        print_info_msg("Deleting master key file at location: " + str(
          masterKeyFile))
      except Exception, e:
        print 'ERROR: Could not remove master key file. %s' % e
    # Blow up the credential store made with previous key, if any
    store_file = get_credential_store_location(properties)
    if os.path.exists(store_file):
      try:
        os.remove(store_file)
      except:
        print_warning_msg("Failed to remove credential store file.")
      pass
    pass
Ejemplo n.º 8
0
def upgrade(args):
    if not is_root():
        err = configDefaults.MESSAGE_ERROR_UPGRADE_NOT_ROOT
        raise FatalException(4, err)
    print 'Updating properties in ' + AMBARI_PROPERTIES_FILE + ' ...'
    retcode = update_ambari_properties()
    if not retcode == 0:
        err = AMBARI_PROPERTIES_FILE + ' file can\'t be updated. Exiting'
        raise FatalException(retcode, err)

    retcode = update_ambari_env()
    if not retcode == 0:
        err = AMBARI_ENV_FILE + ' file can\'t be updated. Exiting'
        raise FatalException(retcode, err)

    retcode = update_krb_jaas_login_properties()
    if retcode == -2:
        pass  # no changes done, let's be silent
    elif retcode == 0:
        print 'File ' + AMBARI_KRB_JAAS_LOGIN_FILE + ' updated.'
    elif not retcode == 0:
        err = AMBARI_KRB_JAAS_LOGIN_FILE + ' file can\'t be updated. Exiting'
        raise FatalException(retcode, err)

    restore_custom_services()
    replay_mpack_logs()
    try:
        update_database_name_property(upgrade=True)
    except FatalException:
        return -1

    # Ignore the server version & database options passed via command-line arguments
    parse_properties_file(args)

    #TODO check database version
    change_objects_owner(args)

    retcode = run_schema_upgrade(args)
    if not retcode == 0:
        print_error_msg(
            "Ambari server upgrade failed. Please look at {0}, for more details."
            .format(configDefaults.SERVER_LOG_FILE))
        raise FatalException(11, 'Schema upgrade failed.')

    user = read_ambari_user()
    if user is None:
        warn = "Can not determine custom ambari user.\n" + SETUP_OR_UPGRADE_MSG
        print_warning_msg(warn)
    else:
        adjust_directory_permissions(user)

    # local repo
    upgrade_local_repo(args)

    # create jdbc symlinks if jdbc drivers are available in resources
    check_jdbc_drivers(args)

    properties = get_ambari_properties()
    if properties == -1:
        err = "Error getting ambari properties"
        print_error_msg(err)
        raise FatalException(-1, err)

    # Move *.py files from custom_actions to custom_actions/scripts
    # This code exists for historic reasons in which custom action python scripts location changed from Ambari 1.7.0 to 2.0.0
    ambari_version = get_ambari_version(properties)
    if ambari_version is None:
        args.warnings.append(
            "*.py files were not moved from custom_actions to custom_actions/scripts."
        )
    elif compare_versions(ambari_version, "2.0.0") == 0:
        move_user_custom_actions()

    # Remove ADMIN_VIEW directory for upgrading Admin View on Ambari upgrade from 1.7.0 to 2.0.0
    admin_views_dirs = get_admin_views_dir(properties)
    for admin_views_dir in admin_views_dirs:
        shutil.rmtree(admin_views_dir)

    # Remove ambari views directory for the rest of the jars, at the time of upgrade. At restart all jars present in Ambari will be extracted into work directory
    views_dir = get_views_dir(properties)
    for views in views_dir:
        shutil.rmtree(views)

    # check if ambari has obsolete LDAP configuration
    if properties.get_property(
            LDAP_PRIMARY_URL_PROPERTY
    ) and not properties.get_property(IS_LDAP_CONFIGURED):
        args.warnings.append(
            "Existing LDAP configuration is detected. You must run the \"ambari-server setup-ldap\" command to adjust existing LDAP configuration."
        )
Ejemplo n.º 9
0
def setup_sso(options):
    print_info_msg("Setup SSO.")

    server_status, pid = is_server_runing()
    if not server_status:
        err = 'Ambari Server is not running.'
        raise FatalException(1, err)

    if not get_silent():
        validate_options(options)

        ambari_properties = get_ambari_properties()

        admin_login, admin_password = get_ambari_admin_username_password_pair(
            options)
        properties = get_sso_properties(ambari_properties, admin_login,
                                        admin_password)

        if not options.sso_enabled:
            ambari_auth_enabled = get_value_from_dictionary(
                properties, AMBARI_SSO_AUTH_ENABLED)
            manage_services = get_value_from_dictionary(
                properties, SSO_MANAGE_SERVICES)

            if ambari_auth_enabled or manage_services:
                if (ambari_auth_enabled and 'true' == ambari_auth_enabled) or \
                  (manage_services and 'true' == manage_services):
                    sso_status = "enabled"
                else:
                    sso_status = "disabled"
            else:
                sso_status = "not configured"
            sys.stdout.write("\nSSO is currently %s\n" % sso_status)

            if sso_status == "enabled":
                enable_sso = not get_YN_input(
                    "Do you want to disable SSO authentication [y/n] (n)? ",
                    False)
            elif get_YN_input(
                    "Do you want to configure SSO authentication [y/n] (y)? ",
                    True):
                enable_sso = True
            else:
                return False
        else:
            enable_sso = options.sso_enabled == 'true'

        if enable_sso:
            populate_sso_provider_url(options, properties)
            populate_sso_public_cert(options, properties)
            populate_ambari_requires_sso(options, properties)
            populate_service_management(options, properties, ambari_properties,
                                        admin_login, admin_password)
            populate_jwt_cookie_name(options, properties)
            populate_jwt_audiences(options, properties)

            update_sso_conf(ambari_properties, properties, admin_login,
                            admin_password)
        else:
            remove_sso_conf(ambari_properties, admin_login, admin_password)

    else:
        warning = "setup-sso is not enabled in silent mode."
        raise NonFatalException(warning)
    pass
Ejemplo n.º 10
0
def setup(options):
  retcode = verify_setup_allowed()
  if not retcode == 0:
    raise FatalException(1, None)

  if not is_root():
    err = configDefaults.MESSAGE_ERROR_SETUP_NOT_ROOT
    raise FatalException(4, err)

  # proceed jdbc properties if they were set
  if _check_jdbc_options(options):
    proceedJDBCProperties(options)
    return

  (retcode, err) = disable_security_enhancements()
  if not retcode == 0:
    raise FatalException(retcode, err)

  #Create ambari user, if needed
  retcode = check_ambari_user()
  if not retcode == 0:
    err = 'Failed to create user. Exiting.'
    raise FatalException(retcode, err)

  print configDefaults.MESSAGE_CHECK_FIREWALL
  check_firewall()

  # proceed jdbc properties if they were set
  if _check_jdbc_options(options):
    proceedJDBCProperties(options)

  print 'Checking JDK...'
  try:
    download_and_install_jdk(options)
  except FatalException as e:
    err = 'Downloading or installing JDK failed: {0}. Exiting.'.format(e)
    raise FatalException(e.code, err)

  print 'Completing setup...'
  retcode = configure_os_settings()
  if not retcode == 0:
    err = 'Configure of OS settings in ambari.properties failed. Exiting.'
    raise FatalException(retcode, err)

  print 'Configuring database...'
  prompt_db_properties(options)

  #DB setup should be done last after doing any setup.

  _setup_database(options)

  check_jdbc_drivers(options)

  print 'Extracting system views...'
  retcode = extract_views()
  if not retcode == 0:
    err = 'Error while extracting system views. Exiting'
    raise FatalException(retcode, err)

  # we've already done this, but new files were created so run it one time.
  adjust_directory_permissions(read_ambari_user())
Ejemplo n.º 11
0
def setup(options):
  logger.info("Setup ambari-server.")
  if options.only_silent:
    if check_setup_already_done():
      print "Nothing was done. Ambari Setup already performed and cannot re-run setup in silent mode. Use \"ambari-server setup\" command without -s option to change Ambari setup."
      sys.exit(0)

  retcode = verify_setup_allowed(options)
  if not retcode == 0:
    raise FatalException(1, None)

  if not is_root():
    warn_msg = configDefaults.MESSAGE_WARN_SETUP_NOT_ROOT
    print warn_msg

  # proceed jdbc properties if they were set
  if _check_jdbc_options(options):
    proceedJDBCProperties(options)
    return

  (retcode, err) = disable_security_enhancements()
  if not retcode == 0:
    raise FatalException(retcode, err)

  #Create ambari user, if needed
  (retcode, register_service, svc_user, svc_password) = check_ambari_user(options)
  if not retcode == 0:
    err = 'Failed to create user. Exiting.'
    raise FatalException(retcode, err)

  print configDefaults.MESSAGE_CHECK_FIREWALL
  check_firewall()

  # proceed jdbc properties if they were set
  if _check_jdbc_options(options):
    proceedJDBCProperties(options)

  print 'Checking JDK...'
  try:
    download_and_install_jdk(options)
  except FatalException as e:
    err = 'Downloading or installing JDK failed: {0}. Exiting.'.format(e)
    raise FatalException(e.code, err)

  print 'Checking GPL software agreement...'
  write_gpl_license_accepted(default_prompt_value=options.accept_gpl)

  print 'Completing setup...'
  retcode = configure_os_settings()
  if not retcode == 0:
    err = 'Configure of OS settings in ambari.properties failed. Exiting.'
    raise FatalException(retcode, err)

  print 'Configuring database...'
  prompt_db_properties(options)

  #DB setup should be done last after doing any setup.

  _setup_database(options)

  check_jdbc_drivers(options)

  if not options.skip_view_extraction:
    print 'Extracting system views...'
    retcode = extract_views(options)
    if not retcode == 0:
      err = 'Error while extracting system views. Exiting'
      raise FatalException(retcode, err)

  json_url = get_json_url_from_repo_file()
  if json_url:
    print "Ambari repo file contains latest json url {0}, updating stacks repoinfos with it...".format(json_url)
    properties = get_ambari_properties()
    stack_root = get_stack_location(properties)
    update_latest_in_repoinfos_for_all_stacks(stack_root, json_url)
  else:
    print "Ambari repo file doesn't contain latest json url, skipping repoinfos modification"

  # we've already done this, but new files were created so run it one time.
  adjust_directory_permissions(svc_user)

  service_setup(register_service, svc_user, svc_password)
Ejemplo n.º 12
0
  def download_and_install_jdk(self, args, properties):
    conf_file = properties.fileName

    jcePolicyWarn = "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."

    if args.java_home:
      #java_home was specified among the command-line arguments. Use it as custom JDK location.
      if not validate_jdk(args.java_home):
        err = "Path to java home " + args.java_home + " or java binary file does not exists"
        raise FatalException(1, err)

      print_warning_msg("JAVA_HOME " + args.java_home + " must be valid on ALL hosts")
      print_warning_msg(jcePolicyWarn)
      IS_CUSTOM_JDK = True

      properties.process_pair(JAVA_HOME_PROPERTY, args.java_home)
      properties.removeOldProp(JDK_NAME_PROPERTY)
      properties.removeOldProp(JCE_NAME_PROPERTY)

      self._ensure_java_home_env_var_is_set(args.java_home)
      self.jdk_index = self.custom_jdk_number
      return

    java_home_var = get_JAVA_HOME()

    if get_silent():
      if not java_home_var:
        #No java_home_var set, detect if java is already installed
        if os.environ.has_key(JAVA_HOME):
          args.java_home = os.environ[JAVA_HOME]

          properties.process_pair(JAVA_HOME_PROPERTY, args.java_home)
          properties.removeOldProp(JDK_NAME_PROPERTY)
          properties.removeOldProp(JCE_NAME_PROPERTY)

          self._ensure_java_home_env_var_is_set(args.java_home)
          self.jdk_index = self.custom_jdk_number
          return
        else:
          # For now, changing the existing JDK to make sure we use a supported one
          pass

    if java_home_var:
      change_jdk = get_YN_input("Do you want to change Oracle JDK [y/n] (n)? ", False)
      if not change_jdk:
        self._ensure_java_home_env_var_is_set(java_home_var)
        self.jdk_index = self.custom_jdk_number
        return

    #Continue with the normal setup, taking the first listed JDK version as the default option
    jdk_num = str(self.jdk_index + 1)
    (self.jdks, jdk_choice_prompt, jdk_valid_choices, self.custom_jdk_number) = self._populate_jdk_configs(properties, jdk_num)

    jdk_num = get_validated_string_input(
      jdk_choice_prompt,
      jdk_num,
      jdk_valid_choices,
      "Invalid number.",
      False
    )

    self.jdk_index = int(jdk_num) - 1

    if self.jdk_index == self.custom_jdk_number:
      print_warning_msg("JDK must be installed on all hosts and JAVA_HOME must be valid on all hosts.")
      print_warning_msg(jcePolicyWarn)
      args.java_home = get_validated_string_input("Path to JAVA_HOME: ", None, None, None, False, False)
      if not os.path.exists(args.java_home) or not os.path.isfile(os.path.join(args.java_home, "bin", self.JAVA_BIN)):
        err = "Java home path or java binary file is unavailable. Please put correct path to java home."
        raise FatalException(1, err)
      print "Validating JDK on Ambari Server...done."

      properties.process_pair(JAVA_HOME_PROPERTY, args.java_home)
      properties.removeOldProp(JDK_NAME_PROPERTY)
      properties.removeOldProp(JCE_NAME_PROPERTY)

      self._ensure_java_home_env_var_is_set(args.java_home)
      return

    jdk_cfg = self.jdks[self.jdk_index]

    try:
      resources_dir = properties[RESOURCES_DIR_PROPERTY]
    except (KeyError), e:
      err = 'Property ' + str(e) + ' is not defined at ' + conf_file
      raise FatalException(1, err)
Ejemplo n.º 13
0
class JDKSetup(object):
  def __init__(self):
    self.JDK_DEFAULT_CONFIGS = []

    self.JDK_PROMPT = "[{0}] {1}\n"
    self.JDK_CUSTOM_CHOICE_PROMPT = "[{0}] - Custom JDK\n==============================================================================\nEnter choice ({1}): "
    self.JDK_VALID_CHOICES = "^[{0}{1:d}]$"
    self.JDK_MIN_FILESIZE = 5000
    self.JAVA_BIN = ""

    self.jdk_index = 0

  #
  # Downloads and installs the JDK and the JCE policy archive
  #
  def download_and_install_jdk(self, args, properties):
    conf_file = properties.fileName

    jcePolicyWarn = "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."

    if args.java_home:
      #java_home was specified among the command-line arguments. Use it as custom JDK location.
      if not validate_jdk(args.java_home):
        err = "Path to java home " + args.java_home + " or java binary file does not exists"
        raise FatalException(1, err)

      print_warning_msg("JAVA_HOME " + args.java_home + " must be valid on ALL hosts")
      print_warning_msg(jcePolicyWarn)
      IS_CUSTOM_JDK = True

      properties.process_pair(JAVA_HOME_PROPERTY, args.java_home)
      properties.removeOldProp(JDK_NAME_PROPERTY)
      properties.removeOldProp(JCE_NAME_PROPERTY)

      self._ensure_java_home_env_var_is_set(args.java_home)
      self.jdk_index = self.custom_jdk_number
      return

    java_home_var = get_JAVA_HOME()

    if get_silent():
      if not java_home_var:
        #No java_home_var set, detect if java is already installed
        if os.environ.has_key(JAVA_HOME):
          args.java_home = os.environ[JAVA_HOME]

          properties.process_pair(JAVA_HOME_PROPERTY, args.java_home)
          properties.removeOldProp(JDK_NAME_PROPERTY)
          properties.removeOldProp(JCE_NAME_PROPERTY)

          self._ensure_java_home_env_var_is_set(args.java_home)
          self.jdk_index = self.custom_jdk_number
          return
        else:
          # For now, changing the existing JDK to make sure we use a supported one
          pass

    if java_home_var:
      change_jdk = get_YN_input("Do you want to change Oracle JDK [y/n] (n)? ", False)
      if not change_jdk:
        self._ensure_java_home_env_var_is_set(java_home_var)
        self.jdk_index = self.custom_jdk_number
        return

    #Continue with the normal setup, taking the first listed JDK version as the default option
    jdk_num = str(self.jdk_index + 1)
    (self.jdks, jdk_choice_prompt, jdk_valid_choices, self.custom_jdk_number) = self._populate_jdk_configs(properties, jdk_num)

    jdk_num = get_validated_string_input(
      jdk_choice_prompt,
      jdk_num,
      jdk_valid_choices,
      "Invalid number.",
      False
    )

    self.jdk_index = int(jdk_num) - 1

    if self.jdk_index == self.custom_jdk_number:
      print_warning_msg("JDK must be installed on all hosts and JAVA_HOME must be valid on all hosts.")
      print_warning_msg(jcePolicyWarn)
      args.java_home = get_validated_string_input("Path to JAVA_HOME: ", None, None, None, False, False)
      if not os.path.exists(args.java_home) or not os.path.isfile(os.path.join(args.java_home, "bin", self.JAVA_BIN)):
        err = "Java home path or java binary file is unavailable. Please put correct path to java home."
        raise FatalException(1, err)
      print "Validating JDK on Ambari Server...done."

      properties.process_pair(JAVA_HOME_PROPERTY, args.java_home)
      properties.removeOldProp(JDK_NAME_PROPERTY)
      properties.removeOldProp(JCE_NAME_PROPERTY)

      self._ensure_java_home_env_var_is_set(args.java_home)
      return

    jdk_cfg = self.jdks[self.jdk_index]

    try:
      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 = os.path.abspath(os.path.join(resources_dir, jdk_cfg.dest_file))
    if os.path.exists(dest_file):
      print "JDK already exists, using " + dest_file
    else:
      ok = get_YN_input("To download the Oracle JDK and the Java Cryptography Extension (JCE) "
                        "Policy Files 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 and you must install the JDK and JCE "
                        "files manually.\nDo you accept the "
                        "Oracle Binary Code License Agreement [y/n] (y)? ", True)
      if not ok:
        print 'Exiting...'
        sys.exit(1)

      jdk_url = jdk_cfg.url

      print 'Downloading JDK from ' + jdk_url + ' to ' + dest_file
      self._download_jdk(jdk_url, dest_file)

    try:
      (retcode, out, java_home_dir) = self._install_jdk(dest_file, jdk_cfg)
    except Exception, e:
      print "Installation of JDK has failed: %s\n" % str(e)
      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)? ", not get_silent())
        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:
          jdk_url = jdk_cfg.url

          print 'Re-downloading JDK from ' + jdk_url + ' to ' + dest_file
          self._download_jdk(jdk_url, dest_file)
          print 'Successfully re-downloaded JDK distribution to ' + dest_file

          try:
            (retcode, out) = self._install_jdk(dest_file, jdk_cfg)
          except Exception, e:
            print "Installation of JDK was failed: %s\n" % str(e)
            err = "Unable to install JDK. Please remove JDK, file found at " + \
                  dest_file + " and re-run Ambari Server setup"
            raise FatalException(1, err)
Ejemplo n.º 14
0
def setup_sso(args):
    if not is_root():
        err = 'ambari-server setup-sso should be run with ' \
              'root-level privileges'
        raise FatalException(4, err)
    if not get_silent():
        properties = get_ambari_properties()

        must_setup_params = False
        store_new_cert = False

        sso_enabled = properties.get_property(JWT_AUTH_ENBABLED).lower() in [
            'true'
        ]

        if sso_enabled:
            if get_YN_input(
                    "Do you want to disable SSO authentication [y/n] (n)?",
                    False):
                properties.process_pair(JWT_AUTH_ENBABLED, "false")
        else:
            if get_YN_input(
                    "Do you want to configure SSO authentication [y/n] (y)?",
                    True):
                properties.process_pair(JWT_AUTH_ENBABLED, "true")
                must_setup_params = True
            else:
                return False

        if must_setup_params:

            provider_url = get_value_from_properties(
                properties, JWT_AUTH_PROVIDER_URL,
                JWT_AUTH_PROVIDER_URL_DEFAULT)
            provider_url = get_validated_string_input(
                "Provider URL [URL] ({0}):".format(provider_url), provider_url,
                REGEX_ANYTHING, "Invalid provider URL", False)
            properties.process_pair(JWT_AUTH_PROVIDER_URL, provider_url)

            cert_path = properties.get_property(JWT_PUBLIC_KEY)
            cert_string = get_multi_line_input(
                "Public Certificate pem ({0})".format(
                    'stored' if cert_path else 'empty'))
            if cert_string is not None:
                store_new_cert = True

            if get_YN_input(
                    "Do you want to configure advanced properties [y/n] (n) ?",
                    False):
                cookie_name = get_value_from_properties(
                    properties, JWT_COOKIE_NAME, JWT_COOKIE_NAME_DEFAULT)
                cookie_name = get_validated_string_input(
                    "JWT Cookie name ({0}):".format(cookie_name), cookie_name,
                    REGEX_ANYTHING, "Invalid cookie name", False)
                properties.process_pair(JWT_COOKIE_NAME, cookie_name)

                audiences = properties.get_property(JWT_AUDIENCES)
                audiences = get_validated_string_input(
                    "JWT audiences list (comma-separated), empty for any ({0}):"
                    .format(audiences), audiences, REGEX_ANYTHING,
                    "Invalid value", False)
                properties.process_pair(JWT_AUDIENCES, audiences)

                # TODO not required for now as we support Knox only
                # orig_query_param = get_value_from_properties(JWT_ORIGINAL_URL_QUERY_PARAM, JWT_ORIGINAL_URL_QUERY_PARAM_DEFAULT)
                # orig_query_param = get_validated_string_input("Original URL query parameter name ({}):".format(orig_query_param),
                #                                               orig_query_param,
                #                                               REGEX_ANYTHING,
                #                                               "Invalid value",
                #                                               False)
                # properties.process_pair(JWT_ORIGINAL_URL_QUERY_PARAM, orig_query_param)

            if store_new_cert:
                full_cert = JWT_PUBLIC_KEY_HEADER + cert_string + JWT_PUBLIC_KEY_FOOTER
                cert_path = store_password_file(full_cert,
                                                JWT_PUBLIC_KEY_FILENAME)

            properties.process_pair(JWT_PUBLIC_KEY, cert_path)

        update_properties(properties)

        pass
    else:
        warning = "setup-sso is not enabled in silent mode."
        raise NonFatalException(warning)

    pass
Ejemplo n.º 15
0
def server_process_main(options, scmStatus=None):
    # debug mode, including stop Java process at startup
    try:
        set_debug_mode_from_options(options)
    except AttributeError:
        pass

    if not check_reverse_lookup():
        print_warning_msg(
            "The hostname was not found in the reverse DNS lookup. "
            "This may result in incorrect behavior. "
            "Please check the DNS setup and fix the issue.")

    check_database_name_property()
    parse_properties_file(options)

    is_active_instance = get_is_active_instance()
    if not is_active_instance:
        print_warning_msg(
            "This instance of ambari server is not designated as active. Cannot start ambari server."
        )
        err = "This is not an active instance. Shutting down..."
        raise FatalException(1, err)

    ambari_user = read_ambari_user()
    current_user = ensure_can_start_under_current_user(ambari_user)

    print_info_msg("Ambari Server is not running...")

    jdk_path = find_jdk()
    if jdk_path is None:
        err = "No JDK found, please run the \"ambari-server setup\" " \
              "command to install a JDK automatically or install any " \
              "JDK manually to " + configDefaults.JDK_INSTALL_DIR
        raise FatalException(1, err)

    properties = get_ambari_properties()

    if not options.skip_properties_validation:
        missing_properties = get_missing_properties(properties)
        if missing_properties:
            err = "Required properties are not found: " + str(missing_properties) + ". To skip properties validation " \
                  "use \"--skip-properties-validation\""
            raise FatalException(1, err)

    # Preparations
    if is_root():
        print configDefaults.MESSAGE_SERVER_RUNNING_AS_ROOT

    ensure_jdbc_driver_is_installed(options, properties)

    ensure_dbms_is_running(options, properties, scmStatus)

    if scmStatus is not None:
        scmStatus.reportStartPending()

    refresh_stack_hash(properties)

    if scmStatus is not None:
        scmStatus.reportStartPending()

    ensure_server_security_is_configured()

    if scmStatus is not None:
        scmStatus.reportStartPending()

    java_exe = get_java_exe_path()

    serverClassPath = ServerClassPath(properties, options)

    debug_mode = get_debug_mode()
    debug_start = (debug_mode & 1) or SERVER_START_DEBUG
    suspend_start = (debug_mode & 2) or SUSPEND_START_MODE
    suspend_mode = 'y' if suspend_start else 'n'

    if options.skip_database_validation:
        global jvm_args
        jvm_args += " -DskipDatabaseConsistencyValidation"

    param_list = generate_child_process_param_list(
        ambari_user, java_exe,
        serverClassPath.get_full_ambari_classpath_escaped_for_shell(
            validate_classpath=True), debug_start, suspend_mode)
    environ = generate_env(options, ambari_user, current_user)

    if not os.path.exists(configDefaults.PID_DIR):
        os.makedirs(configDefaults.PID_DIR, 0755)

    # The launched shell process and sub-processes should have a group id that
    # is different from the parent.
    def make_process_independent():
        if IS_FOREGROUND:  # upstart script is not able to track process from different pgid.
            return

        processId = os.getpid()
        if processId > 0:
            try:
                os.setpgid(processId, processId)
            except OSError, e:
                print_warning_msg('setpgid({0}, {0}) failed - {1}'.format(
                    pidJava, str(e)))
                pass
Ejemplo n.º 16
0
def _install_mpack(options, replay_mode=False, is_upgrade=False):
    """
  Install management pack
  :param options: Command line options
  :param replay_mode: Flag to indicate if executing command in replay mode
  """

    mpack_path = options.mpack_path
    if not mpack_path:
        print_error_msg("Management pack not specified!")
        raise FatalException(-1, 'Management pack not specified!')

    print_info_msg("Installing management pack {0}".format(mpack_path))

    # Download management pack to a temp location
    tmp_archive_path = download_mpack(mpack_path)
    if not (tmp_archive_path and os.path.exists(tmp_archive_path)):
        print_error_msg("Management pack could not be downloaded!")
        raise FatalException(-1, 'Management pack could not be downloaded!')

    # Expand management pack in temp directory
    tmp_root_dir = expand_mpack(tmp_archive_path)

    # Read mpack metadata
    mpack_metadata = read_mpack_metadata(tmp_root_dir)
    if not mpack_metadata:
        raise FatalException(
            -1, 'Malformed management pack {0}. Metadata file missing!'.format(
                mpack_path))

    # Validate management pack prerequisites
    # Skip validation in replay mode
    if not replay_mode:
        validate_mpack_prerequisites(mpack_metadata)

    if is_upgrade:
        # Execute pre upgrade hook
        _execute_hook(mpack_metadata, BEFORE_UPGRADE_HOOK_NAME, tmp_root_dir)
    else:
        # Execute pre install hook
        _execute_hook(mpack_metadata, BEFORE_INSTALL_HOOK_NAME, tmp_root_dir)

    # Purge previously installed stacks and management packs
    if options.purge and options.purge_list:
        purge_stacks_and_mpacks(options.purge_list.split(","), replay_mode)

    # Get ambari mpack properties
    stack_location, extension_location, service_definitions_location, mpacks_staging_location = get_mpack_properties(
    )
    mpacks_cache_location = os.path.join(mpacks_staging_location,
                                         MPACKS_CACHE_DIRNAME)
    # Create directories
    if not os.path.exists(stack_location):
        sudo.makedir(stack_location, 0755)
    if not os.path.exists(extension_location):
        sudo.makedir(extension_location, 0755)
    if not os.path.exists(service_definitions_location):
        sudo.makedir(service_definitions_location, 0755)
    if not os.path.exists(mpacks_staging_location):
        sudo.makedir(mpacks_staging_location, 0755)
    if not os.path.exists(mpacks_cache_location):
        sudo.makedir(mpacks_cache_location, 0755)

    # Stage management pack (Stage at /var/lib/ambari-server/resources/mpacks/mpack_name-mpack_version)
    mpack_name = mpack_metadata.name
    mpack_version = mpack_metadata.version
    mpack_dirname = mpack_name + "-" + mpack_version
    mpack_staging_dir = os.path.join(mpacks_staging_location, mpack_dirname)
    mpack_archive_path = os.path.join(mpacks_cache_location,
                                      os.path.basename(tmp_archive_path))

    print_info_msg(
        "Stage management pack {0}-{1} to staging location {2}".format(
            mpack_name, mpack_version, mpack_staging_dir))
    if os.path.exists(mpack_staging_dir):
        if options.force:
            print_info_msg(
                "Force removing previously installed management pack from {0}".
                format(mpack_staging_dir))
            sudo.rmtree(mpack_staging_dir)
        else:
            error_msg = "Management pack {0}-{1} already installed!".format(
                mpack_name, mpack_version)
            print_error_msg(error_msg)
            raise FatalException(-1, error_msg)
    shutil.move(tmp_root_dir, mpack_staging_dir)
    shutil.move(tmp_archive_path, mpack_archive_path)

    # Process setup steps for all artifacts (stack-definitions, extension-definitions,
    # service-definitions, stack-addon-service-definitions) in the management pack
    for artifact in mpack_metadata.artifacts:
        # Artifact name (Friendly name)
        artifact_name = artifact.name
        # Artifact type (stack-definitions, extension-definitions, service-definitions, etc)
        artifact_type = artifact.type
        # Artifact directory with contents of the artifact
        artifact_source_dir = os.path.join(mpack_staging_dir,
                                           artifact.source_dir)

        print_info_msg("Processing artifact {0} of type {1} in {2}".format(
            artifact_name, artifact_type, artifact_source_dir))
        if artifact.type == STACK_DEFINITIONS_ARTIFACT_NAME:
            process_stack_definitions_artifact(artifact, artifact_source_dir,
                                               options)
        elif artifact.type == EXTENSION_DEFINITIONS_ARTIFACT_NAME:
            process_extension_definitions_artifact(artifact,
                                                   artifact_source_dir,
                                                   options)
        elif artifact.type == SERVICE_DEFINITIONS_ARTIFACT_NAME:
            process_service_definitions_artifact(artifact, artifact_source_dir,
                                                 options)
        elif artifact.type == STACK_ADDON_SERVICE_DEFINITIONS_ARTIFACT_NAME:
            process_stack_addon_service_definitions_artifact(
                artifact, artifact_source_dir, options)
        else:
            print_info_msg("Unknown artifact {0} of type {1}".format(
                artifact_name, artifact_type))

    print_info_msg("Management pack {0}-{1} successfully installed!".format(
        mpack_name, mpack_version))
    return mpack_metadata, mpack_name, mpack_version, mpack_staging_dir, mpack_archive_path
Ejemplo n.º 17
0
    procJava = subprocess.Popen(param_list,
                                env=environ,
                                preexec_fn=make_process_independent)

    pidJava = procJava.pid
    if pidJava <= 0:
        procJava.terminate()
        exitcode = procJava.returncode
        exitfile = os.path.join(configDefaults.PID_DIR, EXITCODE_NAME)
        save_pid(exitcode, exitfile)

        if scmStatus is not None:
            scmStatus.reportStopPending()

        raise FatalException(
            -1,
            AMBARI_SERVER_DIE_MSG.format(exitcode,
                                         configDefaults.SERVER_OUT_FILE))
    else:
        pidfile = os.path.join(configDefaults.PID_DIR, PID_NAME)
        save_pid(pidJava, pidfile)
        print "Server PID at: " + pidfile
        print "Server out at: " + configDefaults.SERVER_OUT_FILE
        print "Server log at: " + configDefaults.SERVER_LOG_FILE

        wait_for_server_start(pidfile, scmStatus)

    if scmStatus is not None:
        scmStatus.reportStarted()

    if IS_FOREGROUND:
        procJava.communicate()
Ejemplo n.º 18
0
def ensure_jdbc_driver_is_installed(options, properties):
    factory = DBMSConfigFactory()
    dbms = factory.create(options, properties)
    result = dbms._is_jdbc_driver_installed(properties)
    if result == -1:
        raise FatalException(-1, dbms.JDBC_DRIVER_INSTALL_MSG)
Ejemplo n.º 19
0
def sync_ldap(options):
  if not is_root():
    err = 'Ambari-server sync-ldap should be run with ' \
          'root-level privileges'
    raise FatalException(4, err)

  server_status, pid = is_server_runing()
  if not server_status:
    err = 'Ambari Server is not running.'
    raise FatalException(1, err)

  properties = get_ambari_properties()
  if properties == -1:
    raise FatalException(1, "Failed to read properties file.")

  ldap_configured = properties.get_property(IS_LDAP_CONFIGURED)
  if ldap_configured != 'true':
    err = "LDAP is not configured. Run 'ambari-server setup-ldap' first."
    raise FatalException(1, err)

  # set ldap sync options
  ldap_sync_options = LdapSyncOptions(options)

  if ldap_sync_options.no_ldap_sync_options_set():
    err = 'Must specify a sync option (all, existing, users or groups).  Please invoke ambari-server.py --help to print the options.'
    raise FatalException(1, err)

  admin_login = get_validated_string_input(prompt="Enter Ambari Admin login: "******"Enter Ambari Admin password: "******"Event":{"specs":[{"principal_type":"users","sync_type":"all"},{"principal_type":"groups","sync_type":"all"}]}}]
  elif ldap_sync_options.ldap_sync_existing:
    sys.stdout.write('Syncing existing.')
    bodies = [{"Event":{"specs":[{"principal_type":"users","sync_type":"existing"},{"principal_type":"groups","sync_type":"existing"}]}}]
  else:
    sys.stdout.write('Syncing specified users and groups.')
    bodies = [{"Event":{"specs":[]}}]
    body = bodies[0]
    events = body['Event']
    specs = events['specs']

    if ldap_sync_options.ldap_sync_users is not None:
      new_specs = [{"principal_type":"users","sync_type":"specific","names":""}]
      get_ldap_event_spec_names(ldap_sync_options.ldap_sync_users, specs, new_specs)
    if ldap_sync_options.ldap_sync_groups is not None:
      new_specs = [{"principal_type":"groups","sync_type":"specific","names":""}]
      get_ldap_event_spec_names(ldap_sync_options.ldap_sync_groups, specs, new_specs)

  if get_verbose():
    sys.stdout.write('\nCalling API ' + url + ' : ' + str(bodies) + '\n')

  request.add_data(json.dumps(bodies))
  request.get_method = lambda: 'POST'

  try:
    response = urllib2.urlopen(request)
  except Exception as e:
    err = 'Sync event creation failed. Error details: %s' % e
    raise FatalException(1, err)

  response_status_code = response.getcode()
  if response_status_code != 201:
    err = 'Error during syncing. Http status code - ' + str(response_status_code)
    raise FatalException(1, err)
  response_body = json.loads(response.read())

  url = response_body['resources'][0]['href']
  request = urllib2.Request(url)
  request.add_header('Authorization', 'Basic %s' % admin_auth)
  request.add_header('X-Requested-By', 'ambari')
  body = [{"LDAP":{"synced_groups":"*","synced_users":"*"}}]
  request.add_data(json.dumps(body))
  request.get_method = lambda: 'GET'
  request_in_progress = True

  while request_in_progress:
    sys.stdout.write('.')
    sys.stdout.flush()

    try:
      response = urllib2.urlopen(request)
    except Exception as e:
      request_in_progress = False
      err = 'Sync event check failed. Error details: %s' % e
      raise FatalException(1, err)

    response_status_code = response.getcode()
    if response_status_code != 200:
      err = 'Error during syncing. Http status code - ' + str(response_status_code)
      raise FatalException(1, err)
    response_body = json.loads(response.read())
    sync_info = response_body['Event']

    if sync_info['status'] == 'ERROR':
      raise FatalException(1, str(sync_info['status_detail']))
    elif sync_info['status'] == 'COMPLETE':
      print '\n\nCompleted LDAP Sync.'
      print 'Summary:'
      for principal_type, summary in sync_info['summary'].iteritems():
        print '  {0}:'.format(principal_type)
        for action, amount in summary.iteritems():
          print '    {0} = {1!s}'.format(action, amount)
      request_in_progress = False
    else:
      time.sleep(1)

  sys.stdout.write('\n')
  sys.stdout.flush()
Ejemplo n.º 20
0
 def change_db_files_owner(self):
     if self._is_local_database():
         retcode = self._change_db_files_owner()
         if not retcode == 0:
             raise FatalException(
                 20, 'Unable to change owner of database objects')
Ejemplo n.º 21
0
def setup_ldap():
  if not is_root():
    err = 'Ambari-server setup-ldap should be run with ' \
          'root-level privileges'
    raise FatalException(4, err)

  properties = get_ambari_properties()
  isSecure = get_is_secure(properties)

  ldap_property_list_reqd = init_ldap_properties_list_reqd(properties)

  ldap_property_list_opt = ["authentication.ldap.managerDn",
                            LDAP_MGR_PASSWORD_PROPERTY,
                            SSL_TRUSTSTORE_TYPE_PROPERTY,
                            SSL_TRUSTSTORE_PATH_PROPERTY,
                            SSL_TRUSTSTORE_PASSWORD_PROPERTY]

  ldap_property_list_truststore=[SSL_TRUSTSTORE_TYPE_PROPERTY,
                                 SSL_TRUSTSTORE_PATH_PROPERTY,
                                 SSL_TRUSTSTORE_PASSWORD_PROPERTY]

  ldap_property_list_passwords=[LDAP_MGR_PASSWORD_PROPERTY,
                                SSL_TRUSTSTORE_PASSWORD_PROPERTY]

  LDAP_MGR_DN_DEFAULT = get_value_from_properties(properties, ldap_property_list_opt[0])

  SSL_TRUSTSTORE_TYPE_DEFAULT = get_value_from_properties(properties, SSL_TRUSTSTORE_TYPE_PROPERTY, "jks")
  SSL_TRUSTSTORE_PATH_DEFAULT = get_value_from_properties(properties, SSL_TRUSTSTORE_PATH_PROPERTY)


  ldap_property_value_map = {}
  for ldap_prop in ldap_property_list_reqd:
    input = get_validated_string_input(ldap_prop.ldap_prop_val_prompt, ldap_prop.ldap_prop_name, ldap_prop.prompt_regex,
                                       "Invalid characters in the input!", False, ldap_prop.allow_empty_prompt)
    if input is not None and input != "":
      ldap_property_value_map[ldap_prop.prop_name] = input

  bindAnonymously = ldap_property_value_map["authentication.ldap.bindAnonymously"]
  anonymous = (bindAnonymously and bindAnonymously.lower() == 'true')
  mgr_password = None
  # Ask for manager credentials only if bindAnonymously is false
  if not anonymous:
    username = get_validated_string_input("Manager DN* {0}: ".format(
      get_prompt_default(LDAP_MGR_DN_DEFAULT)), LDAP_MGR_DN_DEFAULT, ".*",
                                          "Invalid characters in the input!", False, False)
    ldap_property_value_map[LDAP_MGR_USERNAME_PROPERTY] = username
    mgr_password = configure_ldap_password()
    ldap_property_value_map[LDAP_MGR_PASSWORD_PROPERTY] = mgr_password

  useSSL = ldap_property_value_map["authentication.ldap.useSSL"]
  ldaps = (useSSL and useSSL.lower() == 'true')
  ts_password = None

  if ldaps:
    truststore_default = "n"
    truststore_set = bool(SSL_TRUSTSTORE_PATH_DEFAULT)
    if truststore_set:
      truststore_default = "y"
    custom_trust_store = get_YN_input("Do you want to provide custom TrustStore for Ambari [y/n] ({0})?".
                                      format(truststore_default),
                                      truststore_set)
    if custom_trust_store:
      ts_type = get_validated_string_input(
        "TrustStore type [jks/jceks/pkcs12] {0}:".format(get_prompt_default(SSL_TRUSTSTORE_TYPE_DEFAULT)),
        SSL_TRUSTSTORE_TYPE_DEFAULT,
        "^(jks|jceks|pkcs12)?$", "Wrong type", False)
      ts_path = None
      while True:
        ts_path = get_validated_string_input(
          "Path to TrustStore file {0}:".format(get_prompt_default(SSL_TRUSTSTORE_PATH_DEFAULT)),
          SSL_TRUSTSTORE_PATH_DEFAULT,
          ".*", False, False)
        if os.path.exists(ts_path):
          break
        else:
          print 'File not found.'

      ts_password = read_password("", ".*", "Password for TrustStore:", "Invalid characters in password")

      ldap_property_value_map[SSL_TRUSTSTORE_TYPE_PROPERTY] = ts_type
      ldap_property_value_map[SSL_TRUSTSTORE_PATH_PROPERTY] = ts_path
      ldap_property_value_map[SSL_TRUSTSTORE_PASSWORD_PROPERTY] = ts_password
      pass
    else:
      properties.removeOldProp(SSL_TRUSTSTORE_TYPE_PROPERTY)
      properties.removeOldProp(SSL_TRUSTSTORE_PATH_PROPERTY)
      properties.removeOldProp(SSL_TRUSTSTORE_PASSWORD_PROPERTY)
    pass
  pass

  print '=' * 20
  print 'Review Settings'
  print '=' * 20
  for property in ldap_property_list_reqd:
    if property in ldap_property_value_map:
      print("%s: %s" % (property, ldap_property_value_map[property]))

  for property in ldap_property_list_opt:
    if ldap_property_value_map.has_key(property):
      if property not in ldap_property_list_passwords:
        print("%s: %s" % (property, ldap_property_value_map[property]))
      else:
        print("%s: %s" % (property, BLIND_PASSWORD))

  save_settings = get_YN_input("Save settings [y/n] (y)? ", True)

  if save_settings:
    ldap_property_value_map[CLIENT_SECURITY_KEY] = 'ldap'
    if isSecure:
      if mgr_password:
        encrypted_passwd = encrypt_password(LDAP_MGR_PASSWORD_ALIAS, mgr_password)
        if mgr_password != encrypted_passwd:
          ldap_property_value_map[LDAP_MGR_PASSWORD_PROPERTY] = encrypted_passwd
      pass
      if ts_password:
        encrypted_passwd = encrypt_password(SSL_TRUSTSTORE_PASSWORD_ALIAS, ts_password)
        if ts_password != encrypted_passwd:
          ldap_property_value_map[SSL_TRUSTSTORE_PASSWORD_PROPERTY] = encrypted_passwd
      pass
    pass

    # Persisting values
    ldap_property_value_map[IS_LDAP_CONFIGURED] = "true"
    if mgr_password:
      ldap_property_value_map[LDAP_MGR_PASSWORD_PROPERTY] = store_password_file(mgr_password, LDAP_MGR_PASSWORD_FILENAME)
    update_properties_2(properties, ldap_property_value_map)
    print 'Saving...done'

  return 0
Ejemplo n.º 22
0
def setup_https(args):
    if not is_root():
        warn = 'ambari-server setup-https is run as ' \
              'non-root user, some sudo privileges might be required'
        print warn
    args.exit_message = None
    if not get_silent():
        properties = get_ambari_properties()
        try:
            security_server_keys_dir = properties.get_property(SSL_KEY_DIR)
            client_api_ssl_port = DEFAULT_SSL_API_PORT if properties.get_property(SSL_API_PORT) in ("") \
                  else properties.get_property(SSL_API_PORT)
            api_ssl = properties.get_property(SSL_API) in ['true']
            client_api_ssl_port_old_value = properties.get_property(
                SSL_API_PORT)
            api_ssl_old_value = properties.get_property(SSL_API)
            cert_was_imported = False
            cert_must_import = True
            if api_ssl:
                if get_YN_input("Do you want to disable HTTPS [y/n] (n)? ",
                                False):
                    properties.process_pair(SSL_API, "false")
                    cert_must_import = False
                else:
                    properties.process_pair(SSL_API_PORT, \
                                            get_validated_string_input( \
                                                "SSL port ["+str(client_api_ssl_port)+"] ? ", \
                                                str(client_api_ssl_port), \
                                                "^[0-9]{1,5}$", "Invalid port.", False, validatorFunction = is_valid_https_port))
                    cert_was_imported = import_cert_and_key_action(
                        security_server_keys_dir, properties)
            else:
                if get_YN_input("Do you want to configure HTTPS [y/n] (y)? ",
                                True):
                    properties.process_pair(SSL_API_PORT, \
                                            get_validated_string_input("SSL port ["+str(client_api_ssl_port)+"] ? ", \
                                                                         str(client_api_ssl_port), "^[0-9]{1,5}$", "Invalid port.", False, validatorFunction = is_valid_https_port))
                    cert_was_imported = import_cert_and_key_action(
                        security_server_keys_dir, properties)
                else:
                    return False

            if cert_must_import and not cert_was_imported:
                print 'Setup of HTTPS failed. Exiting.'
                return False

            conf_file = find_properties_file()
            f = open(conf_file, 'w')
            properties.store(f,
                             "Changed by 'ambari-server setup-https' command")

            if api_ssl_old_value != properties.get_property(SSL_API) \
                or client_api_ssl_port_old_value != properties.get_property(SSL_API_PORT):
                print "Ambari server URL changed. To make use of the Tez View in Ambari " \
                      "please update the property tez.tez-ui.history-url.base in tez-site"

            ambari_user = read_ambari_user()
            if ambari_user:
                adjust_directory_permissions(ambari_user)
            return True
        except (KeyError), e:
            err = 'Property ' + str(e) + ' is not defined'
            raise FatalException(1, err)
Ejemplo n.º 23
0
def server_process_main(options, scmStatus=None):
  # debug mode, including stop Java process at startup
  try:
    set_debug_mode_from_options(options)
  except AttributeError:
    pass

  if not check_reverse_lookup():
    print_warning_msg("The hostname was not found in the reverse DNS lookup. "
                      "This may result in incorrect behavior. "
                      "Please check the DNS setup and fix the issue.")

  check_database_name_property()
  parse_properties_file(options)

  is_active_instance = get_is_active_instance()
  if not is_active_instance:
      print_warning_msg("This instance of ambari server is not designated as active. Cannot start ambari server.")
      err = "This is not an active instance. Shutting down..."
      raise FatalException(1, err)

  ambari_user = read_ambari_user()
  current_user = ensure_can_start_under_current_user(ambari_user)

  print_info_msg("Ambari Server is not running...")

  jdk_path = find_jdk()
  if jdk_path is None:
    err = "No JDK found, please run the \"ambari-server setup\" " \
          "command to install a JDK automatically or install any " \
          "JDK manually to " + configDefaults.JDK_INSTALL_DIR
    raise FatalException(1, err)

  properties = get_ambari_properties()

  # Preparations
  if is_root():
    print configDefaults.MESSAGE_SERVER_RUNNING_AS_ROOT

  ensure_jdbc_driver_is_installed(options, properties)

  ensure_dbms_is_running(options, properties, scmStatus)

  if scmStatus is not None:
    scmStatus.reportStartPending()

  refresh_stack_hash(properties)

  if scmStatus is not None:
    scmStatus.reportStartPending()

  ensure_server_security_is_configured()

  if scmStatus is not None:
    scmStatus.reportStartPending()

  java_exe = get_java_exe_path()

  class_path = get_conf_dir()
  class_path = os.path.abspath(class_path) + os.pathsep + get_ambari_classpath()
  jdbc_driver_path = get_jdbc_driver_path(options, properties)
  if jdbc_driver_path not in class_path:
    class_path = class_path + os.pathsep + jdbc_driver_path

  if SERVER_CLASSPATH_KEY in os.environ:
      class_path =  os.environ[SERVER_CLASSPATH_KEY] + os.pathsep + class_path

  native_libs_path = get_native_libs_path(options, properties)
  if native_libs_path is not None:
    if LIBRARY_PATH_KEY in os.environ:
      native_libs_path = os.environ[LIBRARY_PATH_KEY] + os.pathsep + native_libs_path
    os.environ[LIBRARY_PATH_KEY] = native_libs_path

  debug_mode = get_debug_mode()
  debug_start = (debug_mode & 1) or SERVER_START_DEBUG
  suspend_start = (debug_mode & 2) or SUSPEND_START_MODE
  suspend_mode = 'y' if suspend_start else 'n'

  param_list = generate_child_process_param_list(ambari_user, java_exe,
                                                 class_path, debug_start,
                                                 suspend_mode)
  environ = generate_env(ambari_user, current_user)

  if not os.path.exists(configDefaults.PID_DIR):
    os.makedirs(configDefaults.PID_DIR, 0755)

  print_info_msg("Running server: " + str(param_list))
  procJava = subprocess.Popen(param_list, env=environ)

  pidJava = procJava.pid
  if pidJava <= 0:
    procJava.terminate()
    exitcode = procJava.returncode
    exitfile = os.path.join(configDefaults.PID_DIR, EXITCODE_NAME)
    save_pid(exitcode, exitfile)

    if scmStatus is not None:
      scmStatus.reportStopPending()

    raise FatalException(-1, AMBARI_SERVER_DIE_MSG.format(exitcode, configDefaults.SERVER_OUT_FILE))
  else:
    # Change the group id to the process id of the parent so that the launched
    # process and sub-processes have a group id that is different from the parent.
    try:
      os.setpgid(pidJava, 0)
    except OSError, e:
      print_warning_msg('setpgid({0}, 0) failed - {1}'.format(pidJava, str(e)))
      pass
    pidfile = os.path.join(configDefaults.PID_DIR, PID_NAME)
    save_pid(pidJava, pidfile)
    print "Server PID at: "+pidfile
    print "Server out at: "+configDefaults.SERVER_OUT_FILE
    print "Server log at: "+configDefaults.SERVER_LOG_FILE

    wait_for_server_start(pidfile, scmStatus)
Ejemplo n.º 24
0
def setup_truststore(import_cert=False):
    if not get_silent():
        jdk_path = find_jdk()
        if jdk_path is None:
            err = "No JDK found, please run the \"ambari-server setup\" " \
                  "command to install a JDK automatically or install any " \
                  "JDK manually to " + configDefaults.JDK_INSTALL_DIR
            raise FatalException(1, err)

        properties = get_ambari_properties()

        if get_YN_input("Do you want to configure a truststore [y/n] (y)? ",
                        True):

            #Re-configuration enabled only for option "Setup truststore"
            if not import_cert and properties.get_property(SSL_TRUSTSTORE_TYPE_PROPERTY)\
              and get_YN_input(
                  "The truststore is already configured. Do you want to re-configure "
                  "the truststore [y/n] (y)? ", True):
                properties.removeProp(SSL_TRUSTSTORE_TYPE_PROPERTY)
                properties.removeProp(SSL_TRUSTSTORE_PATH_PROPERTY)
                properties.removeProp(SSL_TRUSTSTORE_PASSWORD_PROPERTY)

            truststore_type = get_and_persist_truststore_type(properties)
            truststore_path = get_and_persist_truststore_path(properties)
            truststore_password = get_and_persist_truststore_password(
                properties)

            if import_cert:

                if get_YN_input(
                        "Do you want to import a certificate [y/n] (y)? ",
                        True):

                    alias = get_validated_string_input(
                        "Please enter an alias for the certificate: ", "",
                        None, None, False, False)

                    run_os_command(
                        get_delete_cert_command(jdk_path, alias,
                                                truststore_path,
                                                truststore_password))

                    import_cert_path = get_validated_filepath_input( \
                        "Enter path to certificate: ", \
                        "Certificate not found")

                    run_component_https_cmd(
                        get_import_cert_command(jdk_path, alias,
                                                truststore_type,
                                                import_cert_path,
                                                truststore_path,
                                                truststore_password))

        else:
            return

        conf_file = find_properties_file()
        f = open(conf_file, 'w')
        properties.store(f,
                         "Changed by 'ambari-server setup-security' command")
    else:
        print "setup-security is not enabled in silent mode."
Ejemplo n.º 25
0
    def download_and_install_jdk(self, args, properties):
        conf_file = properties.fileName

        jcePolicyWarn = "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."

        if args.java_home:
            #java_home was specified among the command-line arguments. Use it as custom JDK location.
            if not validate_jdk(args.java_home):
                err = "Path to java home " + args.java_home + " or java binary file does not exists"
                raise FatalException(1, err)

            print_warning_msg("JAVA_HOME " + args.java_home +
                              " must be valid on ALL hosts")
            print_warning_msg(jcePolicyWarn)
            IS_CUSTOM_JDK = True

            properties.process_pair(JAVA_HOME_PROPERTY, args.java_home)
            properties.removeOldProp(JDK_NAME_PROPERTY)
            properties.removeOldProp(JCE_NAME_PROPERTY)

            self._ensure_java_home_env_var_is_set(args.java_home)
            self.jdk_index = self.custom_jdk_number
            return

        java_home_var = get_JAVA_HOME()
        if OS_FAMILY == OSConst.WINSRV_FAMILY:
            progress_func = None
        else:
            progress_func = download_progress

        if java_home_var:
            change_jdk = get_YN_input(
                "Do you want to change Oracle JDK [y/n] (n)? ", False)
            if not change_jdk:
                self._ensure_java_home_env_var_is_set(java_home_var)
                self.jdk_index = self.custom_jdk_number
                return

        #Continue with the normal setup, taking the first listed JDK version as the default option
        jdk_num = str(self.jdk_index + 1)
        (self.jdks, jdk_choice_prompt, jdk_valid_choices,
         self.custom_jdk_number) = self._populate_jdk_configs(
             properties, jdk_num)

        jdk_num = get_validated_string_input(jdk_choice_prompt, jdk_num,
                                             jdk_valid_choices,
                                             "Invalid number.", False)

        self.jdk_index = int(jdk_num) - 1

        if self.jdk_index == self.custom_jdk_number:
            print_warning_msg(
                "JDK must be installed on all hosts and JAVA_HOME must be valid on all hosts."
            )
            print_warning_msg(jcePolicyWarn)
            args.java_home = get_validated_string_input(
                "Path to JAVA_HOME: ", None, None, None, False, False)
            if not os.path.exists(args.java_home) or not os.path.isfile(
                    os.path.join(args.java_home, "bin", self.JAVA_BIN)):
                err = "Java home path or java binary file is unavailable. Please put correct path to java home."
                raise FatalException(1, err)
            print "Validating JDK on Ambari Server...done."

            properties.process_pair(JAVA_HOME_PROPERTY, args.java_home)
            properties.removeOldProp(JDK_NAME_PROPERTY)
            properties.removeOldProp(JCE_NAME_PROPERTY)

            # Make sure any previously existing JDK and JCE name properties are removed. These will
            # confuse things in a Custom JDK scenario
            properties.removeProp(JDK_NAME_PROPERTY)
            properties.removeProp(JCE_NAME_PROPERTY)

            self._ensure_java_home_env_var_is_set(args.java_home)
            return

        jdk_cfg = self.jdks[self.jdk_index]

        resources_dir = get_resources_location(properties)

        dest_file = os.path.abspath(
            os.path.join(resources_dir, jdk_cfg.dest_file))
        if os.path.exists(dest_file):
            print "JDK already exists, using " + dest_file
        elif properties[JDK_DOWNLOAD_SUPPORTED_PROPERTY].upper() == "FALSE":
            print "ERROR: Oracle JDK is not found in {1}. JDK download is not supported in this distribution. Please download Oracle JDK " \
                  "archive ({0}) manually from Oracle site, place it into {1} and re-run this script.".format(jdk_cfg.dest_file, dest_file)
            print "NOTE: If you have already downloaded the file, please verify if the name is exactly same as {0}.".format(
                jdk_cfg.dest_file)
            print 'Exiting...'
            sys.exit(1)
        else:
            ok = get_YN_input(
                "To download the Oracle JDK and the Java Cryptography Extension (JCE) "
                "Policy Files 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 and you must install the JDK and JCE "
                "files manually.\nDo you accept the "
                "Oracle Binary Code License Agreement [y/n] (y)? ", True)
            if not ok:
                print 'Exiting...'
                sys.exit(1)

            jdk_url = jdk_cfg.url

            print 'Downloading JDK from ' + jdk_url + ' to ' + dest_file
            self._download_jdk(jdk_url, dest_file, progress_func)

        try:
            (retcode, out,
             java_home_dir) = self._install_jdk(dest_file, jdk_cfg)
        except Exception, e:
            print "Installation of JDK has failed: %s\n" % str(e)
            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)? ",
                    not get_silent())
                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:
                    jdk_url = jdk_cfg.url

                    print 'Re-downloading JDK from ' + jdk_url + ' to ' + dest_file
                    self._download_jdk(jdk_url, dest_file, progress_func)
                    print 'Successfully re-downloaded JDK distribution to ' + dest_file

                    try:
                        (retcode, out) = self._install_jdk(dest_file, jdk_cfg)
                    except Exception, e:
                        print "Installation of JDK was failed: %s\n" % str(e)
                        err = "Unable to install JDK. Please remove JDK, file found at " + \
                              dest_file + " and re-run Ambari Server setup"
                        raise FatalException(1, err)
Ejemplo n.º 26
0
  return


#
# Server resource files location
#
def get_resources_location(properties):
  err = 'Invalid directory'
  try:
    resources_dir = properties[RESOURCES_DIR_PROPERTY]
    if not resources_dir:
      resources_dir = configDefaults.SERVER_RESOURCES_DIR
  except (KeyError), e:
    err = 'Property ' + str(e) + ' is not defined at ' + properties.fileName
    resources_dir = configDefaults.SERVER_RESOURCES_DIR

  if not os.path.exists(os.path.abspath(resources_dir)):
    msg = 'Resources dir ' + resources_dir + ' is incorrectly configured: ' + err
    raise FatalException(1, msg)

  return resources_dir

#
# Stack upgrade
#
def get_stack_location(properties):
  stack_location = properties[STACK_LOCATION_KEY]
  if stack_location is None:
    stack_location = configDefaults.STACK_LOCATION_DEFAULT
  return stack_location
Ejemplo n.º 27
0
    try:
        for custom_action_file_name in os.listdir(custom_actions_dir_path):
            custom_action_file_path = os.path.join(custom_actions_dir_path,
                                                   custom_action_file_name)
            if os.path.isfile(custom_action_file_path
                              ) and custom_action_file_path.endswith('.py'):
                print_info_msg(
                    'Moving %s to %s' %
                    (custom_action_file_path, custom_actions_scripts_dir_path))
                shutil.move(custom_action_file_path,
                            custom_actions_scripts_dir_path)
    except (OSError, shutil.Error) as e:
        err = 'Upgrade failed. Can not move *.py files from %s to %s. ' % (
            custom_actions_dir_path, custom_actions_scripts_dir_path) + str(e)
        print_error_msg(err)
        raise FatalException(1, err)


def upgrade(args):
    print_info_msg("Upgrade Ambari Server", True)
    if not is_root():
        err = configDefaults.MESSAGE_ERROR_UPGRADE_NOT_ROOT
        raise FatalException(4, err)
    print_info_msg(
        'Updating Ambari Server properties in {0} ...'.format(
            AMBARI_PROPERTIES_FILE), True)
    retcode = update_ambari_properties()
    if not retcode == 0:
        err = AMBARI_PROPERTIES_FILE + ' file can\'t be updated. Exiting'
        raise FatalException(retcode, err)
Ejemplo n.º 28
0
def encrypt_password(alias, password):
  properties = get_ambari_properties()
  if properties == -1:
    raise FatalException(1, None)
  return get_encrypted_password(alias, password, properties)
Ejemplo n.º 29
0
def set_current(options):
    logger.info("Set current cluster version.")
    server_status, pid = is_server_runing()
    if not server_status:
        err = 'Ambari Server is not running.'
        raise FatalException(1, err)

    finalize_options = SetCurrentVersionOptions(options)

    if finalize_options.no_finalize_options_set():
        err = 'Must specify --cluster-name and --version-display-name. Please invoke ambari-server.py --help to print the options.'
        raise FatalException(1, err)

    admin_login = get_validated_string_input(
        prompt="Enter Ambari Admin login: "******"Enter Ambari Admin password: "******"Failed to read properties file.")

    base_url = get_ambari_server_api_base(properties)
    url = base_url + "clusters/{0}/stack_versions".format(
        finalize_options.cluster_name)
    admin_auth = base64.encodestring(
        '%s:%s' % (admin_login, admin_password)).replace('\n', '')
    request = urllib2.Request(url)
    request.add_header('Authorization', 'Basic %s' % admin_auth)
    request.add_header('X-Requested-By', 'ambari')

    data = {
        "ClusterStackVersions": {
            "repository_version": finalize_options.desired_repo_version,
            "state": "CURRENT",
            "force": finalize_options.force_repo_version
        }
    }

    if get_verbose():
        sys.stdout.write('\nCalling API ' + url + ' : ' + str(data) + '\n')

    request.add_data(json.dumps(data))
    request.get_method = lambda: 'PUT'

    try:
        response = urllib2.urlopen(request)
    except urllib2.HTTPError, e:
        code = e.getcode()
        content = e.read()
        err = 'Error during setting current version. Http status code - {0}. \n {1}'.format(
            code, content)
        raise FatalException(1, err)
Ejemplo n.º 30
0
def server_process_main(options, scmStatus=None):
    if scmStatus is not None:
        scmStatus.reportStartPending()

    # debug mode
    try:
        global DEBUG_MODE
        DEBUG_MODE = options.debug
    except AttributeError:
        pass

    # stop Java process at startup?
    try:
        global SUSPEND_START_MODE
        SUSPEND_START_MODE = options.suspend_start
    except AttributeError:
        pass

    #options.conf_dir <= --config
    if not os.path.isdir(options.conf_dir):
        err = 'ERROR: Cannot find configuration directory "{0}"'.format(
            options.conf_dir)
        raise FatalException(1, err)

    #execute ams-env.cmd
    exec_ams_env_cmd(options)

    #Ensure the 3 Hadoop services required are started on the local machine
    if not options.no_embedded_hbase:
        from amc_service import ensure_hdp_service_soft_dependencies
        ensure_hdp_service_soft_dependencies()

    if scmStatus is not None:
        scmStatus.reportStartPending()

    java_exe = get_java_exe_path()
    java_class_path = get_java_cp()
    java_heap_max = build_jvm_args()
    command_base = SERVER_START_CMD_DEBUG if (
        DEBUG_MODE or SERVER_START_DEBUG) else SERVER_START_CMD
    suspend_mode = 'y' if SUSPEND_START_MODE else 'n'
    command = command_base.format(java_class_path, java_heap_max, suspend_mode)
    if not os.path.exists(PID_DIR):
        os.makedirs(PID_DIR, 0755)

    #Ignore the requirement to run as root. In Windows, by default the child process inherits the security context
    # and the environment from the parent process.
    param_list = java_exe + " " + command

    print_info_msg("Running server: " + str(param_list))
    procJava = subprocess.Popen(param_list, env=os.environ)

    #wait for server process for SERVER_START_TIMEOUT seconds
    print "Waiting for server start..."

    pidJava = procJava.pid
    if pidJava <= 0:
        procJava.terminate()
        exitcode = procJava.returncode
        save_pid(exitcode, EXITCODE_OUT_FILE)

        if scmStatus is not None:
            scmStatus.reportStopPending()

        raise FatalException(-1, AMC_DIE_MSG.format(exitcode, SERVER_OUT_FILE))
    else:
        save_pid(pidJava, PID_OUT_FILE)
        print "Server PID at: " + PID_OUT_FILE
        print "Server out at: " + SERVER_OUT_FILE
        print "Server log at: " + SERVER_LOG_FILE

    if scmStatus is not None:
        scmStatus.reportStarted()

    return procJava