<s> """<STR_LIT>""" <EOL> import os <EOL> import shutil <EOL> from ironic_lib import disk_utils <EOL> from ironic_lib import utils as ironic_utils <EOL> import jinja2 <EOL> from oslo_concurrency import processutils <EOL> from oslo_config import cfg <EOL> from oslo_log import log as logging <EOL> from oslo_utils import fileutils <EOL> from ironic . common import exception <EOL> from ironic . common . glance_service import service_utils as glance_utils <EOL> from ironic . common . i18n import _ <EOL> from ironic . common . i18n import _LE <EOL> from ironic . common import image_service as service <EOL> from ironic . common import paths <EOL> from ironic . common import utils <EOL> LOG = logging . getLogger ( __name__ ) <EOL> image_opts = [ <EOL> cfg . BoolOpt ( '<STR_LIT>' , <EOL> default = True , <EOL> help = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = _ ( '<STR_LIT>' ) ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = paths . basedir_def ( '<STR_LIT>' ) , <EOL> help = _ ( '<STR_LIT>' ) ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = paths . basedir_def ( '<STR_LIT>' ) , <EOL> help = _ ( '<STR_LIT>' ) ) , <EOL> ] <EOL> CONF = cfg . CONF <EOL> CONF . register_opts ( image_opts ) <EOL> def _create_root_fs ( root_directory , files_info ) : <EOL> """<STR_LIT>""" <EOL> for src_file , path in files_info . items ( ) : <EOL> target_file = os . path . join ( root_directory , path ) <EOL> dirname = os . path . dirname ( target_file ) <EOL> if not os . path . exists ( dirname ) : <EOL> os . makedirs ( dirname ) <EOL> shutil . copyfile ( src_file , target_file ) <EOL> def _umount_without_raise ( mount_dir ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> utils . umount ( mount_dir ) <EOL> except processutils . ProcessExecutionError : <EOL> pass <EOL> def create_vfat_image ( output_file , files_info = None , parameters = None , <EOL> parameters_file = '<STR_LIT>' , fs_size_kib = <NUM_LIT:100> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ironic_utils . dd ( '<STR_LIT>' , output_file , '<STR_LIT>' , <EOL> "<STR_LIT>" % fs_size_kib ) <EOL> except processutils . ProcessExecutionError as e : <EOL> raise exception . ImageCreationFailed ( image_type = '<STR_LIT>' , error = e ) <EOL> with utils . tempdir ( ) as tmpdir : <EOL> try : <EOL> ironic_utils . mkfs ( '<STR_LIT>' , output_file , label = "<STR_LIT>" ) <EOL> utils . mount ( output_file , tmpdir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> except processutils . ProcessExecutionError as e : <EOL> raise exception . ImageCreationFailed ( image_type = '<STR_LIT>' , error = e ) <EOL> try : <EOL> if files_info : <EOL> _create_root_fs ( tmpdir , files_info ) <EOL> if parameters : <EOL> parameters_file = os . path . join ( tmpdir , parameters_file ) <EOL> params_list = [ '<STR_LIT>' % { '<STR_LIT:key>' : k , '<STR_LIT>' : v } <EOL> for k , v in parameters . items ( ) ] <EOL> file_contents = '<STR_LIT:\n>' . join ( params_list ) <EOL> utils . write_to_file ( parameters_file , file_contents ) <EOL> except Exception as e : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) , e ) <EOL> raise exception . ImageCreationFailed ( image_type = '<STR_LIT>' , error = e ) <EOL> finally : <EOL> try : <EOL> utils . umount ( tmpdir ) <EOL> except processutils . ProcessExecutionError as e : <EOL> raise exception . ImageCreationFailed ( image_type = '<STR_LIT>' , error = e ) <EOL> def _generate_cfg ( kernel_params , template , options ) : <EOL> """<STR_LIT>""" <EOL> if not kernel_params : <EOL> kernel_params = [ ] <EOL> kernel_params_str = '<STR_LIT:U+0020>' . join ( kernel_params ) <EOL> tmpl_path , tmpl_file = os . path . split ( template ) <EOL> env = jinja2 . Environment ( loader = jinja2 . FileSystemLoader ( tmpl_path ) ) <EOL> template = env . get_template ( tmpl_file ) <EOL> options . update ( { '<STR_LIT>' : kernel_params_str } ) <EOL> cfg = template . render ( options ) <EOL> return cfg <EOL> def create_isolinux_image_for_bios ( output_file , kernel , ramdisk , <EOL> kernel_params = None ) : <EOL> """<STR_LIT>""" <EOL> ISOLINUX_BIN = '<STR_LIT>' <EOL> ISOLINUX_CFG = '<STR_LIT>' <EOL> options = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> with utils . tempdir ( ) as tmpdir : <EOL> files_info = { <EOL> kernel : '<STR_LIT>' , <EOL> ramdisk : '<STR_LIT>' , <EOL> CONF . isolinux_bin : ISOLINUX_BIN , <EOL> } <EOL> try : <EOL> _create_root_fs ( tmpdir , files_info ) <EOL> except ( OSError , IOError ) as e : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) ) <EOL> raise exception . ImageCreationFailed ( image_type = '<STR_LIT>' , error = e ) <EOL> cfg = _generate_cfg ( kernel_params , <EOL> CONF . isolinux_config_template , options ) <EOL> isolinux_cfg = os . path . join ( tmpdir , ISOLINUX_CFG ) <EOL> utils . write_to_file ( isolinux_cfg , cfg ) <EOL> try : <EOL> utils . execute ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , "<STR_LIT>" , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:4>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , ISOLINUX_BIN , '<STR_LIT>' , output_file , tmpdir ) <EOL> except processutils . ProcessExecutionError as e : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) ) <EOL> raise exception . ImageCreationFailed ( image_type = '<STR_LIT>' , error = e ) <EOL> def create_isolinux_image_for_uefi ( output_file , deploy_iso , kernel , ramdisk , <EOL> kernel_params = None ) : <EOL> """<STR_LIT>""" <EOL> ISOLINUX_BIN = '<STR_LIT>' <EOL> ISOLINUX_CFG = '<STR_LIT>' <EOL> isolinux_options = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> grub_options = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> with utils . tempdir ( ) as tmpdir : <EOL> files_info = { <EOL> kernel : '<STR_LIT>' , <EOL> ramdisk : '<STR_LIT>' , <EOL> CONF . isolinux_bin : ISOLINUX_BIN , <EOL> } <EOL> with utils . tempdir ( ) as mountdir : <EOL> uefi_path_info , e_img_rel_path , grub_rel_path = ( <EOL> _mount_deploy_iso ( deploy_iso , mountdir ) ) <EOL> files_info . update ( uefi_path_info ) <EOL> try : <EOL> _create_root_fs ( tmpdir , files_info ) <EOL> except ( OSError , IOError ) as e : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) ) <EOL> raise exception . ImageCreationFailed ( image_type = '<STR_LIT>' , error = e ) <EOL> finally : <EOL> _umount_without_raise ( mountdir ) <EOL> cfg = _generate_cfg ( kernel_params , <EOL> CONF . isolinux_config_template , isolinux_options ) <EOL> isolinux_cfg = os . path . join ( tmpdir , ISOLINUX_CFG ) <EOL> utils . write_to_file ( isolinux_cfg , cfg ) <EOL> grub_cfg = os . path . join ( tmpdir , grub_rel_path ) <EOL> grub_conf = _generate_cfg ( kernel_params , <EOL> CONF . grub_config_template , grub_options ) <EOL> utils . write_to_file ( grub_cfg , grub_conf ) <EOL> try : <EOL> utils . execute ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , "<STR_LIT>" , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:4>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , ISOLINUX_BIN , '<STR_LIT>' , <EOL> '<STR_LIT>' , e_img_rel_path , '<STR_LIT>' , <EOL> '<STR_LIT>' , output_file , tmpdir ) <EOL> except processutils . ProcessExecutionError as e : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) ) <EOL> raise exception . ImageCreationFailed ( image_type = '<STR_LIT>' , error = e ) <EOL> def fetch ( context , image_href , path , force_raw = False ) : <EOL> image_service = service . get_image_service ( image_href , <EOL> context = context ) <EOL> LOG . debug ( "<STR_LIT>" % <EOL> { '<STR_LIT>' : image_service . __class__ , <EOL> '<STR_LIT>' : image_href } ) <EOL> with fileutils . remove_path_on_error ( path ) : <EOL> with open ( path , "<STR_LIT:wb>" ) as image_file : <EOL> image_service . download ( image_href , image_file ) <EOL> if force_raw : <EOL> image_to_raw ( image_href , path , "<STR_LIT>" % path ) <EOL> def image_to_raw ( image_href , path , path_tmp ) : <EOL> with fileutils . remove_path_on_error ( path_tmp ) : <EOL> data = disk_utils . qemu_img_info ( path_tmp ) <EOL> fmt = data . file_format <EOL> if fmt is None : <EOL> raise exception . ImageUnacceptable ( <EOL> reason = _ ( "<STR_LIT>" ) , <EOL> image_id = image_href ) <EOL> backing_file = data . backing_file <EOL> if backing_file is not None : <EOL> raise exception . ImageUnacceptable ( <EOL> image_id = image_href , <EOL> reason = _ ( "<STR_LIT>" ) % <EOL> { '<STR_LIT>' : fmt , '<STR_LIT>' : backing_file } ) <EOL> if fmt != "<STR_LIT>" : <EOL> staged = "<STR_LIT>" % path <EOL> LOG . debug ( "<STR_LIT>" % <EOL> { '<STR_LIT:image>' : image_href , '<STR_LIT>' : fmt } ) <EOL> with fileutils . remove_path_on_error ( staged ) : <EOL> disk_utils . convert_image ( path_tmp , staged , '<STR_LIT>' ) <EOL> os . unlink ( path_tmp ) <EOL> data = disk_utils . qemu_img_info ( staged ) <EOL> if data . file_format != "<STR_LIT>" : <EOL> raise exception . ImageConvertFailed ( <EOL> image_id = image_href , <EOL> reason = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % data . file_format ) <EOL> os . rename ( staged , path ) <EOL> else : <EOL> os . rename ( path_tmp , path ) <EOL> def image_show ( context , image_href , image_service = None ) : <EOL> if image_service is None : <EOL> image_service = service . get_image_service ( image_href , context = context ) <EOL> return image_service . show ( image_href ) <EOL> def download_size ( context , image_href , image_service = None ) : <EOL> return image_show ( context , image_href , image_service ) [ '<STR_LIT:size>' ] <EOL> def converted_size ( path ) : <EOL> """<STR_LIT>""" <EOL> data = disk_utils . qemu_img_info ( path ) <EOL> return data . virtual_size <EOL> def get_image_properties ( context , image_href , properties = "<STR_LIT:all>" ) : <EOL> """<STR_LIT>""" <EOL> img_service = service . get_image_service ( image_href , context = context ) <EOL> iproperties = img_service . show ( image_href ) [ '<STR_LIT>' ] <EOL> if properties == "<STR_LIT:all>" : <EOL> return iproperties <EOL> return { p : iproperties . get ( p ) for p in properties } <EOL> def get_temp_url_for_glance_image ( context , image_uuid ) : <EOL> """<STR_LIT>""" <EOL> glance_service = service . GlanceImageService ( version = <NUM_LIT:2> , context = context ) <EOL> image_properties = glance_service . show ( image_uuid ) <EOL> LOG . debug ( '<STR_LIT>' , <EOL> { '<STR_LIT:info>' : image_properties , '<STR_LIT>' : image_uuid } ) <EOL> return glance_service . swift_temp_url ( image_properties ) <EOL> def create_boot_iso ( context , output_filename , kernel_href , <EOL> ramdisk_href , deploy_iso_href , root_uuid = None , <EOL> kernel_params = None , boot_mode = None ) : <EOL> """<STR_LIT>""" <EOL> with utils . tempdir ( ) as tmpdir : <EOL> kernel_path = os . path . join ( tmpdir , kernel_href . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] ) <EOL> ramdisk_path = os . path . join ( tmpdir , ramdisk_href . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] ) <EOL> fetch ( context , kernel_href , kernel_path ) <EOL> fetch ( context , ramdisk_href , ramdisk_path ) <EOL> params = [ ] <EOL> if root_uuid : <EOL> params . append ( '<STR_LIT>' % root_uuid ) <EOL> if kernel_params : <EOL> params . append ( kernel_params ) <EOL> if boot_mode == '<STR_LIT>' : <EOL> deploy_iso = os . path . join ( tmpdir , deploy_iso_href . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] ) <EOL> fetch ( context , deploy_iso_href , deploy_iso ) <EOL> create_isolinux_image_for_uefi ( output_filename , <EOL> deploy_iso , <EOL> kernel_path , <EOL> ramdisk_path , <EOL> params ) <EOL> else : <EOL> create_isolinux_image_for_bios ( output_filename , <EOL> kernel_path , <EOL> ramdisk_path , <EOL> params ) <EOL> def is_whole_disk_image ( ctx , instance_info ) : <EOL> """<STR_LIT>""" <EOL> image_source = instance_info . get ( '<STR_LIT>' ) <EOL> if not image_source : <EOL> return <EOL> is_whole_disk_image = False <EOL> if glance_utils . is_glance_image ( image_source ) : <EOL> try : <EOL> iproperties = get_image_properties ( ctx , image_source ) <EOL> except Exception : <EOL> return <EOL> is_whole_disk_image = ( not iproperties . get ( '<STR_LIT>' ) and <EOL> not iproperties . get ( '<STR_LIT>' ) ) <EOL> else : <EOL> if ( not instance_info . get ( '<STR_LIT>' ) and <EOL> not instance_info . get ( '<STR_LIT>' ) ) : <EOL> is_whole_disk_image = True <EOL> return is_whole_disk_image <EOL> def _mount_deploy_iso ( deploy_iso , mountdir ) : <EOL> """<STR_LIT>""" <EOL> e_img_rel_path = None <EOL> e_img_path = None <EOL> grub_rel_path = None <EOL> grub_path = None <EOL> try : <EOL> utils . mount ( deploy_iso , mountdir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> except processutils . ProcessExecutionError as e : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) ) <EOL> raise exception . ImageCreationFailed ( image_type = '<STR_LIT>' , error = e ) <EOL> try : <EOL> for ( dir , subdir , files ) in os . walk ( mountdir ) : <EOL> if '<STR_LIT>' in files : <EOL> e_img_path = os . path . join ( dir , '<STR_LIT>' ) <EOL> e_img_rel_path = os . path . relpath ( e_img_path , <EOL> mountdir ) <EOL> if '<STR_LIT>' in files : <EOL> grub_path = os . path . join ( dir , '<STR_LIT>' ) <EOL> grub_rel_path = os . path . relpath ( grub_path , <EOL> mountdir ) <EOL> except ( OSError , IOError ) as e : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) ) <EOL> _umount_without_raise ( mountdir ) <EOL> raise exception . ImageCreationFailed ( image_type = '<STR_LIT>' , error = e ) <EOL> if not ( e_img_path and e_img_rel_path and grub_path and grub_rel_path ) : <EOL> error = ( _ ( "<STR_LIT>" ) ) <EOL> _umount_without_raise ( mountdir ) <EOL> raise exception . ImageCreationFailed ( image_type = '<STR_LIT>' , error = error ) <EOL> uefi_path_info = { e_img_path : e_img_rel_path , <EOL> grub_path : grub_rel_path } <EOL> return uefi_path_info , e_img_rel_path , grub_rel_path </s>
