Add default configuration

This commit is contained in:
Bo Maryniuk 2016-06-07 14:41:47 +02:00
parent 94f98b4ab8
commit a52f9f7107
4 changed files with 50 additions and 31 deletions

View file

@ -13,3 +13,35 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from salt.modules.inspectlib.exceptions import InspectorSnapshotException
from salt.modules.inspectlib.dbhandle import DBHandle
class EnvLoader(object):
'''
Load environment.
'''
PID_FILE = '_minion_collector.pid'
DB_FILE = '_minion_collector.db'
DEFAULT_PID_PATH = '/var/run'
DEFAULT_CACHE_PATH = '/var/cache/salt'
def __init__(self, cachedir=None, piddir=None):
'''
Constructor.
:param options:
:param db_path:
:param pid_file:
'''
if not cachedir and '__salt__' in globals():
cachedir = globals().get('__salt__')['config.get']('inspector.db', '')
self.dbfile = os.path.join(cachedir or self.DEFAULT_CACHE_PATH, self.DB_FILE)
self.db = DBHandle(self.dbfile)
if not piddir and '__salt__' in globals():
piddir = globals().get('__salt__')['config.get']('inspector.pid', '')
self.pidfile = os.path.join(piddir or self.DEFAULT_PID_PATH, self.PID_FILE)

View file

@ -21,14 +21,15 @@ import sys
from subprocess import Popen, PIPE, STDOUT
# Import Salt Libs
from salt.modules.inspectlib.dbhandle import DBHandle
from salt.modules.inspectlib.exceptions import (InspectorSnapshotException)
from salt.modules.inspectlib import EnvLoader
from salt.modules.inspectlib.dbhandle import DBHandle
import salt.utils
from salt.utils import fsutils
from salt.utils import reinit_crypto
class Inspector(object):
class Inspector(EnvLoader):
DEFAULT_MINION_CONFIG_PATH = '/etc/salt/minion'
MODE = ['configuration', 'payload', 'all']
@ -38,27 +39,10 @@ class Inspector(object):
"/var/lib/rpm", "/.snapshots", "/.zfs", "/etc/ssh",
"/root", "/home"]
def __init__(self, db_path=None, pid_file=None):
# Configured path
if not db_path and '__salt__' in globals():
db_path = globals().get('__salt__')['config.get']('inspector.db', '')
if not db_path:
raise InspectorSnapshotException('Inspector database location is not configured yet in minion.\n'
'Add "inspector.db: /path/to/cache" in "/etc/salt/minion".')
self.dbfile = db_path
self.db = DBHandle(self.dbfile)
def __init__(self, cachedir=None, piddir=None):
EnvLoader.__init__(self, cachedir=cachedir, piddir=piddir)
self.db.open()
if not pid_file and '__salt__' in globals():
pid_file = globals().get('__salt__')['config.get']('inspector.pid', '')
if not pid_file:
raise InspectorSnapshotException("Inspector PID file location is not configured yet in minion.\n"
'Add "inspector.pid: /path/to/pids in "/etc/salt/minion".')
self.pidfile = pid_file
def _syscall(self, command, input=None, env=None, *params):
'''
Call an external system command.
@ -411,7 +395,7 @@ class Inspector(object):
self._prepare_full_scan(**kwargs)
os.system("nice -{0} python {1} {2} {3} {4} & > /dev/null".format(
priority, __file__, self.pidfile, self.dbfile, mode))
priority, __file__, os.path.dirname(self.pidfile), os.path.dirname(self.dbfile), mode))
def is_alive(pidfile):
@ -458,7 +442,7 @@ if __name__ == '__main__':
pid = os.fork()
if pid > 0:
reinit_crypto()
fpid = open(pidfile, "w")
fpid = open(os.path.join(pidfile, EnvLoader.PID_FILE), "w")
fpid.write("{0}\n".format(pid))
fpid.close()
sys.exit(0)

View file

@ -22,8 +22,8 @@ import logging
# Import Salt Libs
import salt.utils.network
from salt.modules.inspectlib.dbhandle import DBHandle
from salt.modules.inspectlib.exceptions import (InspectorQueryException, SIException)
from salt.modules.inspectlib import EnvLoader
log = logging.getLogger(__name__)
@ -53,8 +53,8 @@ class SysInfo(object):
log.error(msg)
raise SIException(msg)
devpath, blocks, used, available, used_p, mountpoint = [elm for elm in out['stdout'].split(os.linesep)[-1].split(" ") if elm]
devpath, blocks, used, available, used_p, mountpoint = [elm for elm in
out['stdout'].split(os.linesep)[-1].split(" ") if elm]
return {
'device': devpath, 'blocks': blocks, 'used': used,
'available': available, 'used (%)': used_p, 'mounted': mountpoint,
@ -135,7 +135,7 @@ class SysInfo(object):
}
class Query(object):
class Query(EnvLoader):
'''
Query the system.
This class is actually puts all Salt features together,
@ -153,7 +153,7 @@ class Query(object):
SCOPES = ["changes", "configuration", "identity", "system", "software", "services", "payload", "all"]
def __init__(self, scope):
def __init__(self, scope, cachedir=None):
'''
Constructor.
@ -163,8 +163,8 @@ class Query(object):
if scope not in self.SCOPES:
raise InspectorQueryException(
"Unknown scope: {0}. Must be one of: {1}".format(repr(scope), ", ".join(self.SCOPES)))
EnvLoader.__init__(self, cachedir=cachedir)
self.scope = '_' + scope
self.db = DBHandle(globals()['__salt__']['config.get']('inspector.db', ''))
self.local_identity = dict()
def __call__(self, *args, **kwargs):

View file

@ -19,6 +19,7 @@ Module for full system inspection.
'''
from __future__ import absolute_import
import logging
import os
from salt.modules.inspectlib.exceptions import (InspectorQueryException,
InspectorSnapshotException)
@ -89,7 +90,9 @@ def inspect(mode='all', priority=19, **kwargs):
'''
collector = _("collector")
try:
return collector.Inspector().request_snapshot(mode, priority=priority, **kwargs)
return collector.Inspector(cachedir=__opts__['cachedir'],
piddir=os.path.dirname(__opts__['pidfile']))\
.request_snapshot(mode, priority=priority, **kwargs)
except InspectorSnapshotException as ex:
raise CommandExecutionError(ex)
except Exception as ex:
@ -154,7 +157,7 @@ def query(scope, **kwargs):
'''
query = _("query")
try:
return query.Query(scope)(**kwargs)
return query.Query(scope, cachedir=__opts__['cachedir'])(**kwargs)
except InspectorQueryException as ex:
raise CommandExecutionError(ex)
except Exception as ex: