Python3: Make monasca-setup compatible with py35

Change-Id: Ieb4039e5af8d876218ce9b20d6f293c3dff989a9
This commit is contained in:
Adrian Czarnecki 2018-06-20 12:09:39 +02:00
parent 96f08da015
commit 92ac626c9b
13 changed files with 47 additions and 42 deletions

View File

@ -32,9 +32,8 @@ import uuid
import logging
import logging.handlers
from six import integer_types
from numbers import Number
from six import integer_types
log = logging.getLogger(__name__)

View File

@ -102,10 +102,10 @@ def save_plugin_config(config_dir, plugin_name, user, conf):
with open(config_path, 'w') as config_file:
# The gid is created on service activation which we assume has happened
config_file.write(yaml.safe_dump(conf,
encoding='utf-8',
allow_unicode=True,
default_flow_style=False))
config_file.write((yaml.safe_dump(conf,
encoding='utf-8',
allow_unicode=True,
default_flow_style=False)).decode('utf-8'))
stat = pwd.getpwnam(user)
gid = stat.pw_gid

View File

@ -11,11 +11,11 @@
# License for the specific language governing permissions and limitations
# under the License.
from args_plugin import ArgsPlugin # noqa
from plugin import Plugin # noqa
from service_plugin import ServicePlugin # noqa
from utils import find_process_cmdline # noqa
from utils import find_process_name # noqa
from utils import find_process_service # noqa
from utils import watch_process # noqa
from utils import watch_process_by_username # noqa
from monasca_setup.detection.args_plugin import ArgsPlugin # noqa
from monasca_setup.detection.plugin import Plugin # noqa
from monasca_setup.detection.service_plugin import ServicePlugin # noqa
from monasca_setup.detection.utils import find_process_cmdline # noqa
from monasca_setup.detection.utils import find_process_name # noqa
from monasca_setup.detection.utils import find_process_service # noqa
from monasca_setup.detection.utils import watch_process # noqa
from monasca_setup.detection.utils import watch_process_by_username # noqa

View File

@ -13,7 +13,7 @@
import logging
from plugin import Plugin
from monasca_setup.detection.plugin import Plugin
log = logging.getLogger(__name__)

View File

@ -68,7 +68,7 @@ class Plugin(object):
conf = self.build_config()
if conf is None:
return None
for plugin_type in conf.itervalues():
for plugin_type in conf.values():
for inst in plugin_type['instances']:
inst['built_by'] = self.__class__.__name__
return conf

View File

