Skip to content

Commit

Permalink
Charmhelper sync pulling coordinator
Browse files Browse the repository at this point in the history
Change-Id: Ia92c8d8544534ffcab9a7fd31ba6f35a3bf076ca
  • Loading branch information
Liam Young committed Feb 7, 2022
1 parent 2212383 commit c63cd1b
Show file tree
Hide file tree
Showing 46 changed files with 1,074 additions and 436 deletions.
1 change: 1 addition & 0 deletions charm-helpers-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ destination: charmhelpers
repo: https://github.com/juju/charm-helpers
include:
- fetch
- coordinator
- core
- cli
- osplatform
Expand Down
17 changes: 1 addition & 16 deletions charmhelpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,15 @@

# Bootstrap charm-helpers, installing its dependencies if necessary using
# only standard libraries.
from __future__ import print_function
from __future__ import absolute_import

import functools
import inspect
import subprocess
import sys

try:
import six # NOQA:F401
except ImportError:
if sys.version_info.major == 2:
subprocess.check_call(['apt-get', 'install', '-y', 'python-six'])
else:
subprocess.check_call(['apt-get', 'install', '-y', 'python3-six'])
import six # NOQA:F401

try:
import yaml # NOQA:F401
except ImportError:
if sys.version_info.major == 2:
subprocess.check_call(['apt-get', 'install', '-y', 'python-yaml'])
else:
subprocess.check_call(['apt-get', 'install', '-y', 'python3-yaml'])
subprocess.check_call(['apt-get', 'install', '-y', 'python3-yaml'])
import yaml # NOQA:F401


Expand Down
13 changes: 2 additions & 11 deletions charmhelpers/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
import argparse
import sys

import six
from six.moves import zip

import charmhelpers.core.unitdata


Expand Down Expand Up @@ -149,10 +146,7 @@ def wrapper(decorated):
def run(self):
"Run cli, processing arguments and executing subcommands."
arguments = self.argument_parser.parse_args()
if six.PY2:
argspec = inspect.getargspec(arguments.func)
else:
argspec = inspect.getfullargspec(arguments.func)
argspec = inspect.getfullargspec(arguments.func)
vargs = []
for arg in argspec.args:
vargs.append(getattr(arguments, arg))
Expand All @@ -177,10 +171,7 @@ def describe_arguments(func):
Analyze a function's signature and return a data structure suitable for
passing in as arguments to an argparse parser's add_argument() method."""

if six.PY2:
argspec = inspect.getargspec(func)
else:
argspec = inspect.getfullargspec(func)
argspec = inspect.getfullargspec(func)
# we should probably raise an exception somewhere if func includes **kwargs
if argspec.defaults:
positional_args = argspec.args[:-len(argspec.defaults)]
Expand Down
40 changes: 39 additions & 1 deletion charmhelpers/contrib/charmsupport/nrpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import yaml

