Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions ambari-server/src/main/python/ambari-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,11 @@ def setup_security(args):
iAction = 0
for actionDesc in actions:
iAction += 1
print(' [{0}] {1}'.format(iAction, actionDesc[1]))
print(f' [{iAction}] {actionDesc[1]}')
print('=' * 75)

choice_prompt = 'Enter choice, (1-{0}): '.format(iAction)
choice_re = '[1-{0}]'.format(iAction)
choice_prompt = f'Enter choice, (1-{iAction}): '
choice_re = f'[1-{iAction}]'
choice = get_validated_string_input(choice_prompt, '0', choice_re,
'Invalid choice', False, False)

Expand Down Expand Up @@ -358,13 +358,11 @@ def print_action_arguments_help(action):
required_options = _action_option_dependence_map[action][0]
optional_options = _action_option_dependence_map[action][1]
if required_options or optional_options:
print("Options used by action {0}:".format(action))
print(f"Options used by action {action}:")
if required_options:
print(" required:{0}".format(
";".join([print_opt for print_opt, _ in required_options])))
print(f" required:{';'.join([print_opt for print_opt, _ in required_options])}")
if optional_options:
print(" optional:{0}".format(
";".join([print_opt for print_opt, _ in optional_options])))
print(f" optional:{';'.join([print_opt for print_opt, _ in optional_options])}")

@OsFamilyFuncImpl(OSConst.WINSRV_FAMILY)
def init_action_parser(action, parser):
Expand Down Expand Up @@ -439,7 +437,7 @@ def init_action_parser(action, parser):
default_purge_resources = ",".join([STACK_DEFINITIONS_RESOURCE_NAME, MPACKS_RESOURCE_NAME])
add_parser_options('--purge-list',
default=default_purge_resources,
help="Comma separated list of resources to purge ({0}). By default ({1}) will be purged.".format(purge_resources, default_purge_resources),
help=f"Comma separated list of resources to purge ({purge_resources}). By default ({default_purge_resources}) will be purged.",
dest="purge_list",
parser=parser,
optional_for_actions=[INSTALL_MPACK_ACTION]
Expand Down Expand Up @@ -560,7 +558,7 @@ def init_ldap_setup_parser_options(parser):
parser.add_option('--ldap-secondary-host', action="callback", callback=check_ldap_url_options, type='str', default=None, help="Secondary Host for LDAP (must not be used together with --ldap-secondary-url)", dest="ldap_secondary_host")
parser.add_option('--ldap-secondary-port', action="callback", callback=check_ldap_url_options, type='int', default=None, help="Secondary Port for LDAP (must not be used together with --ldap-secondary-url)", dest="ldap_secondary_port")
parser.add_option('--ldap-ssl', default=None, help="Use SSL [true/false] for LDAP", dest="ldap_ssl")
parser.add_option('--ldap-type', default=None, help="Specify ldap type [{}] for offering defaults for missing options.".format("/".join(LDAP_TYPES)), dest="ldap_type")
parser.add_option('--ldap-type', default=None, help=f"Specify ldap type [{'/'.join(LDAP_TYPES)}] for offering defaults for missing options.", dest="ldap_type")
parser.add_option('--ldap-user-class', default=None, help="User Attribute Object Class for LDAP", dest="ldap_user_class")
parser.add_option('--ldap-user-attr', default=None, help="User Attribute Name for LDAP", dest="ldap_user_attr")
parser.add_option('--ldap-user-group-member-attr', default=None, help="User Group Member Attribute for LDAP", dest="ldap_user_group_member_attr")
Expand Down Expand Up @@ -683,7 +681,7 @@ def init_install_mpack_parser_options(parser):
default_purge_resources = ",".join([STACK_DEFINITIONS_RESOURCE_NAME, MPACKS_RESOURCE_NAME])

parser.add_option('--purge-list', default=default_purge_resources,
help="Comma separated list of resources to purge ({0}). By default ({1}) will be purged.".format(purge_resources, default_purge_resources),
help=f"Comma separated list of resources to purge ({purge_resources}). By default ({default_purge_resources}) will be purged.",
dest="purge_list")
parser.add_option('--force', action="store_true", default=False, help="Force install management pack", dest="force")

Expand Down Expand Up @@ -924,7 +922,7 @@ def setup_logging(logger, filename, logging_level):

logging.basicConfig(format=formatstr, level=logging_level, filename=filename)
logger.setLevel(logging_level)
logger.info("loglevel=logging.{0}".format(logging._levelToName[logging_level]))
logger.info(f"loglevel=logging.{logging._levelToName[logging_level]}")

def init_logging():
# init logger
Expand Down Expand Up @@ -999,15 +997,15 @@ def main(options, args, parser):
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_message = f"Ambari Server '{action}' completed successfully."
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(f"Missing option {opt_str} for action {action}")
print_action_arguments_help(action)
print("Run ambari-server.py --help to see detailed description of each option")
raise FatalException(1, "Missing option")
Expand All @@ -1023,15 +1021,15 @@ def main(options, args, parser):
for warning in options.warnings:
print_warning_msg(warning)
pass
options.exit_message = "Ambari Server '%s' completed with warnings." % action
options.exit_message = f"Ambari Server '{action}' completed with warnings."
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))
print_error_msg(f"Exiting with exit code {e.code}. \nREASON: {e.reason}")
logger.exception(str(e))
sys.exit(e.code)
except NonFatalException as e:
options.exit_message = "Ambari Server '%s' completed with warnings." % action
options.exit_message = f"Ambari Server '{action}' completed with warnings."
if e.reason is not None:
print_warning_msg(e.reason)

