From 496b97d706365da98f38ad4965a5bcb69be2db48 Mon Sep 17 00:00:00 2001 From: m0duspwnens Date: Tue, 17 Oct 2023 15:42:42 -0400 Subject: [PATCH 1/4] handle the mine file not being present before checking the size --- salt/salt/engines/master/checkmine.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/salt/salt/engines/master/checkmine.py b/salt/salt/engines/master/checkmine.py index 09e624ba3..0cfb4032d 100644 --- a/salt/salt/engines/master/checkmine.py +++ b/salt/salt/engines/master/checkmine.py @@ -26,8 +26,16 @@ def start(interval=60): for minion in manage_alived: mine_path = os.path.join(cachedir, 'minions', minion, 'mine.p') - mine_size = os.path.getsize(mine_path) - log.debug('checkmine engine: minion: %s mine_size: %i' % (minion, mine_size)) + # it is possible that a minion is alive, but there isn't a mine.p file + try: + mine_size = os.path.getsize(mine_path) + log.debug('checkmine engine: minion: %s mine_size: %i' % (minion, mine_size)) + except FileNotFoundError: + log.warning('checkmine engine: minion: %s %s does not exist' % (minion, mine_path)) + mine_flush(minion) + mine_update(minion) + continue + # For some reason the mine file can be corrupt and only be 1 byte in size if mine_size == 1: log.error('checkmine engine: found %s to be 1 byte' % mine_path) From d9862aefcfa84f10dedf2b1e95eaf685b5f6f517 Mon Sep 17 00:00:00 2001 From: m0duspwnens Date: Tue, 17 Oct 2023 17:09:52 -0400 Subject: [PATCH 2/4] handle mine.p not being present. only check if mine_ip exists, dont compare to alived ip --- salt/salt/engines/master/checkmine.py | 35 +++++++++++++++------------ 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/salt/salt/engines/master/checkmine.py b/salt/salt/engines/master/checkmine.py index 0cfb4032d..c62c1f058 100644 --- a/salt/salt/engines/master/checkmine.py +++ b/salt/salt/engines/master/checkmine.py @@ -9,6 +9,10 @@ log = logging.getLogger(__name__) local = salt.client.LocalClient() def start(interval=60): + def mine_delete(minion, func): + log.warning('checkmine engine: deleting mine function %s for %s' % (func, minion)) + local.cmd(minion, 'mine.delete', [func]) + def mine_flush(minion): log.warning('checkmine engine: flushing mine cache for %s' % minion) local.cmd(minion, 'mine.flush') @@ -21,36 +25,35 @@ def start(interval=60): cachedir = __opts__['cachedir'] while True: log.debug('checkmine engine: checking which minions are alive') - manage_alived = __salt__['saltutil.runner']('manage.alived', show_ip=True) + manage_alived = __salt__['saltutil.runner']('manage.alived', show_ip=False) log.debug('checkmine engine: alive minions: %s' % ' , '.join(manage_alived)) for minion in manage_alived: mine_path = os.path.join(cachedir, 'minions', minion, 'mine.p') - # it is possible that a minion is alive, but there isn't a mine.p file + # it is possible that a minion is alive, but hasn't created a mine file yet try: mine_size = os.path.getsize(mine_path) log.debug('checkmine engine: minion: %s mine_size: %i' % (minion, mine_size)) + # For some reason the mine file can be corrupt and only be 1 byte in size + if mine_size == 1: + log.error('checkmine engine: found %s to be 1 byte' % mine_path) + mine_flush(minion) + mine_update(minion) + continue except FileNotFoundError: log.warning('checkmine engine: minion: %s %s does not exist' % (minion, mine_path)) mine_flush(minion) mine_update(minion) continue - # For some reason the mine file can be corrupt and only be 1 byte in size - if mine_size == 1: - log.error('checkmine engine: found %s to be 1 byte' % mine_path) - mine_flush(minion) - mine_update(minion) # Update the mine if the ip in the mine doesn't match returned from manage.alived - else: - network_ip_addrs = __salt__['saltutil.runner']('mine.get', tgt=minion, fun='network.ip_addrs') + network_ip_addrs = __salt__['saltutil.runner']('mine.get', tgt=minion, fun='network.ip_addrs') + try: mine_ip = network_ip_addrs[minion][0] - log.debug('checkmine engine: minion: %s mine_ip: %s' % (minion, mine_ip)) - manage_alived_ip = manage_alived[minion] - log.debug('checkmine engine: minion: %s managed_alived_ip: %s' % (minion, manage_alived_ip)) - if mine_ip != manage_alived_ip: - log.error('checkmine engine: found minion %s has manage_alived_ip %s but a mine_ip of %s' % (minion, manage_alived_ip, mine_ip)) - mine_flush(minion) - mine_update(minion) + log.debug('checkmine engine: found minion %s has mine_ip: %s' % (minion, mine_ip)) + except IndexError: + log.error('checkmine engine: found minion %s does\'t have a mine_ip' % (minion)) + mine_delete(minion, 'network.ip_addrs') + mine_update(minion) sleep(interval) From 928fb23e963c425098a4105ef91c965a9a6f814e Mon Sep 17 00:00:00 2001 From: m0duspwnens Date: Tue, 17 Oct 2023 17:28:28 -0400 Subject: [PATCH 3/4] only add node to pillar if returned ip from mine --- pillar/logstash/nodes.sls | 22 +++++++++++++--------- pillar/node_data/ips.sls | 24 ++++++++++++++---------- pillar/nodegroups/init.sls | 0 3 files changed, 27 insertions(+), 19 deletions(-) create mode 100644 pillar/nodegroups/init.sls diff --git a/pillar/logstash/nodes.sls b/pillar/logstash/nodes.sls index 8d3bdab65..228122b03 100644 --- a/pillar/logstash/nodes.sls +++ b/pillar/logstash/nodes.sls @@ -7,19 +7,23 @@ tgt_type='compound') | dictsort() %} -{% set hostname = cached_grains[minionid]['host'] %} -{% set node_type = minionid.split('_')[1] %} -{% if node_type not in node_types.keys() %} -{% do node_types.update({node_type: {hostname: ip[0]}}) %} -{% else %} -{% if hostname not in node_types[node_type] %} -{% do node_types[node_type].update({hostname: ip[0]}) %} +# only add a node to the pillar if it returned an ip from the mine +{% if ip[0] | length > 0%} +{% set hostname = cached_grains[minionid]['host'] %} +{% set node_type = minionid.split('_')[1] %} +{% if node_type not in node_types.keys() %} +{% do node_types.update({node_type: {hostname: ip[0]}}) %} {% else %} -{% do node_types[node_type][hostname].update(ip[0]) %} +{% if hostname not in node_types[node_type] %} +{% do node_types[node_type].update({hostname: ip[0]}) %} +{% else %} +{% do node_types[node_type][hostname].update(ip[0]) %} +{% endif %} {% endif %} -{% endif %} +{% fi %} {% endfor %} + logstash: nodes: {% for node_type, values in node_types.items() %} diff --git a/pillar/node_data/ips.sls b/pillar/node_data/ips.sls index 59c598879..5801d36f1 100644 --- a/pillar/node_data/ips.sls +++ b/pillar/node_data/ips.sls @@ -4,18 +4,22 @@ {% set hostname = minionid.split('_')[0] %} {% set node_type = minionid.split('_')[1] %} {% set is_alive = False %} -{% if minionid in manage_alived.keys() %} -{% if ip[0] == manage_alived[minionid] %} -{% set is_alive = True %} + +# only add a node to the pillar if it returned an ip from the mine +{% if ip | length > 0%} +{% if minionid in manage_alived.keys() %} +{% if ip[0] == manage_alived[minionid] %} +{% set is_alive = True %} +{% endif %} {% endif %} -{% endif %} -{% if node_type not in node_types.keys() %} -{% do node_types.update({node_type: {hostname: {'ip':ip[0], 'alive':is_alive }}}) %} -{% else %} -{% if hostname not in node_types[node_type] %} -{% do node_types[node_type].update({hostname: {'ip':ip[0], 'alive':is_alive}}) %} +{% if node_type not in node_types.keys() %} +{% do node_types.update({node_type: {hostname: {'ip':ip[0], 'alive':is_alive }}}) %} {% else %} -{% do node_types[node_type][hostname].update({'ip':ip[0], 'alive':is_alive}) %} +{% if hostname not in node_types[node_type] %} +{% do node_types[node_type].update({hostname: {'ip':ip[0], 'alive':is_alive}}) %} +{% else %} +{% do node_types[node_type][hostname].update({'ip':ip[0], 'alive':is_alive}) %} +{% endif %} {% endif %} {% endif %} {% endfor %} diff --git a/pillar/nodegroups/init.sls b/pillar/nodegroups/init.sls new file mode 100644 index 000000000..e69de29bb From fb9a0ab8b627e51dc1c6898bd7b3f4f05ca2b3aa Mon Sep 17 00:00:00 2001 From: m0duspwnens Date: Tue, 17 Oct 2023 17:33:53 -0400 Subject: [PATCH 4/4] endif not fi in jinja --- pillar/logstash/nodes.sls | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pillar/logstash/nodes.sls b/pillar/logstash/nodes.sls index 228122b03..a77978821 100644 --- a/pillar/logstash/nodes.sls +++ b/pillar/logstash/nodes.sls @@ -8,7 +8,7 @@ %} # only add a node to the pillar if it returned an ip from the mine -{% if ip[0] | length > 0%} +{% if ip | length > 0%} {% set hostname = cached_grains[minionid]['host'] %} {% set node_type = minionid.split('_')[1] %} {% if node_type not in node_types.keys() %} @@ -20,7 +20,7 @@ {% do node_types[node_type][hostname].update(ip[0]) %} {% endif %} {% endif %} -{% fi %} +{% endif %} {% endfor %}