Skip to content

Commit

Permalink
Add tracing of execution
Browse files Browse the repository at this point in the history
(to make it working, there should be NO --check flag) i.e.
$ ./ansible-playbook check.yml -i inventory -e env=dev # --check
IMPORTANT: Make sure there is an empty file ansible.txt
The blockinfile doesn't create it even if you specify
- blockinfile:
    state: absent

Besides, there are
* Workaround: crmsh_callback: comment out crmsh.ui_check.
This module cannot be found and the whole crmsh_callback won't be
loaded.
* Remove unnecessary checks
  • Loading branch information
Aleksei Burlakov committed Jul 24, 2024
1 parent 4b38e34 commit aeb9d34
Show file tree
Hide file tree
Showing 77 changed files with 1,386 additions and 1,115 deletions.
33 changes: 33 additions & 0 deletions checks/ansible-playbook
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/python3.11
# EASY-INSTALL-ENTRY-SCRIPT: 'ansible-core==2.16.8','console_scripts','ansible-playbook'
import re
import sys

# for compatibility with easy_install; see #2198
__requires__ = 'ansible-core==2.16.8'

try:
from importlib.metadata import distribution
except ImportError:
try:
from importlib_metadata import distribution
except ImportError:
from pkg_resources import load_entry_point


def importlib_load_entry_point(spec, group, name):
dist_name, _, _ = spec.partition('==')
matches = (
entry_point
for entry_point in distribution(dist_name).entry_points
if entry_point.group == group and entry_point.name == name
)
return next(matches).load()


globals().setdefault('load_entry_point', importlib_load_entry_point)


if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(load_entry_point('ansible-core==2.16.8', 'console_scripts', 'ansible-playbook')())
Empty file added checks/ansible.txt
Empty file.
13 changes: 11 additions & 2 deletions checks/callback_plugins/crmsh_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@
import os
import yaml
import requests
from crmsh.ui_check import load_checks_meta_information
#from crmsh.ui_check import load_checks_meta_information
#import callback_plugins.scripts.collect as collect
#import callback_plugins.scripts.hahealth as hahealth
#import callback_plugins.scripts.report as report

from ansible.plugins.callback import CallbackBase

TEST_RESULT_TASK_NAME = "set_test_result"
TEST_INCLUDE_TASK_NAME = "run_checks"
CHECK_ID = "id"

with open('qwerty.txt', 'a+') as f:
f.write('HUI')

class Results(object):
"""
Expand Down Expand Up @@ -135,6 +140,10 @@ def __init__(self):
self.play = None
self.results = Results()
self.meta_information = {}
# collect.do_collect()
# hahealth.do_hahealth()
report.do_report()


def v2_playbook_on_start(self, playbook):
"""
Expand All @@ -149,7 +158,7 @@ def v2_playbook_on_play_start(self, play):
self.play = play
self._initialize_results()
env = self._all_vars()["env"]
self.meta_information = load_checks_meta_information(env)
#self.meta_information = load_checks_meta_information(env)

def v2_runner_on_ok(self, result):
"""
Expand Down
115 changes: 115 additions & 0 deletions checks/callback_plugins/scripts/collect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#!/usr/bin/python3
from __future__ import unicode_literals
from builtins import str
import os
import pwd
import hashlib
import platform
import callback_plugins.scripts.crm_script as crm_script

import crmsh.log
crmsh.log.setup_logging()
from crmsh.report import utils

try:
import json
except ImportError:
import simplejson as json

data = crm_script.get_input()

PACKAGES = ['booth', 'cluster-glue', 'corosync', 'crmsh', 'csync2', 'drbd',
'fence-agents', 'gfs2', 'gfs2-utils', 'ha-cluster-bootstrap',
'haproxy', 'hawk', 'libdlm', 'libqb', 'ocfs2', 'ocfs2-tools',
'pacemaker', 'pacemaker-mgmt', 'resource-agents', 'sbd']


def rpm_info():
return crm_script.rpmcheck(PACKAGES)


def logrotate_info():
return {}


def get_user():
return pwd.getpwuid(os.getuid()).pw_name


def sys_info():
sysname, nodename, release, version, machine = os.uname()
# The first three columns measure CPU and IO utilization of the
# last one, five, and 15 minute periods. The fourth column shows
# the number of currently running processes and the total number of
# processes. The last column displays the last process ID used.
system, node, release, version, machine, processor = platform.uname()
distname = utils.get_distro_info()
hostname = os.uname()[1]

uptime = open('/proc/uptime').read().split()
loadavg = open('/proc/loadavg').read().split()

return {'system': system,
'node': node,
'release': release,
'version': version,
'machine': machine,
'processor': processor,
'distname': distname,
'user': get_user(),
'hostname': hostname,
'uptime': uptime[0],
'idletime': uptime[1],
'loadavg': loadavg[2] # 15 minute average
}


def disk_info():
rc, out, err = crm_script.call(['df'], shell=False)
if rc == 0:
disk_use = []
for line in out.split('\n')[1:]:
line = line.strip()
if line:
data = line.split()
if len(data) >= 6:
disk_use.append((data[5], data[4]))
return disk_use
return []


# configurations out of sync

FILES = [
'/etc/csync2/key_hagroup',
'/etc/csync2/csync2.cfg',
'/etc/corosync/corosync.conf',
'/etc/sysconfig/sbd'
]


def files_info():
ret = {}
for f in FILES:
if os.path.isfile(f):
try:
ret[f] = hashlib.sha1(open(f).read().encode('utf-8')).hexdigest()
except IOError as e:
ret[f] = "error: %s" % (e)
else:
ret[f] = ""
return ret

def do_collect():
try:
data = {
'rpm': rpm_info(),
'logrotate': logrotate_info(),
'system': sys_info(),
'disk': disk_info(),
'files': files_info()
}
#crm_script.exit_ok(data)
print(json.dumps(data))
except Exception as e:
crm_script.exit_fail(str(e))
46 changes: 46 additions & 0 deletions checks/callback_plugins/scripts/crm_clean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/python3
import os
import sys
import shutil
errors = []
mydir = os.path.dirname(os.path.abspath(sys.modules[__name__].__file__))


def bad(path):
return ((not os.path.isabs(path)) or os.path.dirname(path) == '/' or
path.startswith('/var') or path.startswith('/usr') or
(not path.startswith(mydir)))

for f in sys.argv[1:]:
if bad(f):
errors.append("cannot remove %s from %s" % (f, mydir))
continue
try:
if os.path.isfile(f):
os.remove(f)
elif os.path.isdir(f):
if os.path.isfile(os.path.join(f, 'crm_script.debug')):
print(open(os.path.join(f, 'crm_script.debug')).read())

# to check whether this clean request came from health
# if it does, delete all except health-report
del_flag = 0
for x in os.listdir(f):
if x.startswith("health-report"):
del_flag = 1

if del_flag == 1:
for x in os.listdir(f):
if x.startswith("health-report"):
continue
if os.path.isfile(x):
os.remove(x)
elif os.path.isdir(x):
shutil.rmtree(x)
else:
shutil.rmtree(f)
except OSError as e:
errors.append(e)
if errors:
print('\n'.join(errors), file=sys.stderr)
sys.exit(1)
Loading

0 comments on commit aeb9d34

Please sign in to comment.