Commit 3cc4ecb3 authored by Marco Mariani's avatar Marco Mariani

SlapObject: long methods cleanup

extracted methods download_binary_cache(), update_binary_cache(),
_pre_destroy()
parent 3aa3a2c4
...@@ -104,6 +104,22 @@ class Software(object): ...@@ -104,6 +104,22 @@ class Software(object):
self.upload_to_binary_cache_url_blacklist = \ self.upload_to_binary_cache_url_blacklist = \
upload_to_binary_cache_url_blacklist upload_to_binary_cache_url_blacklist
def download_binary_cache(self, tarpath):
"""
returns True after a successful download
"""
return download_network_cached(
self.download_binary_cache_url,
self.download_binary_dir_url,
self.url,
self.software_root,
self.software_url_hash,
tarpath,
self.logger,
self.signature_certificate_list,
self.download_from_binary_cache_url_blacklist
)
def install(self): def install(self):
""" Fetches binary cache if possible. """ Fetches binary cache if possible.
Installs from buildout otherwise. Installs from buildout otherwise.
...@@ -113,15 +129,8 @@ class Software(object): ...@@ -113,15 +129,8 @@ class Software(object):
try: try:
tarpath = os.path.join(cache_dir, self.software_url_hash) tarpath = os.path.join(cache_dir, self.software_url_hash)
# Check if we can download from cache # Check if we can download from cache
if (not os.path.exists(self.software_path)) \ if (not os.path.exists(self.software_path)
and download_network_cached( and self.download_binary_cache(tarpath)):
self.download_binary_cache_url,
self.download_binary_dir_url,
self.url, self.software_root,
self.software_url_hash,
tarpath, self.logger,
self.signature_certificate_list,
self.download_from_binary_cache_url_blacklist):
tar = tarfile.open(tarpath) tar = tarfile.open(tarpath)
try: try:
self.logger.info("Extracting archive of cached software release...") self.logger.info("Extracting archive of cached software release...")
...@@ -130,18 +139,7 @@ class Software(object): ...@@ -130,18 +139,7 @@ class Software(object):
tar.close() tar.close()
else: else:
self._install_from_buildout() self._install_from_buildout()
# Upload to binary cache if possible and allowed self.update_binary_cache(tarpath)
if all([self.software_root, self.url, self.software_url_hash,
self.upload_binary_cache_url, self.upload_binary_dir_url]):
blacklisted = False
for url in self.upload_to_binary_cache_url_blacklist:
if self.url.startswith(url):
blacklisted = True
self.logger.info("Can't upload to binary cache: "
"Software Release URL is blacklisted.")
break
if not blacklisted:
self.uploadSoftwareRelease(tarpath)
finally: finally:
shutil.rmtree(cache_dir) shutil.rmtree(cache_dir)
...@@ -224,6 +222,19 @@ class Software(object): ...@@ -224,6 +222,19 @@ class Software(object):
""".format(url=url))) """.format(url=url)))
self._set_ownership(buildout_cfg) self._set_ownership(buildout_cfg)
def update_binary_cache(self, tarpath):
"""
Upload to binary cache if possible and allowed
"""
if all([self.software_root, self.url, self.software_url_hash,
self.upload_binary_cache_url, self.upload_binary_dir_url]):
if any(self.url.startswith(url)
for url in self.upload_to_binary_cache_url_blacklist):
self.logger.info("Can't upload to binary cache: "
"Software Release URL is blacklisted.")
else:
self.uploadSoftwareRelease(tarpath)
def uploadSoftwareRelease(self, tarpath): def uploadSoftwareRelease(self, tarpath):
""" """
Try to tar and upload an installed Software Release. Try to tar and upload an installed Software Release.
...@@ -557,18 +568,10 @@ class Partition(object): ...@@ -557,18 +568,10 @@ class Partition(object):
else: else:
self.logger.info("Requested stop of %s..." % self.computer_partition.getId()) self.logger.info("Requested stop of %s..." % self.computer_partition.getId())
def destroy(self): def _pre_destroy(self, destroy_binary):
"""Destroys the partition and makes it available for subsequent use."
"""
self.logger.info("Destroying Computer Partition %s..."
% self.computer_partition.getId())
# Launches "destroy" binary if exists
destroy_executable_location = os.path.join(self.instance_path, 'sbin',
'destroy')
if os.path.exists(destroy_executable_location):
uid, gid = self.getUserGroupId() uid, gid = self.getUserGroupId()
self.logger.debug('Invoking %r' % destroy_executable_location) self.logger.debug('Invoking %r', destroy_binary)
process_handler = SlapPopen([destroy_executable_location], process_handler = SlapPopen([destroy_binary],
preexec_fn=lambda: dropPrivileges(uid, gid, logger=self.logger), preexec_fn=lambda: dropPrivileges(uid, gid, logger=self.logger),
cwd=self.instance_path, cwd=self.instance_path,
env=getCleanEnvironment(logger=self.logger, env=getCleanEnvironment(logger=self.logger,
...@@ -577,10 +580,20 @@ class Partition(object): ...@@ -577,10 +580,20 @@ class Partition(object):
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
logger=self.logger) logger=self.logger)
if process_handler.returncode is None or process_handler.returncode != 0: if process_handler.returncode is None or process_handler.returncode != 0:
message = 'Failed to destroy Computer Partition in %r.' % \ message = 'Failed to destroy Computer Partition in %r.' % self.instance_path
self.instance_path
self.logger.error(message) self.logger.error(message)
raise subprocess.CalledProcessError(message, process_handler.output) raise subprocess.CalledProcessError(message, process_handler.output)
def destroy(self):
"""Destroys the partition and makes it available for subsequent use."
"""
self.logger.info("Destroying Computer Partition %s..."
% self.computer_partition.getId())
# Launches "destroy" binary if exists
destroy_binary = os.path.join(self.instance_path, 'sbin', 'destroy')
if os.path.exists(destroy_binary):
self._pre_destroy(destroy_binary)
# Manually cleans what remains # Manually cleans what remains
try: try:
for f in [self.key_file, self.cert_file]: for f in [self.key_file, self.cert_file]:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment