desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Take a load and send it across the network to connected minions'
def _send_pub(self, load):
for (transport, opts) in iter_transport_opts(self.opts): chan = salt.transport.server.PubServerChannel.factory(opts) chan.publish(load)
'Take a given load and perform the necessary steps to prepare a publication. TODO: This is really only bound by temporal cohesion and thus should be refactored even further.'
def _prep_pub(self, minions, jid, clear_load, extra):
clear_load[u'jid'] = jid delimiter = clear_load.get(u'kwargs', {}).get(u'delimiter', DEFAULT_TARGET_DELIM) self.event.fire_event({u'minions': minions}, clear_load[u'jid']) new_job_load = {u'jid': clear_load[u'jid'], u'tgt_type': clear_load[u'tgt_type'], u'tgt': clear_load[u'tgt'], u'user': clear_load[u'...
'Send the load back to the sender.'
def ping(self, clear_load):
return clear_load
'Prepare the needed objects and socket for iteration within ioflo'
def setup(self):
salt.utils.appendproctitle(self.__class__.__name__) self.clear_funcs = salt.master.ClearFuncs(self.opts, self.key) self.aes_funcs = salt.master.AESFuncs(self.opts) self.context = zmq.Context(1) self.socket = self.context.socket(zmq.REP) if (self.opts.get(u'ipc_mode', u'') == u'tcp'): sel...
'Handle a single request'
def handle_request(self):
try: polled = self.poller.poll(1) if polled: package = self.socket.recv() self._update_aes() payload = self.serial.loads(package) ret = self.serial.dumps(self._handle_payload(payload)) self.socket.send(ret) except KeyboardInterrupt: ...
'string value, only works for full repr'
def __str__(self):
return self.dict_to_line(self.criteria)
'always works'
def __repr__(self):
return str(self.criteria)
'returns an instance with just those keys'
def pick(self, keys):
subset = dict([(key, self.criteria[key]) for key in keys]) return self.__class__(**subset)
'Store non-empty, non-null values to use as filter'
def __init__(self, **criteria):
items = [key_value for key_value in six.iteritems(criteria) if (key_value[1] is not None)] items = [(key_value1[0], str(key_value1[1])) for key_value1 in items] self.criteria = dict(items)
'Resolve equivalent paths equivalently'
@staticmethod def norm_path(path):
return os.path.normcase(os.path.normpath(path))
'compare potentially partial criteria against line'
def match(self, line):
entry = self.dict_from_line(line) for (key, value) in six.iteritems(self.criteria): if (entry[key] != value): return False return True
'string value, only works for full repr'
def __str__(self):
return self.dict_to_line(self.criteria)
'always works'
def __repr__(self):
return str(self.criteria)
'returns an instance with just those keys'
def pick(self, keys):
subset = dict([(key, self.criteria[key]) for key in keys]) return self.__class__(**subset)
'Store non-empty, non-null values to use as filter'
def __init__(self, **criteria):
items = [key_value for key_value in six.iteritems(criteria) if (key_value[1] is not None)] items = [(key_value1[0], str(key_value1[1])) for key_value1 in items] self.criteria = dict(items)
'Resolve equivalent paths equivalently'
@staticmethod def norm_path(path):
return os.path.normcase(os.path.normpath(path))
'compare potentially partial criteria against line'
def match(self, line):
entry = self.dict_from_line(line) for (key, value) in six.iteritems(self.criteria): if (entry[key] != value): return False return True
'this function generates a search string. simplifying the search function while still providing as many features as possible.'
def AutoSearch(self):
search_string = '' searchParams = [] if self.skipInstalled: searchParams.append('IsInstalled=0') else: searchParams.append('IsInstalled=1') if self.skipHidden: searchParams.append('IsHidden=0') else: searchParams.append('IsHidden=1') if self.skipReboot: ...
'this gets results of installation process.'
def GetInstallationResults(self):
log.debug('blugger has {0} updates in it'.format(self.install_collection.Count)) if (self.install_collection.Count == 0): return {} updates = [] log.debug('repairing update list') for i in range(self.install_collection.Count): updates.append('{0}: {1}'.format(...
'converts the installation results into a pretty print.'
def GetInstallationResultsPretty(self):
updates = self.GetInstallationResults() ret = 'The following are the updates and their return codes.\n' for i in updates: ret += ' DCTB {0}\n'.format(updates[i]) return ret
'Reduce full updates information to the most important information.'
def GetSearchResults(self, fields=None):
updates_verbose = self.GetSearchResultsVerbose() if (fields is not None): updates = [dict(((k, v) for (k, v) in update.items() if (k in fields))) for update in updates_verbose] return updates return [update['Title'] for update in updates_verbose]
'get the value for user in gsettings'
def _get(self):
user = self.USER try: uid = pwd.getpwnam(user).pw_uid except KeyError: log.info('User does not exist') return False cmd = 'dbus-launch --exit-with-session gsettings get {0} {1}'.format(self.SCHEMA, self.KEY) environ = {} environ['XDG_RUNTIME_DIR'] ...
'set the value for user in gsettings'
def _set(self, value):
user = self.USER try: uid = pwd.getpwnam(user).pw_uid except KeyError: log.info('User does not exist') result = {} result['retcode'] = 1 result['stdout'] = 'User {0} does not exist'.format(user) return result cmd = 'dbus-launch --ex...
'String value, only works for full repr'
def __str__(self):
return self.dict_to_line(self.criteria)
'Always works'
def __repr__(self):
return str(self.criteria)
'Returns an instance with just those keys'
def pick(self, keys):
subset = dict([(key, self.criteria[key]) for key in keys]) return self.__class__(**subset)
'Store non-empty, non-null values to use as filter'
def __init__(self, **criteria):
self.criteria = {key: str(value) for (key, value) in six.iteritems(criteria) if (value is not None)}
'Resolve equivalent paths equivalently'
@staticmethod def norm_path(path):
return os.path.normcase(os.path.normpath(path))
'Compare potentially partial criteria against a complete line'
def match(self, line):
entry = self.dict_from_line(line) for (key, value) in six.iteritems(self.criteria): if (entry[key] != value): return False return True
'Builds the header of a syslog-ng configuration object.'
def build_header(self):
return ''
'Builds the tail of a syslog-ng configuration object.'
def build_tail(self):
return ''
'Builds the body of a syslog-ng configuration object.'
def build_body(self):
_increase_indent() body_array = [x.build() for x in self.iterable] nl = ('\n' if self.append_extra_newline else '') if (len(self.iterable) >= 1): body = (self.join_body_on.join(body_array) + nl) else: body = '' _decrease_indent() return body
'Builds the textual representation of the whole configuration object with it\'s children.'
def build(self):
header = self.build_header() body = self.build_body() tail = self.build_tail() return ((header + body) + tail)
'Alternate constructor that accept multiple recipients and rooms'
@classmethod def create_multi(cls, jid, password, msg, recipients=None, rooms=None, nick='SaltStack Bot'):
obj = SendMsgBot(jid, password, None, msg) obj.recipients = ([] if (recipients is None) else recipients) obj.rooms = ([] if (rooms is None) else rooms) obj.nick = nick return obj
'Constructor'
def __init__(self):
self.__called = False self._reset()
'Resets values of the call setup. :return:'
def _reset(self):
self.__cmd = ['zypper', '--non-interactive'] self.__exit_code = 0 self.__call_result = dict() self.__error_msg = '' self.__env = {'SALT_RUNNING': '1'} self.__xml = False self.__no_lock = False self.__no_raise = False self.__refresh = False self.__ignore_repo_failure = False s...
':param args: :param kwargs: :return:'
def __call__(self, *args, **kwargs):
if ('no_repo_failure' in kwargs): self.__ignore_repo_failure = kwargs['no_repo_failure'] if ('systemd_scope' in kwargs): self.__systemd_scope = kwargs['systemd_scope'] return self
'Call configurator. :param item: :return:'
def __getattr__(self, item):
if self.__called: self._reset() self.__called = False if (item == 'xml'): self.__xml = True elif (item == 'nolock'): self.__no_lock = True elif (item == 'noraise'): self.__no_raise = True elif (item == 'refreshable'): self.__refresh = True elif (it...
'Is this is an error code? :return:'
def _is_error(self):
return (self.exit_code not in self.SUCCESS_EXIT_CODES)
'Is this is a lock error code? :return:'
def _is_lock(self):
return (self.exit_code == self.LOCK_EXIT_CODE)
'Is Zypper\'s output is in XML format? :return:'
def _is_xml_mode(self):
return (([itm for itm in self.XML_DIRECTIVES if (itm in self.__cmd)] and True) or False)
'Check and set the result of a zypper command. In case of an error, either raise a CommandExecutionError or extract the error. result The result of a zypper command called with cmd.run_all'
def _check_result(self):
if (not self.__call_result): raise CommandExecutionError('No output result from Zypper?') self.exit_code = self.__call_result['retcode'] if self._is_lock(): return False if self._is_error(): _error_msg = list() if (not self._is_xml_mode()): msg = (...
'Call Zypper. :param state: :return:'
def __call(self, *args, **kwargs):
self.__called = True if self.__xml: self.__cmd.append('--xmlout') if (not self.__refresh): self.__cmd.append('--no-refresh') self.__cmd.extend(args) kwargs['output_loglevel'] = 'trace' kwargs['python_shell'] = False kwargs['env'] = self.__env.copy() if self.__no_lock: ...
':type zypper: a reference to an instance of a _Zypper class.'
def __init__(self, zypper):
self.name = None self.version = None self.zypper = zypper self._attr_solvable_version = 'edition' self._op = None
'Convert a string wildcard to a zypper query. :param pkg_name: :param pkg_version: :return:'
def __call__(self, pkg_name, pkg_version):
if pkg_version: self.name = pkg_name self._set_version(pkg_version) versions = sorted([LooseVersion(vrs) for vrs in self._get_scope_versions(self._get_available_versions())]) return ((versions and '{0}{1}'.format((self._op or ''), versions[(-1)])) or None)
'Get available versions of the package. :return:'
def _get_available_versions(self):
solvables = self.zypper.nolock.xml.call('se', '-xv', self.name).getElementsByTagName('solvable') if (not solvables): raise CommandExecutionError("No packages found matching '{0}'".format(self.name)) return sorted(set([slv.getAttribute(self._attr_solvable_version) for slv in solvables if ...
'Get available difference between next possible matches. :return:'
def _get_scope_versions(self, pkg_versions):
get_in_versions = [] for p_version in pkg_versions: if fnmatch.fnmatch(p_version, self.version): get_in_versions.append(p_version) return get_in_versions
'Stash operator from the version, if any. :return:'
def _set_version(self, version):
if (not version): return exact_version = re.sub('[<>=+]*', '', version) self._op = (version.replace(exact_version, '') or None) if (self._op and (self._op not in self.Z_OP)): raise CommandExecutionError('Zypper do not supports operator "{0}".'.format(self._op)) self.ve...
'Constructor. :param options: :param db_path: :param pid_file:'
def __init__(self, cachedir=None, piddir=None, pidfilename=None):
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())): piddi...
'Load data by keys. :param data: :return:'
def load(self, **descr):
for (obj, data) in descr.items(): setattr(self._data, obj, data) return self
'Export to the Kiwi config.xml as text. :return:'
def export(self, name):
self.name = name root = self._create_doc() self._set_description(root) self._set_preferences(root) self._set_repositories(root) self._set_users(root) self._set_packages(root) return '\n'.join([line for line in minidom.parseString(etree.tostring(root, encoding='UTF-8', pretty_print=True))...
'Get package manager. :return:'
def _get_package_manager(self):
ret = None if (self.__grains__.get('os_family') in ('Kali', 'Debian')): ret = 'apt-get' elif (self.__grains__.get('os_family', '') == 'Suse'): ret = 'zypper' elif (self.__grains__.get('os_family', '') == 'redhat'): ret = 'yum' if (ret is None): raise InspectorKiwiProc...
'Set preferences. :return:'
def _set_preferences(self, node):
pref = etree.SubElement(node, 'preferences') pacman = etree.SubElement(pref, 'packagemanager') pacman.text = self._get_package_manager() p_version = etree.SubElement(pref, 'version') p_version.text = '0.0.1' p_type = etree.SubElement(pref, 'type') p_type.set('image', 'vmx') for (disk_id,...
'Get user groups. :param user: :return:'
def _get_user_groups(self, user):
return ([g.gr_name for g in grp.getgrall() if (user in g.gr_mem)] + [grp.getgrgid(pwd.getpwnam(user).pw_gid).gr_name])
'Create existing local users. <users group="root"> <user password="$1$wYJUgpM5$RXMMeASDc035eX.NbYWFl0" home="/root" name="root"/> </users> :param node: :return:'
def _set_users(self, node):
shadow = {} with salt.utils.files.fopen('/etc/shadow') as rfh: for sh_line in rfh.read().split(os.linesep): if sh_line.strip(): (login, pwd) = sh_line.split(':')[:2] if (pwd and (pwd[0] not in '!*')): shadow[login] = {'p': pwd} with sal...
'Create repositories. :param node: :return:'
def _set_repositories(self, node):
priority = 99 for (repo_id, repo_data) in self._data.software.get('repositories', {}).items(): if (type(repo_data) == list): repo_data = repo_data[0] if (repo_data.get('enabled') or (not repo_data.get('disabled'))): uri = repo_data.get('baseurl', repo_data.get('uri')) ...
'Set packages and collections. :param node: :return:'
def _set_packages(self, node):
pkgs = etree.SubElement(node, 'packages') for (pkg_name, pkg_version) in sorted(self._data.software.get('packages', {}).items()): pkg = etree.SubElement(pkgs, 'package') pkg.set('name', pkg_name) if (self.__grains__.get('os_family', '') == 'Suse'): for (ptn_id, ptn_data) in self._dat...
'Create a system description. :return:'
def _set_description(self, node):
hostname = (socket.getfqdn() or platform.node()) descr = etree.SubElement(node, 'description') author = etree.SubElement(descr, 'author') author.text = 'salt.modules.node on {0}'.format(hostname) contact = etree.SubElement(descr, 'contact') contact.text = 'root@{0}'.format(hostname) sp...
'Create document. :return:'
def _create_doc(self):
root = etree.Element('image') root.set('schemaversion', '6.3') root.set('name', self.name) return root
'An alias for grains getter.'
def _grain(self, grain):
return __grains__.get(grain, 'N/A')
'Get a size of a disk.'
def _get_disk_size(self, device):
out = __salt__['cmd.run_all']('df {0}'.format(device)) if out['retcode']: msg = 'Disk size info error: {0}'.format(out['stderr']) log.error(msg) raise SIException(msg) (devpath, blocks, used, available, used_p, mountpoint) = [elm for elm in out['stdout'].split(os.lines...
'Get available file systems and their types.'
def _get_fs(self):
data = dict() for (dev, dev_data) in salt.utils.fsutils._blkid().items(): dev = self._get_disk_size(dev) device = dev.pop('device') dev['type'] = dev_data['type'] data[device] = dev return data
'Get mounted FS on the system.'
def _get_mounts(self):
return salt.utils.fsutils._get_mounts()
'Get available CPU information.'
def _get_cpu(self):
out = __salt__['cmd.run_all']('lscpu') salt.utils.fsutils._verify_run(out) data = dict() for (descr, value) in [elm.split(':', 1) for elm in out['stdout'].split(os.linesep)]: data[descr.strip()] = value.strip() return data
'Get memory.'
def _get_mem(self):
out = __salt__['cmd.run_all']('vmstat -s') if out['retcode']: raise SIException('Memory info error: {0}'.format(out['stderr'])) ret = dict() for line in out['stdout'].split(os.linesep): line = line.strip() if (not line): continue (size, descr) = li...
'Get network configuration.'
def _get_network(self):
data = dict() data['interfaces'] = salt.utils.network.interfaces() data['subnets'] = salt.utils.network.subnets() return data
'Get operating system summary'
def _get_os(self):
return {'name': self._grain('os'), 'family': self._grain('os_family'), 'arch': self._grain('osarch'), 'release': self._grain('osrelease')}
'Constructor. :param scope: :return:'
def __init__(self, scope, cachedir=None):
if (scope and (scope not in self.SCOPES)): raise InspectorQueryException('Unknown scope: {0}. Must be one of: {1}'.format(repr(scope), ', '.join(self.SCOPES))) elif (not scope): raise InspectorQueryException('Scope cannot be empty. Must be one of: ...
'Call the query with the defined scope. :param args: :param kwargs: :return:'
def __call__(self, *args, **kwargs):
return getattr(self, self.scope)(*args, **kwargs)
'Returns all diffs to the configuration files.'
def _changes(self, *args, **kwargs):
raise Exception('Not yet implemented')
'Return configuration files.'
def _configuration(self, *args, **kwargs):
data = dict() self.db.open() for pkg in self.db.get(Package): configs = list() for pkg_cfg in self.db.get(PackageCfgFile, eq={'pkgid': pkg.id}): configs.append(pkg_cfg.path) data[pkg.name] = configs if (not data): raise InspectorQueryException('No inspected...
'Return all known local accounts to the system.'
def _get_local_users(self, disabled=None):
users = dict() path = '/etc/passwd' with salt.utils.files.fopen(path, 'r') as fp_: for line in fp_: line = line.strip() if (':' not in line): continue (name, password, uid, gid, gecos, directory, shell) = line.split(':') active = (not (...
'Return all known local groups to the system.'
def _get_local_groups(self):
groups = dict() path = '/etc/group' with salt.utils.files.fopen(path, 'r') as fp_: for line in fp_: line = line.strip() if (':' not in line): continue (name, password, gid, users) = line.split(':') groups[name] = {'gid': gid} ...
'Return all known accounts, excluding local accounts.'
def _get_external_accounts(self, locals):
users = dict() out = __salt__['cmd.run_all']('passwd -S -a') if out['retcode']: return users status = {'L': 'Locked', 'NP': 'No password', 'P': 'Usable password', 'LK': 'Locked'} for data in [elm.strip().split(' ') for elm in out['stdout'].split(os.linesep) if elm.strip()]: ...
'Local users and groups. accounts Can be either \'local\', \'remote\' or \'all\' (equal to "local,remote"). Remote accounts cannot be resolved on all systems, but only those, which supports \'passwd -S -a\'. disabled True (or False, default) to return only disabled accounts.'
def _identity(self, *args, **kwargs):
LOCAL = 'local accounts' EXT = 'external accounts' data = dict() data[LOCAL] = self._get_local_users(disabled=kwargs.get('disabled')) data[EXT] = (self._get_external_accounts(data[LOCAL].keys()) or 'N/A') data['local groups'] = self._get_local_groups() return data
'This basically calls grains items and picks out only necessary information in a certain structure. :param args: :param kwargs: :return:'
def _system(self, *args, **kwargs):
sysinfo = SysInfo(__grains__.get('kernel')) data = dict() data['cpu'] = sysinfo._get_cpu() data['disks'] = sysinfo._get_fs() data['mounts'] = sysinfo._get_mounts() data['memory'] = sysinfo._get_mem() data['network'] = sysinfo._get_network() data['os'] = sysinfo._get_os() return data
'Return installed software.'
def _software(self, *args, **kwargs):
data = dict() if ('exclude' in kwargs): excludes = kwargs['exclude'].split(',') else: excludes = list() os_family = __grains__.get('os_family').lower() if (os_family == 'suse'): LOCKS = 'pkg.list_locks' if ('products' not in excludes): products = __salt__[...
'Get list of enabled and disabled services on the particular system.'
def _services(self, *args, **kwargs):
return {'enabled': __salt__['service.get_enabled'](), 'disabled': __salt__['service.get_disabled']()}
'Resolve local users and groups. :param iid: :param named: :param uid: :return:'
def _id_resolv(self, iid, named=True, uid=True):
if (not self.local_identity): self.local_identity['users'] = self._get_local_users() self.local_identity['groups'] = self._get_local_groups() if (not named): return iid for (name, meta) in self.local_identity[((uid and 'users') or 'groups')].items(): if ((uid and (int(meta.ge...
'Find all unmanaged files. Returns maximum 1000 values. Parameters: * **filter**: Include only results which path starts from the filter string. * **time**: Display time in Unix ticks or format according to the configured TZ (default) Values: ticks, tz (default) * **size**: Format size. Values: B, KB, MB, GB * **owners...
def _payload(self, *args, **kwargs):
def _size_format(size, fmt): if (fmt is None): return size fmt = fmt.lower() if (fmt == 'b'): return '{0} Bytes'.format(size) elif (fmt == 'kb'): return '{0} Kb'.format(round((float(size) / 1024), 2)) elif (fmt == 'mb'): r...
'Return all the summary of the particular system.'
def _all(self, *args, **kwargs):
data = dict() data['software'] = self._software(**kwargs) data['system'] = self._system(**kwargs) data['services'] = self._services(**kwargs) try: data['configuration'] = self._configuration(**kwargs) except InspectorQueryException as ex: data['configuration'] = 'N/A' log...
'Constructor.'
def __init__(self, path):
self._path = path self.init_queries = list() self._db = CsvDB(self._path)
'Init the database, if required.'
def open(self, new=False):
(self._db.new() if new else self._db.open()) self._run_init_queries()
'Initialization queries'
def _run_init_queries(self):
for obj in (Package, PackageCfgFile, PayloadFile, IgnoredDir, AllowedDir): self._db.create_table_from_object(obj())
'Purge whole database.'
def purge(self):
for table_name in self._db.list_tables(): self._db.flush(table_name) self._run_init_queries()
'Flush the table.'
def flush(self, table):
self._db.flush(table)
'Close the database connection.'
def close(self):
self._db.close()
'Proxy methods from the Database instance. :param item: :return:'
def __getattr__(self, item):
return getattr(self._db, item)
'Keep singleton.'
def __new__(cls, *args, **kwargs):
if (not cls.__instance): cls.__instance = super(DBHandle, cls).__new__(cls) return cls.__instance
'Database handle for the specific :param path: :return:'
def __init__(self, path):
DBHandleBase.__init__(self, path)
'Serialize the object to a row for CSV according to the table description. :return:'
def _serialize(self, description):
return [getattr(self, attr) for attr in description]
'Constructor to store the database files. :param path:'
def __init__(self, path):
self._prepare(path) self._opened = False self.db_path = None self._opened = False self._tables = {}
'Create label of the database, based on the date-time. :return:'
def _label(self):
return datetime.datetime.utcnow().strftime('%Y%m%d-%H%M%S')
'Create a new database and opens it. :return:'
def new(self):
dbname = self._label() self.db_path = os.path.join(self.path, dbname) if (not os.path.exists(self.db_path)): os.makedirs(self.db_path) self._opened = True self.list_tables() return dbname
'Purge the database. :param dbid: :return:'
def purge(self, dbid):
db_path = os.path.join(self.path, dbid) if os.path.exists(db_path): shutil.rmtree(db_path, ignore_errors=True) return True return False
'Flush table. :param table: :return:'
def flush(self, table):
table_path = os.path.join(self.db_path, table) if os.path.exists(table_path): os.unlink(table_path)
'List all the databases on the given path. :return:'
def list(self):
databases = [] for dbname in os.listdir(self.path): databases.append(dbname) return list(reversed(sorted(databases)))
'Load existing tables and their descriptions. :return:'
def list_tables(self):
if (not self._tables): for table_name in os.listdir(self.db_path): self._tables[table_name] = self._load_table(table_name) return self._tables.keys()
'Open database from the path with the name or latest. If there are no yet databases, create a new implicitly. :return:'
def open(self, dbname=None):
databases = self.list() if self.is_closed(): self.db_path = os.path.join(self.path, (dbname or ((databases and databases[0]) or self.new()))) if (not self._opened): self.list_tables() self._opened = True
'Close the database. :return:'
def close(self):
self._opened = False