Skip to content
Draft
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
5 changes: 2 additions & 3 deletions Diagnostic/DistroSpecific.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Azure Linux extension
# Distribution-specific actions
Expand All @@ -18,7 +18,6 @@
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import exceptions
import time
import subprocess
import re
Expand Down Expand Up @@ -360,4 +359,4 @@ def get_distro_actions(name, version, logger):
return DistroMap[name_and_major_version](logger)
if name in DistroMap:
return DistroMap[name](logger)
raise exceptions.LookupError('{0} is not a supported distro'.format(name_and_version))
raise LookupError('{0} is not a supported distro'.format(name_and_version))
4 changes: 2 additions & 2 deletions Diagnostic/Providers/Builtin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Azure Linux extension
#
Expand Down Expand Up @@ -212,7 +212,7 @@ def AddMetric(counter_spec):
try:
metric = BuiltinMetric(counter_spec)
except ProvUtil.ParseException as ex:
print "Couldn't create metric: ", ex
print("Couldn't create metric: ", ex)
return None

# (class, instanceId, sampleRate) -> [ metric ]
Expand Down
2 changes: 1 addition & 1 deletion Diagnostic/Utils/LadDiagnosticUtil.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Azure Linux extension
#
Expand Down
2 changes: 1 addition & 1 deletion Diagnostic/Utils/ProviderUtil.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Azure Linux extension
#
Expand Down
2 changes: 1 addition & 1 deletion Diagnostic/Utils/XmlUtil.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Azure Linux extension
#
Expand Down
36 changes: 24 additions & 12 deletions Diagnostic/Utils/imds_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Azure Linux extension
#
Expand All @@ -18,10 +18,22 @@
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import datetime
import urllib2
import time
import traceback

# Python 2/3 compatibility for urllib
try:
# Python 3
import urllib.request
import urllib.error
urlopen = urllib.request.urlopen
Request = urllib.request.Request
except ImportError:
# Python 2
import urllib2
urlopen = urllib2.urlopen
Request = urllib2.Request


