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 03f93dbc76.

Change-Id: I2b722014fc1ed5823635a6b45b3307326fd901af
This commit is contained in:
Alex Kavanagh (tinwood) 2019-03-14 15:12:50 +00:00
parent 03f93dbc76
commit 79d8a0f7b7
11 changed files with 50 additions and 83 deletions

View File

@ -1,3 +1,4 @@
- project:
templates:
- python35-charm-jobs
- python-charm-jobs
- openstack-python35-jobs-nonvoting

View File

@ -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,

1
hooks/charmhelpers Symbolic link
View File

@ -0,0 +1 @@
../charmhelpers

View File

@ -11,7 +11,7 @@ check_and_install() {
fi
}
PYTHON="python3"
PYTHON="python"
for dep in ${DEPS[@]}; do
check_and_install ${PYTHON} ${dep}

View File

@ -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:

View File

@ -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

View File

@ -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

View File

@ -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')

View File

@ -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:

View File

@ -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")

View File

@ -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