import subprocess
import urllib.parse
import time
+import warnings
# Third party modules
+import requests
# Own modules
import webhooks
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()
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')
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
# -------------------------------------------------------------------------