From: Frank Brehm Date: Mon, 27 Nov 2017 14:59:57 +0000 (+0100) Subject: Performing HTTP requests with requests module X-Git-Tag: 0.8.4~2 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=69ce054dc014a3a3fce82c477815b603685f4107;p=pixelpark%2Fpuppetmaster-webhooks.git Performing HTTP requests with requests module --- diff --git a/lib/webhooks/__init__.py b/lib/webhooks/__init__.py index abbf885..299156b 100644 --- a/lib/webhooks/__init__.py +++ b/lib/webhooks/__init__.py @@ -1,6 +1,6 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '0.6.4' +__version__ = '0.7.1' # vim: ts=4 et list diff --git a/lib/webhooks/r10k.py b/lib/webhooks/r10k.py index 5b69980..aa7e4fe 100644 --- a/lib/webhooks/r10k.py +++ b/lib/webhooks/r10k.py @@ -17,8 +17,10 @@ import pipes import subprocess import urllib.parse import time +import warnings # Third party modules +import requests # Own modules import webhooks @@ -58,6 +60,8 @@ class R10kHookApp(BaseHookApp): super(R10kHookApp, self).__init__( appname=appname, verbose=verbose, version=version) + self.user_agent = 'pp-webhook-client/' + __version__ + self.search_r10k_bin() self.check_cert_files() @@ -188,6 +192,13 @@ class R10kHookApp(BaseHookApp): self.puppetmaster_ssl_dir, 'private_keys', self.puppetmaster_host + '.pem') cert_file = os.path.join( self.puppetmaster_ssl_dir, 'certs', self.puppetmaster_host + '.pem') + cert = (cert_file, key_file) + session = requests.Session() + + headers = dict() + headers.update({'User-Agent': self.user_agent}) + headers.update({'Content-Type': 'text/plain'}) + ca_file = os.path.join( self.puppetmaster_ssl_dir, 'certs', 'ca.pem') @@ -198,51 +209,41 @@ class R10kHookApp(BaseHookApp): h=self.puppetmaster_host, po=self.puppetmaster_api_port, pa=path) LOG.debug("Requesting DELETE from {} ...".format(url)) - cmd = [] - if self.do_sudo: - cmd = ['sudo', '-n'] - - cmd.append(self.curl_bin) - if self.verbose: - cmd.append('-i') - - cmd += [ - '--cert', cert_file, - '--key', key_file, - '--cacert', ca_file, - '--silent', '--show-error', - '-X', 'DELETE', - url, - ] - cmd_str = ' '.join(map(lambda x: pipes.quote(x), cmd)) - if self.verbose > 2: - LOG.debug("Cmd: {}".format(pp(cmd))) - LOG.info("Executing: {}".format(cmd_str)) - if self.simulate: - LOG.info("Simulation mode, don't executing {}.".format(self.curl_bin)) + LOG.info("Simulation mode, don't requesting URL.") return - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - (stdoutdata, stderrdata) = proc.communicate() - ret_val = proc.wait() - - LOG.debug("Return value: {}".format(ret_val)) - if stdoutdata: - msg = "Output:\n{}".format(to_str(stdoutdata)) - LOG.info(msg) - self.print_out(msg) - else: - LOG.debug("No output.") - if stderrdata: - msg = "Error messages on '{c}':\n{e}".format(c=cmd_str, e=to_str(stderrdata)) - if ret_val: - LOG.warn(msg) - self.error_data.append(msg) + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + response = session.request( + 'DELETE', url, headers=headers, timeout=self.http_timeout, + cert=cert, verify=ca_file) + if w: + warn_class = w[-1].category.__name__ + warn_msg = '{}: {}'.format( + warn_class, w[-1].message) + if warn_class == 'SubjectAltNameWarning': + LOG.debug(warn_msg) + else: + LOG.warn(warn_msg) + + if response.ok: + LOG.debug("Got status code: {}.".format(response.status_code)) + if response.text: + msg = "Output:\n{}".format(response.text) else: - LOG.info(msg) + msg = "No output." + LOG.info(msg) self.print_out(msg) + return + msg = "Got status code: {}.".format(response.status_code) + if response.text: + msg += "\nOutput:\n{}".format(response.text) + else: + msg += " No output." + LOG.warn(msg) + self.print_out(msg) return # -------------------------------------------------------------------------