def get_imds_data(node, json=True):
"""
Expand All @@ -38,8 +50,8 @@ def get_imds_data(node, json=True):
imds_url = 'http://169.254.169.254{0}{1}{2}'.format(
separator, node, '?format=json&api-version=latest_internal' if json else '')
imds_headers = {'Metadata': 'True'}
req = urllib2.Request(url=imds_url, headers=imds_headers)
resp = urllib2.urlopen(req)
req = Request(url=imds_url, headers=imds_headers)
resp = urlopen(req)
data = resp.read()
data_str = data.decode('utf-8')
return data_str
Expand Down Expand Up @@ -112,20 +124,20 @@ def log_imds_data_if_right_time(self, log_as_ext_event=False):

def fake_get_imds_data(node, json=True):
result = 'fake_get_imds_data(node="{0}", json="{1}")'.format(node, json)
print result
print(result)
return result


def default_ext_logger(msg):
print 'default_ext_logger(msg="{0}")'.format(msg)
print('default_ext_logger(msg="{0}")'.format(msg))


def default_ext_event_logger(*args, **kwargs):
print 'default_ext_event_logger(*args, **kwargs)'
print 'args:'
print('default_ext_event_logger(*args, **kwargs)')
print('args:')
for arg in args:
print arg
print 'kwargs:'
print(arg)
print('kwargs:')
for k in kwargs:
print('"{0}"="{1}"'.format(k, kwargs[k]))

Expand All @@ -137,10 +149,10 @@ def default_ext_event_logger(*args, **kwargs):
done = False
while not done:
now = datetime.datetime.now()
print 'Test loop iteration starting at {0}'.format(now)
print('Test loop iteration starting at {0}'.format(now))
imds_logger.log_imds_data_if_right_time()
if now >= start_time + datetime.timedelta(minutes=2):
done = True
else:
print 'Sleeping 10 seconds'
print('Sleeping 10 seconds')
time.sleep(10)
2 changes: 1 addition & 1 deletion Diagnostic/Utils/lad_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Azure Linux extension
#
Expand Down
2 changes: 1 addition & 1 deletion Diagnostic/Utils/lad_ext_settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Azure Linux extension
#
Expand Down
6 changes: 3 additions & 3 deletions Diagnostic/Utils/lad_logging_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Azure Linux extension
#
Expand Down Expand Up @@ -137,7 +137,7 @@ def get_rsyslog_config(self):
self._rsyslog_config = \
'\n'.join('{0}.{1} @127.0.0.1:%SYSLOG_PORT%'.format(syslog_name_to_rsyslog_name(fac),
syslog_name_to_rsyslog_name(sev))
for fac, sev in self._fac_sev_map.iteritems()) + '\n'
for fac, sev in self._fac_sev_map.items()) + '\n'
return self._rsyslog_config

def get_syslog_ng_config(self):
Expand All @@ -159,7 +159,7 @@ def get_syslog_ng_config(self):
'destination(d_LAD_oms); }};'.format(get_syslog_ng_src_name(),
syslog_name_to_rsyslog_name(fac),
syslog_name_to_rsyslog_name(sev))
for fac, sev in self._fac_sev_map.iteritems()) + '\n'
for fac, sev in self._fac_sev_map.items()) + '\n'
return self._syslog_ng_config


Expand Down
2 changes: 1 addition & 1 deletion Diagnostic/Utils/mdsd_xml_templates.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Azure Linux extension
#
Expand Down
15 changes: 12 additions & 3 deletions Diagnostic/Utils/misc_helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Azure Linux extension
#
Expand All @@ -21,6 +21,15 @@
from Utils.WAAgentUtil import waagent
from Utils.lad_exceptions import LadLoggingConfigException

# Python 2/3 compatibility
import sys
if sys.version_info[0] == 2:
# Python 2
string_types = basestring
else:
# Python 3
string_types = str


def get_extension_operation_type(command):
if re.match("^([-/]*)(enable)", command):
Expand Down Expand Up @@ -209,7 +218,7 @@ def encrypt_secret_with_cert(run_command, logger, cert_path, secret):
cmd = "echo -n '{0}' | openssl smime -aes256 -encrypt -outform DER -out {1} {2}"
cmd_to_run = cmd.format(secret, f.name, cert_path)
ret_status, ret_msg = run_command(cmd_to_run, should_log=False)
if ret_status is not 0:
if ret_status != 0:
logger("Encrypting storage secret failed with the following message: " + ret_msg)
return None
encrypted_secret = f.read()
Expand Down Expand Up @@ -248,7 +257,7 @@ def get_mdsd_proxy_config(waagent_setting, ext_settings, logger):
proxy_config = ext_settings.read_protected_config(proxy_setting_name) # Protected setting has next priority
if not proxy_config:
proxy_config = ext_settings.read_public_config(proxy_setting_name)
if not isinstance(proxy_config, basestring):
if not isinstance(proxy_config, string_types):
logger('Error: mdsdHttpProxy config is not a string. Ignored.')
else:
proxy_config = proxy_config.strip()
Expand Down
4 changes: 2 additions & 2 deletions Diagnostic/Utils/omsagent_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Azure Linux extension
#
Expand Down Expand Up @@ -405,7 +405,7 @@ def setup_omsagent(configurator, run_command, logger_log, logger_error):
if need_fresh_install_omi:
# Check if OMI is configured to listen to any non-zero port and reconfigure if so.
omi_listens_to_nonzero_port = run_command(r"grep '^\s*httpsport\s*=' /etc/opt/omi/conf/omiserver.conf "
r"| grep -v '^\s*httpsport\s*=\s*0\s*$'")[0] is 0
r"| grep -v '^\s*httpsport\s*=\s*0\s*$'")[0] == 0
if omi_listens_to_nonzero_port:
run_command("/opt/omi/bin/omiconfigeditor httpsport -s 0 < /etc/opt/omi/conf/omiserver.conf "
"> /etc/opt/omi/conf/omiserver.conf_temp")
Expand Down
31 changes: 15 additions & 16 deletions Diagnostic/diagnostic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Azure Linux extension
#
Expand All @@ -17,7 +17,6 @@
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import datetime
import exceptions
import os.path
import platform
import signal
Expand Down Expand Up @@ -58,9 +57,9 @@


except Exception as e:
print 'A local import (e.g., waagent) failed. Exception: {0}\n' \
'Stacktrace: {1}'.format(e, traceback.format_exc())
print "Can't proceed. Exiting with a special exit code 119."
print('A local import (e.g., waagent) failed. Exception: {0}\n' \
'Stacktrace: {1}'.format(e, traceback.format_exc()))
print("Can't proceed. Exiting with a special exit code 119.")
sys.exit(119) # This is the only thing we can do, as all logging depends on waagent/hutil.


Expand Down Expand Up @@ -119,7 +118,7 @@ def init_distro_specific_actions():
hutil.log("os version: {0}:{1}".format(name.lower(), version))
g_dist_config = DistroSpecific.get_distro_actions(name.lower(), version, hutil.log)
RunGetOutput = g_dist_config.log_run_get_output
except exceptions.LookupError as ex:
except LookupError as ex:
hutil.error("os version: {0}:{1} not supported".format(dist[0], dist[1]))
# TODO Exit immediately if distro is unknown. This is currently done in main().
g_dist_config = None
Expand Down Expand Up @@ -186,7 +185,7 @@ def setup_dependencies_and_mdsd(configurator):
# Set up omsagent
omsagent_setup_exit_code, omsagent_setup_output = oms.setup_omsagent(configurator, RunGetOutput,
hutil.log, hutil.error)
if omsagent_setup_exit_code is not 0:
if omsagent_setup_exit_code != 0:
return 3, omsagent_setup_output

# Install lad-mdsd pkg (/usr/local/lad/bin/mdsd). Must be done after omsagent install because of dependencies
Expand Down Expand Up @@ -417,7 +416,7 @@ def main(command):
if g_dist_config.use_systemd():
install_lad_as_systemd_service()
RunGetOutput('systemctl enable mdsd-lde')
mdsd_lde_active = RunGetOutput('systemctl status mdsd-lde')[0] is 0
mdsd_lde_active = RunGetOutput('systemctl status mdsd-lde')[0] == 0
if not mdsd_lde_active or hutil.is_current_config_seq_greater_inused():
RunGetOutput('systemctl restart mdsd-lde')
else:
Expand All @@ -432,7 +431,7 @@ def main(command):
# If the -daemon detects a problem, e.g. bad configuration, it will overwrite this status with a more
# informative one. If it succeeds, all is well.

elif g_ext_op_type is "Daemon":
elif g_ext_op_type == "Daemon":
configurator = create_core_components_configs()
if configurator:
start_mdsd(configurator)
Expand All @@ -453,7 +452,7 @@ def start_daemon():
raise an exception (often OSError)
:return: None
"""
args = ['python2', g_diagnostic_py_filepath, "-daemon"]
args = ['python3', g_diagnostic_py_filepath, "-daemon"]
log = open(os.path.join(os.getcwd(), 'daemon.log'), 'w')
hutil.log('start daemon ' + str(args))
subprocess.Popen(args, stdout=log, stderr=log)
Expand Down Expand Up @@ -509,7 +508,7 @@ def start_mdsd(configurator):
config_validate_cmd = '{0}{1}{2} -v -c {3} -r {4}'.format(added_env_str, ' ' if added_env_str else '',
g_mdsd_bin_path, xml_file, g_ext_dir)
config_validate_cmd_status, config_validate_cmd_msg = RunGetOutput(config_validate_cmd)
if config_validate_cmd_status is not 0:
if config_validate_cmd_status != 0:
# Invalid config. Log error and report success.
g_lad_log_helper.log_and_report_invalid_mdsd_cfg(g_ext_op_type,
config_validate_cmd_msg, read_file_to_string(xml_file))
Expand All @@ -518,7 +517,7 @@ def start_mdsd(configurator):
# Start OMI if it's not running.
# This shouldn't happen, but this measure is put in place just in case (e.g., Ubuntu 16.04 systemd).
# Don't check if starting succeeded, as it'll be done in the loop below anyway.
omi_running = RunGetOutput("/opt/omi/bin/service_control is-running", should_log=False)[0] is 1
omi_running = RunGetOutput("/opt/omi/bin/service_control is-running", should_log=False)[0] == 1
if not omi_running:
hutil.log("OMI is not running. Restarting it.")
RunGetOutput("/opt/omi/bin/service_control restart")
Expand Down Expand Up @@ -823,7 +822,7 @@ def restart_omi_if_crashed(omi_installed, mdsd):
should_restart_omi = False
if omi_installed:
cmd_exit_status, cmd_output = RunGetOutput(cmd=omicli_noop_query_cmd, should_log=False)
should_restart_omi = cmd_exit_status is not 0
should_restart_omi = cmd_exit_status != 0
if should_restart_omi:
hutil.error("OMI noop query failed. Output: " + cmd_output + ". OMI crash suspected. "
"Restarting OMI and sending SIGHUP to mdsd after 5 seconds.")
Expand All @@ -834,7 +833,7 @@ def restart_omi_if_crashed(omi_installed, mdsd):
# Query OMI once again to make sure restart fixed the issue.
# If not, attempt to re-install OMI as last resort.
cmd_exit_status, cmd_output = RunGetOutput(cmd=omicli_noop_query_cmd, should_log=False)
should_reinstall_omi = cmd_exit_status is not 0
should_reinstall_omi = cmd_exit_status != 0
if should_reinstall_omi:
hutil.error("OMI noop query failed even after OMI was restarted. Attempting to re-install the components.")
configurator = create_core_components_configs()
Expand All @@ -851,7 +850,7 @@ def restart_omi_if_crashed(omi_installed, mdsd):
# so it's still better to signal anyway.
should_signal_mdsd = should_restart_omi or omi_reinstalled
if should_signal_mdsd:
omi_up_and_running = RunGetOutput(omicli_noop_query_cmd)[0] is 0
omi_up_and_running = RunGetOutput(omicli_noop_query_cmd)[0] == 0
if omi_up_and_running:
mdsd.send_signal(signal.SIGHUP)
hutil.log("SIGHUP sent to mdsd")
Expand Down Expand Up @@ -892,7 +891,7 @@ def restart_omi_if_crashed(omi_installed, mdsd):
# Trick to print backtrace in case we execute './diagnostic.py -xxx yyy' from a terminal for testing.
# By just adding one more cmdline arg with any content, the above if condition becomes false,\
# thus allowing us to run code here, printing the exception message with the stack trace.
print msg
print(msg)
# Need to exit with an error code, so that this situation can be detected by waagent and also
# reported to customer through agent/extension status blob.
hutil.do_exit(42, wala_event_type, 'Error', '42', msg) # What's 42? Ask Abhi.
2 changes: 1 addition & 1 deletion Diagnostic/lad_config_all.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Azure Linux extension
#
Expand Down
4 changes: 2 additions & 2 deletions Diagnostic/mdsd/parseglibc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ def LogError(msg):
global totalErrors
totalErrors = totalErrors + 1
msg2 = "%s: Error: %s" % (sys.argv[0], msg)
print msg2
print(msg2)


def LogInfo(msg):
print msg
print(msg)


def ParseCmdLine():
Expand Down
Loading