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
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@ def get_metric_collectors_from_properties_file(sink_name):
try:
hadoop_conf_dir = conf_select.get_hadoop_conf_dir()
except Exception as e:
raise Exception("Couldn't define hadoop_conf_dir: {0}".format(e))
raise Exception(f"Couldn't define hadoop_conf_dir: {e}")
properties_filepath = os.path.join(hadoop_conf_dir, DEFAULT_METRICS2_PROPERTIES_FILE_NAME)

if not os.path.exists(properties_filepath):
raise Exception("Properties file doesn't exist : {0}. Can't define metric collector hosts".format(properties_filepath))
raise Exception(f"Properties file doesn't exist : {properties_filepath}. Can't define metric collector hosts")
props = load_properties_from_file(properties_filepath)

property_key = sink_name + DEFAULT_COLLECTOR_SUFFIX
if property_key in props:
return props.get(property_key)
else:
raise Exception("Properties file doesn't contain {0}. Can't define metric collector hosts".format(property_key))
raise Exception(f"Properties file doesn't contain {property_key}. Can't define metric collector hosts")

def load_properties_from_file(filepath, sep='=', comment_char='#'):
"""
Expand Down Expand Up @@ -105,12 +105,11 @@ def create_ams_client(alert_id, ams_app_id, configurations, parameters):
else:
# ams-site/timeline.metrics.service.webapp.address is required
if not METRICS_COLLECTOR_WEBAPP_ADDRESS_KEY in configurations:
raise Exception('{0} is a required parameter for the script'.format(METRICS_COLLECTOR_WEBAPP_ADDRESS_KEY))
raise Exception(f'{METRICS_COLLECTOR_WEBAPP_ADDRESS_KEY} is a required parameter for the script')

collector_webapp_address = configurations[METRICS_COLLECTOR_WEBAPP_ADDRESS_KEY].split(":")
if not _valid_collector_webapp_address(collector_webapp_address):
raise Exception('{0} value should be set as "fqdn_hostname:port", but set to {1}'.format(
METRICS_COLLECTOR_WEBAPP_ADDRESS_KEY, configurations[METRICS_COLLECTOR_WEBAPP_ADDRESS_KEY]))
raise Exception(f'{METRICS_COLLECTOR_WEBAPP_ADDRESS_KEY} value should be set as "fqdn_hostname:port", but set to {configurations[METRICS_COLLECTOR_WEBAPP_ADDRESS_KEY]}')

ams_collector_hosts = default("/clusterHostInfo/metrics_collector_hosts", [])
if not ams_collector_hosts:
Expand Down Expand Up @@ -154,7 +153,7 @@ def load_metric(self, ams_metric, host_filter):
break
except Exception as exception:
if logger.isEnabledFor(logging.DEBUG):
logger.exception("[Alert][{0}] Unable to retrieve metrics from AMS ({1}:{2}): {3}".format(self.alert_id, ams_collector_host, self.ams_collector_port, str(exception)))
logger.exception(f"[Alert][{self.alert_id}] Unable to retrieve metrics from AMS ({ams_collector_host}:{self.ams_collector_port}): {str(exception)}")

if not http_code:
raise Exception("Ambari metrics is not available: no response")
Expand Down Expand Up @@ -199,29 +198,28 @@ def _load_metric(self, ams_collector_host, ams_metric, host_filter):
data = response.read()
except Exception as exception:
if logger.isEnabledFor(logging.DEBUG):
logger.exception("[Alert][{0}] Unable to retrieve metrics from AMS: {1}".format(self.alert_id, str(exception)))
logger.exception(f"[Alert][{self.alert_id}] Unable to retrieve metrics from AMS: {str(exception)}")
status = response.status if response else None
return None, status
finally:
if logger.isEnabledFor(logging.DEBUG):
logger.debug("""
AMS request parameters - {0}
AMS response - {1}
""".format(encoded_get_metrics_parameters, data))
logger.debug(f"""
AMS request parameters - {encoded_get_metrics_parameters}
AMS response - {data}
""")
# explicitly close the connection as we've seen python hold onto these
if conn is not None:
try:
conn.close()
except:
logger.debug("[Alert][{0}] Unable to close URL connection to {1}".format(self.alert_id, url))
logger.debug(f"[Alert][{self.alert_id}] Unable to close URL connection to {url}")

data_json = None
try:
data_json = json.loads(data)
except Exception as exception:
if logger.isEnabledFor(logging.DEBUG):
logger.exception("[Alert][{0}] Convert response to json failed or json doesn't contain needed data: {1}".
format(self.alert_id, str(exception)))
logger.exception(f"[Alert][{self.alert_id}] Convert response to json failed or json doesn't contain needed data: {str(exception)}")

if not data_json:
return None, response.status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get_password_from_credential_store(alias, provider_path, cs_lib_path, java_h
downloadjar(cs_lib_path, jdk_location)

# Execute a get command on the CredentialUtil CLI to get the password for the specified alias
java_bin = '{java_home}/bin/java'.format(java_home=java_home)
java_bin = f'{java_home}/bin/java'
cmd = (java_bin, '-cp', cs_lib_path, credential_util_cmd, 'get', alias, '-provider', provider_path)
cmd_result, std_out_msg = checked_call(cmd, quiet=True)
std_out_lines = std_out_msg.split('\n')
Expand All @@ -59,7 +59,7 @@ def list_aliases_from_credential_store(provider_path, cs_lib_path, java_home, jd
downloadjar(cs_lib_path, jdk_location)

# Execute a get command on the CredentialUtil CLI to list all the aliases
java_bin = '{java_home}/bin/java'.format(java_home=java_home)
java_bin = f'{java_home}/bin/java'
cmd = (java_bin, '-cp', cs_lib_path, credential_util_cmd, 'list', '-provider', provider_path)
cmd_result, std_out_msg = checked_call(cmd, quiet=True)
std_out_lines = std_out_msg.split('\n')
Expand All @@ -70,7 +70,7 @@ def delete_alias_from_credential_store(alias, provider_path, cs_lib_path, java_h
downloadjar(cs_lib_path, jdk_location)

#Execute the creation and overwrite password
java_bin = '{java_home}/bin/java'.format(java_home=java_home)
java_bin = f'{java_home}/bin/java'
cmd = (java_bin, '-cp', cs_lib_path, credential_util_cmd, 'delete', alias, '-provider', provider_path, '-f')
Execute(cmd)

Expand All @@ -79,6 +79,6 @@ def create_password_in_credential_store(alias, provider_path, cs_lib_path, java_
downloadjar(cs_lib_path, jdk_location)

#Execute the creation and overwrite password
java_bin = '{java_home}/bin/java'.format(java_home=java_home)
java_bin = f'{java_home}/bin/java'
cmd = (java_bin, '-cp', cs_lib_path, credential_util_cmd, 'create', alias, '-value', PasswordString(password) ,'-provider', provider_path, '-f')
Execute(cmd)
7 changes: 3 additions & 4 deletions ambari-common/src/main/python/ambari_commons/firewall.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def run_command(self):
self.stdoutdata = out
self.stderrdata = err
except Exception as ex:
print_warning_msg("Unable to check firewall status: {0}".format(ex))
print_warning_msg(f"Unable to check firewall status: {ex}")

def check_firewall(self):
try:
Expand Down Expand Up @@ -188,7 +188,7 @@ def run_command(self):

def check_result(self):
if self.returncode != 0:
print_warning_msg("Unable to check firewall status:{0}".format(self.stderrdata))
print_warning_msg(f"Unable to check firewall status:{self.stderrdata}")
return False
profiles_status = [i for i in self.stdoutdata.split("\n") if not i == ""]
if "1" in profiles_status:
Expand All @@ -200,7 +200,6 @@ def check_result(self):
if profiles_status[2] == "1":
enabled_profiles.append("PublicProfile")
print_warning_msg(
"Following firewall profiles are enabled:{0}. Make sure that the firewall is properly configured.".format(
",".join(enabled_profiles)))
f'Following firewall profiles are enabled:{",".join(enabled_profiles)}. Make sure that the firewall is properly configured.')
return True
return False
26 changes: 13 additions & 13 deletions ambari-common/src/main/python/ambari_commons/inet_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,35 +60,35 @@ def openurl(url, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, *args, **kwargs):


def download_file(link, destination, chunk_size=16 * 1024, progress_func = None):
print_info_msg("Downloading {0} to {1}".format(link, destination))
print_info_msg(f"Downloading {link} to {destination}")
if os.path.exists(destination):
print_warning_msg("File {0} already exists, assuming it was downloaded before".format(destination))
print_warning_msg(f"File {destination} already exists, assuming it was downloaded before")
return

force_download_file(link, destination, chunk_size, progress_func = progress_func)


def download_file_anyway(link, destination, chunk_size=16 * 1024, progress_func = None):
print_info_msg("Trying to download {0} to {1} with python lib [urllib2].".format(link, destination))
print_info_msg(f"Trying to download {link} to {destination} with python lib [urllib2].")
if os.path.exists(destination):
print_warning_msg("File {0} already exists, assuming it was downloaded before".format(destination))
print_warning_msg(f"File {destination} already exists, assuming it was downloaded before")
return
try:
force_download_file(link, destination, chunk_size, progress_func = progress_func)
except:
print_error_msg("Download {0} with python lib [urllib2] failed with error: {1}".format(link, str(sys.exc_info())))
print_error_msg(f"Download {link} with python lib [urllib2] failed with error: {str(sys.exc_info())}")

if not os.path.exists(destination):
print("Trying to download {0} to {1} with [curl] command.".format(link, destination))
#print_info_msg("Trying to download {0} to {1} with [curl] command.".format(link, destination))
print(f"Trying to download {link} to {destination} with [curl] command.")
#print_info_msg(f"Trying to download {link} to {destination} with [curl] command.")
curl_command = "curl --fail -k -o %s %s" % (destination, link)
retcode, out, err = os_run_os_command(curl_command)
if retcode != 0:
print_error_msg("Download file {0} with [curl] command failed with error: {1}".format(link, out + err))
print_error_msg(f"Download file {link} with [curl] command failed with error: {out + err}")


if not os.path.exists(destination):
print_error_msg("Unable to download file {0}!".format(link))
print_error_msg(f"Unable to download file {link}!")
print("ERROR: unable to donwload file %s!" % (link))


Expand Down Expand Up @@ -125,7 +125,7 @@ def find_range_components(meta):
if len(range_comp1) > 1:
range_comp2 = range_comp1[0].split(' ') #split away the "bytes" prefix
if len(range_comp2) == 0:
raise FatalException(12, 'Malformed Content-Range response header: "{0}".'.format(hdr_range))
raise FatalException(12, f'Malformed Content-Range response header: "{hdr_range}".')
range_comp3 = range_comp2[1].split('-')
seek_pos = int(range_comp3[0])
if range_comp1[1] != '*': #'*' == unknown length
Expand All @@ -146,7 +146,7 @@ def force_download_file(link, destination, chunk_size = 16 * 1024, progress_func

if os.path.exists(destination) and not os.path.isfile(destination):
#Directory specified as target? Must be a mistake. Bail out, don't assume anything.
err = 'Download target {0} is a directory.'.format(destination)
err = f'Download target {destination} is a directory.'
raise FatalException(1, err)

(dest_path, file_name) = os.path.split(destination)
Expand Down Expand Up @@ -203,11 +203,11 @@ def force_download_file(link, destination, chunk_size = 16 * 1024, progress_func
sys.stdout.write('\n')
sys.stdout.flush()

print_info_msg("Finished downloading {0} to {1}".format(link, destination))
print_info_msg(f"Finished downloading {link} to {destination}")

downloaded_size = os.stat(temp_dest).st_size
if downloaded_size != file_size:
err = 'Size of downloaded file {0} is {1} bytes, it is probably damaged or incomplete'.format(destination, downloaded_size)
err = f'Size of downloaded file {destination} is {downloaded_size} bytes, it is probably damaged or incomplete'
raise FatalException(1, err)

# when download is complete -> mv temp_dest destination
Expand Down
3 changes: 1 addition & 2 deletions ambari-common/src/main/python/ambari_commons/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ def check_ssl_certificate_and_return_ssl_version(host, port, ca_certs, ssl_versi
try:
ssl.get_server_certificate((host, port), ssl_version=ssl_version, ca_certs=ca_certs)
except ssl.SSLError as ssl_error:
raise Fail("Failed to verify the SSL certificate for https://{0}:{1} with CA certificate in {2}. Error : {3}"
.format(host, port, ca_certs, str(ssl_error)))
raise Fail(f"Failed to verify the SSL certificate for https://{host}:{port} with CA certificate in {ca_certs}. Error : {str(ssl_error)}")
return ssl_version


Expand Down
4 changes: 2 additions & 2 deletions ambari-common/src/main/python/ambari_commons/os_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def initialize_data(cls):
f.close()

if JSON_OS_MAPPING not in json_data:
raise Exception("Invalid {0}".format(OSFAMILY_JSON_RESOURCE))
raise Exception(f"Invalid {OSFAMILY_JSON_RESOURCE}")

json_mapping_data = json_data[JSON_OS_MAPPING]

Expand Down Expand Up @@ -188,7 +188,7 @@ def os_distribution():
release = REL_2012
elif minor == 3:
release = REL_2012R2
distribution = (release, "{0}.{1}".format(major,minor),"WindowsServer")
distribution = (release, f"{major}.{minor}","WindowsServer")
else:
# we are on unsupported desktop os
distribution = ("", "", "")
Expand Down
6 changes: 3 additions & 3 deletions ambari-common/src/main/python/ambari_commons/os_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def copy_file(src, dest_file):
try:
shutil.copyfile(src, dest_file)
except Exception as e:
err = "Can not copy file {0} to {1} due to: {2} . Please check file " \
"permissions and free disk space.".format(src, dest_file, e)
err = f"Can not copy file {src} to {dest_file} due to: {e} . Please check file " \
"permissions and free disk space."
raise FatalException(1, err)

def copy_files(files, dest_dir):
Expand Down Expand Up @@ -142,7 +142,7 @@ def is_service_exist(serviceName):
def find_in_path(file):
full_path = _search_file(file, os.environ["PATH"], os.pathsep)
if full_path is None:
raise Exception("File {0} not found in PATH".format(file))
raise Exception(f"File {file} not found in PATH")
return full_path

def extract_path_component(path, path_fragment):
Expand Down
10 changes: 5 additions & 5 deletions ambari-common/src/main/python/ambari_commons/os_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def run_powershell_script(script_content):
script_file = open(os.path.join(tmp_dir,random_filename+".ps1"),"w")
script_file.write(script_content)
script_file.close()
result = os_run_os_command("powershell -ExecutionPolicy unrestricted -File {0}".format(script_file.name))
result = os_run_os_command(f"powershell -ExecutionPolicy unrestricted -File {script_file.name}")
os.remove(script_file.name)
return result

Expand All @@ -378,8 +378,8 @@ def os_is_root():
'''
retcode, out, err = os_run_os_command(WHOAMI_GROUPS)
if retcode != 0:
err_msg = "Unable to check the current user's group memberships. " \
"Command {0} returned exit code {1} with message: {2}".format(WHOAMI_GROUPS, retcode, err)
err_msg = f"Unable to check the current user's group memberships. " \
f"Command {WHOAMI_GROUPS} returned exit code {retcode} with message: {err}"
print_warning_msg(err_msg)
raise FatalException(retcode, err_msg)

Expand Down Expand Up @@ -407,10 +407,10 @@ def os_set_file_permissions(file, mod, recursive, user):
# print_warning_msg(WARN_MSG.format(command, file, err))

# rights = mod
# acls_remove_cmd = "icacls {0} /remove {1}".format(file, user)
# acls_remove_cmd = f"icacls {file} /remove {user}"
# retcode, out, err = os_run_os_command(acls_remove_cmd)
# if retcode == 0:
# acls_modify_cmd = "icacls {0} /grant {1}:{2}".format(file, user, rights)
# acls_modify_cmd = f"icacls {file} /grant {user}:{rights}"
# retcode, out, err = os_run_os_command(acls_modify_cmd)
return retcode

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ def get_new_instance(cls, os_family=None):
if OSCheck.is_in_family(os_family, OSConst.WINSRV_FAMILY):
return ChocoManager()

raise RuntimeError("Not able to create Repository Manager object for unsupported OS family {0}".format(os_family))
raise RuntimeError(f"Not able to create Repository Manager object for unsupported OS family {os_family}")
Loading