From 23d0680a90546adab594444a1c6f87e501c1ab05 Mon Sep 17 00:00:00 2001 From: Mike Place Date: Thu, 21 Nov 2013 15:40:38 -0700 Subject: [PATCH 1/7] Broader exception handling for invalid options in the salt command. Refs #8016. --- salt/utils/parsers.py | 73 ++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/salt/utils/parsers.py b/salt/utils/parsers.py index 8d026d6b2b..008fcfc6c0 100644 --- a/salt/utils/parsers.py +++ b/salt/utils/parsers.py @@ -1119,10 +1119,13 @@ class SaltCMDOptionParser(OptionParser, ConfigDirMixIn, MergeConfigMixIn, self.args[2] = self.args[2] if self.options.list: - if ',' in self.args[0]: - self.config['tgt'] = self.args[0].split(',') - else: - self.config['tgt'] = self.args[0].split() + try: + if ',' in self.args[0]: + self.config['tgt'] = self.args[0].split(',') + else: + self.config['tgt'] = self.args[0].split() + except IndexError: + self.exit(42, '\nCannot execute command without defining a target.\n\n') else: try: self.config['tgt'] = self.args[0] @@ -1130,35 +1133,39 @@ class SaltCMDOptionParser(OptionParser, ConfigDirMixIn, MergeConfigMixIn, self.exit(42, '\nCannot execute command without defining a target.\n\n') # Detect compound command and set up the data for it if self.args: - if ',' in self.args[1]: - self.config['fun'] = self.args[1].split(',') - self.config['arg'] = [[]] - cmd_index = 0 - if (self.args[2:].count(self.options.args_separator) == - len(self.config['fun']) - 1): - # new style parsing: standalone argument separator - for arg in self.args[2:]: - if arg == self.options.args_separator: - cmd_index += 1 - self.config['arg'].append([]) - else: - self.config['arg'][cmd_index].append(arg) - else: - # old style parsing: argument separator can be inside args - for arg in self.args[2:]: - if self.options.args_separator in arg: - sub_args = arg.split(self.options.args_separator) - for sub_arg_index, sub_arg in enumerate(sub_args): - if sub_arg: - self.config['arg'][cmd_index].append(sub_arg) - if sub_arg_index != len(sub_args) - 1: - cmd_index += 1 - self.config['arg'].append([]) - else: - self.config['arg'][cmd_index].append(arg) - if len(self.config['fun']) != len(self.config['arg']): - self.exit(42, 'Cannot execute compound command without ' - 'defining all arguments.') + try: + if ',' in self.args[1]: + self.config['fun'] = self.args[1].split(',') + self.config['arg'] = [[]] + cmd_index = 0 + if (self.args[2:].count(self.options.args_separator) == + len(self.config['fun']) - 1): + # new style parsing: standalone argument separator + for arg in self.args[2:]: + if arg == self.options.args_separator: + cmd_index += 1 + self.config['arg'].append([]) + else: + self.config['arg'][cmd_index].append(arg) + else: + # old style parsing: argument separator can be inside args + for arg in self.args[2:]: + if self.options.args_separator in arg: + sub_args = arg.split(self.options.args_separator) + for sub_arg_index, sub_arg in enumerate(sub_args): + if sub_arg: + self.config['arg'][cmd_index].append(sub_arg) + if sub_arg_index != len(sub_args) - 1: + cmd_index += 1 + self.config['arg'].append([]) + else: + self.config['arg'][cmd_index].append(arg) + if len(self.config['fun']) != len(self.config['arg']): + self.exit(42, 'Cannot execute compound command without ' + 'defining all arguments.') + except IndexError: + self.exit(42, '\nIncomplete options passed.\n\n') + else: self.config['fun'] = self.args[1] self.config['arg'] = self.args[2:] From 667eff7cfcfd4eeaca60d39852760971d785b3ec Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Fri, 22 Nov 2013 13:25:25 +0000 Subject: [PATCH 2/7] PEP8 fixes W293 W391(whitespace and extra lines). --- salt/modules/status.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/salt/modules/status.py b/salt/modules/status.py index 923a98c47d..04429ec466 100644 --- a/salt/modules/status.py +++ b/salt/modules/status.py @@ -504,7 +504,7 @@ def pid(sig): cmd = "{0[ps]} | grep {1} | grep -v grep | awk '{{print $2}}'".format( __grains__, sig) return (__salt__['cmd.run_stdout'](cmd) or '') - + def version(): ''' @@ -522,6 +522,3 @@ def version(): ret = salt.utils.fopen(procf, 'r').read().strip() return ret - - - From 52cda3aca11cb2bbac743d746f81a30fc51830c0 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Fri, 22 Nov 2013 13:35:30 +0000 Subject: [PATCH 3/7] PyLint's `enable-msg` is deprecated. Use `enable`. --- salt/cloud/libcloudfuncs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/cloud/libcloudfuncs.py b/salt/cloud/libcloudfuncs.py index afc3f8a0b3..5f4718c240 100644 --- a/salt/cloud/libcloudfuncs.py +++ b/salt/cloud/libcloudfuncs.py @@ -18,7 +18,7 @@ from libcloud.compute.deployment import ( ScriptDeployment, SSHKeyDeployment ) -# pylint: enable-msg=W0611 +# pylint: enable=W0611 # Import salt libs From aa6df17aa971bf1238daa39622af5739fa4b41da Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Fri, 22 Nov 2013 13:37:34 +0000 Subject: [PATCH 4/7] PyLint fixes. W291, W293, E302. --- salt/states/lvs_server.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/salt/states/lvs_server.py b/salt/states/lvs_server.py index dbbc8c7302..0ff93d43df 100644 --- a/salt/states/lvs_server.py +++ b/salt/states/lvs_server.py @@ -7,6 +7,7 @@ Management of LVS(Linux Virtual Server) Real Server. This lvs_server module is used to add and manage LVS Real Server in the specified service. Server can be set as either absent or present. ''' + def __virtual__(): ''' @@ -14,12 +15,13 @@ def __virtual__(): ''' return 'lvs_server' if 'lvs.get_rules' in __salt__ else False + def present(name, protocol=None, service_address=None, server_address=None, packet_forward_method='dr', - weight=1 + weight=1 ): ''' Ensure that the named service is present. @@ -35,7 +37,7 @@ def present(name, server_address The real server address. - + packet_forward_method The LVS packet forwarding method(``dr`` for direct routing, ``tunnel`` for tunneling, ``nat`` for network access translation). @@ -111,22 +113,22 @@ def present(name, return ret return ret - + def absent(name, protocol=None, service_address=None, server_address=None): ''' - + Ensure the LVS Real Server in specified service is absent. name The name of the LVS server. - + protocol The service protocol(only support ``tcp``, ``udp`` and ``fwmark`` service). - + service_address The LVS service adress. - + server_address The LVS real server address. ''' @@ -134,7 +136,7 @@ def absent(name, protocol=None, service_address=None, server_address=None): 'changes': {}, 'result': True, 'comment': ''} - + #check if server exists and remove it server_check = __salt__['lvs.check_server'](protocol=protocol, service_address=service_address, From a344ae79895add86ffc9f2412ca3b68a7e8bf6c3 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Fri, 22 Nov 2013 13:38:54 +0000 Subject: [PATCH 5/7] Pylint fixes. W293, E302. --- salt/states/lvs_service.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/salt/states/lvs_service.py b/salt/states/lvs_service.py index 3db15ab904..04905c89ca 100644 --- a/salt/states/lvs_service.py +++ b/salt/states/lvs_service.py @@ -7,6 +7,7 @@ Management of LVS(Linux Virtual Server) Service. This lvs_service module is used to create and manage LVS Service. Service can be set as either absent or present. ''' + def __virtual__(): ''' @@ -14,6 +15,7 @@ def __virtual__(): ''' return 'lvs_service' if 'lvs.get_rules' in __salt__ else False + def present(name, protocol=None, service_address=None, @@ -93,11 +95,11 @@ def present(name, return ret return ret - + def absent(name, protocol=None, service_address=None): ''' - + Ensure the LVS service is absent. name @@ -113,7 +115,7 @@ def absent(name, protocol=None, service_address=None): 'changes': {}, 'result': True, 'comment': ''} - + #check if service exists and remove it service_check = __salt__['lvs.check_service'](protocol=protocol, service_address=service_address) From 31c45491e09e4b4100026aac0bb2369d2fea29f1 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Fri, 22 Nov 2013 13:41:55 +0000 Subject: [PATCH 6/7] Several PyLint Fixes. Indentation, whitespace, etc. --- salt/modules/lvs.py | 80 +++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/salt/modules/lvs.py b/salt/modules/lvs.py index f8a5a77086..e8d88732bd 100644 --- a/salt/modules/lvs.py +++ b/salt/modules/lvs.py @@ -11,6 +11,7 @@ import salt.utils import salt.utils.decorators as decorators from salt.exceptions import SaltException + # Cache the output of running which('ipvsadm') @decorators.memoize def __detect_os(): @@ -26,6 +27,7 @@ def __virtual__(): return 'lvs' + def _build_cmd(**kwargs): ''' @@ -59,26 +61,24 @@ def _build_cmd(**kwargs): if kwargs['server_address']: cmd += ' -r {0}'.format(kwargs['server_address']) if 'packet_forward_method' in kwargs and kwargs['packet_forward_method']: - if kwargs['packet_forward_method'] == 'dr': - cmd += ' -g' - elif kwargs['packet_forward_method'] == 'tunnel': - cmd += ' -i' - elif kwargs['packet_forward_method'] == 'nat': - cmd += ' -m' - else: - raise SaltException('Error: only support dr, tunnel and nat') - del kwargs['packet_forward_method'] + if kwargs['packet_forward_method'] == 'dr': + cmd += ' -g' + elif kwargs['packet_forward_method'] == 'tunnel': + cmd += ' -i' + elif kwargs['packet_forward_method'] == 'nat': + cmd += ' -m' + else: + raise SaltException('Error: only support dr, tunnel and nat') + del kwargs['packet_forward_method'] if 'weight' in kwargs and kwargs['weight']: - cmd += ' -w {0}'.format(kwargs['weight']) - del kwargs['weight'] + cmd += ' -w {0}'.format(kwargs['weight']) + del kwargs['weight'] else: raise SaltException('Error: server_address should specified') del kwargs['server_address'] return cmd - - def add_service(protocol=None, service_address=None, scheduler='wlc'): ''' @@ -100,7 +100,7 @@ def add_service(protocol=None, service_address=None, scheduler='wlc'): salt '*' lvs.add_service tcp 1.1.1.1:80 rr ''' - + cmd = '{0} -A {1}'.format(__detect_os(), _build_cmd(protocol=protocol, service_address=service_address, @@ -135,7 +135,7 @@ def edit_service(protocol=None, service_address=None, scheduler=None): salt '*' lvs.edit_service tcp 1.1.1.1:80 rr ''' - + cmd = '{0} -E {1}'.format(__detect_os(), _build_cmd(protocol=protocol, service_address=service_address, @@ -160,12 +160,12 @@ def delete_service(protocol=None, service_address=None): service_address The LVS service address. - + CLI Example: - + .. code-block:: bash - + salt '*' lvs.delete_service tcp 1.1.1.1:80 ''' @@ -195,18 +195,18 @@ def add_server(protocol=None, service_address=None, server_address=None, packet_ server_address The real server address. - + packet_forward_method The LVS packet forwarding method(``dr`` for direct routing, ``tunnel`` for tunneling, ``nat`` for network access translation). weight The capacity of a server relative to the others in the pool. - + CLI Example: - + .. code-block:: bash - + salt '*' lvs.add_server tcp 1.1.1.1:80 192.168.0.11:8080 nat 1 ''' @@ -240,18 +240,18 @@ def edit_server(protocol=None, service_address=None, server_address=None, packet server_address The real server address. - + packet_forward_method The LVS packet forwarding method(``dr`` for direct routing, ``tunnel`` for tunneling, ``nat`` for network access translation). weight The capacity of a server relative to the others in the pool. - + CLI Example: - + .. code-block:: bash - + salt '*' lvs.edit_server tcp 1.1.1.1:80 192.168.0.11:8080 nat 1 ''' @@ -285,12 +285,12 @@ def delete_server(protocol=None, service_address=None, server_address=None): server_address The real server address. - - + + CLI Example: .. code-block:: bash - + salt '*' lvs.delete_server tcp 1.1.1.1:80 192.168.0.11:8080 ''' @@ -310,7 +310,7 @@ def delete_server(protocol=None, service_address=None, server_address=None): def clear(): ''' - + Clear the virtual server table CLI Example: @@ -334,11 +334,11 @@ def clear(): def get_rules(): ''' - + Get the virtual server rules CLI Example: - + .. code-block:: bash salt '*' lvs.get_rules @@ -356,12 +356,12 @@ def list(protocol=None, service_address=None): List the virtual server table if service_address is not specified. If a service_address is selected, list this service only. CLI Example: - + .. code-block:: bash salt '*' lvs.list - ''' - + ''' + if service_address: cmd = '{0} -L {1} -n'.format(__detect_os(), _build_cmd(protocol=protocol, @@ -378,15 +378,16 @@ def list(protocol=None, service_address=None): return ret + def zero(protocol=None, service_address=None): ''' Zero the packet, byte and rate counters in a service or all services. CLI Example: - + .. code-block:: bash - + salt '*' lvs.zero ''' @@ -410,7 +411,7 @@ def check_service(protocol=None, service_address=None, **kwargs): ''' Check the virtual service exists. - + CLI Example: .. code-block:: bash @@ -423,7 +424,7 @@ def check_service(protocol=None, service_address=None, **kwargs): **kwargs)) # Exact match if not kwargs: - cmd += ' ' + cmd += ' ' all_rules = get_rules() out = all_rules.find(cmd) @@ -434,6 +435,7 @@ def check_service(protocol=None, service_address=None, **kwargs): ret = 'Error: service not exists' return ret + def check_server(protocol=None, service_address=None, server_address=None, **kwargs): ''' From 0687027a51967fbf7fc57d4de56ed2df46c24130 Mon Sep 17 00:00:00 2001 From: Erik Johnson Date: Fri, 22 Nov 2013 11:57:04 -0600 Subject: [PATCH 7/7] Correct inaccuracy in docstring warning message --- salt/modules/pkgng.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/salt/modules/pkgng.py b/salt/modules/pkgng.py index 1f0fc94494..86ddaed068 100644 --- a/salt/modules/pkgng.py +++ b/salt/modules/pkgng.py @@ -5,12 +5,12 @@ Support for ``pkgng``, the new package manager for FreeBSD .. warning:: This module has been completely rewritten. Up to and includng version - 0.17.0, it was available as the ``pkgng`` module, (``pkgng.install``, + 0.17.x, it was available as the ``pkgng`` module, (``pkgng.install``, ``pkgng.delete``, etc.), but moving forward this module will no longer be available as ``pkgng``, as it will behave like a normal Salt ``pkg`` provider. The documentation below should not be considered to apply to this - module in versions <= 0.17.0. If your minion is running one of these - versions, then the documentation for this module can be viewed using the + module in versions <= 0.17.x. If your minion is running a 0.17.x release or + older, then the documentation for this module can be viewed using the :mod:`sys.doc ` function: .. code-block:: bash