Switch the charm to support py3

Some major changes:
* the charm has been rebased (from a Python perspective) to be rooted in
  the charm directory.  This is a single root.
* Imports have been changed so that the don't add lots of imports to the
  namespace of the module doing the import.
* The code that used to run at module import time has been made lazy
  such that it only has to run if the relevant functions are called.
  This includes restart_on_change parameters, the harden function and
  the parameters to the guard_map.  Appropriate changes will be
  submitted to charm-helpers.
* Several tests had to be re-written as (incorrect) mocking meant that
  text fixtures didn't actually match what the code was doing.  Thus,
  the tests were meaningless.
* This has had a net positive impact on the unit tests wrt to importing
  modules and mocking.

Change-Id: Id07d9d1caaa9b29453a63c2e49ba831071e9457f
This commit is contained in:
Alex Kavanagh 2018-10-15 16:39:39 +01:00
parent 7832164cad
commit 9c12812735
178 changed files with 1838 additions and 1769 deletions

View File

@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python3
#
# Copyright 2016 Canonical Ltd
#
@ -17,40 +17,39 @@
import os
import sys
sys.path.append('hooks/')
_path = os.path.dirname(os.path.realpath(__file__))
_root = os.path.abspath(os.path.join(_path, '..'))
from charmhelpers.core.hookenv import (
action_fail,
action_get,
action_set,
)
from nova_cc_utils import (
pause_unit_helper,
resume_unit_helper,
register_configs,
archive_deleted_rows,
)
def _add_path(path):
if path not in sys.path:
sys.path.insert(1, path)
_add_path(_root)
import charmhelpers.core.hookenv as hookenv
import hooks.nova_cc_utils as utils
def pause(args):
"""Pause the Ceilometer services.
@raises Exception should the service fail to stop.
"""
pause_unit_helper(register_configs())
utils.pause_unit_helper(utils.register_configs())
def resume(args):
"""Resume the Ceilometer services.
@raises Exception should the service fail to start."""
resume_unit_helper(register_configs())
utils.resume_unit_helper(utils.register_configs())
def archive_data(args):
"""Run data archival process
@raises Exception should the archival fail"""
action_set({
'archive-deleted-rows': archive_deleted_rows(
max_rows=action_get('batch-size'))})
hookenv.action_set({
'archive-deleted-rows': utils.archive_deleted_rows(
max_rows=hookenv.action_get('batch-size'))})
# A dictionary of all the defined actions to callables (which take
@ -71,7 +70,7 @@ def main(args):
try:
action(args)
except Exception as e:
action_fail(str(e))
hookenv.action_fail(str(e))
if __name__ == "__main__":

View File

@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python3
#
# Copyright 2016 Canonical Ltd
#
@ -14,28 +14,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
sys.path.append('hooks/')
_path = os.path.dirname(os.path.realpath(__file__))
_root = os.path.abspath(os.path.join(_path, '..'))
from charmhelpers.contrib.openstack.utils import (
do_action_openstack_upgrade,
)
from charmhelpers.core.hookenv import (
relation_ids,
)
def _add_path(path):
if path not in sys.path:
sys.path.insert(1, path)
from nova_cc_utils import (
do_openstack_upgrade,
)
_add_path(_root)
from nova_cc_hooks import (
config_changed,
CONFIGS,
neutron_api_relation_joined,
db_joined,
)
import charmhelpers.contrib.openstack.utils as ch_utils
import charmhelpers.core.hookenv as hookenv
import hooks.nova_cc_utils as utils
import hooks.nova_cc_hooks as hooks
def openstack_upgrade():
@ -46,16 +41,17 @@ def openstack_upgrade():
code to run, otherwise a full service level upgrade will fire
on config-changed."""
if (do_action_openstack_upgrade('nova-common',
do_openstack_upgrade,
CONFIGS)):
[neutron_api_relation_joined(rid=rid, remote_restart=True)
for rid in relation_ids('neutron-api')]
if (ch_utils.do_action_openstack_upgrade('nova-common',
utils.do_openstack_upgrade,
hooks.CONFIGS)):
for rid in hookenv.relation_ids('neutron-api'):
hooks.neutron_api_relation_joined(rid=rid, remote_restart=True)
# NOTE(thedac): Force re-fire of shared-db joined hook
# to ensure that nova_api database is setup if required.
[db_joined(relation_id=r_id)
for r_id in relation_ids('shared-db')]
config_changed()
for r_id in hookenv.relation_ids('shared-db'):
hooks.db_joined(relation_id=r_id)
hooks.config_changed()
if __name__ == '__main__':
hooks.resolve_CONFIGS()
openstack_upgrade()

View File

@ -1,5 +1,5 @@
repo: https://github.com/juju/charm-helpers
destination: hooks/charmhelpers
destination: charmhelpers
include:
- core
- osplatform

View File

@ -27,6 +27,8 @@ from charmhelpers.contrib.hardening.ssh.checks import run_ssh_checks
from charmhelpers.contrib.hardening.mysql.checks import run_mysql_checks
from charmhelpers.contrib.hardening.apache.checks import run_apache_checks
_DISABLE_HARDENING_FOR_UNIT_TEST = False
def harden(overrides=None):
"""Hardening decorator.
@ -48,9 +50,18 @@ def harden(overrides=None):
:returns: Returns value returned by decorated function once executed.
"""
def _harden_inner1(f):
log("Hardening function '%s'" % (f.__name__), level=DEBUG)
# As this has to be py2.7 compat, we can't use nonlocal. Use a trick
# to capture the dictionary that can then be updated.
_logged = {'done': False}
def _harden_inner2(*args, **kwargs):
# knock out hardening via a config var; normally it won't get
# disabled.
if _DISABLE_HARDENING_FOR_UNIT_TEST:
return f(*args, **kwargs)
if not _logged['done']:
log("Hardening function '%s'" % (f.__name__), level=DEBUG)
_logged['done'] = True
RUN_CATALOG = OrderedDict([('os', run_os_checks),
('ssh', run_ssh_checks),
('mysql', run_mysql_checks),

Some files were not shown because too many files have changed in this diff Show More