diff --git a/salt/modules/selinux.py b/salt/modules/selinux.py index 27f3529dc805..dfbd14fec08c 100644 --- a/salt/modules/selinux.py +++ b/salt/modules/selinux.py @@ -4,6 +4,7 @@ import os + def __virtual__(): ''' Check if the os is Linux, and then if selinux is running in permissive or @@ -15,6 +16,7 @@ def __virtual__(): return 'selinux' return False + def getenforce(): ''' Return the mode selinux is running in @@ -28,6 +30,7 @@ def getenforce(): else: return 'Enforcing' + def setenforce(mode): ''' Set the enforcing mode diff --git a/salt/modules/service.py b/salt/modules/service.py index 16db878915dd..29f5cbd58784 100644 --- a/salt/modules/service.py +++ b/salt/modules/service.py @@ -2,16 +2,18 @@ Top level package command wrapper, used to translate the os detected by the grains to the correct service manager ''' + import os grainmap = { 'Arch': '/etc/rc.d', + 'Debian': '/etc/init.d', 'Fedora': '/etc/init.d', 'RedHat': '/etc/init.d', - 'Debian': '/etc/init.d', 'Ubuntu': '/etc/init.d', } + def start(name): ''' Start the specified service @@ -24,6 +26,7 @@ def start(name): name + ' start') return not __salt__['cmd.retcode'](cmd) + def stop(name): ''' Stop the specified service @@ -36,6 +39,7 @@ def stop(name): name + ' stop') return not __salt__['cmd.retcode'](cmd) + def restart(name): ''' Restart the named service @@ -48,6 +52,7 @@ def restart(name): name + ' restart') return not __salt__['cmd.retcode'](cmd) + def status(name, sig=None): ''' Return the status for a service, returns the PID or an empty string if the diff --git a/salt/modules/shadow.py b/salt/modules/shadow.py index ff697bb2f0c5..98e4a49e9b0e 100644 --- a/salt/modules/shadow.py +++ b/salt/modules/shadow.py @@ -1,10 +1,11 @@ ''' Manage the shadow file ''' -# Import python libs + import os import spwd + def info(name): ''' Return the information for the specified user @@ -36,6 +37,7 @@ def info(name): 'expire': ''} return ret + def set_password(name, password): ''' Set the password for a named user, the password must be a properly defined @@ -65,4 +67,3 @@ def set_password(name, password): if uinfo['pwd'] == password: return True return False - diff --git a/salt/modules/solr.py b/salt/modules/solr.py index 20ff6c7527f4..259ccf4dcce8 100644 --- a/salt/modules/solr.py +++ b/salt/modules/solr.py @@ -11,24 +11,19 @@ questions or want a feature request please ask. """ -############################################################################### -# APACHE SOLR SALT MODULE # -# Author: Jed Glazner # -# Version: 0.1 # -# Modified: 9/20/2011 # -# # -# ############################################################################# -import urllib import json +import urllib + # Override these in the minion config. solr.cores as an empty list indicates # this is not a multi-core setup. __opts__ = {'solr.cores': [], 'solr.baseurl': 'http://localhost:8983/solr', - 'solr.type':'master', + 'solr.type': 'master', 'solr.init_script': '/etc/rc.d/solr'} + def __virtual__(): ''' PRIVATE METHOD @@ -39,7 +34,6 @@ def __virtual__(): TODO: currently __salt__ is not available to call in this method because all the salt modules have not been loaded yet. Use a grains module? ''' - return 'solr' names = ['solr', 'apache-solr'] for name in names: if __salt__['pkg.version'](name): @@ -47,6 +41,7 @@ def __virtual__(): return False + def __check_for_cores__(): ''' PRIVATE METHOD @@ -60,6 +55,7 @@ def __check_for_cores__(): else: return False + def _get_return_dict(success=True, data={}, errors=[], warnings=[]): ''' PRIVATE METHOD @@ -70,13 +66,14 @@ def _get_return_dict(success=True, data={}, errors=[], warnings=[]): Param: list errors Default = [] Param: list warnings Default= [] ''' - ret = {'success':success, + ret = {'success': success, 'data':data, 'errors':errors, 'warnings':warnings} return ret + def _update_return_dict(ret, success, data, errors, warnings=[]): ''' PRIVATE METHOD @@ -95,7 +92,7 @@ def _update_return_dict(ret, success, data, errors, warnings=[]): return ret -def _format_url(handler,core_name=None,extra=[]): +def _format_url(handler, core_name=None, extra=[]): ''' PRIVATE METHOD Formats the url based on parameters, and if cores are used or not @@ -115,7 +112,8 @@ def _format_url(handler,core_name=None,extra=[]): return "{0}/{1}/{2}?wt=json".format(baseurl, core_name, handler) else: return "{0}/{1}/{2}?wt=json&{3}".format(baseurl, core_name, - handler,"&".join(extra)) + handler, "&".join(extra)) + def _http_request(url): ''' @@ -129,9 +127,10 @@ def _http_request(url): ''' try: data = json.load(urllib.urlopen(url)) - return _get_return_dict(True, data,[]) + return _get_return_dict(True, data, []) except Exception as e: - return _get_return_dict(False, {}, ["{0} : {1}".format(url,e)]) + return _get_return_dict(False, {}, ["{0} : {1}".format(url, e)]) + def _replication_request(replication_command, core_name=None, params=[]): ''' @@ -149,9 +148,10 @@ def _replication_request(replication_command, core_name=None, params=[]): Return: dict {'success':bool, 'data':dict, 'errors':list, 'warnings':list} ''' extra = ["command={0}".format(replication_command)] + params - url = _format_url('replication',core_name=core_name,extra=extra) + url = _format_url('replication', core_name=core_name, extra=extra) return _http_request(url) + def _get_admin_info(command, core_name=None): ''' PRIVATE METHOD @@ -167,9 +167,10 @@ def _get_admin_info(command, core_name=None): Return: dict {'success':bool, 'data':dict, 'errors':list, 'warnings':list} ''' url = _format_url("admin/{0}".format(command), core_name=core_name) - resp = _http_request(url) + resp = _http_request(url) return resp + def lucene_version(core_name=None): ''' Gets the lucene version that solr is using. If you are running a multi-core @@ -191,25 +192,27 @@ def lucene_version(core_name=None): ret = _get_return_dict() #do we want to check for all the cores? if core_name is None and __check_for_cores__(): - success=True + success = True for name in __opts__['solr.cores']: - resp = _get_admin_info('system', core_name=name ) + resp = _get_admin_info('system', core_name=name) if resp['success']: version = resp['data']['lucene']['lucene-spec-version'] - data = {name: {'lucene_version':version}} - else:#generally this means that an exception happened. - data = {name:{'lucene_version':None}} - success=False - ret = _update_return_dict(ret,success, data, resp['errors']) + data = {name: {'lucene_version': version}} + #generally this means that an exception happened. + else: + data = {name: {'lucene_version': None}} + success = False + ret = _update_return_dict(ret, success, data, resp['errors']) return ret else: resp = _get_admin_info('system', core_name=core_name) if resp['success']: version = resp['data']['lucene']['lucene-spec-version'] - return _get_return_dict(True, {'version':version}, resp['errors']) + return _get_return_dict(True, {'version': version}, resp['errors']) else: return resp + def version(core_name=None): ''' Gets the solr version for the core specified. You should specify a core @@ -231,15 +234,15 @@ def version(core_name=None): ret = _get_return_dict() #do we want to check for all the cores? if core_name is None and __check_for_cores__(): - success=True + success = True for name in __opts__['solr.cores']: - resp = _get_admin_info('system', core_name=name ) + resp = _get_admin_info('system', core_name=name) if resp['success']: lucene = resp['data']['lucene'] - data = {name:{'version':lucene['solr-spec-version']}} + data = {name: {'version': lucene['solr-spec-version']}} else: - success=False - data = {name:{'version':None}} + success = False + data = {name: {'version': None}} ret = _update_return_dict(ret, success, data, resp['errors'], resp['warnings']) return ret @@ -247,11 +250,12 @@ def version(core_name=None): resp = _get_admin_info('system', core_name=core_name) if resp['success']: version = resp['data']['lucene']['solr-spec-version'] - return _get_return_dict(True, {'version':version}, + return _get_return_dict(True, {'version': version}, reps['errors'], resp['warnings']) else: return resp + def optimize(core_name=None): ''' RUN ON THE MASTER ONLY @@ -276,27 +280,30 @@ def optimize(core_name=None): # since only masters can call this let's check the config: if __opts__['solr.type'] != 'master': errors = ['Only minions configured as solr masters can run this'] - return ret.update({'success':False, 'errors':errors}) + return ret.update({'success': False, 'errors': errors}) if core_name is None and __check_for_cores__(): - success=True + success = True for name in __opts__['solr.cores']: - url = _format_url('update',core_name=name,extra=["optimize=true"]) + url = _format_url('update', core_name=name, + extra=["optimize=true"]) resp = _http_request(url) if resp['success']: - data = {name : {'data':resp['data']}} - ret = _update_return_dict(ret, success, data, - resp['errors'], resp['warnings']) + data = {name: {'data': resp['data']}} + ret = _update_return_dict(ret, success, data, + resp['errors'], resp['warnings']) else: - success=False - data = {name : {'data':resp['data']}} - ret = _update_return_dict(ret, success, data, + success = False + data = {name: {'data': resp['data']}} + ret = _update_return_dict(ret, success, data, resp['errors'], resp['warnings']) return ret else: - url = _format_url('update',core_name=core_name,extra=["optimize=true"]) + url = _format_url('update', core_name=core_name, + extra=["optimize=true"]) return _http_request(url) + def ping(core_name=None): ''' Does a health check on solr, makes sure solr can talk to the indexes. @@ -315,23 +322,24 @@ def ping(core_name=None): ''' ret = _get_return_dict() if core_name is None and __check_for_cores__(): - success=True + success = True for name in __opts__['solr.cores']: resp = _get_admin_info('ping', core_name=name) if resp['success']: - data = {name:{'status':resp['data']['status']}} + data = {name: {'status': resp['data']['status']}} else: - success=False - data = {name:{'status':None}} - ret = _update_return_dict(ret,success, data, resp['errors']) + success = False + data = {name: {'status': None}} + ret = _update_return_dict(ret, success, data, resp['errors']) return ret else: resp = _get_admin_info('ping', core_name=core_name) if resp['success']: - return _get_return_dict(ret,True, resp['data'], resp['errors']) + return _get_return_dict(ret, True, resp['data'], resp['errors']) else: return resp + def is_replication_enabled(core_name=None): ''' USED ONLY BY SLAVES @@ -354,24 +362,26 @@ def is_replication_enabled(core_name=None): # since only slaves can call this let's check the config: if __opts__['solr.type'] != 'slave': errors = ['Only minions configured as solr slaves can run this'] - return ret.update({'success':False, 'errors':errors}) + return ret.update({'success': False, 'errors': errors}) + #define a convenience method so we dont duplicate code - def _checks(ret, success, resp,core): + def _checks(ret, success, resp, core): if response['success']: slave = resp['data']['details']['slave'] # we need to initialize this to false in case there is an error # on the master and we can't get this info. - replication_enabled = 'false' + replication_enabled = 'false' master_url = slave['masterUrl'] #check for errors on the slave if 'ERROR' in slave: - success=False + success = False err = "{0}: {1} - {2}".format(name, slave['ERROR'], master_url) resp['errors'].append(err) #if there is an error return everything - data = slave if core is None else {core : {'data':slave}} + data = slave if core is None else {core: {'data': slave}} else: - enabled = slave['masterDetails']['master']['replicationEnabled'] + enabled = (slave['masterDetails'] + ['master']['replicationEnabled']) #if replication is turned off on the master, or polling is #isabled we need to return false. These may not not errors, #but the purpose of this call is to check to see if the slaves @@ -390,13 +400,14 @@ def _checks(ret, success, resp,core): if core_name is None and __check_for_cores__(): for name in __opts__['solr.cores']: response = _replication_request('details', core_name=name) - ret, success = _checks(ret, success, response,name) + ret, success = _checks(ret, success, response, name) else: response = _replication_request('details', core_name=core_name) - ret, success = _checks(ret, success, response,core_name) + ret, success = _checks(ret, success, response, core_name) return ret + def match_index_versions(core_name=None): ''' SLAVE ONLY @@ -421,7 +432,7 @@ def match_index_versions(core_name=None): # since only slaves can call this let's check the config: if __opts__['solr.type'] != 'slave': errors = ['Only minions configured as solr slaves can run this'] - return ret.update({'success':False, 'errors':errors}) + return ret.update({'success': False, 'errors': errors}) def _match(ret, success, resp, core): if response['success']: @@ -429,24 +440,24 @@ def _match(ret, success, resp, core): master_url = resp['data']['details']['slave']['masterUrl'] if 'ERROR' in slave: error = slave['ERROR'] - success=False + success = False err = "{0}: {1} - {2}".format(name, error, master_url) resp['errors'].append(err) #if there was an error return the entire response so the #alterer can get what it wants - data = slave if core is None else {core : {'data': slave}} + data = slave if core is None else {core: {'data': slave}} else: - versions = {'master':slave['masterDetails']['indexVersion'], - 'slave' : resp['data']['details']['indexVersion'], - 'next_replication' : slave['nextExecutionAt'], - 'failed_list' : slave['replicationFailedAtList'] + versions = {'master': slave['masterDetails']['indexVersion'], + 'slave': resp['data']['details']['indexVersion'], + 'next_replication': slave['nextExecutionAt'], + 'failed_list': slave['replicationFailedAtList'] } #check the index versions if index_versions['master'] != index_versions['slave']: success = False err = "Master and Slave index versions do not match." resp['errors'].append(err) - data = versions if core is None else {core:{'data':versions}} + data = versions if core is None else {core: {'data': versions}} ret = _update_return_dict(ret, success, data, resp['errors'], resp['warnings']) return (ret, success) @@ -459,10 +470,11 @@ def _match(ret, success, resp, core): ret, success = _match(ret, success, response, name) else: response = _replication_request('details', core_name=core_name) - ret, success = _match(ret , success, response, core_name) + ret, success = _match(ret, success, response, core_name) return ret + def replication_details(core_name=None): ''' Get the full replication details. @@ -481,21 +493,22 @@ def replication_details(core_name=None): ''' ret = _get_return_dict() if core_name is None: - success=True + success = True for name in __opts__['solr.cores']: resp = _replication_request('details', core_name=name) - data = {name : {'data':resp['data']}} + data = {name: {'data': resp['data']}} ret = _update_return_dict(ret, success, data, resp['errors'], resp['warnings']) else: resp = _replication_request('details', core_name=core_name) if resp['success']: - ret = _update_return_dict(ret, success, resp['data'], - resp['errors'], resp['warnings']) + ret = _update_return_dict(ret, success, resp['data'], + resp['errors'], resp['warnings']) else: return resp return ret + def backup_master(core_name=None, path=None): ''' Tell the master to make a backup. If you don't pass a core name and you are @@ -526,13 +539,13 @@ def backup_master(core_name=None, path=None): path = path + "{0}" ret = _get_return_dict() if core_name is None and __check_for_cores__(): - success=True + success = True for name in __opts__['solr.cores']: extra = "&location={0}".format(path + name) resp = _replication_request('backup', core_name=name, extra=extra) if not resp['success']: - success=False - data = {name : {'data': resp['data']}} + success = False + data = {name: {'data': resp['data']}} ret = _update_return_dict(ret, success, data, resp['errors'], resp['warnings']) return ret @@ -542,9 +555,11 @@ def backup_master(core_name=None, path=None): else: path = path.format("/{0}".format(core_name)) params = ["location={0}".format(path)] - resp = _replication_request('backup',core_name=core_name,params=params) + resp = _replication_request('backup', core_name=core_name, + params=params) return resp + def set_is_polling(polling, core_name=None): ''' SLAVE ONLY @@ -570,16 +585,16 @@ def set_is_polling(polling, core_name=None): # since only slaves can call this let's check the config: if __opts__['solr.type'] != 'slave': err = ['Only minions configured as solr slaves can run this'] - return ret.update({'success':False, 'errors':err}) + return ret.update({'success': False, 'errors': err}) cmd = "enablepoll" if polling else "disapblepoll" if core_name is None and __check_for_cores__(): - success=True + success = True for name in __opts__['solr.cores']: resp = _replication_request(cmd, core_name=name) if not resp['success']: success = False - data = {name : {'data' : resp['data']}} + data = {name: {'data': resp['data']}} ret = _update_return_dict(ret, success, data, resp['errors'], resp['warnings']) return ret @@ -587,6 +602,7 @@ def set_is_polling(polling, core_name=None): resp = _replication_request(cmd, core_name=name) return resp + def signal(signal=None): ''' Signals Apache Solr to start, stop, or restart. Obvioulsy this is only