@ -15,7 +15,8 @@
import logging
import os
import urllib2
from six.moves import urllib
import monasca_setup.agent_config
import monasca_setup.detection
@ -144,19 +145,19 @@ class Apache(monasca_setup.detection.Plugin):
return config
if apache_user and apache_pass:
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr = urllib.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None,
apache_url,
apache_user,
apache_pass)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
handler = urllib.HTTPBasicAuthHandler(password_mgr)
else:
if 'https' in apache_url:
handler = urllib2.HTTPSHandler()
handler = urllib.HTTPSHandler()
else:
handler = urllib2.HTTPHandler()
handler = urllib.HTTPHandler()
opener = urllib2.build_opener(handler)
opener = urllib.build_opener(handler)
try:
request = opener.open(apache_url)
@ -171,7 +172,7 @@ class Apache(monasca_setup.detection.Plugin):
log.info("\tSuccessfully setup Apache plugin.")
else:
log.warn('Unable to access the Apache server-status URL;' + error_msg)
except urllib2.URLError as e:
except urllib.URLError as e:
exception_msg = (
'\tError {0} received when accessing url {1}.'.format(e.reason, apache_url) +
'\n\tPlease ensure the Apache web server is running and your configuration ' +

View File

@ -11,10 +11,11 @@
# License for the specific language governing permissions and limitations
# under the License.
import ConfigParser
import logging
import os
from six.moves import configparser
from monasca_agent.common.psutil_wrapper import psutil
import monasca_setup.agent_config
import monasca_setup.detection
@ -76,7 +77,7 @@ class Congestion(monasca_setup.detection.Plugin):
"""Build the config as a Plugins object and return. """
config = monasca_setup.agent_config.Plugins()
log.info("Configuring congestion plugin")
nova_cfg = ConfigParser.SafeConfigParser()
nova_cfg = configparser.SafeConfigParser()
log.info("\tUsing nova configuration file {0}".format(self.nova_conf))
nova_cfg.read(self.nova_conf)
# Which configuration options are needed for the plugin YAML?

View File

@ -11,13 +11,13 @@
# License for the specific language governing permissions and limitations
# under the License.
import ConfigParser
import logging
import os
import re
from oslo_config import cfg
from oslo_utils import importutils
from six.moves import configparser
from monasca_setup import agent_config
from monasca_setup import detection
@ -125,7 +125,7 @@ class Ovs(detection.Plugin):
self.neutron_conf = neutron_conf
def _is_neutron_conf_valid(self, conf_file):
neutron_cfg = ConfigParser.SafeConfigParser()
neutron_cfg = configparser.SafeConfigParser()
neutron_cfg.read(conf_file)
for section in self.REQUIRED_CONF_SECTIONS:
@ -194,7 +194,7 @@ class Ovs(detection.Plugin):
else:
try:
init_config['region_name'] = str(self.get_option('nova', 'region_name'))
except ConfigParser.NoOptionError:
except configparser.NoOptionError:
log.debug(('Option region_name was not found in nova '
'section, nova_region_name option from '
'DEFAULT section will be used.'))

View File

@ -12,7 +12,8 @@
# under the License.
import logging
import urllib2
from six.moves import urllib
import monasca_setup.agent_config
import monasca_setup.detection
@ -89,13 +90,13 @@ class RabbitMQ(monasca_setup.detection.Plugin):
:return: bool status of the test
"""
url = self.api_url + '/aliveness-test/%2F'
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr = urllib.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None,
self.api_url,
self.user,
self.password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
handler = urllib.HTTPBasicAuthHandler(password_mgr)
opener = urllib.build_opener(handler)
request = opener.open(url)
response = request.read()

View File

@ -11,9 +11,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import ConfigParser
import logging
from six.moves import configparser
from monasca_agent.common.psutil_wrapper import psutil
import monasca_setup.agent_config
from monasca_setup.detection import Plugin
@ -75,7 +76,7 @@ class VCenter(Plugin):
config = monasca_setup.agent_config.Plugins()
if self.dependencies_installed():
nova_cfg = ConfigParser.SafeConfigParser()
nova_cfg = configparser.SafeConfigParser()
instance = {}
if self.nova_conf is None:
log.warn("Nova compute configuration file was not found.")

View File

@ -12,7 +12,8 @@
# under the License.
import logging
import urlparse
from six.moves import urllib
from monasca_setup import agent_config
from monasca_setup.detection.plugin import Plugin
@ -187,7 +188,7 @@ class ServicePlugin(Plugin):
if self.service_api_url and self.search_pattern:
# Check if there is something listening on the host/port
parsed = urlparse.urlparse(self.service_api_url)
parsed = urllib.parse.urlparse(self.service_api_url)
host, port = parsed.netloc.split(':')
listening = find_addrs_listening_on_port(port)
@ -197,7 +198,7 @@ class ServicePlugin(Plugin):
set(['127.0.0.1', '0.0.0.0', '::', '::1']) & set(listening)) == 0:
new_url = list(parsed)
new_url[1] = listening[0] + ':' + port
api_url = urlparse.urlunparse(new_url)
api_url = urllib.parse.urlunparse(new_url)
else:
api_url = self.service_api_url

View File

@ -82,7 +82,7 @@ def main(argv=None):
# Collect the set of detection plugins to run
detected_plugins = utils.discover_plugins(CUSTOM_PLUGIN_PATH)
if args.system_only:
from detection.plugins.system import System
from monasca_setup.detection.plugins.system import System
plugins = [System]
elif args.detection_plugins is not None:
plugins = utils.select_plugins(args.detection_plugins,

View File

@ -53,6 +53,7 @@ def discover_plugins(custom_path):
plugin_path)
except Exception:
log.exception('Unable to import detection plugin {0}'.format(plugin_path))
continue
# Verify this is a subclass of Plugin
classes = inspect.getmembers(plugin, inspect.isclass)
@ -113,10 +114,10 @@ def write_template(template_path,
contents = template.read().format(**variables)
with open(out_path, 'w') as conf:
if is_yaml:
conf.write(yaml.safe_dump(yaml.safe_load(contents),
encoding='utf-8',
allow_unicode=True,
default_flow_style=False))
conf.write((yaml.safe_dump(yaml.safe_load(contents),
encoding='utf-8',
allow_unicode=True,
default_flow_style=False)).decode('utf-8'))
else:
conf.write(contents)