From 61773bdaf90e253a93aabd9b919d52e3a0731459 Mon Sep 17 00:00:00 2001 From: Jeff Schroeder Date: Tue, 13 Dec 2011 22:42:40 -0800 Subject: [PATCH 01/15] Fix a bug in ssh.host_keys --- salt/modules/ssh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/modules/ssh.py b/salt/modules/ssh.py index fdc6d7e28e..0947ccb207 100644 --- a/salt/modules/ssh.py +++ b/salt/modules/ssh.py @@ -86,7 +86,7 @@ def host_keys(keydir=None): # Set up the default keydir - needs to support sshd_config parsing in the # future if not keydir: - if __grains__['Linux']: + if __grains__['kernel'] == 'Linux': keydir = '/etc/ssh' keys = {} for fn_ in os.listdir(keydir): From 509c1aa7894c0e63ef1ace90ebf3ed758e7c99b1 Mon Sep 17 00:00:00 2001 From: Jeff Schroeder Date: Tue, 13 Dec 2011 22:52:13 -0800 Subject: [PATCH 02/15] A few cleanups for the grains module - grains.item and grains.list are now shorter - grains.item can be called without an argument and won't barf --- salt/modules/grains.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/salt/modules/grains.py b/salt/modules/grains.py index 911aca1108..c4eb5ea56c 100644 --- a/salt/modules/grains.py +++ b/salt/modules/grains.py @@ -23,7 +23,7 @@ def items(): return __grains__ -def item(key): +def item(key=None): ''' Return a singe component of the grains data @@ -31,9 +31,7 @@ def item(key): salt '*' grains.item os ''' - if key in __grains__: - return __grains__[key] - return '' + return __grains__.get(key, '') def list(): ''' @@ -43,7 +41,7 @@ def list(): salt '*' grains.list ''' - return sorted(__grains__.keys()) + return sorted(__grains__) # Keep the wise 'nix beards happy ls = list From be4d74189b6ea0b24166470a8b7afde83f9b802d Mon Sep 17 00:00:00 2001 From: Jeff Schroeder Date: Tue, 13 Dec 2011 23:04:07 -0800 Subject: [PATCH 03/15] Light cleanup of the kmod module --- salt/modules/kmod.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/salt/modules/kmod.py b/salt/modules/kmod.py index 5d52cf5f84..1ba578d840 100644 --- a/salt/modules/kmod.py +++ b/salt/modules/kmod.py @@ -52,10 +52,10 @@ def available(): for path in __salt__['cmd.run']('modprobe -l').split('\n'): bpath = os.path.basename(path) comps = bpath.split('.') - if comps.count('ko'): + if 'ko' in comps: # This is a kernel module, return it without the .ko extension ret.append('.'.join(comps[:comps.index('ko')])) - return ret + return sorted(list(ret)) def check_available(mod): @@ -66,10 +66,7 @@ def check_available(mod): salt '*' kmod.check_available kvm ''' - if available().count(mod): - # the module is available, return True - return True - return False + return mod in available() def lsmod(): @@ -87,10 +84,11 @@ def lsmod(): continue if comps[0] == 'Module': continue - mdat = {} - mdat['module'] = comps[0] - mdat['size'] = comps[1] - mdat['depcount'] = comps[2] + mdat = { + 'size': comps[1], + 'module': comps[0], + 'depcount': comps[2], + } if len(comps) > 3: mdat['deps'] = comps[3].split(',') else: From 751119f0c90920bd7e5e39e634cc2d9edbaba19c Mon Sep 17 00:00:00 2001 From: Jeff Schroeder Date: Tue, 13 Dec 2011 23:21:58 -0800 Subject: [PATCH 04/15] Use cmd.run in disk and status modules instead of subprocess --- salt/modules/disk.py | 7 +------ salt/modules/status.py | 9 ++------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/salt/modules/disk.py b/salt/modules/disk.py index f1c6ecce90..0a0a07aa85 100644 --- a/salt/modules/disk.py +++ b/salt/modules/disk.py @@ -2,9 +2,6 @@ Module for gathering disk information ''' -# FIXME: we want module internal calls rather than using subprocess directly -import subprocess - def usage(): ''' @@ -16,9 +13,7 @@ def usage(): ''' cmd = 'df -P' ret = {} - out = subprocess.Popen(cmd, - shell=True, - stdout=subprocess.PIPE).communicate()[0].split('\n') + out = __salt__['cmd.run'](cmd).split('\n') for line in out: if not line.count(' '): continue diff --git a/salt/modules/status.py b/salt/modules/status.py index 4690f89210..d67cfa98a8 100644 --- a/salt/modules/status.py +++ b/salt/modules/status.py @@ -6,8 +6,6 @@ These data can be useful for compiling into stats later. import fnmatch import os import re -import subprocess - __opts__ = {} @@ -71,8 +69,7 @@ def uptime(): salt '*' status.uptime ''' - return subprocess.Popen(['uptime'], - stdout=subprocess.PIPE).communicate()[0].strip() + return __salt__['cmd.run']('uptime').strip() def loadavg(): @@ -376,10 +373,8 @@ def w(): salt '*' status.w ''' - users = subprocess.Popen(['w -h'], - shell=True, - stdout=subprocess.PIPE).communicate()[0].split('\n') user_list = [] + users = __salt__['cmd.run']('w -h').split('\n') for row in users: if not row.count(' '): continue From 241d825e13ec0a000c1e716698c34dedb0ea4797 Mon Sep 17 00:00:00 2001 From: Jeff Schroeder Date: Tue, 13 Dec 2011 23:25:36 -0800 Subject: [PATCH 05/15] Clean up the network module a bit --- salt/modules/network.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/salt/modules/network.py b/salt/modules/network.py index c4eac866d1..2844b09c5b 100644 --- a/salt/modules/network.py +++ b/salt/modules/network.py @@ -4,7 +4,11 @@ Module for gathering and managing network information from string import ascii_letters, digits import socket -import subprocess +import salt.utils + +__outputter__ = { + 'ping': 'txt', +} def _sanitize_host(host): @@ -25,11 +29,7 @@ def ping(host): salt '*' network.ping archlinux.org -c 4 ''' cmd = 'ping -c 4 %s' % _sanitize_host(host) - - out = subprocess.Popen(cmd, - shell=True, - stdout=subprocess.PIPE).communicate()[0] - return out + return __salt__['cmd.run'](cmd) def netstat(): @@ -40,13 +40,11 @@ def netstat(): salt '*' network.netstat ''' - cmd = 'netstat -tulpnea' ret = [] - out = subprocess.Popen(cmd, - shell=True, - stdout=subprocess.PIPE).communicate()[0].split('\n') + cmd = 'netstat -tulpnea' + out = __salt__['cmd.run'](cmd) for line in out: - if not line.count(' '): + if ' ' not in line: continue comps = line.split() if line.startswith('tcp'): From fd74f8b0b60b3553fd0a88a050f4ef1628737e1f Mon Sep 17 00:00:00 2001 From: Jeff Schroeder Date: Wed, 14 Dec 2011 10:00:29 -0800 Subject: [PATCH 06/15] Remove grains.list in favor of grains.ls It overrides a python builtin --- salt/modules/grains.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/salt/modules/grains.py b/salt/modules/grains.py index c4eb5ea56c..7953fef40f 100644 --- a/salt/modules/grains.py +++ b/salt/modules/grains.py @@ -33,7 +33,7 @@ def item(key=None): ''' return __grains__.get(key, '') -def list(): +def ls(): ''' Return a list of all available grains @@ -42,6 +42,3 @@ def list(): salt '*' grains.list ''' return sorted(__grains__) - -# Keep the wise 'nix beards happy -ls = list From f001b4d8a299cb69b8e0c96e8213327ef149b82a Mon Sep 17 00:00:00 2001 From: Jeff Schroeder Date: Tue, 13 Dec 2011 23:25:36 -0800 Subject: [PATCH 07/15] Remove subprocess from the network module --- salt/modules/network.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/salt/modules/network.py b/salt/modules/network.py index 2844b09c5b..797c4aaae8 100644 --- a/salt/modules/network.py +++ b/salt/modules/network.py @@ -79,13 +79,12 @@ def traceroute(host): salt '*' network.traceroute archlinux.org ''' - cmd = 'traceroute %s' % _sanitize_host(host) ret = [] - out = subprocess.Popen(cmd, - shell=True, - stdout=subprocess.PIPE).communicate()[0].split('\n') + cmd = 'traceroute %s' % _sanitize_host(host) + out = __salt__['cmd.run'](cmd) + for line in out: - if not line.count(' '): + if not ' ' in line: continue if line.startswith('traceroute'): continue @@ -113,11 +112,7 @@ def dig(host): salt '*' network.dig archlinux.org ''' cmd = 'dig %s' % _sanitize_host(host) - - out = subprocess.Popen(cmd, - shell=True, - stdout=subprocess.PIPE).communicate()[0] - return out + return __salt__['cmd.run'](cmd) def isportopen(host, port): @@ -136,4 +131,3 @@ def isportopen(host, port): out = sock.connect_ex((_sanitize_host(host), int(port))) return out - From e3b9c8e10302f45c421a3b1d976d874a57f344a3 Mon Sep 17 00:00:00 2001 From: Jeff Schroeder Date: Wed, 14 Dec 2011 20:05:34 -0800 Subject: [PATCH 08/15] Add a few FIXMEs to the network module - Add outputters for dig and netstat - Remove a pointless check from network.netstat() - The network module doesn't work in Ubuntu 10.10 - The traceroute module doesn't work in Ubuntu 10.10 --- salt/modules/network.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/salt/modules/network.py b/salt/modules/network.py index 797c4aaae8..55bd07f03c 100644 --- a/salt/modules/network.py +++ b/salt/modules/network.py @@ -7,7 +7,9 @@ import socket import salt.utils __outputter__ = { - 'ping': 'txt', + 'dig': 'txt', + 'ping': 'txt', + 'netstat': 'txt', } @@ -32,6 +34,7 @@ def ping(host): return __salt__['cmd.run'](cmd) +# FIXME: Does not work with: netstat 1.42 (2001-04-15) from net-tools 1.6.0 (Ubuntu 10.10) def netstat(): ''' Return information on open ports and states @@ -44,8 +47,6 @@ def netstat(): cmd = 'netstat -tulpnea' out = __salt__['cmd.run'](cmd) for line in out: - if ' ' not in line: - continue comps = line.split() if line.startswith('tcp'): ret.append({ @@ -71,6 +72,8 @@ def netstat(): return ret +# FIXME: This is broken on: Modern traceroute for Linux, version 2.0.14, May 10 2010 (Ubuntu 10.10) +# FIXME: traceroute is deprecated, make this fall back to tracepath def traceroute(host): ''' Performs a traceroute to a 3rd party host From 98148b33e41d8fe3d45af9a67e6dfedd46e45225 Mon Sep 17 00:00:00 2001 From: Jeff Schroeder Date: Wed, 14 Dec 2011 20:12:03 -0800 Subject: [PATCH 09/15] Add cron.ls and cron.rm as aliases For cron.list_jobs and cron.rm_tab. This just keeps things a bit consistent and more obvious to any 'nix geek who happenst to look --- salt/modules/cron.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/salt/modules/cron.py b/salt/modules/cron.py index 348cd37180..fadd04438e 100644 --- a/salt/modules/cron.py +++ b/salt/modules/cron.py @@ -100,6 +100,9 @@ def list_tab(user): ret['pre'].append(line) return ret +# For consistency's sake +ls = list_tab + def set_special(user, special, cmd): ''' @@ -188,3 +191,5 @@ def rm_job(user, minute, hour, dom, month, dow, cmd): # Failed to commit, return the error return comdat['stderr'] return ret + +rm = rm_job From 5e6f23fbefa7bad8b7c06ef310f3d875179d50e5 Mon Sep 17 00:00:00 2001 From: Jeff Schroeder Date: Wed, 14 Dec 2011 20:47:13 -0800 Subject: [PATCH 10/15] Add disk.inodeusage and fix disk.usage logic Previously, disk.usage was returning a dictionary where the key was in the "Filesystem" column. It seemed proper to return every filesystem, even the virtual ones so the dict key was changed to the mountpoint. $ df -P Filesystem 1024-blocks Used Available Capacity Mounted on /dev/sda8 177822980 12504764 156285272 8% / none 6154104 312 6153792 1% /dev none 6161372 3736 6157636 1% /dev/shm none 6161372 336 6161036 1% /var/run none 6161372 0 6161372 0% /var/lock /dev/sda1 101086 85423 15663 85% /boot /dev/sda5 240308484 219822840 20485644 92% /home /dev/sda7 54791132 1104204 50903672 3% /var This can be changed to skip anything that has a Filesystem of none down the road, but I don't have any non-Linux hosts to test on. --- salt/modules/disk.py | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/salt/modules/disk.py b/salt/modules/disk.py index 0a0a07aa85..52b8b91d4a 100644 --- a/salt/modules/disk.py +++ b/salt/modules/disk.py @@ -20,11 +20,42 @@ def usage(): if line.startswith('Filesystem'): continue comps = line.split() - ret[comps[0]] = { - '1K-blocks': comps[1], - 'available': comps[3], - 'capacity': comps[4], - 'mountpoint': comps[5], - 'used': comps[2] + ret[comps[5]] = { + 'filesystem': comps[0], + '1K-blocks': comps[1], + 'used': comps[2], + 'available': comps[3], + 'capacity': comps[4], } return ret + +def inodeusage(): + ''' + Return inode usage information for volumes mounted on this minion + + CLI Example:: + + salt '*' disk.inodeusage + ''' + cmd = 'df -i' + ret = {} + out = __salt__['cmd.run'](cmd).split('\n') + for line in out: + if line.startswith('Filesystem'): + continue + comps = line.split() + # Don't choke on empty lines + if not comps: + continue + + try: + ret[comps[5]] = { + 'inodes': comps[1], + 'used': comps[2], + 'free': comps[3], + 'use': comps[4], + 'filesystem': comps[0], + } + except IndexError: + print "DEBUG: comps='%s'" % comps + return ret From 19700165050fdd3ef94b17c4b2f4e729c863b884 Mon Sep 17 00:00:00 2001 From: Jeff Schroeder Date: Wed, 14 Dec 2011 20:53:27 -0800 Subject: [PATCH 11/15] Change solr and ebuild modules from dict.has_key() to key in dict As part of this branch, I'm trying to read through most all of the modules and clean things up. This is small, and might not normally warrant a commit, but I'm trying to keep things easy to bisect. --- salt/modules/ebuild.py | 10 +++++----- salt/modules/solr.py | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/salt/modules/ebuild.py b/salt/modules/ebuild.py index 5f2bc34366..0862b3f0da 100644 --- a/salt/modules/ebuild.py +++ b/salt/modules/ebuild.py @@ -5,7 +5,7 @@ Support for Portage try: import portage except ImportError: - None + pass def __virtual__(): ''' @@ -101,7 +101,7 @@ def install(pkg, refresh=False): new_pkgs = list_pkgs() for pkg in new_pkgs: - if old_pkgs.has_key(pkg): + if pkg in old_pkgs: if old_pkgs[pkg] == new_pkgs[pkg]: continue else: @@ -136,7 +136,7 @@ def update(pkg, refresh=False): new_pkgs = list_pkgs() for pkg in new_pkgs: - if old_pkgs.has_key(pkg): + if pkg in old_pkgs: if old_pkgs[pkg] == new_pkgs[pkg]: continue else: @@ -170,7 +170,7 @@ def upgrade(refresh=False): new_pkgs = list_pkgs() for pkg in new_pkgs: - if old_pkgs.has_key(pkg): + if pkg in old_pkgs: if old_pkgs[pkg] == new_pkgs[pkg]: continue else: @@ -200,7 +200,7 @@ def remove(pkg): new_pkgs = list_pkgs() for pkg in old_pkgs: - if not new_pkgs.has_key(pkg): + if not pkg in new_pkgs: ret_pkgs.append(pkg) return ret_pkgs diff --git a/salt/modules/solr.py b/salt/modules/solr.py index ba289de972..bcb24dad5d 100644 --- a/salt/modules/solr.py +++ b/salt/modules/solr.py @@ -621,7 +621,7 @@ def is_replication_enabled(host=None, core_name=None): replication_enabled = 'false' master_url = slave['masterUrl'] #check for errors on the slave - if slave.has_key('ERROR'): + if 'ERROR' in slave: success=False err = "{0}: {1} - {2}".format(name, slave['ERROR'], master_url) resp['errors'].append(err) @@ -691,7 +691,7 @@ def match_index_versions(host=None,core_name=None): if response['success']: slave = resp['data']['details']['slave'] master_url = resp['data']['details']['slave']['masterUrl'] - if slave.has_key('ERROR'): + if 'ERROR' in slave: error = slave['ERROR'] success=False err = "{0}: {1} - {2}".format(name, error, master_url) From f90fc0654dfa4a129f0fcca156cafe1d2d3ee15e Mon Sep 17 00:00:00 2001 From: Jeff Schroeder Date: Wed, 14 Dec 2011 21:38:41 -0800 Subject: [PATCH 12/15] Major update to the apache module - Add apache.signal text outputter - Add the following signals: status, fullstatus, configtest - Lots of style cleanups to fit better with the rest of salt - Output information about what happened when signals are ran --- salt/modules/apache.py | 58 +++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/salt/modules/apache.py b/salt/modules/apache.py index 40eb33b050..e976d7e966 100644 --- a/salt/modules/apache.py +++ b/salt/modules/apache.py @@ -2,18 +2,22 @@ Support for Apache ''' -from re import sub +import re + +__outputter__ = { + 'signal': 'txt', +} def __detect_os(): ''' Apache commands and paths differ depending on packaging ''' - httpd = 'CentOS Scientific RedHat Fedora' - apache2 = 'Ubuntu' - if httpd.count(__grains__['os']): + httpd = ('CentOS', 'Scientific', 'RedHat', 'Fedora') + apache2 = ('Ubuntu',) + if __grains__['os'] in httpd: return 'apachectl' - elif apache2.count(__grains__['os']): + elif __grains__['os'] in apache2: return 'apache2ctl' else: return 'apachectl' @@ -46,10 +50,10 @@ def fullversion(): ret['compiled_with'] = [] out = __salt__['cmd.run'](cmd).split('\n') for line in out: - if not line.count(' '): - continue if ': ' in line: comps = line.split(': ') + if not comps: + continue ret[comps[0].strip().lower().replace(' ', '_')] = comps[1].strip() elif ' -D' in line: cw = line.strip(' -D ') @@ -71,9 +75,9 @@ def modules(): ret['shared'] = [] out = __salt__['cmd.run'](cmd).split('\n') for line in out: - if not line.count(' '): - continue comps = line.split() + if not comps: + continue if '(static)' in line: ret['static'].append(comps[0]) if '(shared)' in line: @@ -93,7 +97,7 @@ def servermods(): ret = [] out = __salt__['cmd.run'](cmd).split('\n') for line in out: - if not line.count(' '): + if not line: continue if '.c' in line: ret.append(line.strip()) @@ -114,7 +118,7 @@ def directives(): out = __salt__['cmd.run'](cmd) out = out.replace('\n\t', '\t') for line in out.split('\n'): - if not line.count(' '): + if not line: continue comps = line.split('\t') desc = '\n'.join(comps[1:]) @@ -148,11 +152,11 @@ def vhosts(): if comps[0] == 'default': ret[namevhost]['default'] = {} ret[namevhost]['default']['vhost'] = comps[2] - ret[namevhost]['default']['conf'] = sub(r'\(|\)', '', comps[3]) + ret[namevhost]['default']['conf'] = re.sub(r'\(|\)', '', comps[3]) if comps[0] == 'port': ret[namevhost][comps[3]] = {} ret[namevhost][comps[3]]['vhost'] = comps[3] - ret[namevhost][comps[3]]['conf'] = sub(r'\(|\)', '', comps[4]) + ret[namevhost][comps[3]]['conf'] = re.sub(r'\(|\)', '', comps[4]) ret[namevhost][comps[3]]['port'] = comps[1] return ret @@ -165,8 +169,28 @@ def signal(signal=None): salt '*' apache.signal restart ''' - valid_signals = 'start stop restart graceful graceful-stop' - if not valid_signals.count(signal): + no_extra_args = ('configtest', 'status', 'fullstatus') + valid_signals = ('start', 'stop', 'restart', 'graceful', 'graceful-stop') + + if signal not in valid_signals and signal not in no_extra_args: return - cmd = __detect_os() + ' -k %s' % signal - out = __salt__['cmd.run'](cmd) + # Make sure you use the right arguments + if signal in valid_signals: + arguments = ' -k {0}'.format(signal) + else: + arguments = ' {0}'.format(signal) + cmd = __detect_os() + arguments + out = __salt__['cmd.run_all'](cmd) + + # A non-zero return code means fail + if out['retcode'] and out['stderr']: + ret = out['stderr'].strip() + # 'apachectl configtest' returns 'Syntax OK' to stderr + elif out['stderr']: + ret = out['stderr'].strip() + elif out['stdout']: + ret = out['stdout'].strip() + # No output for something like: apachectl graceful + else: + ret = 'Command: "{0}" completed successfully!'.format(cmd) + return ret From 4b5aad02ddc90ce53390d3320d6a8a923e208e35 Mon Sep 17 00:00:00 2001 From: Jeff Schroeder Date: Wed, 14 Dec 2011 22:05:27 -0800 Subject: [PATCH 13/15] Pretty-print an error if an invalid signal is sent to solr.signal --- salt/modules/solr.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/salt/modules/solr.py b/salt/modules/solr.py index bcb24dad5d..650104a7ff 100644 --- a/salt/modules/solr.py +++ b/salt/modules/solr.py @@ -931,9 +931,13 @@ def signal(signal=None): ''' ret = _get_return_dict() - valid_signals = 'start stop restart' - if not valid_signals.count(signal): - return + valid_signals = ('start', 'stop', 'restart') + + # Give a friendly error message for invalid signals + # TODO: Fix this logic to be reusable and used by apache.signal + if signal not in valid_signals: + msg = valid_signals[:-1] + ('or {0}'.format(valid_signals[-1]),) + return '{0} is an invalid signal. Try: one of: {1}'.format(signal, ', '.join(msg)) cmd = "{0} {1}".format(__opts__['solr.init_script'], signal) out = __salt__['cmd.run'](cmd) From fb2a8bcbcf6b8f1a71fa82d3e59b477a20844701 Mon Sep 17 00:00:00 2001 From: Jeff Schroeder Date: Wed, 14 Dec 2011 22:22:12 -0800 Subject: [PATCH 14/15] Major change from 'some str'.count(search) to search in 'some str' This patch is a function noop, but makes the code easier to read --- salt/modules/apache.py | 2 +- salt/modules/apt.py | 4 ++-- salt/modules/disk.py | 2 +- salt/modules/freebsdkmod.py | 4 ++-- salt/modules/hosts.py | 4 ++-- salt/modules/linux_sysctl.py | 6 +++--- salt/modules/moosefs.py | 8 ++++---- salt/modules/pacman.py | 6 +++--- salt/modules/ssh.py | 4 ++-- salt/modules/status.py | 16 ++++++++-------- salt/modules/tomcat.py | 4 ++-- salt/modules/virt.py | 12 +++++------- salt/modules/yumpkg.py | 4 ++-- salt/modules/yumpkg5.py | 6 +++--- 14 files changed, 40 insertions(+), 42 deletions(-) diff --git a/salt/modules/apache.py b/salt/modules/apache.py index e976d7e966..d506cbe613 100644 --- a/salt/modules/apache.py +++ b/salt/modules/apache.py @@ -142,7 +142,7 @@ def vhosts(): namevhost = '' out = __salt__['cmd.run'](cmd) for line in out.split('\n'): - if not line.count(' '): + if not line: continue comps = line.split() if 'is a NameVirtualHost' in line: diff --git a/salt/modules/apt.py b/salt/modules/apt.py index 5ea974eab9..8ac260852d 100644 --- a/salt/modules/apt.py +++ b/salt/modules/apt.py @@ -69,7 +69,7 @@ def refresh_db(): if not len(cols): continue ident = " ".join(cols[1:4]) - if cols[0].count('Get'): + if 'Get' in cols[0]: servers[ident] = True else: servers[ident] = False @@ -222,7 +222,7 @@ def list_pkgs(regex_string=""): for line in out.split('\n'): cols = line.split() - if len(cols) and cols[0].count('ii'): + if len(cols) and 'ii' in cols[0]: ret[cols[1]] = cols[2] return ret diff --git a/salt/modules/disk.py b/salt/modules/disk.py index 52b8b91d4a..7078dbca07 100644 --- a/salt/modules/disk.py +++ b/salt/modules/disk.py @@ -15,7 +15,7 @@ def usage(): ret = {} out = __salt__['cmd.run'](cmd).split('\n') for line in out: - if not line.count(' '): + if not line: continue if line.startswith('Filesystem'): continue diff --git a/salt/modules/freebsdkmod.py b/salt/modules/freebsdkmod.py index ac6b607f3e..eca7fdcb37 100644 --- a/salt/modules/freebsdkmod.py +++ b/salt/modules/freebsdkmod.py @@ -52,7 +52,7 @@ def available(): for path in __salt__['cmd.run']('ls /boot/kernel | grep .ko$').split('\n'): bpath = os.path.basename(path) comps = bpath.split('.') - if comps.count('ko'): + if 'ko' in comps: # This is a kernel module, return it without the .ko extension ret.append('.'.join(comps[:comps.index('ko')])) return ret @@ -66,7 +66,7 @@ def check_available(mod): salt '*' kmod.check_available kvm ''' - if available().count(mod): + if mod in available(): # the module is available, return True return True return False diff --git a/salt/modules/hosts.py b/salt/modules/hosts.py index bf2fd804c6..39359cbaa5 100644 --- a/salt/modules/hosts.py +++ b/salt/modules/hosts.py @@ -42,7 +42,7 @@ def get_ip(host): return '' # Look for the op for addr in hosts: - if hosts[addr].count(host): + if host in hosts[addr]: return addr # ip not found return '' @@ -71,7 +71,7 @@ def has_pair(ip, alias): hosts = list_hosts() if ip not in hosts: return False - if hosts[ip].count(alias): + if alias in hosts[ip]: return True return False diff --git a/salt/modules/linux_sysctl.py b/salt/modules/linux_sysctl.py index 4317c32b1e..16f0c9a008 100644 --- a/salt/modules/linux_sysctl.py +++ b/salt/modules/linux_sysctl.py @@ -24,9 +24,9 @@ def show(): ret = {} out = __salt__['cmd.run'](cmd).split('\n') for line in out: - if not line.count(' '): + if not line: continue - if not line.count(' = '): + if ' = ' not in line: continue comps = line.split(' = ') ret[comps[0]] = comps[1] @@ -80,7 +80,7 @@ def persist(name, value, config='/etc/sysctl.conf'): if line.startswith('#'): nlines.append(line) continue - if not line.count('='): + if '=' not in line: nlines.append(line) continue comps = line.split('=') diff --git a/salt/modules/moosefs.py b/salt/modules/moosefs.py index d02a945c80..a3f098d6ee 100644 --- a/salt/modules/moosefs.py +++ b/salt/modules/moosefs.py @@ -20,7 +20,7 @@ def dirinfo(path, opts=None): output = out['stdout'].split('\n') for line in output: - if not line.count(' '): + if not line: continue comps = line.split(':') ret[comps[0].strip()] = comps[1].strip() @@ -42,7 +42,7 @@ def fileinfo(path): output = out['stdout'].split('\n') for line in output: - if not line.count(' '): + if not line: continue if '/' in line: comps = line.split('/') @@ -85,7 +85,7 @@ def mounts(): output = out['stdout'].split('\n') for line in output: - if not line.count(' '): + if not line: continue if 'fuse.mfs' in line: comps = line.split(' ') @@ -130,7 +130,7 @@ def getgoal(path, opts=None): } else: for line in output: - if not line.count(' '): + if not line: continue if path in line: continue diff --git a/salt/modules/pacman.py b/salt/modules/pacman.py index e636fa5fc9..aa886da5ef 100644 --- a/salt/modules/pacman.py +++ b/salt/modules/pacman.py @@ -62,7 +62,7 @@ def list_pkgs(): ret = {} out = __salt__['cmd.run'](cmd).split('\n') for line in out: - if not line.count(' '): + if not line: continue comps = line.split() ret[comps[0]] = comps[1] @@ -88,9 +88,9 @@ def refresh_db(): if not line: continue key = line.strip().split()[0] - if line.count('is up to date'): + if 'is up to date' in line: ret[key] = False - elif line.count('downloading'): + elif 'downloading' in line: ret[key] = True return ret diff --git a/salt/modules/ssh.py b/salt/modules/ssh.py index 0947ccb207..534788086a 100644 --- a/salt/modules/ssh.py +++ b/salt/modules/ssh.py @@ -12,9 +12,9 @@ def _refine_enc(enc): ''' rsa = ['r', 'rsa', 'ssh-rsa'] dss = ['d', 'dsa', 'dss', 'ssh-dss'] - if rsa.count(enc): + if enc in rsa: return 'ssh-rsa' - elif dss.count(enc): + elif enc in dss: return 'ssh-dss' else: return 'ssh-rsa' diff --git a/salt/modules/status.py b/salt/modules/status.py index d67cfa98a8..251e9975c5 100644 --- a/salt/modules/status.py +++ b/salt/modules/status.py @@ -104,7 +104,7 @@ def cpustats(): stats = open(procf, 'r').read().split('\n') ret = {} for line in stats: - if not line.count(' '): + if not line: continue comps = line.split() if comps[0] == 'cpu': @@ -141,7 +141,7 @@ def meminfo(): stats = open(procf, 'r').read().split('\n') ret = {} for line in stats: - if not line.count(' '): + if not line: continue comps = line.split() comps[0] = comps[0].replace(':', '') @@ -167,7 +167,7 @@ def cpuinfo(): stats = open(procf, 'r').read().split('\n') ret = {} for line in stats: - if not line.count(' '): + if not line: continue comps = line.split(':') comps[0] = comps[0].strip() @@ -192,7 +192,7 @@ def diskstats(): stats = open(procf, 'r').read().split('\n') ret = {} for line in stats: - if not line.count(' '): + if not line: continue comps = line.split() ret[comps[2]] = {'major': _number(comps[0]), @@ -282,7 +282,7 @@ def vmstats(): stats = open(procf, 'r').read().split('\n') ret = {} for line in stats: - if not line.count(' '): + if not line: continue comps = line.split() ret[comps[0]] = _number(comps[1]) @@ -304,7 +304,7 @@ def netstats(): ret = {} headers = [''] for line in stats: - if not line.count(' '): + if not line: continue comps = line.split() if comps[0] == headers[0]: @@ -336,7 +336,7 @@ def netdev(): stats = open(procf, 'r').read().split('\n') ret = {} for line in stats: - if not line.count(' '): + if not line: continue if line.find(':') < 0: continue @@ -376,7 +376,7 @@ def w(): user_list = [] users = __salt__['cmd.run']('w -h').split('\n') for row in users: - if not row.count(' '): + if not row: continue comps = row.split() rec = {'idle': comps[3], diff --git a/salt/modules/tomcat.py b/salt/modules/tomcat.py index 3617039a0e..bd37d50f9e 100644 --- a/salt/modules/tomcat.py +++ b/salt/modules/tomcat.py @@ -27,7 +27,7 @@ def version(): out = __salt__['cmd.run'](cmd).split('\n') ret = out[0].split(': ') for line in out: - if not line.count(' '): + if not line: continue if 'Server version' in line: comps = line.split(': ') @@ -46,7 +46,7 @@ def fullversion(): ret = {} out = __salt__['cmd.run'](cmd).split('\n') for line in out: - if not line.count(' '): + if not line: continue if ': ' in line: comps = line.split(': ') diff --git a/salt/modules/virt.py b/salt/modules/virt.py index 991b539bd8..99ab139ba2 100644 --- a/salt/modules/virt.py +++ b/salt/modules/virt.py @@ -40,7 +40,7 @@ def _get_dom(vm_): Return a domain object for the named vm ''' conn = __get_conn() - if not list_vms().count(vm_): + if vm_ not in list_vms(): raise Exception('The specified vm is not present') return conn.lookupByName(vm_) @@ -169,8 +169,8 @@ def get_disks(vm_): target = targets[0] else: continue - if target.attributes.keys().count('dev')\ - and source.attributes.keys().count('file'): + if 'dev' in target.attributes.keys(): + and 'file' in source.attributes.keys(): disks[target.getAttribute('dev')] =\ {'file': source.getAttribute('file')} for dev in disks: @@ -509,11 +509,9 @@ def is_kvm_hyper(): ''' if __grains__['virtual'] != 'physical': return False - if not open('/proc/modules').read().count('kvm_'): + if 'kvm_' not in open('/proc/modules').read(): return False - libvirt_ret = subprocess.Popen('ps aux', - shell=True, - stdout=subprocess.PIPE).communicate()[0].count('libvirtd') + libvirt_ret = __salt__['cmd.run'](__grains__['ps']).count('libvirtd') if not libvirt_ret: return False return True diff --git a/salt/modules/yumpkg.py b/salt/modules/yumpkg.py index a56fdf7dd3..e6ba591646 100644 --- a/salt/modules/yumpkg.py +++ b/salt/modules/yumpkg.py @@ -11,14 +11,14 @@ def __virtual__(): ''' # Return this for pkg on RHEL/Fedora based distros that ship with python # 2.6 or greater. - dists = 'CentOS Scientific RedHat' + dists = ('CentOS', 'Scientific', 'RedHat') if __grains__['os'] == 'Fedora': if int(__grains__['osrelease'].split('.')[0]) >= 11: return 'pkg' else: return False else: - if dists.count(__grains__['os']): + if __grains__['os'] in dists: if int(__grains__['osrelease'].split('.')[0]) >= 6: return 'pkg' else: diff --git a/salt/modules/yumpkg5.py b/salt/modules/yumpkg5.py index 670881cb5c..69e53f0f6b 100644 --- a/salt/modules/yumpkg5.py +++ b/salt/modules/yumpkg5.py @@ -7,14 +7,14 @@ def __virtual__(): ''' # Return this for pkg on RHEL/Fedora based distros that do not ship with # python 2.6 or greater. - dists = 'CentOS Scientific RedHat' + dists = ('CentOS', 'Scientific', 'RedHat') if __grains__['os'] == 'Fedora': if int(__grains__['osrelease'].split('.')[0]) < 11: return 'pkg' else: return False else: - if dists.count(__grains__['os']): + if __grains__['os'] in dists: if int(__grains__['osrelease'].split('.')[0]) <= 5: return 'pkg' else: @@ -84,7 +84,7 @@ def list_pkgs(): ret = {} out = __salt__['cmd.run_stdout'](cmd) for line in out.split(';'): - if not line.count(':'): + if ':' not in line: continue comps = line.split(':') ret[comps[0]] = comps[1] From d24f85687f2a5af58b21622e17d8ce85bb18b991 Mon Sep 17 00:00:00 2001 From: Jeff Schroeder Date: Wed, 14 Dec 2011 22:31:57 -0800 Subject: [PATCH 15/15] Fix a few syntax errors introduced to the virt module --- salt/modules/virt.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/salt/modules/virt.py b/salt/modules/virt.py index 99ab139ba2..db6214e233 100644 --- a/salt/modules/virt.py +++ b/salt/modules/virt.py @@ -169,12 +169,12 @@ def get_disks(vm_): target = targets[0] else: continue - if 'dev' in target.attributes.keys(): + if 'dev' in target.attributes.keys() \ and 'file' in source.attributes.keys(): - disks[target.getAttribute('dev')] =\ + disks[target.getAttribute('dev')] = \ {'file': source.getAttribute('file')} for dev in disks: - disks[dev].update(yaml.safe_load(subprocess.Popen('qemu-img info '\ + disks[dev].update(yaml.safe_load(subprocess.Popen('qemu-img info ' \ + disks[dev]['file'], shell=True, stdout=subprocess.PIPE).communicate()[0]))