<s> import abc <EOL> import six <EOL> from rally . common import broker <EOL> from rally . common . i18n import _ <EOL> from rally . common import logging <EOL> from rally . common import utils <EOL> from rally import consts <EOL> from rally import osclients <EOL> from rally . plugins . openstack . scenarios . nova import utils as nova_utils <EOL> from rally . plugins . openstack . scenarios . vm import vmtasks <EOL> from rally . task import context <EOL> from rally . task import types <EOL> LOG = logging . getLogger ( __name__ ) <EOL> @ six . add_metaclass ( abc . ABCMeta ) <EOL> @ context . configure ( name = "<STR_LIT>" , order = <NUM_LIT> , hidden = True ) <EOL> class BaseCustomImageGenerator ( context . Context ) : <EOL> """<STR_LIT>""" <EOL> CONFIG_SCHEMA = { <EOL> "<STR_LIT:type>" : "<STR_LIT:object>" , <EOL> "<STR_LIT>" : consts . JSON_SCHEMA , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:image>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:object>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:name>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } <EOL> } <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:object>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:name>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } <EOL> } <EOL> } , <EOL> "<STR_LIT:username>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } , <EOL> "<STR_LIT:password>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } , <EOL> "<STR_LIT:port>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT> <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> } <EOL> } , <EOL> "<STR_LIT>" : [ "<STR_LIT:image>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : False <EOL> } <EOL> DEFAULT_CONFIG = { <EOL> "<STR_LIT:username>" : "<STR_LIT:root>" , <EOL> "<STR_LIT:port>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT:1> <EOL> } <EOL> @ logging . log_task_wrapper ( LOG . info , _ ( "<STR_LIT>" ) ) <EOL> def setup ( self ) : <EOL> """<STR_LIT>""" <EOL> if "<STR_LIT>" in self . context : <EOL> user = self . context [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> tenant = self . context [ "<STR_LIT>" ] [ user [ "<STR_LIT>" ] ] <EOL> nics = None <EOL> if "<STR_LIT>" in tenant : <EOL> nics = [ { "<STR_LIT>" : tenant [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT:id>" ] } ] <EOL> custom_image = self . create_one_image ( user , nics = nics ) <EOL> self . make_image_public ( custom_image ) <EOL> for tenant in self . context [ "<STR_LIT>" ] . values ( ) : <EOL> tenant [ "<STR_LIT>" ] = custom_image <EOL> else : <EOL> def publish ( queue ) : <EOL> users = self . context . get ( "<STR_LIT>" , [ ] ) <EOL> for user , tenant_id in utils . iterate_per_tenants ( users ) : <EOL> queue . append ( ( user , tenant_id ) ) <EOL> def consume ( cache , args ) : <EOL> user , tenant_id = args <EOL> tenant = self . context [ "<STR_LIT>" ] [ tenant_id ] <EOL> tenant [ "<STR_LIT>" ] = self . create_one_image ( user ) <EOL> broker . run ( publish , consume , self . config [ "<STR_LIT>" ] ) <EOL> def create_one_image ( self , user , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> clients = osclients . Clients ( user [ "<STR_LIT>" ] ) <EOL> image_id = types . ImageResourceType . transform ( <EOL> clients = clients , resource_config = self . config [ "<STR_LIT:image>" ] ) <EOL> flavor_id = types . FlavorResourceType . transform ( <EOL> clients = clients , resource_config = self . config [ "<STR_LIT>" ] ) <EOL> vm_scenario = vmtasks . VMTasks ( self . context , clients = clients ) <EOL> server , fip = vm_scenario . _boot_server_with_fip ( <EOL> image = image_id , flavor = flavor_id , <EOL> floating_network = self . config . get ( "<STR_LIT>" ) , <EOL> userdata = self . config . get ( "<STR_LIT>" ) , <EOL> key_name = user [ "<STR_LIT>" ] [ "<STR_LIT:name>" ] , <EOL> security_groups = [ user [ "<STR_LIT>" ] [ "<STR_LIT:name>" ] ] , <EOL> ** kwargs ) <EOL> try : <EOL> LOG . debug ( "<STR_LIT>" , server , fip [ "<STR_LIT>" ] ) <EOL> self . customize_image ( server , fip , user ) <EOL> LOG . debug ( "<STR_LIT>" , server ) <EOL> vm_scenario . _stop_server ( server ) <EOL> LOG . debug ( "<STR_LIT>" , server ) <EOL> custom_image = vm_scenario . _create_image ( server ) . to_dict ( ) <EOL> finally : <EOL> vm_scenario . _delete_server_with_fip ( server , fip ) <EOL> return custom_image <EOL> def make_image_public ( self , custom_image ) : <EOL> """<STR_LIT>""" <EOL> admin_clients = osclients . Clients ( self . context [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <EOL> LOG . debug ( "<STR_LIT>" , custom_image [ "<STR_LIT:id>" ] ) <EOL> admin_clients . glance ( ) . images . get ( <EOL> custom_image [ "<STR_LIT:id>" ] ) . update ( is_public = True ) <EOL> @ logging . log_task_wrapper ( LOG . info , _ ( "<STR_LIT>" ) ) <EOL> def cleanup ( self ) : <EOL> """<STR_LIT>""" <EOL> if "<STR_LIT>" in self . context : <EOL> user = self . context [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> tenant = self . context [ "<STR_LIT>" ] [ user [ "<STR_LIT>" ] ] <EOL> if "<STR_LIT>" in tenant : <EOL> self . delete_one_image ( user , tenant [ "<STR_LIT>" ] ) <EOL> tenant . pop ( "<STR_LIT>" ) <EOL> else : <EOL> def publish ( queue ) : <EOL> users = self . context . get ( "<STR_LIT>" , [ ] ) <EOL> for user , tenant_id in utils . iterate_per_tenants ( users ) : <EOL> queue . append ( ( user , tenant_id ) ) <EOL> def consume ( cache , args ) : <EOL> user , tenant_id = args <EOL> tenant = self . context [ "<STR_LIT>" ] [ tenant_id ] <EOL> if "<STR_LIT>" in tenant : <EOL> self . delete_one_image ( user , tenant [ "<STR_LIT>" ] ) <EOL> tenant . pop ( "<STR_LIT>" ) <EOL> broker . run ( publish , consume , self . config [ "<STR_LIT>" ] ) <EOL> def delete_one_image ( self , user , custom_image ) : <EOL> """<STR_LIT>""" <EOL> clients = osclients . Clients ( user [ "<STR_LIT>" ] ) <EOL> nova_scenario = nova_utils . NovaScenario ( <EOL> context = self . context , clients = clients ) <EOL> with logging . ExceptionLogger ( <EOL> LOG , _ ( "<STR_LIT>" ) % custom_image [ "<STR_LIT:id>" ] ) : <EOL> custom_image = nova_scenario . clients ( "<STR_LIT>" ) . images . get ( <EOL> custom_image [ "<STR_LIT:id>" ] ) <EOL> nova_scenario . _delete_image ( custom_image ) <EOL> @ logging . log_task_wrapper ( LOG . info , <EOL> _ ( "<STR_LIT>" ) ) <EOL> def customize_image ( self , server , ip , user ) : <EOL> return self . _customize_image ( server , ip , user ) <EOL> @ abc . abstractmethod <EOL> def _customize_image ( self , server , ip , user ) : <EOL> """<STR_LIT>""" <EOL> pass </s>
