From 79d8a0f7b78590edfd1b25fb13eb4e8a5c3e02bf Mon Sep 17 00:00:00 2001 From: "Alex Kavanagh (tinwood)" Date: Thu, 14 Mar 2019 15:12:50 +0000 Subject: [PATCH] Revert "Convert the charm to Python 3" It's broken at trusty and needs to be re-worked due to a lack of python3-mysqldb at trusty This reverts commit 03f93dbc7610ec8bbee9294372d6b48c7dd7a4f3. Change-Id: I2b722014fc1ed5823635a6b45b3307326fd901af --- .zuul.yaml | 3 ++- actions/actions.py | 13 +--------- hooks/charmhelpers | 1 + hooks/install | 2 +- hooks/percona_hooks.py | 18 +++----------- hooks/percona_utils.py | 23 +++++++++-------- tox.ini | 7 +++++- unit_tests/__init__.py | 18 ++------------ unit_tests/test_percona_hooks.py | 2 +- unit_tests/test_percona_utils.py | 42 +++++++++++++++----------------- unit_tests/test_utils.py | 4 +-- 11 files changed, 50 insertions(+), 83 deletions(-) create mode 120000 hooks/charmhelpers diff --git a/.zuul.yaml b/.zuul.yaml index 7051aee..aa9c508 100644 --- a/.zuul.yaml +++ b/.zuul.yaml @@ -1,3 +1,4 @@ - project: templates: - - python35-charm-jobs + - python-charm-jobs + - openstack-python35-jobs-nonvoting diff --git a/actions/actions.py b/actions/actions.py index d82b85c..cd65c3f 100755 --- a/actions/actions.py +++ b/actions/actions.py @@ -6,18 +6,7 @@ import subprocess import traceback from time import gmtime, strftime -_path = os.path.dirname(os.path.realpath(__file__)) -_hooks = os.path.abspath(os.path.join(_path, '../hooks')) -_root = os.path.abspath(os.path.join(_path, '..')) - - -def _add_path(path): - if path not in sys.path: - sys.path.insert(1, path) - -_add_path(_hooks) -_add_path(_root) - +sys.path.append('hooks') from charmhelpers.core.hookenv import ( action_get, diff --git a/hooks/charmhelpers b/hooks/charmhelpers new file mode 120000 index 0000000..702de73 --- /dev/null +++ b/hooks/charmhelpers @@ -0,0 +1 @@ +../charmhelpers \ No newline at end of file diff --git a/hooks/install b/hooks/install index f3dd1cc..2971ad5 100755 --- a/hooks/install +++ b/hooks/install @@ -11,7 +11,7 @@ check_and_install() { fi } -PYTHON="python3" +PYTHON="python" for dep in ${DEPS[@]}; do check_and_install ${PYTHON} ${dep} diff --git a/hooks/percona_hooks.py b/hooks/percona_hooks.py index 58c39e5..54b1b6a 100755 --- a/hooks/percona_hooks.py +++ b/hooks/percona_hooks.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/python # TODO: Support changes to root and sstuser passwords import sys import json @@ -6,18 +6,6 @@ import os import socket import subprocess -_path = os.path.dirname(os.path.realpath(__file__)) -_root = os.path.abspath(os.path.join(_path, '..')) - - -def _add_path(path): - if path not in sys.path: - sys.path.insert(1, path) - - -_add_path(_root) - - from charmhelpers.core.hookenv import ( Hooks, UnregisteredHookError, is_relation_made, @@ -621,7 +609,7 @@ def cluster_changed(): # NOTE(jamespage): deprecated - leader-election rdata = relation_get() inc_list = [] - for attr in rdata.keys(): + for attr in rdata.iterkeys(): if attr not in ['hostname', 'private-address', 'cluster-address', 'public-address', 'ready']: inc_list.append(attr) @@ -852,7 +840,7 @@ def shared_db_changed(relation_id=None, unit=None): # } # databases = {} - for k, v in settings.items(): + for k, v in settings.iteritems(): db = k.split('_')[0] x = '_'.join(k.split('_')[1:]) if db not in databases: diff --git a/hooks/percona_utils.py b/hooks/percona_utils.py index ae3fe99..0abbd18 100644 --- a/hooks/percona_utils.py +++ b/hooks/percona_utils.py @@ -147,13 +147,13 @@ def seeded(): def mark_seeded(): ''' Mark service unit as seeded ''' with open(SEEDED_MARKER.format(data_dir=resolve_data_dir()), - 'wt') as seeded: + 'w') as seeded: seeded.write('done') def setup_percona_repo(): ''' Configure service unit to use percona repositories ''' - with open('/etc/apt/sources.list.d/percona.list', 'wt') as sources: + with open('/etc/apt/sources.list.d/percona.list', 'w') as sources: sources.write(REPO.format(release=lsb_release()['DISTRIB_CODENAME'])) subprocess.check_call(['apt-key', 'add', KEY]) @@ -339,12 +339,10 @@ def configure_mysql_root_password(password): m_helper = get_db_helper() root_pass = m_helper.get_mysql_root_password(password) for package in packages: - dconf.stdin.write("{} {}/root_password password {}\n" - .format(package, package, root_pass) - .encode('ascii')) - dconf.stdin.write("{} {}/root_password_again password {}\n" - .format(package, package, root_pass) - .encode('ascii')) + dconf.stdin.write("%s %s/root_password password %s\n" % + (package, package, root_pass)) + dconf.stdin.write("%s %s/root_password_again password %s\n" % + (package, package, root_pass)) dconf.communicate() dconf.wait() @@ -368,7 +366,7 @@ def update_hosts_file(map): See https://bugs.launchpad.net/galera/+bug/1130595 for some more info. """ - with open(HOSTS_FILE, 'rt') as hosts: + with open(HOSTS_FILE, 'r') as hosts: lines = hosts.readlines() log("Updating %s with: %s (current: %s)" % (HOSTS_FILE, map, lines), @@ -394,7 +392,7 @@ def update_hosts_file(map): lines += newlines with tempfile.NamedTemporaryFile(delete=False) as tmpfile: - with open(tmpfile.name, 'wt') as hosts: + with open(tmpfile.name, 'w') as hosts: for line in lines: hosts.write(line) @@ -417,7 +415,8 @@ def _cmp(x, y): def unit_sorted(units): """Return a sorted list of unit names.""" - return sorted(units, key=lambda a: int(a.split('/')[-1])) + return sorted( + units, lambda a, b: _cmp(int(a.split('/')[-1]), int(b.split('/')[-1]))) def install_mysql_ocf(): @@ -1216,7 +1215,7 @@ def get_databases_to_replicate(): except InvalidCharacters as e: raise InvalidDatabasesToReplicate( "The configuration setting databases-to-replicate is malformed. {}" - .format(str(e))) + .format(e.message)) return databases_to_replicate diff --git a/tox.ini b/tox.ini index 9a66225..e516fc4 100644 --- a/tox.ini +++ b/tox.ini @@ -2,7 +2,7 @@ # This file is managed centrally by release-tools and should not be modified # within individual charm repos. [tox] -envlist = pep8,py3{5,6} +envlist = pep8,py27 skipsdist = True [testenv] @@ -16,6 +16,11 @@ commands = stestr run {posargs} whitelist_externals = juju passenv = HOME TERM AMULET_* CS_API_* +[testenv:py27] +basepython = python2.7 +deps = -r{toxinidir}/requirements.txt + -r{toxinidir}/test-requirements.txt + [testenv:py35] basepython = python3.5 deps = -r{toxinidir}/requirements.txt diff --git a/unit_tests/__init__.py b/unit_tests/__init__.py index 547d85f..e4bb018 100644 --- a/unit_tests/__init__.py +++ b/unit_tests/__init__.py @@ -1,18 +1,4 @@ -import os import sys -_path = os.path.dirname(os.path.realpath(__file__)) -_actions = os.path.abspath(os.path.join(_path, '../actions')) -_hooks = os.path.abspath(os.path.join(_path, '../hooks')) -_charmhelpers = os.path.abspath(os.path.join(_path, '../charmhelpers')) -_unit_tests = os.path.abspath(os.path.join(_path, '../unit_tests')) - - -def _add_path(path): - if path not in sys.path: - sys.path.insert(1, path) - -_add_path(_actions) -_add_path(_hooks) -_add_path(_charmhelpers) -_add_path(_unit_tests) +sys.path.append('hooks') +sys.path.append('actions') diff --git a/unit_tests/test_percona_hooks.py b/unit_tests/test_percona_hooks.py index 2e2d55c..5e9adf8 100644 --- a/unit_tests/test_percona_hooks.py +++ b/unit_tests/test_percona_hooks.py @@ -717,7 +717,7 @@ class TestConfigs(CharmTestCase): ''' default_config = {} config = self._load_config() - for k, v in config.items(): + for k, v in config.iteritems(): if 'default' in v: default_config[k] = v['default'] else: diff --git a/unit_tests/test_percona_utils.py b/unit_tests/test_percona_utils.py index d0477dc..50a63ee 100644 --- a/unit_tests/test_percona_utils.py +++ b/unit_tests/test_percona_utils.py @@ -1,4 +1,3 @@ -import collections import os import sys import tempfile @@ -28,62 +27,61 @@ class UtilsTests(CharmTestCase): @mock.patch("percona_utils.log") def test_update_empty_hosts_file(self, mock_log): - _map = {'1.2.3.4': 'my-host'} + map = {'1.2.3.4': 'my-host'} with tempfile.NamedTemporaryFile(delete=False) as tmpfile: percona_utils.HOSTS_FILE = tmpfile.name percona_utils.HOSTS_FILE = tmpfile.name - percona_utils.update_hosts_file(_map) + percona_utils.update_hosts_file(map) - with open(tmpfile.name, 'rt') as fd: + with open(tmpfile.name, 'r') as fd: lines = fd.readlines() os.remove(tmpfile.name) self.assertEqual(len(lines), 1) - self.assertEqual(lines[0], "%s %s\n" % (list(_map.items())[0])) + self.assertEqual(lines[0], "%s %s\n" % (map.items()[0])) @mock.patch("percona_utils.log") def test_update_hosts_file_w_dup(self, mock_log): - _map = {'1.2.3.4': 'my-host'} + map = {'1.2.3.4': 'my-host'} with tempfile.NamedTemporaryFile(delete=False) as tmpfile: percona_utils.HOSTS_FILE = tmpfile.name - with open(tmpfile.name, 'wt') as fd: - fd.write("%s %s\n" % (list(_map.items())[0])) + with open(tmpfile.name, 'w') as fd: + fd.write("%s %s\n" % (map.items()[0])) - percona_utils.update_hosts_file(_map) + percona_utils.update_hosts_file(map) - with open(tmpfile.name, 'rt') as fd: + with open(tmpfile.name, 'r') as fd: lines = fd.readlines() os.remove(tmpfile.name) self.assertEqual(len(lines), 1) - self.assertEqual(lines[0], "%s %s\n" % (list(_map.items())[0])) + self.assertEqual(lines[0], "%s %s\n" % (map.items()[0])) @mock.patch("percona_utils.log") def test_update_hosts_file_entry(self, mock_log): altmap = {'1.1.1.1': 'alt-host'} - _map = collections.OrderedDict([('1.1.1.1', 'hosta'), - ('2.2.2.2', 'hostb'), - ('3.3.3.3', 'hostc'), - ('4.4.4.4', 'hostd')]) + map = {'1.1.1.1': 'hostA', + '2.2.2.2': 'hostB', + '3.3.3.3': 'hostC', + '4.4.4.4': 'hostD'} with tempfile.NamedTemporaryFile(delete=False) as tmpfile: percona_utils.HOSTS_FILE = tmpfile.name - with open(tmpfile.name, 'wt') as fd: + with open(tmpfile.name, 'w') as fd: fd.write("#somedata\n") - fd.write("%s %s\n" % (list(altmap.items())[0])) + fd.write("%s %s\n" % (altmap.items()[0])) - percona_utils.update_hosts_file(_map) + percona_utils.update_hosts_file(map) - with open(percona_utils.HOSTS_FILE, 'rt') as fd: + with open(percona_utils.HOSTS_FILE, 'r') as fd: lines = fd.readlines() os.remove(tmpfile.name) self.assertEqual(len(lines), 5) self.assertEqual(lines[0], "#somedata\n") - _map_items = list(_map.items()) - self.assertEqual(lines[1], "%s %s\n" % (_map_items[0])) - self.assertEqual(lines[4], "%s %s\n" % (_map_items[3])) + self.assertEqual(lines[1], "%s %s\n" % (map.items()[0])) + self.assertEqual(lines[4], "%s %s\n" % (map.items()[3])) @mock.patch("percona_utils.get_cluster_host_ip") @mock.patch("percona_utils.log") diff --git a/unit_tests/test_utils.py b/unit_tests/test_utils.py index 42f2f4e..d8838ca 100644 --- a/unit_tests/test_utils.py +++ b/unit_tests/test_utils.py @@ -37,7 +37,7 @@ def get_default_config(): ''' default_config = {} config = load_config() - for k, v in config.items(): + for k, v in config.iteritems(): if 'default' in v: default_config[k] = v['default'] else: @@ -137,5 +137,5 @@ def patch_open(): mock_open(*args, **kwargs) yield mock_file - with patch('builtins.open', stub_open): + with patch('__builtin__.open', stub_open): yield mock_open, mock_file