def _check_import(isamAppliance, id, filename, check_mode=False): """ Checks if file on the Appliance exists and if so, whether it is different from filename """ tmpdir = get_random_temp_dir() tmp_original_file = os.path.join(tmpdir, os.path.basename(id)) if _check(isamAppliance, id): export_file(isamAppliance, id, tmp_original_file, check_mode=False, force=True) logger.debug("file already exists on appliance") if files_same(tmp_original_file, filename): logger.debug("files are the same, so we don't want to do anything") shutil.rmtree(tmpdir) return False else: logger.debug( "files are different, so we delete existing file in preparation for import" ) delete(isamAppliance, id, check_mode=check_mode, force=True) shutil.rmtree(tmpdir) return True else: logger.debug( "file does not exist on appliance, so we'll want to import") shutil.rmtree(tmpdir) return True
def _check_import(isamAppliance, instance_id, filename): """ Checks if runtime template zip from server and client differ :param isamAppliance: :param filename: :return: """ if not instance._check(isamAppliance, instance_id): logger.info( "instance {} does not exist on this server. Skip import".format( instance_id)) return False tempdir = get_random_temp_dir() tempfilename = "management_root.zip" tempfile = os.path.join(tempdir, tempfilename) export_zip(isamAppliance, instance_id, tempfile) identical = files_same_zip_content(filename, tempfile) shutil.rmtree(tempdir) if identical: logger.info( "management_root files {} are identical with the server content. No update necessary." .format(filename)) return False else: logger.info( "management_root files {} differ from the server content. Updating management_root files necessary." .format(filename)) return True
def _check_import(isamAppliance, filename): """ Checks if file on the Appliance exists and if so, whether it is different from filename """ warnings = [] (d, f) = os.path.split(filename) # this means the name of the bundle has to match name of file to import ret_obj = get(isamAppliance, f) tmpdir = get_random_temp_dir() if ret_obj['data'] != {} and ret_obj['data']['extensions'] != []: tmp_original_file = os.path.join(tmpdir, ret_obj['data']['filename']) export_bundle(isamAppliance, ret_obj['data']['filename'], tmp_original_file, check_mode=False, force=True) logger.debug("file already exists on appliance") if files_same(tmp_original_file, filename): logger.debug("files are the same, so we don't want to do anything") shutil.rmtree(tmpdir) return warnings, False else: logger.debug("files are different, so we replace existing file") bundle_id = ret_obj['data']['id'] shutil.rmtree(tmpdir) return warnings, bundle_id elif ret_obj['data'] != {} and ret_obj['data']['extensions'] == []: bundle_id = ret_obj['data']['id'] return warnings, bundle_id else: warnings.append("Bundle does not exist on appliance, create a bundle first and then import.") return warnings, False
def _check_import(isamAppliance, filename): """ Checks if runtime template zip from server and client differ :param isamAppliance: :param filename: :return: """ tempdir = get_random_temp_dir() tempfilename = "template_files.zip" tempfile = os.path.join(tempdir, tempfilename) export_file(isamAppliance, tempfile) if os.path.exists(tempfile): identical = files_same_zip_content(filename, tempfile) shutil.rmtree(tempdir) if identical: logger.info( "runtime template files {} are identical with the server content. No update necessary." .format(filename)) return False else: logger.info( "runtime template files {} differ from the server content. Updating runtime template files necessary." .format(filename)) return True else: logger.info("missing zip file from server. Comparison skipped.") return False
def _check_import(isamAppliance, id, filepath): """ Checks if the file to be imported is the same as the file that's already on the instance """ tmpdir = get_random_temp_dir() tmp_original_file = os.path.join(tmpdir, os.path.basename("tempfile.txt")) export_file(isamAppliance, instance_id=id, filepath=tmp_original_file, check_mode=False, force=True) if files_same(tmp_original_file, filepath): logger.debug("files are the same, so we don't want to do anything") shutil.rmtree(tmpdir) return False else: logger.debug("files are different, so we return True to indicate the new file should be imported") shutil.rmtree(tmpdir) return True
def import_db(isamAppliance, kdb, stash, zip=None, check_mode=False, force=False): """ Import certificate database """ # Grab the filename to use as identifier (strip path and extension) import os.path tmpdir = None if zip != None: with zipfile.ZipFile(zip, "r") as zip_ref: tmpdir = get_random_temp_dir() zip_ref.extractall(tmpdir) kdb = tmpdir + '/' + kdb stash = tmpdir + '/' + stash kdb_id = os.path.basename(kdb) kdb_id = os.path.splitext(kdb_id)[0] try: if force is True or _check(isamAppliance, kdb_id) is False: if check_mode is True: return isamAppliance.create_return_object(changed=True) else: return isamAppliance.invoke_post_files( "Import certificate database", "/isam/ssl_certificates", [{ 'file_formfield': 'kdb', 'filename': kdb, 'mimetype': 'application/octet-stream' }, { 'file_formfield': 'stash', 'filename': stash, 'mimetype': 'application/octet-stream' }], {}) finally: if tmpdir != None: shutil.rmtree(tmpdir) return isamAppliance.create_return_object()
def import_zip(isamAppliance, instance_id, filename, delete_missing=False, check_mode=False, force=False): """ Importing the contents of a .zip file to the administration pages root Feature delete_missing will compare import zip with server content and delete missing files in the import zip from server """ warnings = [] if force is True or _check_import(isamAppliance, instance_id, filename): if delete_missing is True: tempdir = get_random_temp_dir() tempfilename = "management_root.zip" tempfile = os.path.join(tempdir, tempfilename) export_zip(isamAppliance, instance_id, tempfile) zServerFile = zipfile.ZipFile(tempfile) zClientFile = zipfile.ZipFile(filename) files_on_server = [] for info in zServerFile.infolist(): files_on_server.append(info.filename) files_on_client = [] for info in zClientFile.infolist(): files_on_client.append(info.filename) missing_client_files = [ x for x in files_on_server if x not in files_on_client ] if missing_client_files != []: logger.info( "list all missing files in {}, which will be deleted on the server: {}." .format(filename, missing_client_files)) for x in missing_client_files: if x.endswith('/'): search_dir = os.path.dirname(x[:-1]) + '/' if search_dir not in missing_client_files: logger.debug( "delete directory on the server: {0}.".format(x)) directory.delete(isamAppliance, instance_id, x, check_mode=check_mode) else: search_dir = os.path.dirname(x) + '/' if search_dir not in missing_client_files: logger.debug( "delete file on the server: {0}.".format(x)) file.delete(isamAppliance, instance_id, x, check_mode=check_mode) shutil.rmtree(tempdir) if check_mode is True: return isamAppliance.create_return_object(changed=True) else: return isamAppliance.invoke_post_files( "Importing the contents of a .zip file to the administration pages root", "/wga/reverseproxy/{0}/management_root".format(instance_id), [{ 'file_formfield': 'file', 'filename': filename, 'mimetype': 'application/octet-stream' }], {"force": force}, json_response=False) return isamAppliance.create_return_object(warnings=warnings)
def import_file(isamAppliance, filename, delete_missing=False, check_mode=False, force=False): """ Import all Runtime Template Files """ warnings = [] if force is True or _check_import(isamAppliance, filename): if delete_missing is True: tempdir = get_random_temp_dir() tempfilename = "template_files.zip" tempfile = os.path.join(tempdir, tempfilename) export_file(isamAppliance, tempfile) zServerFile = zipfile.ZipFile(tempfile) zClientFile = zipfile.ZipFile(filename) files_on_server = [] for info in zServerFile.infolist(): files_on_server.append(info.filename) files_on_client = [] for info in zClientFile.infolist(): files_on_client.append(info.filename) missing_client_files = [ x for x in files_on_server if x not in files_on_client ] if missing_client_files != []: logger.info( "list all missing files in {}, which will be deleted on the server: {}." .format(filename, missing_client_files)) for x in missing_client_files: if x.endswith('/'): search_dir = os.path.dirname(x[:-1]) + '/' if search_dir not in missing_client_files: logger.debug( "delete directory on the server: {0}.".format(x)) delete(isamAppliance, x, "directory", check_mode=check_mode) else: search_dir = os.path.dirname(x) + '/' if search_dir not in missing_client_files: logger.debug( "delete file on the server: {0}.".format(x)) delete(isamAppliance, x, "file", check_mode=check_mode) shutil.rmtree(tempdir) if check_mode is True: return isamAppliance.create_return_object(changed=True) else: return isamAppliance.invoke_post_files( "Replace all Runtime Template Files", uri, [{ 'file_formfield': 'file', 'filename': filename, 'mimetype': 'application/octet-stream' }], {"force": force}, json_response=False) return isamAppliance.create_return_object(warnings=warnings)