from charmhelpers.core.hookenv import (
application_name,
config,
hook_name,
local_unit,
Expand Down Expand Up @@ -174,7 +175,8 @@ def _locate_cmd(self, check_cmd):
if os.path.exists(os.path.join(path, parts[0])):
command = os.path.join(path, parts[0])
if len(parts) > 1:
command += " " + " ".join(parts[1:])
safe_args = [shlex.quote(arg) for arg in parts[1:]]
command += " " + " ".join(safe_args)
return command
log('Check command not found: {}'.format(parts[0]))
return ''
Expand Down Expand Up @@ -520,3 +522,39 @@ def remove_deprecated_check(nrpe, deprecated_services):
for dep_svc in deprecated_services:
log('Deprecated service: {}'.format(dep_svc))
nrpe.remove_check(shortname=dep_svc)


def add_deferred_restarts_check(nrpe):
"""
Add NRPE check for services with deferred restarts.
:param NRPE nrpe: NRPE object to add check to
"""
unit_name = local_unit().replace('/', '-')
shortname = unit_name + '_deferred_restarts'
check_cmd = 'check_deferred_restarts.py --application {}'.format(
application_name())

log('Adding deferred restarts nrpe check: {}'.format(shortname))
nrpe.add_check(
shortname=shortname,
description='Check deferred service restarts {}'.format(unit_name),
check_cmd=check_cmd)


def remove_deferred_restarts_check(nrpe):
"""
Remove NRPE check for services with deferred service restarts.
:param NRPE nrpe: NRPE object to remove check from
"""
unit_name = local_unit().replace('/', '-')
shortname = unit_name + '_deferred_restarts'
check_cmd = 'check_deferred_restarts.py --application {}'.format(
application_name())

log('Removing deferred restarts nrpe check: {}'.format(shortname))
nrpe.remove_check(
shortname=shortname,
description='Check deferred service restarts {}'.format(unit_name),
check_cmd=check_cmd)
15 changes: 6 additions & 9 deletions charmhelpers/contrib/hahelpers/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@

from socket import gethostname as get_unit_hostname

import six

from charmhelpers.core.hookenv import (
log,
relation_ids,
Expand Down Expand Up @@ -125,16 +123,16 @@ def is_crm_dc():
"""
cmd = ['crm', 'status']
try:
status = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
if not isinstance(status, six.text_type):
status = six.text_type(status, "utf-8")
status = subprocess.check_output(
cmd, stderr=subprocess.STDOUT).decode('utf-8')
except subprocess.CalledProcessError as ex:
raise CRMDCNotFound(str(ex))

current_dc = ''
for line in status.split('\n'):
if line.startswith('Current DC'):
# Current DC: juju-lytrusty-machine-2 (168108163) - partition with quorum
# Current DC: juju-lytrusty-machine-2 (168108163)
# - partition with quorum
current_dc = line.split(':')[1].split()[0]
if current_dc == get_unit_hostname():
return True
Expand All @@ -158,9 +156,8 @@ def is_crm_leader(resource, retry=False):
return is_crm_dc()
cmd = ['crm', 'resource', 'show', resource]
try:
status = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
if not isinstance(status, six.text_type):
status = six.text_type(status, "utf-8")
status = subprocess.check_output(
cmd, stderr=subprocess.STDOUT).decode('utf-8')
except subprocess.CalledProcessError:
status = None

Expand Down
5 changes: 1 addition & 4 deletions charmhelpers/contrib/hardening/apache/checks/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import os
import re
import six
import subprocess


Expand Down Expand Up @@ -95,9 +94,7 @@ def __call__(self):
settings = utils.get_settings('apache')
ctxt = settings['hardening']

out = subprocess.check_output(['apache2', '-v'])
if six.PY3:
out = out.decode('utf-8')
out = subprocess.check_output(['apache2', '-v']).decode('utf-8')
ctxt['apache_version'] = re.search(r'.+version: Apache/(.+?)\s.+',
out).group(1)
ctxt['apache_icondir'] = '/usr/share/apache2/icons/'
Expand Down
8 changes: 2 additions & 6 deletions charmhelpers/contrib/hardening/audits/apache.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import re
import subprocess

import six

from charmhelpers.core.hookenv import (
log,
INFO,
Expand All @@ -35,7 +33,7 @@ class DisabledModuleAudit(BaseAudit):
def __init__(self, modules):
if modules is None:
self.modules = []
elif isinstance(modules, six.string_types):
elif isinstance(modules, str):
self.modules = [modules]
else:
self.modules = modules
Expand Down Expand Up @@ -68,9 +66,7 @@ def ensure_compliance(self):
@staticmethod
def _get_loaded_modules():
"""Returns the modules which are enabled in Apache."""
output = subprocess.check_output(['apache2ctl', '-M'])
if six.PY3:
output = output.decode('utf-8')
output = subprocess.check_output(['apache2ctl', '-M']).decode('utf-8')
modules = []
for line in output.splitlines():
# Each line of the enabled module output looks like:
Expand Down
5 changes: 1 addition & 4 deletions charmhelpers/contrib/hardening/audits/apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import # required for external apt import
from six import string_types

from charmhelpers.fetch import (
apt_cache,
apt_purge
Expand Down Expand Up @@ -51,7 +48,7 @@ class RestrictedPackages(BaseAudit):

def __init__(self, pkgs, **kwargs):
super(RestrictedPackages, self).__init__(**kwargs)
if isinstance(pkgs, string_types) or not hasattr(pkgs, '__iter__'):
if isinstance(pkgs, str) or not hasattr(pkgs, '__iter__'):
self.pkgs = pkgs.split()
else:
self.pkgs = pkgs
Expand Down
3 changes: 1 addition & 2 deletions charmhelpers/contrib/hardening/audits/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
check_call,
)
from traceback import format_exc
from six import string_types
from stat import (
S_ISGID,
S_ISUID
Expand Down Expand Up @@ -63,7 +62,7 @@ def __init__(self, paths, always_comply=False, *args, **kwargs):
"""
super(BaseFileAudit, self).__init__(*args, **kwargs)
self.always_comply = always_comply
if isinstance(paths, string_types) or not hasattr(paths, '__iter__'):
if isinstance(paths, str) or not hasattr(paths, '__iter__'):
self.paths = [paths]
else:
self.paths = paths
Expand Down
13 changes: 5 additions & 8 deletions charmhelpers/contrib/hardening/harden.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import six

from collections import OrderedDict

from charmhelpers.core.hookenv import (
Expand Down Expand Up @@ -53,18 +51,17 @@ def harden(overrides=None):
overrides = []

def _harden_inner1(f):
# 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}
_logged = False

def _harden_inner2(*args, **kwargs):
# knock out hardening via a config var; normally it won't get
# disabled.
nonlocal _logged
if _DISABLE_HARDENING_FOR_UNIT_TEST:
return f(*args, **kwargs)
if not _logged['done']:
if not _logged:
log("Hardening function '%s'" % (f.__name__), level=DEBUG)
_logged['done'] = True
_logged = True
RUN_CATALOG = OrderedDict([('os', run_os_checks),
('ssh', run_ssh_checks),
('mysql', run_mysql_checks),
Expand All @@ -74,7 +71,7 @@ def _harden_inner2(*args, **kwargs):
if enabled:
modules_to_run = []
# modules will always be performed in the following order
for module, func in six.iteritems(RUN_CATALOG):
for module, func in RUN_CATALOG.items():
if module in enabled:
enabled.remove(module)
modules_to_run.append(func)
Expand Down
4 changes: 1 addition & 3 deletions charmhelpers/contrib/hardening/host/checks/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from six import string_types

from charmhelpers.contrib.hardening.audits.file import TemplatedFile
from charmhelpers.contrib.hardening.host import TEMPLATES_DIR
from charmhelpers.contrib.hardening import utils
Expand Down Expand Up @@ -41,7 +39,7 @@ def __call__(self):
# a string assume it to be octal and turn it into an octal
# string.
umask = settings['environment']['umask']
if not isinstance(umask, string_types):
if not isinstance(umask, str):
umask = '%s' % oct(umask)

ctxt = {
Expand Down
7 changes: 3 additions & 4 deletions charmhelpers/contrib/hardening/host/checks/sysctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import os
import platform
import re
import six
import subprocess

from charmhelpers.core.hookenv import (
Expand Down Expand Up @@ -183,9 +182,9 @@ def __call__(self):

ctxt['sysctl'][key] = d[2] or None

# Translate for python3
return {'sysctl_settings':
[(k, v) for k, v in six.iteritems(ctxt['sysctl'])]}
return {
'sysctl_settings': [(k, v) for k, v in ctxt['sysctl'].items()]
}


class SysctlConf(TemplatedFile):
Expand Down
7 changes: 3 additions & 4 deletions charmhelpers/contrib/hardening/mysql/checks/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import six
import subprocess

from charmhelpers.core.hookenv import (
Expand Down Expand Up @@ -82,6 +81,6 @@ class MySQLConfContext(object):
"""
def __call__(self):
settings = utils.get_settings('mysql')
# Translate for python3
return {'mysql_settings':
[(k, v) for k, v in six.iteritems(settings['security'])]}
return {
'mysql_settings': [(k, v) for k, v in settings['security'].items()]
}
6 changes: 1 addition & 5 deletions charmhelpers/contrib/hardening/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

import os
import six

from charmhelpers.core.hookenv import (
log,
Expand All @@ -27,10 +26,7 @@
from charmhelpers.fetch import apt_install
from charmhelpers.fetch import apt_update
apt_update(fatal=True)
if six.PY2:
apt_install('python-jinja2', fatal=True)
else:
apt_install('python3-jinja2', fatal=True)
apt_install('python3-jinja2', fatal=True)
from jinja2 import FileSystemLoader, Environment


Expand Down
3 changes: 1 addition & 2 deletions charmhelpers/contrib/hardening/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import grp
import os
import pwd
import six
import yaml

from charmhelpers.core.hookenv import (
Expand Down Expand Up @@ -91,7 +90,7 @@ def _apply_overrides(settings, overrides, schema):
:returns: dictionary of modules config with user overrides applied.
"""
if overrides:
for k, v in six.iteritems(overrides):
for k, v in overrides.items():
if k in schema:
if schema[k] is None:
settings[k] = v
Expand Down
Loading

0 comments on commit c63cd1b

Please sign in to comment.