pythonsalt-project

Unable to import custom libs in salt module


I have a module to work in bmi_minion (which is inside a container that is works in Kubernetes) to fetch some data. for this main module, I've written some custom libs which are stored in the same dir as the main module (/srv/salt/_modules).

My custom lib script is like below.

import subprocess
import json
import os
import logging


log = logging.getLogger(__file__)
CACHE_FILE_PATH = '/tmp/cache_hw_type_data.json'


class SystemInfoCollector:
    @staticmethod
    def execute_shell_cmd(cmd):
        ...

    @staticmethod
    def get_ipmi_sdr_list():
        ...

    @staticmethod
    def get_dmidecode_list(option='sys'):
        ...

    @staticmethod
    def get_ras_data(*args, option=''):
        ...

    @staticmethod
    def get_system_info(*args, option='os'):
        ...


class HWTypeCacheData:
    @staticmethod
    def load_hw_type_from_cache():
        ...

    @staticmethod
    def save_data_to_cache(hw_type):
        ...

    @staticmethod
    def get_hw_type():
        ...

My issue is, when I import this script as a lib in my main salt module, and ran the same by targeting the ms_minion, I'm facing issue like mentioned below.

My Main salt module

import logging
import re
from datetime import datetime, date, timedelta

from iaas_asset_manager.device42.client import Device42Client
from dimm_constants import DIMMConstants, DIMMMapping
from system_info_collector import SystemInfoCollector, HWTypeCacheData

log = logging.getLogger(__file__)

__virtualname__ = "dimm_errors"
CACHE_FILE_PATH = '/tmp/cache_hw_type_data.json'


def __virtual__():
    return __virtualname__


def _get_hardware_info():
    # Retrieve hardware information like make, model, and serial number
    dmidecode_bm_data = SystemInfoCollector.get_dmidecode_list(option='sys')
    bm_data = {}
    for data in dmidecode_bm_data:
        key = data.split(':')[0].strip()
        if key == 'Manufacturer':
.......

Error I'm getting when I target the minion

arun.srinivasan@dcim-fault-detection-none-20069166:/srv/salt/_modules$ sudo salt 'salt.bmi_minion.20083693' dimm_errors
salt.bmoi_minion.20083693:
    'dimm_errors' __virtual__ returned False: cannot import name 'HWTypeCacheData' from 'system_info_collector' (/var/cache/salt/minion/extmods/modules/system_info_collector.py)

Note: If I separate the custom lib script into two files containing both classes separately, importing is working fine.

What is the issue with current structure of these files?


Solution

  • I figured out the issue. Sometimes, custom libs are imported and cached in the memory. After new edits, we need to do sync_all, state.apply and restart minion. Unless restarted, new changes are not applied to the minions.