in LAD-AMA-Common/metrics_ext_utils/metrics_ext_handler.py [0:0]
def setup_me(is_lad, managed_identity="sai", HUtilObj=None):
"""
The main method for creating and writing MetricsExtension configuration as well as service setup
:param is_lad: Boolean value for whether the extension is Lad or not (AMA)
"""
# query imds to get the required information
az_resource_id, subscription_id, location, az_environment, data = get_imds_values(is_lad)
arm_domain = get_arm_domain(az_environment)
# get tenantID
# The url request will fail due to missing authentication header, but we get the auth url from the header of the request fail exception
aad_auth_url = ""
arm_url = "https://{0}/subscriptions/{1}?api-version=2014-04-01".format(arm_domain, subscription_id)
try:
req = urllib.Request(arm_url, headers={'Content-Type':'application/json'})
res = urllib.urlopen(req)
except urlerror.HTTPError as e:
err_res = e.headers["WWW-Authenticate"]
for line in err_res.split(","):
if "Bearer authorization_uri" in line:
data = line.split("=")
aad_auth_url = data[1][1:-1] # Removing the quotes from the front and back
break
except Exception as e:
message = "Failed to retrieve AAD Authentication URL from " + arm_url + " with Exception='{0}'. ".format(e)
message += "Continuing with metrics setup without AAD auth url."
if HUtilObj is not None:
HUtilObj.log(message)
else:
print('Info: {0}'.format(message))
#create metrics conf
me_conf = create_metrics_extension_conf(az_resource_id, aad_auth_url)
#create custom metrics conf
if az_environment.lower() == ArcACloudName:
ingestion_endpoint = get_arca_ingestion_endpoint_from_mcs()
custom_conf = create_custom_metrics_conf(location, ingestion_endpoint)
else:
custom_conf = create_custom_metrics_conf(location)
#write configs to disk
logFolder, configFolder = get_handler_vars()
me_config_dir = configFolder + "/metrics_configs/"
# Clear older config directory if exists.
if os.path.exists(me_config_dir):
rmtree(me_config_dir)
os.mkdir(me_config_dir)
me_conf_path = me_config_dir + "MetricsExtensionV1_Configuration.json"
with open(me_conf_path, "w") as f:
f.write(me_conf)
if is_lad:
me_monitoring_account = "CUSTOMMETRIC_"+ subscription_id
else:
me_monitoring_account = "CUSTOMMETRIC_"+ subscription_id + "_" +location
custom_conf_path = me_config_dir + me_monitoring_account.lower() +"_MonitoringAccount_Configuration.json"
with open(custom_conf_path, "w") as f:
f.write(custom_conf)
# Copy MetricsExtension Binary to the bin location
me_bin_local_path = os.getcwd() + "/MetricsExtensionBin/MetricsExtension"
if is_lad:
metrics_ext_bin = metrics_constants.lad_metrics_extension_bin
else:
metrics_ext_bin = metrics_constants.ama_metrics_extension_bin
if is_lad:
lad_bin_path = "/usr/local/lad/bin/"
# Checking if directory exists before copying ME bin over to /usr/local/lad/bin/
if not os.path.exists(lad_bin_path):
os.makedirs(lad_bin_path)
# Check if previous file exist at the location, compare the two binaries,
# If the files are not same, remove the older file, and copy the new one
# If they are the same, then we ignore it and don't copy
if os.path.isfile(me_bin_local_path):
if os.path.isfile(metrics_ext_bin):
if not filecmp.cmp(me_bin_local_path, metrics_ext_bin):
# Removing the file in case it is already being run in a process,
# in which case we can get an error "text file busy" while copying
os.remove(metrics_ext_bin)
copyfile(me_bin_local_path, metrics_ext_bin)
os.chmod(metrics_ext_bin, stat.S_IXGRP | stat.S_IRGRP | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IXOTH | stat.S_IROTH)
else:
# No previous binary exist, simply copy it and make it executable
copyfile(me_bin_local_path, metrics_ext_bin)
os.chmod(metrics_ext_bin, stat.S_IXGRP | stat.S_IRGRP | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IXOTH | stat.S_IROTH)
else:
raise Exception("Unable to copy MetricsExtension Binary, could not find file at the location {0} . Failed to set up ME.".format(me_bin_local_path))
if is_lad:
me_influx_port = metrics_constants.lad_metrics_extension_udp_port
else:
me_influx_port = metrics_constants.ama_metrics_extension_udp_port
# setup metrics extension service
# If the VM has systemd, then we use that to start/stop
if metrics_utils.is_systemd():
setup_me_service(is_lad, me_config_dir, me_monitoring_account, metrics_ext_bin, me_influx_port, managed_identity, HUtilObj)
return True