]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Performing HTTP requests with requests module
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 27 Nov 2017 14:59:57 +0000 (15:59 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 27 Nov 2017 14:59:57 +0000 (15:59 +0100)
lib/webhooks/__init__.py
lib/webhooks/r10k.py

index abbf8850f389b0db257a0d1ff40a7ee48a4b82cb..299156b6c59a6339513f15340d9e15be1d22b8f5 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/env python3
 # -*- coding: utf-8 -*-
 
-__version__ = '0.6.4'
+__version__ = '0.7.1'
 
 # vim: ts=4 et list
index 5b699808a964b7eeaa03a35e4a1d0a59207f6ca9..aa7e4fe1959e9ded095a0bb4100cf7941e920f3a 100644 (file)
@@ -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
 
     # -------------------------------------------------------------------------