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

View File

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

View File

@ -1,5 +1,5 @@
repo: https://github.com/juju/charm-helpers repo: https://github.com/juju/charm-helpers
destination: hooks/charmhelpers destination: charmhelpers
include: include:
- core - core
- osplatform - 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.mysql.checks import run_mysql_checks
from charmhelpers.contrib.hardening.apache.checks import run_apache_checks from charmhelpers.contrib.hardening.apache.checks import run_apache_checks
_DISABLE_HARDENING_FOR_UNIT_TEST = False
def harden(overrides=None): def harden(overrides=None):
"""Hardening decorator. """Hardening decorator.
@ -48,9 +50,18 @@ def harden(overrides=None):
:returns: Returns value returned by decorated function once executed. :returns: Returns value returned by decorated function once executed.
""" """
def _harden_inner1(f): 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): 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), RUN_CATALOG = OrderedDict([('os', run_os_checks),
('ssh', run_ssh_checks), ('ssh', run_ssh_checks),
('mysql', run_mysql_checks), ('mysql', run_mysql_checks),

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