import ssl
import pipes
import subprocess
+import urllib.parse
+import traceback
from http.client import HTTPSConnection
def __init__(self, appname=None, verbose=0, version=__version__):
"""Constructor."""
- self.ignore_projects = []
self.r10k_bin = None
self.description = textwrap.dedent('''\
Receives push events as JSON-Data and synchronizes
self.locale = 'de_DE.utf8'
self.simulate = False
+ self.http_timeout = 30
self.puppetmaster_host = 'puppetmaster01.pixelpark.com'
self.puppetmaster_api_port = 8140
LOG.error("Puppetmaster SSL directory {!r} is not an absolute path name.".format(
self.puppetmaster_ssl_dir))
sys.exit(10)
+
+ pdir = os.path.dirname(self.puppetmaster_ssl_dir)
+ if not os.path.isdir(pdir):
+ LOG.error("Directory {!r} does not exists.".format(pdir))
+ sys.exit(10)
+
+ if not os.access(pdir, os.R_OK):
+ LOG.error((
+ "Directory {!r} is read protected, "
+ "cannot check existence of cert files.").format(pdir))
+ return
+
if not os.path.isdir(self.puppetmaster_ssl_dir):
LOG.error("Puppetmaster SSL directory {!r} does not exists.".format(
self.puppetmaster_ssl_dir))
if 'simulate' in config:
self.simulate = to_bool(config['simulate'])
- if 'ignore_projects' in config:
- if config['ignore_projects'] is None:
- self.ignore_projects = []
- elif isinstance(config['ignore_projects'], str):
- if config['ignore_projects']:
- self.ignore_projects = [config['ignore_projects']]
- elif isinstance(config['ignore_projects'], list):
- self.ignore_projects = config['ignore_projects']
-
- if 'add_ignore_projects' in config and config['add_ignore_projects']:
- if isinstance(config['add_ignore_projects'], str):
- self.ignore_projects.append(config['add_ignore_projects'])
- elif isinstance(config['add_ignore_projects'], list):
- self.ignore_projects += config['add_ignore_projects']
-
if 'locale' in config and config['locale']:
self.locale = config['locale']
if not super(R10kHookApp, self).pre_run():
return False
- if self.full_name in self.ignore_projects or self.name in self.ignore_projects:
- LOG.info("Ignoring project {!r}.".format(self.full_name))
- return False
-
cur_loc = locale.getlocale()
cur_lang = os.environ.get('LANG', None)
if self.verbose > 1:
def run(self):
"""Main routine."""
- LOG.info("Starting {} ...".format(self.appname))
-
if not self.exec_r10k():
+ LOG.warn("Executing {!r} was not successful.".format(self.r10k_bin))
return
- ssl_context = ssl.SSLContext()
- ssl_context.verify_mode = ssl.CERT_NONE
+ ssl_context = None
+ try:
+ ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
+ except Exception as e:
+ LOG.error("Got a {c}: {e}".format(c=e.__class__.__name__, e=e))
+ else:
+ ssl_context.verify_mode = ssl.CERT_NONE
+ ssl_context.check_hostname = False
+
+ key_file = os.path.join(
+ self.puppetmaster_ssl_dir, 'private_keys', self.puppetmaster_host + '.pem')
+ cert_file = os.path.join(
+ self.puppetmaster_ssl_dir, 'certs', self.puppetmaster_host + '.pem')
+
+ LOG.debug("Creating connection to https://{h}:{p} ...".format(
+ h=self.puppetmaster_host, p=self.puppetmaster_api_port))
+ conn = HTTPSConnection(
+ self.puppetmaster_host, self.puppetmaster_api_port,
+ key_file=key_file, cert_file=cert_file, timeout=self.http_timeout,
+ context=ssl_context)
+ if self.verbose > 1:
+ LOG.debug("HTTPS connection object: {!r}".format(conn))
+
+ path = (
+ self.puppetmaster_api_path + '/environment-cache?environment=' +
+ urllib.parse.quote(self.ref))
+ url = 'https://{h}:{po}{pa}'.format(
+ h=self.puppetmaster_host, po=self.puppetmaster_api_port, pa=path)
+ LOG.info("Requesting DELETE from {} ...".format(url))
+
+ if self.simulate:
+ LOG.info("Simulation mode, don't requesting {}.".format(url))
+ return
+
+ conn.request('DELETE', path)
+ response = conn.getresponse()
+
+ LOG.info("Response: {s} {r}".format(s=response.status, r=response.reason))
+ if response.status != 200:
+ msg = 'Error on clearing Puppet cache:'
+ self.error_data.append(msg)
+ LOG.error(msg)
+
+ return
# -------------------------------------------------------------------------
def exec_r10k(self):