def _download(self, task_url): task_rpm_name = os.path.split(task_url)[1] rpm_file = os.path.join(self.task_dir, task_rpm_name) if not os.path.exists(rpm_file): try: with atomically_replaced_file(rpm_file) as f: siphon(urllib2.urlopen(task_url), f) f.flush() except urllib2.HTTPError as err: self.logger.critical(err) #unlink_ignore(rpm_file) except Exception as err: self.logger.critical(err) unlink_ignore(rpm_file) else: self.logger.debug('Downloaded %s' % task_rpm_name) self.tasks_added.append(task_rpm_name) self.t_downloaded = self.t_downloaded + 1 else: self.logger.debug('Already downloaded %s' % task_rpm_name) self.tasks_added.append(task_rpm_name) self.t_downloaded = self.t_downloaded + 1 return
def _get_images(tftp_root, distro_tree_id, url, images): dest_dir = os.path.join(tftp_root, 'distrotrees', str(distro_tree_id)) makedirs_ignore(dest_dir, mode=0755) for image_type, path in images: if image_type in ('kernel', 'initrd'): dest_path = os.path.join(dest_dir, image_type) if os.path.isfile(dest_path): print 'Skipping existing %s for distro tree %s' % (image_type, distro_tree_id) else: image_url = urlparse.urljoin(url, path) print 'Fetching %s %s for distro tree %s' % (image_type, image_url, distro_tree_id) with atomically_replaced_file(dest_path) as dest: siphon(urllib2.urlopen(image_url), dest)
def _get_images(tftp_root, distro_tree_id, url, images): dest_dir = os.path.join(tftp_root, 'distrotrees', str(distro_tree_id)) makedirs_ignore(dest_dir, mode=0o755) for image_type, path in images: if image_type in ('kernel', 'initrd'): dest_path = os.path.join(dest_dir, image_type) if os.path.isfile(dest_path): print('Skipping existing %s for distro tree %s' % (image_type, distro_tree_id)) else: image_url = urlparse.urljoin(url, path) print('Fetching %s %s for distro tree %s' % (image_type, image_url, distro_tree_id)) with atomically_replaced_file(dest_path) as dest: siphon(urllib2.urlopen(image_url), dest)
def copy_ignore(path, source_file): """ Creates and populates a file by copying from a source file object. The destination file will remain untouched if it already exists. """ try: f = open(path, 'wx') # not sure this is portable to Python 3! except IOError as e: if e.errno == errno.EEXIST: return else: raise try: logger.debug("%s didn't exist, writing it", path) siphon(source_file, f) finally: f.close()
def fetch_images(distro_tree_id, kernel_url, initrd_url, fqdn): """ Creates references to kernel and initrd files at: <get_tftp_root()>/images/<fqdn>/kernel <get_tftp_root()>/images/<fqdn>/initrd """ images_dir = os.path.join(get_tftp_root(), 'images', fqdn) makedirs_ignore(images_dir, 0o755) # Only look for fetched images if distro_tree is registered if distro_tree_id is not None: distrotree_dir = os.path.join(get_tftp_root(), 'distrotrees', str(distro_tree_id)) # beaker-pxemenu might have already fetched the images, so let's try there # before anywhere else. try: atomic_link(os.path.join(distrotree_dir, 'kernel'), os.path.join(images_dir, 'kernel')) atomic_link(os.path.join(distrotree_dir, 'initrd'), os.path.join(images_dir, 'initrd')) logger.debug('Using images from distro tree %s for %s', distro_tree_id, fqdn) return except OSError as e: if e.errno != errno.ENOENT: raise # No luck there, so try something else... timeout = get_conf().get('IMAGE_FETCH_TIMEOUT') logger.debug('Fetching kernel %s for %s', kernel_url, fqdn) with atomically_replaced_file(os.path.join(images_dir, 'kernel')) as dest: try: siphon(urllib2.urlopen(kernel_url, timeout=timeout), dest) except Exception as e: raise ImageFetchingError(kernel_url, distro_tree_id, e) logger.debug('Fetching initrd %s for %s', initrd_url, fqdn) with atomically_replaced_file(os.path.join(images_dir, 'initrd')) as dest: try: siphon(urllib2.urlopen(initrd_url, timeout=timeout), dest) except Exception as e: raise ImageFetchingError(initrd_url, distro_tree_id, e)
def _write_data_from_url(f): siphon(urllib2.urlopen(task_url), f) f.flush()
# before anywhere else. try: atomic_link(os.path.join(distrotree_dir, 'kernel'), os.path.join(images_dir, 'kernel')) atomic_link(os.path.join(distrotree_dir, 'initrd'), os.path.join(images_dir, 'initrd')) logger.debug('Using images from distro tree %s for %s', distro_tree_id, fqdn) return except OSError, e: if e.errno != errno.ENOENT: raise # No luck there, so try something else... logger.debug('Fetching kernel %s for %s', kernel_url, fqdn) with atomically_replaced_file(os.path.join(images_dir, 'kernel')) as dest: siphon(urllib2.urlopen(kernel_url), dest) logger.debug('Fetching initrd %s for %s', initrd_url, fqdn) with atomically_replaced_file(os.path.join(images_dir, 'initrd')) as dest: siphon(urllib2.urlopen(initrd_url), dest) def have_images(fqdn): return os.path.exists(os.path.join(get_tftp_root(), 'images', fqdn)) def clear_images(fqdn): """Removes kernel and initrd images """ images_dir = os.path.join(get_tftp_root(), 'images', fqdn) logger.debug('Removing images for %s', fqdn) shutil.rmtree(images_dir, ignore_errors=True) def pxe_basename(fqdn): # pxelinux uses upper-case hex IP address for config filename
def write_data(f): siphon(task_rpm.file, f)
def copy_ignore(path, source_file): """ Creates and populates a file by copying from a source file object. The destination file will remain untouched if it already exists. """ try: f = open(path, 'wx') # not sure this is portable to Python 3! except IOError, e: if e.errno == errno.EEXIST: return else: raise try: logger.debug("%s didn't exist, writing it", path) siphon(source_file, f) finally: f.close() def write_ignore(path, content): """ Creates and populates a file with the given string content. The destination file will remain untouched if it already exists. """ copy_ignore(path, StringIO(content)) def copy_path_ignore(dest_path, source_path): """ Creates and populates a file by copying from a source file. The destination file will remain untouched if it already exists. Nothing will be copied if the source file does not exist.
def copy_ignore(path, source_file): """ Creates and populates a file by copying from a source file object. The destination file will remain untouched if it already exists. """ try: f = open(path, 'wx') # not sure this is portable to Python 3! except IOError, e: if e.errno == errno.EEXIST: return else: raise try: logger.debug("%s didn't exist, writing it", path) siphon(source_file, f) finally: f.close() def write_ignore(path, content): """ Creates and populates a file with the given string content. The destination file will remain untouched if it already exists. """ copy_ignore(path, StringIO(content)) def copy_path_ignore(dest_path, source_path): """ Creates and populates a file by copying from a source file.