Merge pull request #359 from SEJeff/module-cleanups

RFC: Module cleanups
This commit is contained in:
Thomas S Hatch 2011-12-14 22:38:25 -08:00
commit 8be963fb09
20 changed files with 173 additions and 133 deletions

View File

@ -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:])
@ -138,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:
@ -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

View File

@ -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

View File

@ -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

View File

@ -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,20 +13,49 @@ 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(' '):
if not line:
continue
if line.startswith('Filesystem'):
continue
comps = line.split()
ret[comps[0]] = {
ret[comps[5]] = {
'filesystem': comps[0],
'1K-blocks': comps[1],
'used': comps[2],
'available': comps[3],
'capacity': comps[4],
'mountpoint': comps[5],
'used': comps[2]
}
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

View File

@ -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

View File

@ -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

View File

@ -23,7 +23,7 @@ def items():
return __grains__
def item(key):
def item(key=None):
'''
Return a singe component of the grains data
@ -31,11 +31,9 @@ def item(key):
salt '*' grains.item os
'''
if key in __grains__:
return __grains__[key]
return ''
return __grains__.get(key, '')
def list():
def ls():
'''
Return a list of all available grains
@ -43,7 +41,4 @@ def list():
salt '*' grains.list
'''
return sorted(__grains__.keys())
# Keep the wise 'nix beards happy
ls = list
return sorted(__grains__)

View File

@ -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

View File

@ -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:

View File

@ -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('=')

View File

@ -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

View File

@ -4,7 +4,13 @@ Module for gathering and managing network information
from string import ascii_letters, digits
import socket
import subprocess
import salt.utils
__outputter__ = {
'dig': 'txt',
'ping': 'txt',
'netstat': 'txt',
}
def _sanitize_host(host):
@ -25,13 +31,10 @@ 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)
# 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
@ -40,14 +43,10 @@ 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(' '):
continue
comps = line.split()
if line.startswith('tcp'):
ret.append({
@ -73,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
@ -81,13 +82,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
@ -115,11 +115,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):
@ -138,4 +134,3 @@ def isportopen(host, port):
out = sock.connect_ex((_sanitize_host(host), int(port)))
return out

View File

@ -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

View File

@ -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)
@ -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)

View File

@ -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'
@ -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):

View File

@ -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():
@ -107,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':
@ -144,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(':', '')
@ -170,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()
@ -195,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]),
@ -285,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])
@ -307,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]:
@ -339,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,12 +373,10 @@ 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(' '):
if not row:
continue
comps = row.split()
rec = {'idle': comps[3],

View File

@ -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(': ')

View File

@ -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,12 +169,12 @@ def get_disks(vm_):
target = targets[0]
else:
continue
if target.attributes.keys().count('dev')\
and source.attributes.keys().count('file'):
disks[target.getAttribute('dev')] =\
if 'dev' in target.attributes.keys() \
and 'file' in source.attributes.keys():
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]))
@ -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

View File

@ -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:

View File

@ -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]