<s> import copy <EOL> from cloudferry . lib . base . action import action <EOL> from cloudferry . lib . base import exception <EOL> from cloudferry . lib . utils import log <EOL> from cloudferry . lib . utils import utils as utl <EOL> LOG = log . getLogger ( __name__ ) <EOL> def get_boot_volume ( instance ) : <EOL> return instance [ utl . INSTANCE_BODY ] [ '<STR_LIT>' ] <EOL> def get_image_id_from_volume ( volume , storage ) : <EOL> volumes = storage . read_info ( id = volume [ '<STR_LIT:id>' ] ) [ utl . VOLUMES_TYPE ] <EOL> volume_details = volumes [ volume [ '<STR_LIT:id>' ] ] [ utl . VOLUME_BODY ] <EOL> return volume_details [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> class ConvertComputeToImage ( action . Action ) : <EOL> def __init__ ( self , init , cloud = None , target_output = '<STR_LIT>' ) : <EOL> super ( ConvertComputeToImage , self ) . __init__ ( init , cloud ) <EOL> self . target_output = target_output <EOL> def run ( self , info = None , ** kwargs ) : <EOL> info = copy . deepcopy ( info ) <EOL> image_info = { utl . IMAGES_TYPE : { } } <EOL> images_body = image_info [ utl . IMAGES_TYPE ] <EOL> image_resource = self . cloud . resources [ utl . IMAGE_RESOURCE ] <EOL> dst_image_resource = self . dst_cloud . resources [ utl . IMAGE_RESOURCE ] <EOL> storage_resource = self . cloud . resources [ utl . STORAGE_RESOURCE ] <EOL> compute_ignored_images = { } <EOL> missing_images = { } <EOL> for instance_id , instance in info [ utl . INSTANCES_TYPE ] . iteritems ( ) : <EOL> _instance = instance [ utl . INSTANCE_BODY ] <EOL> image_id = None <EOL> if _instance [ '<STR_LIT>' ] == utl . BOOT_FROM_VOLUME : <EOL> if _instance [ '<STR_LIT>' ] : <EOL> volume = get_boot_volume ( instance ) <EOL> image_id = get_image_id_from_volume ( volume , <EOL> storage_resource ) <EOL> else : <EOL> image_id = _instance [ '<STR_LIT>' ] <EOL> if image_id and not dst_image_resource . image_exists ( image_id ) : <EOL> if image_id in images_body : <EOL> images_body [ image_id ] [ utl . META_INFO ] [ <EOL> utl . INSTANCE_BODY ] . append ( instance ) <EOL> else : <EOL> images_body [ image_id ] = { utl . IMAGE_BODY : { } , <EOL> utl . META_INFO : { <EOL> utl . INSTANCE_BODY : [ instance ] } } <EOL> try : <EOL> image_resource . get_ref_image ( image_id ) <EOL> except exception . ImageDownloadError : <EOL> missing_images [ instance_id ] = image_id <EOL> continue <EOL> img = image_resource . get_image_by_id_converted ( image_id ) <EOL> img = img [ utl . IMAGES_TYPE ] <EOL> if img : <EOL> images_body . update ( img ) <EOL> images_body [ image_id ] [ utl . META_INFO ] [ <EOL> utl . INSTANCE_BODY ] = [ instance ] <EOL> else : <EOL> LOG . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> missing_images [ instance_id ] = image_id <EOL> else : <EOL> compute_ignored_images [ instance_id ] = instance <EOL> if missing_images : <EOL> LOG . warning ( '<STR_LIT>' , missing_images . values ( ) ) <EOL> return { <EOL> self . target_output : image_info , <EOL> '<STR_LIT>' : compute_ignored_images , <EOL> '<STR_LIT>' : missing_images <EOL> } </s>