Expand Down Expand Up @@ -1069,7 +1067,7 @@ def mainBody():
try:
main(options, args, parser)
except Exception as e:
print_error_msg("Unexpected {0}: {1}".format((e).__class__.__name__, str(e)) +\
print_error_msg(f"Unexpected {e.__class__.__name__}: {str(e)}" +\
"\nFor more info run ambari-server with -v or --verbose option")
sys.exit(1)

Expand Down
2 changes: 1 addition & 1 deletion ambari-server/src/main/python/ambari_server/dbCleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,5 @@ def validate_args(options):
try:
datetime.datetime.strptime(options.purge_from_date, "%Y-%m-%d")
except ValueError as e:
print_error_msg("The --from-date argument has an invalid format. {0}".format(e.args[0]))
print_error_msg(f"The --from-date argument has an invalid format. {e.args[0]}")
return 1
12 changes: 6 additions & 6 deletions ambari-server/src/main/python/ambari_server/dbConfiguration.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def configure_database(self, properties, options):
return result

def setup_database(self):
print('Configuring {0} database...'.format(self.db_title))
print(f'Configuring {self.db_title} database...')

#DB setup should be done last after doing any setup.
if self._is_local_database():
Expand Down Expand Up @@ -175,7 +175,7 @@ def ensure_jdbc_driver_installed(self, properties):
# 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)
ambari_should_use_existing_default_jdbc = get_YN_input(f"Should ambari use existing default jdbc {default_driver_path} [y/n] (y)? ", True)
if ambari_should_use_existing_default_jdbc:
properties.process_pair(JDBC_DRIVER_PATH_PROPERTY, default_driver_path)
update_properties(properties)
Expand All @@ -190,15 +190,15 @@ def ensure_jdbc_driver_installed(self, properties):
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)
replace_jdbc_in_share_dir = get_YN_input(f"You already have file {custom_jdbc_name} in /usr/share/java/. Should it be replaced? [y/n] (y)? ", True)
if replace_jdbc_in_share_dir:
try:
os.remove(os.path.join(configDefaults.JAVA_SHARE_PATH, custom_jdbc_name))
except Exception as ee:
err = 'ERROR: Could not remove jdbc file. %s' % os.path.join(configDefaults.JAVA_SHARE_PATH, custom_jdbc_name)
err = f'ERROR: Could not remove jdbc file. {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))
print(f"Copying {path_to_custom_jdbc_driver} to {configDefaults.JAVA_SHARE_PATH}")
except Exception as 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))
Expand Down Expand Up @@ -465,7 +465,7 @@ def select_dbms(self, options):
dbms_choices = ''
for desc in self.DBMS_LIST:
if len(desc.storage_name) > 0:
dbms_storage = " ({0})".format(desc.storage_name)
dbms_storage = f" ({desc.storage_name})"
else:
dbms_storage = ""
dbms_choice_prompt += self.DBMS_PROMPT_PATTERN.format(n_dbms, desc.dbms_name, dbms_storage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ class PGConfig(LinuxDBMSConfig):
versioned_script_path = glob.glob("/usr/pgsql-*/bin/postgresql*-setup")
# versioned version of psql
if versioned_script_path:
PG_INITDB_CMD = "{0} initdb".format(versioned_script_path[0])
PG_INITDB_CMD = f"{versioned_script_path[0]} initdb"

psql_service_file = glob.glob("/usr/lib/systemd/system/postgresql-*.service")
if psql_service_file:
Expand All @@ -378,36 +378,36 @@ class PGConfig(LinuxDBMSConfig):
raise FatalException(1, "Cannot find postgresql-setup script.")

SERVICE_CMD = "/usr/bin/env systemctl"
PG_ST_CMD = "%s status %s" % (SERVICE_CMD, PG_SERVICE_NAME)
PG_ST_CMD = f"{SERVICE_CMD} status {PG_SERVICE_NAME}"

PG_START_CMD = AMBARI_SUDO_BINARY + " %s start %s" % (SERVICE_CMD, PG_SERVICE_NAME)
PG_RESTART_CMD = AMBARI_SUDO_BINARY + " %s restart %s" % (SERVICE_CMD, PG_SERVICE_NAME)
PG_HBA_RELOAD_CMD = AMBARI_SUDO_BINARY + " %s reload %s" % (SERVICE_CMD, PG_SERVICE_NAME)
PG_START_CMD = AMBARI_SUDO_BINARY + f" {SERVICE_CMD} start {PG_SERVICE_NAME}"
PG_RESTART_CMD = AMBARI_SUDO_BINARY + f" {SERVICE_CMD} restart {PG_SERVICE_NAME}"
PG_HBA_RELOAD_CMD = AMBARI_SUDO_BINARY + f" {SERVICE_CMD} reload {PG_SERVICE_NAME}"
else:
SERVICE_CMD = "/usr/bin/env service"
if os.path.isfile("/usr/bin/postgresql-setup"):
PG_INITDB_CMD = "/usr/bin/postgresql-setup initdb"
else:
PG_INITDB_CMD = "%s %s initdb" % (SERVICE_CMD, PG_SERVICE_NAME)
PG_INITDB_CMD = f"{SERVICE_CMD} {PG_SERVICE_NAME} initdb"

if OSCheck.is_suse_family() and not is_service_exist(PG_SERVICE_NAME):
versioned_script_paths = glob.glob("/usr/pgsql-*/bin/postgresql*-setup")
if versioned_script_paths:
versioned_script_path_tps = [(re.search(r'postgresql-([0-9]+\.?[0-9]*)', path).group(1), path) for path in versioned_script_paths]
versioned_script_path_tps.sort(key = lambda t: float(t[0]), reverse = True)
for versioned_script_path_tp in versioned_script_path_tps:
pgsql_service_file_name = "postgresql-%s" % versioned_script_path_tp[0]
pgsql_service_file_name = f"postgresql-{versioned_script_path_tp[0]}"
if is_service_exist(pgsql_service_file_name):
PG_SERVICE_NAME = pgsql_service_file_name
PG_INITDB_CMD = "%s initdb" % versioned_script_path_tp[1]
PG_HBA_DIR = "/var/lib/pgsql/%s/data" % versioned_script_path_tp[0]
PG_INITDB_CMD = f"{versioned_script_path_tp[1]} initdb"
PG_HBA_DIR = f"/var/lib/pgsql/{versioned_script_path_tp[0]}/data"
break

PG_ST_CMD = "%s %s status" % (SERVICE_CMD, PG_SERVICE_NAME)
PG_ST_CMD = f"{SERVICE_CMD} {PG_SERVICE_NAME} status"

PG_START_CMD = AMBARI_SUDO_BINARY + " %s %s start" % (SERVICE_CMD, PG_SERVICE_NAME)
PG_RESTART_CMD = AMBARI_SUDO_BINARY + " %s %s restart" % (SERVICE_CMD, PG_SERVICE_NAME)
PG_HBA_RELOAD_CMD = AMBARI_SUDO_BINARY + " %s %s reload" % (SERVICE_CMD, PG_SERVICE_NAME)
PG_START_CMD = AMBARI_SUDO_BINARY + f" {SERVICE_CMD} {PG_SERVICE_NAME} start"
PG_RESTART_CMD = AMBARI_SUDO_BINARY + f" {SERVICE_CMD} {PG_SERVICE_NAME} restart"
PG_HBA_RELOAD_CMD = AMBARI_SUDO_BINARY + f" {SERVICE_CMD} {PG_SERVICE_NAME} reload"

PG_HBA_CONF_FILE = None
PG_HBA_CONF_FILE_BACKUP = None
Expand Down Expand Up @@ -473,7 +473,7 @@ def ensure_dbms_is_running(self, options, properties, scmStatus=None):
if is_root():
(pg_status, retcode, out, err) = PGConfig._check_postgre_up()
if not retcode == 0:
err = 'Unable to start PostgreSQL server. Status {0}. {1}. Exiting'.format(pg_status, err)
err = f'Unable to start PostgreSQL server. Status {pg_status}. {err}. Exiting'
raise FatalException(retcode, err)
else:
print("Unable to check PostgreSQL server status when starting " \
Expand Down Expand Up @@ -539,7 +539,7 @@ def _reset_local_database(self):
default = "no"

# Run automatic reset only for embedded DB
okToRun = get_YN_input("Confirm server reset [yes/no]({0})? ".format(default), get_silent())
okToRun = get_YN_input(f"Confirm server reset [yes/no]({default})? ", get_silent())
if not okToRun:
err = "Ambari Server 'reset' cancelled"
raise FatalException(1, err)
Expand Down Expand Up @@ -687,7 +687,7 @@ def _check_postgre_up():
out, err = process.communicate()
retcode = process.returncode

print_info_msg("Waiting for postgres to start at port {0}...".format(PG_PORT))
print_info_msg(f"Waiting for postgres to start at port {PG_PORT}...")
wait_for_port_opened('127.0.0.1', PG_PORT, PG_PORT_CHECK_TRIES_COUNT, PG_PORT_CHECK_INTERVAL)

pg_status, retcode, out, err = PGConfig._get_postgre_status()
Expand Down Expand Up @@ -893,7 +893,7 @@ def _change_tables_owner(self):
"\" OWNER TO \""+self.database_username+"\"",
self.database_name, False)
if retcode != 0 or "ALTER TABLE" not in stdout:
print_error_msg("Failed to change owner of table:{0} to user:{1}".format(tbl, owner))
print_error_msg(f"Failed to change owner of table:{tbl} to user:{owner}")
return False

return True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def _configure_database_password(showDefault=True):
def _prompt_db_properties(self):
if self.must_set_database_options:
#prompt for SQL Server host and instance name
hostname_prompt = "SQL Server host and instance for the {0} database: ({1}) ".format(self.db_title, self.database_host)
hostname_prompt = f"SQL Server host and instance for the {self.db_title} database: ({self.database_host}) "
self.database_host = get_validated_string_input(hostname_prompt, self.database_host, None, None, False, True)

#prompt for SQL Server authentication method
Expand All @@ -135,12 +135,12 @@ def _prompt_db_properties(self):
else:
self.use_windows_authentication = False

user_prompt = "SQL Server user name for the {0} database: ({1}) ".format(self.db_title, self.database_username)
user_prompt = f"SQL Server user name for the {self.db_title} database: ({self.database_username}) "
username = get_validated_string_input(user_prompt, self.database_username, None, "User name", False,
False)
self.database_username = username

user_prompt = "SQL Server password for the {0} database: ".format(self.db_title)
user_prompt = f"SQL Server password for the {self.db_title} database: "
password = get_validated_string_input(user_prompt, "", None, "Password", True, False)
self.database_password = password

Expand Down Expand Up @@ -170,12 +170,12 @@ def _setup_remote_server(self, properties, options):
pass

def _setup_remote_database(self):
print('Populating the {0} database structure...'.format(self.db_title))
print(f'Populating the {self.db_title} database structure...')

self._populate_database_structure()

def _reset_remote_database(self):
print('Resetting the {0} database structure...'.format(self.db_title))
print(f'Resetting the {self.db_title} database structure...')

self._populate_database_structure()

Expand Down Expand Up @@ -253,10 +253,10 @@ def _get_jdbc_driver_path(self):
return driver_path

def _build_sql_server_connection_string(self):
databaseUrl = "jdbc:sqlserver://{0}".format(ensure_double_backslashes(self.database_host))
databaseUrl = f"jdbc:sqlserver://{ensure_double_backslashes(self.database_host)}"
if self.database_port is not None and self.database_port != "":
databaseUrl += ":{0}".format(self.database_port)
databaseUrl += ";databaseName={0}".format(self.database_name)
databaseUrl += f":{self.database_port}"
databaseUrl += f";databaseName={self.database_name}"
if(self.use_windows_authentication):
databaseUrl += ";integratedSecurity=true"
#No need to append the username and password, the Ambari server adds them by itself when connecting to the database
Expand Down Expand Up @@ -293,10 +293,10 @@ def _populate_database_structure(self):

@staticmethod
def _execute_db_script(databaseHost, databaseScript, minReportedSeverityLevel=10):
dbCmd = 'sqlcmd -S {0} -b -V {1} -i {2}'.format(databaseHost, minReportedSeverityLevel, databaseScript)
dbCmd = f'sqlcmd -S {databaseHost} -b -V {minReportedSeverityLevel} -i {databaseScript}'
retCode, outData, errData = run_os_command(['cmd', '/C', dbCmd])
if not retCode == 0:
err = 'Running database create script failed. Error output: {0} Output: {1} Exiting.'.format(errData, outData)
err = f'Running database create script failed. Error output: {errData} Output: {outData} Exiting.'
raise FatalException(retCode, err)
print_info_msg("sqlcmd output:")
print_info_msg(outData)
Expand Down
2 changes: 1 addition & 1 deletion ambari-server/src/main/python/ambari_server/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def __getattr__(self, name):
if hasattr(self._props, name):
return getattr(self._props, name)
else:
raise NotImplementedError("The method '{}' is not implemented.".format(name))
raise NotImplementedError(f"The method '{name}' is not implemented.")

def __contains__(self, key):
return key in self._props
Expand Down
Loading