mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
more PEP8
Signed-off-by: Markus Gattol <markus.gattol@sunoano.org>
This commit is contained in:
parent
d3bd180d66
commit
eb97e480ba
6 changed files with 100 additions and 42 deletions
|
@ -11,6 +11,7 @@ rc scripts, services can be defined as running or dead.
|
|||
- running
|
||||
'''
|
||||
|
||||
|
||||
def running(name, sig=None):
|
||||
'''
|
||||
Verify that the service is running
|
||||
|
@ -26,21 +27,25 @@ def running(name, sig=None):
|
|||
'changes': {},
|
||||
'result': True,
|
||||
'comment': 'The service is already running'}
|
||||
|
||||
changes = {name: __salt__['service.start'](name)}
|
||||
|
||||
if not changes[name]:
|
||||
return {'name': name,
|
||||
'changes': {},
|
||||
'result': False,
|
||||
'comment': 'Service {0} failed to start'.format(name)}
|
||||
|
||||
return {'name': name,
|
||||
'changes': changes,
|
||||
'result': True,
|
||||
'comment': 'Service {0} started'.format(name)}
|
||||
|
||||
|
||||
def dead(name, sig=None):
|
||||
'''
|
||||
Ensure that the named service is dead
|
||||
|
||||
|
||||
name
|
||||
The name of the init or rc script used to manage the service
|
||||
|
||||
|
@ -52,20 +57,24 @@ def dead(name, sig=None):
|
|||
'changes': {},
|
||||
'result': True,
|
||||
'comment': 'Service {0} is already dead'.format(name)}
|
||||
|
||||
changes = {name: __salt__['service.stop'](name)}
|
||||
|
||||
if not changes[name]:
|
||||
return {'name': name,
|
||||
'changes': {},
|
||||
'result': False,
|
||||
'comment': 'Service {0} failed to stop'.format(name)}
|
||||
|
||||
return {'name': name,
|
||||
'changes': changes,
|
||||
'result': True,
|
||||
'comment': 'Service {0} killed'.format(name)}
|
||||
|
||||
|
||||
def watcher(name, sig=None):
|
||||
'''
|
||||
The service watcher, called to invoke the watch command.
|
||||
The service watcher, called to invoke the watch command.
|
||||
|
||||
name
|
||||
The name of the init or rc script used to manage the service
|
||||
|
@ -79,8 +88,8 @@ def watcher(name, sig=None):
|
|||
'changes': changes,
|
||||
'result': True,
|
||||
'comment': 'Service restarted'}
|
||||
|
||||
return {'name': name,
|
||||
'changes': {},
|
||||
'result': True,
|
||||
'comment': 'Service {0} started'.format(name)}
|
||||
|
||||
|
|
|
@ -14,12 +14,13 @@ controlled via the ssh_auth state:
|
|||
- enc: ssh-dss
|
||||
'''
|
||||
|
||||
|
||||
def present(
|
||||
name,
|
||||
user,
|
||||
enc='ssh-rsa',
|
||||
comment='',
|
||||
options=[],
|
||||
options=[], # FIXME: mutable type; http://goo.gl/ToU2z
|
||||
config='.ssh/authorized_keys'):
|
||||
'''
|
||||
Verifies that the specified ssh key is present for the specified user
|
||||
|
@ -47,6 +48,7 @@ def present(
|
|||
'changes': {},
|
||||
'result': True,
|
||||
'comment': ''}
|
||||
|
||||
data = __salt__['ssh.set_auth_key'](
|
||||
user,
|
||||
name,
|
||||
|
@ -54,17 +56,23 @@ def present(
|
|||
comment,
|
||||
options,
|
||||
config)
|
||||
|
||||
if data == 'replace':
|
||||
ret['changes'][name] = 'Updated'
|
||||
ret['comment'] = 'The authorized host key {0} for user {1} was updated'.format(name, user)
|
||||
ret['comment'] = ('The authorized host key {0} for user {1} was '
|
||||
'updated'.format(name, user))
|
||||
return ret
|
||||
elif data == 'no change':
|
||||
ret['comment'] = 'The authorized host key {0} is already present for user {1}'.format(name, user)
|
||||
ret['comment'] = ('The authorized host key {0} is already present '
|
||||
'for user {1}'.format(name, user))
|
||||
elif data == 'new':
|
||||
ret['changes'][name] = 'New'
|
||||
ret['comment'] = 'The authorized host key {0} for user {1} was added'.format(name, user)
|
||||
ret['comment'] = ('The authorized host key {0} for user {1} was added'
|
||||
.format(name, user))
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def absent(name, user, config='.ssh/authorized_keys'):
|
||||
'''
|
||||
Verifies that the specified ssh key is absent
|
||||
|
@ -83,10 +91,13 @@ def absent(name, user, config='.ssh/authorized_keys'):
|
|||
'changes': {},
|
||||
'result': True,
|
||||
'comment': ''}
|
||||
|
||||
ret['comment'] = __salt__['ssh.rm_auth_key'](user, name, config)
|
||||
|
||||
if ret['comment'] == 'User authorized keys file not present':
|
||||
ret['result'] = False
|
||||
return ret
|
||||
elif ret['comment'] == 'Key removed':
|
||||
ret['changes'][name] = 'Removed'
|
||||
|
||||
return ret
|
||||
|
|
|
@ -12,6 +12,7 @@ Control the kernel sysctl system
|
|||
- value: 20
|
||||
'''
|
||||
|
||||
|
||||
def present(name, value, config='/etc/sysctl.conf'):
|
||||
'''
|
||||
Ensure that the named sysctl value is set
|
||||
|
@ -29,7 +30,9 @@ def present(name, value, config='/etc/sysctl.conf'):
|
|||
'result': True,
|
||||
'changes': {},
|
||||
'comment': ''}
|
||||
|
||||
update = __salt__['sysctl.persist'](name, value, config)
|
||||
|
||||
if update == 'Updated':
|
||||
ret['changes'] = {name: value}
|
||||
ret['comment'] = 'Updated sysctl value {0} = {1}'.format(name, value)
|
||||
|
@ -38,5 +41,5 @@ def present(name, value, config='/etc/sysctl.conf'):
|
|||
name,
|
||||
value
|
||||
)
|
||||
return ret
|
||||
|
||||
return ret
|
||||
|
|
|
@ -19,6 +19,7 @@ as either absent or present
|
|||
- games
|
||||
'''
|
||||
|
||||
|
||||
def present(
|
||||
name,
|
||||
uid=None,
|
||||
|
@ -57,8 +58,10 @@ def present(
|
|||
'changes': {},
|
||||
'result': True,
|
||||
'comment': 'User {0} is present and up to date'.format(name)}
|
||||
|
||||
print password
|
||||
lshad = __salt__['shadow.info'](name)
|
||||
|
||||
for lusr in __salt__['user.getent']():
|
||||
# Scan over the users
|
||||
if lusr['name'] == name:
|
||||
|
@ -100,6 +103,7 @@ def present(
|
|||
if ret['changes']:
|
||||
ret['comment'] = 'Updated user {0}'.format(name)
|
||||
return ret
|
||||
|
||||
# The user is not present, make it!
|
||||
if __salt__['user.add'](name, uid, gid, groups, home, shell):
|
||||
ret['comment'] = 'New user {0} created'.format(name)
|
||||
|
@ -115,8 +119,10 @@ def present(
|
|||
else:
|
||||
ret['comment'] = 'Failed to create new user {0}'.format(name)
|
||||
ret['result'] = False
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def absent(name, purge=False, force=False):
|
||||
'''
|
||||
Ensure that the named user is absent
|
||||
|
@ -135,6 +141,7 @@ def absent(name, purge=False, force=False):
|
|||
'changes': {},
|
||||
'result': True,
|
||||
'comment': ''}
|
||||
|
||||
for lusr in __salt__['user.getent']():
|
||||
# Scan over the users
|
||||
if lusr['name'] == name:
|
||||
|
@ -147,6 +154,7 @@ def absent(name, purge=False, force=False):
|
|||
ret['result'] = False
|
||||
ret['comment'] = 'Failed to remove user {0}'.format(name)
|
||||
return ret
|
||||
ret['comment'] = 'User {0} is not present'.format(name)
|
||||
return ret
|
||||
|
||||
ret['comment'] = 'User {0} is not present'.format(name)
|
||||
|
||||
return ret
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
'''
|
||||
Some of the utils used by salt
|
||||
'''
|
||||
# Import python libs
|
||||
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import logging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
@ -30,6 +30,7 @@ DEFAULT_COLOR = '\033[00m'
|
|||
RED_BOLD = '\033[01;31m'
|
||||
ENDC = '\033[0m'
|
||||
|
||||
|
||||
def get_colors(use=True):
|
||||
'''
|
||||
Return the colors as an easy to use dict, pass False to return the colors
|
||||
|
@ -56,11 +57,14 @@ def get_colors(use=True):
|
|||
'RED_BOLD': '\033[01;31m',
|
||||
'ENDC': '\033[0m',
|
||||
}
|
||||
|
||||
if not use:
|
||||
for color in colors:
|
||||
colors[color] = ''
|
||||
|
||||
return colors
|
||||
|
||||
|
||||
def daemonize():
|
||||
'''
|
||||
Daemonize a process
|
||||
|
@ -89,11 +93,12 @@ def daemonize():
|
|||
print >> sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
|
||||
sys.exit(1)
|
||||
|
||||
dev_null = open('/dev/null','rw')
|
||||
dev_null = open('/dev/null', 'rw')
|
||||
os.dup2(dev_null.fileno(), sys.stdin.fileno())
|
||||
os.dup2(dev_null.fileno(), sys.stdout.fileno())
|
||||
os.dup2(dev_null.fileno(), sys.stderr.fileno())
|
||||
|
||||
|
||||
def check_root():
|
||||
'''
|
||||
Most of the salt scripts need to run as root, this function will simply
|
||||
|
@ -105,6 +110,7 @@ def check_root():
|
|||
'http://xkcd.com/838/')
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def profile_func(filename=None):
|
||||
'''
|
||||
Decorator for adding profiling to a nested function in Salt
|
||||
|
@ -116,11 +122,12 @@ def profile_func(filename=None):
|
|||
try:
|
||||
profiler = cProfile.Profile()
|
||||
retval = profiler.runcall(fun, *args, **kwargs)
|
||||
profiler.dump_stats(filename or '{0}_func.profile'.format(fun.__name__))
|
||||
profiler.dump_stats((filename or '{0}_func.profile'
|
||||
.format(fun.__name__)))
|
||||
except IOError:
|
||||
logging.exception('Could not open profile file {0}'.format(filename))
|
||||
logging.exception(('Could not open profile file {0}'
|
||||
.format(filename)))
|
||||
|
||||
return retval
|
||||
return profiled_func
|
||||
return proffunc
|
||||
|
||||
|
|
|
@ -88,6 +88,7 @@ import stat
|
|||
import sys
|
||||
import time
|
||||
|
||||
|
||||
# Set up logger
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
@ -95,21 +96,20 @@ _REQUIRES_PATH = 1
|
|||
_REQUIRES_STAT = 2
|
||||
_REQUIRES_CONTENTS = 4
|
||||
|
||||
_FILE_TYPES = {'b' : stat.S_IFBLK,
|
||||
'c' : stat.S_IFCHR,
|
||||
'd' : stat.S_IFDIR,
|
||||
'f' : stat.S_IFREG,
|
||||
'l' : stat.S_IFLNK,
|
||||
'p' : stat.S_IFIFO,
|
||||
's' : stat.S_IFSOCK,
|
||||
|
||||
stat.S_IFBLK : 'b',
|
||||
stat.S_IFCHR : 'c',
|
||||
stat.S_IFDIR : 'd',
|
||||
stat.S_IFREG : 'f',
|
||||
stat.S_IFLNK : 'l',
|
||||
stat.S_IFIFO : 'p',
|
||||
stat.S_IFSOCK : 's'}
|
||||
_FILE_TYPES = {'b': stat.S_IFBLK,
|
||||
'c': stat.S_IFCHR,
|
||||
'd': stat.S_IFDIR,
|
||||
'f': stat.S_IFREG,
|
||||
'l': stat.S_IFLNK,
|
||||
'p': stat.S_IFIFO,
|
||||
's': stat.S_IFSOCK,
|
||||
stat.S_IFBLK: 'b',
|
||||
stat.S_IFCHR: 'c',
|
||||
stat.S_IFDIR: 'd',
|
||||
stat.S_IFREG: 'f',
|
||||
stat.S_IFLNK: 'l',
|
||||
stat.S_IFIFO: 'p',
|
||||
stat.S_IFSOCK: 's'}
|
||||
|
||||
_INTERVAL_REGEX = re.compile(r'''
|
||||
^\s*
|
||||
|
@ -122,6 +122,7 @@ _INTERVAL_REGEX = re.compile(r'''
|
|||
''',
|
||||
flags=re.VERBOSE)
|
||||
|
||||
|
||||
def _parse_interval(value):
|
||||
'''
|
||||
Convert an interval string like 1w3d6h into the number of seconds and the
|
||||
|
@ -150,20 +151,21 @@ def _parse_interval(value):
|
|||
|
||||
return result, resolution
|
||||
|
||||
|
||||
def _parse_size(value):
|
||||
scalar = value.strip()
|
||||
|
||||
if scalar.startswith(('-','+')):
|
||||
if scalar.startswith(('-', '+')):
|
||||
style = scalar[0]
|
||||
scalar = scalar[1:]
|
||||
else:
|
||||
style = '='
|
||||
|
||||
if len(scalar) > 0:
|
||||
multiplier = {'k' : 2**10,
|
||||
'm' : 2**20,
|
||||
'g' : 2**30,
|
||||
't' : 2**40}.get(scalar[-1])
|
||||
multiplier = {'k': 2 ** 10,
|
||||
'm': 2 ** 20,
|
||||
'g': 2 ** 30,
|
||||
't': 2 ** 40}.get(scalar[-1])
|
||||
if multiplier:
|
||||
scalar = scalar[:-1].strip()
|
||||
else:
|
||||
|
@ -191,6 +193,7 @@ def _parse_size(value):
|
|||
|
||||
return min_size, max_size
|
||||
|
||||
|
||||
class Option(object):
|
||||
'''
|
||||
Abstract base class for all find options.
|
||||
|
@ -198,6 +201,7 @@ class Option(object):
|
|||
def requires(self):
|
||||
return _REQUIRES_PATH
|
||||
|
||||
|
||||
class NameOption(Option):
|
||||
'''
|
||||
Match files with a case-sensitive glob filename pattern.
|
||||
|
@ -212,6 +216,7 @@ class NameOption(Option):
|
|||
def match(self, dirname, filename, fstat):
|
||||
return self.re.match(filename)
|
||||
|
||||
|
||||
class InameOption(Option):
|
||||
'''
|
||||
Match files with a case-insensitive glob filename pattern.
|
||||
|
@ -227,6 +232,7 @@ class InameOption(Option):
|
|||
def match(self, dirname, filename, fstat):
|
||||
return self.re.match(filename)
|
||||
|
||||
|
||||
class RegexOption(Option):
|
||||
'''Match files with a case-sensitive regular expression.
|
||||
Note: this is the 'basename' portion of a pathname.
|
||||
|
@ -241,6 +247,7 @@ class RegexOption(Option):
|
|||
def match(self, dirname, filename, fstat):
|
||||
return self.re.match(filename)
|
||||
|
||||
|
||||
class IregexOption(Option):
|
||||
'''Match files with a case-insensitive regular expression.
|
||||
Note: this is the 'basename' portion of a pathname.
|
||||
|
@ -255,6 +262,7 @@ class IregexOption(Option):
|
|||
def match(self, dirname, filename, fstat):
|
||||
return self.re.match(filename)
|
||||
|
||||
|
||||
class TypeOption(Option):
|
||||
'''
|
||||
Match files by their file type(s).
|
||||
|
@ -285,6 +293,7 @@ class TypeOption(Option):
|
|||
def match(self, dirname, filename, fstat):
|
||||
return stat.S_IFMT(fstat[stat.ST_MODE]) in self.ftypes
|
||||
|
||||
|
||||
class OwnerOption(Option):
|
||||
'''
|
||||
Match files by their owner name(s) and/or uid(s), e.g. 'root'.
|
||||
|
@ -309,6 +318,7 @@ class OwnerOption(Option):
|
|||
def match(self, dirname, filename, fstat):
|
||||
return fstat[stat.ST_UID] in self.uids
|
||||
|
||||
|
||||
class GroupOption(Option):
|
||||
'''
|
||||
Match files by their group name(s) and/or uid(s), e.g. 'admin'.
|
||||
|
@ -333,6 +343,7 @@ class GroupOption(Option):
|
|||
def match(self, dirname, filename, fstat):
|
||||
return fstat[stat.ST_GID] in self.gids
|
||||
|
||||
|
||||
class SizeOption(Option):
|
||||
'''
|
||||
Match files by their size.
|
||||
|
@ -356,6 +367,7 @@ class SizeOption(Option):
|
|||
def match(self, dirname, filename, fstat):
|
||||
return self.min_size <= fstat[stat.ST_SIZE] <= self.max_size
|
||||
|
||||
|
||||
class MtimeOption(Option):
|
||||
'''
|
||||
Match files modified since the specified time.
|
||||
|
@ -379,6 +391,7 @@ class MtimeOption(Option):
|
|||
def match(self, dirname, filename, fstat):
|
||||
return fstat[stat.ST_MTIME] >= self.min_time
|
||||
|
||||
|
||||
class GrepOption(Option):
|
||||
'''Match files when a pattern occurs within the file.
|
||||
The option name is 'grep', e.g. {'grep' : '(foo)|(bar}'}.
|
||||
|
@ -401,6 +414,7 @@ class GrepOption(Option):
|
|||
return os.path.join(dirname, filename)
|
||||
return None
|
||||
|
||||
|
||||
class PrintOption(Option):
|
||||
'''
|
||||
Return information about a matched file.
|
||||
|
@ -474,12 +488,13 @@ class PrintOption(Option):
|
|||
else:
|
||||
return result
|
||||
|
||||
|
||||
class Finder(object):
|
||||
def __init__(self, options):
|
||||
self.actions = []
|
||||
criteria = {_REQUIRES_PATH : list(),
|
||||
_REQUIRES_STAT : list(),
|
||||
_REQUIRES_CONTENTS : list()}
|
||||
self.actions = []
|
||||
criteria = {_REQUIRES_PATH: list(),
|
||||
_REQUIRES_STAT: list(),
|
||||
_REQUIRES_CONTENTS: list()}
|
||||
for key, value in options.iteritems():
|
||||
if value is None or len(value) == 0:
|
||||
raise ValueError('missing value for "{0}" option'.format(key))
|
||||
|
@ -498,7 +513,7 @@ class Finder(object):
|
|||
if hasattr(obj, 'execute'):
|
||||
self.actions.append(obj)
|
||||
if len(self.actions) == 0:
|
||||
self.actions.append(PrintOption('print',''))
|
||||
self.actions.append(PrintOption('print', ''))
|
||||
# order criteria so that least expensive checks are done first
|
||||
self.criteria = criteria[_REQUIRES_PATH] + \
|
||||
criteria[_REQUIRES_STAT] + \
|
||||
|
@ -527,14 +542,17 @@ class Finder(object):
|
|||
if fullpath is None:
|
||||
fullpath = os.path.join(dirpath, name)
|
||||
for action in self.actions:
|
||||
if fstat is None and action.requires() & _REQUIRES_STAT:
|
||||
if (fstat is None and
|
||||
action.requires() & _REQUIRES_STAT):
|
||||
fstat = os.stat(fullpath)
|
||||
result = action.execute(fullpath, fstat)
|
||||
if result is not None:
|
||||
yield result
|
||||
|
||||
|
||||
def find(path, options):
|
||||
'''
|
||||
WRITEME
|
||||
'''
|
||||
f = Finder(options)
|
||||
for path in f.find(path):
|
||||
|
@ -544,8 +562,10 @@ if __name__ == '__main__':
|
|||
if len(sys.argv) < 2:
|
||||
print >> sys.stderr, "usage: {0} path [options]".format(sys.argv[0])
|
||||
sys.exit(1)
|
||||
|
||||
path = sys.argv[1]
|
||||
criteria = {}
|
||||
|
||||
for arg in sys.argv[2:]:
|
||||
key, value = arg.split('=')
|
||||
criteria[key] = value
|
||||
|
|
Loading…
Add table
Reference in a new issue