<s> from rally . common . i18n import _ <EOL> from rally . common import logging <EOL> from rally . common import utils as rutils <EOL> from rally import consts <EOL> from rally import exceptions <EOL> from rally import osclients <EOL> from rally . plugins . openstack . cleanup import manager as resource_manager <EOL> from rally . plugins . openstack . scenarios . glance import utils as glance_utils <EOL> from rally . plugins . openstack . scenarios . sahara import utils <EOL> from rally . task import context <EOL> LOG = logging . getLogger ( __name__ ) <EOL> @ context . configure ( name = "<STR_LIT>" , order = <NUM_LIT> ) <EOL> class SaharaImage ( 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>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" , <EOL> } , <EOL> "<STR_LIT:username>" : { <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> } , <EOL> "<STR_LIT>" : [ <EOL> { "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT:username>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] } , <EOL> { "<STR_LIT>" : [ "<STR_LIT>" ] } <EOL> ] , <EOL> "<STR_LIT>" : False <EOL> } <EOL> def _create_image ( self , hadoop_version , image_url , plugin_name , user , <EOL> user_name ) : <EOL> scenario = glance_utils . GlanceScenario ( <EOL> { "<STR_LIT:user>" : user , "<STR_LIT>" : self . context [ "<STR_LIT>" ] } ) <EOL> image_name = self . generate_random_name ( ) <EOL> image = scenario . _create_image ( name = image_name , <EOL> container_format = "<STR_LIT>" , <EOL> image_location = image_url , <EOL> disk_format = "<STR_LIT>" ) <EOL> scenario . clients ( "<STR_LIT>" ) . images . update_image ( <EOL> image_id = image . id , user_name = user_name , desc = "<STR_LIT>" ) <EOL> scenario . clients ( "<STR_LIT>" ) . images . update_tags ( <EOL> image_id = image . id , new_tags = [ plugin_name , hadoop_version ] ) <EOL> return image . id <EOL> @ logging . log_task_wrapper ( LOG . info , _ ( "<STR_LIT>" ) ) <EOL> def setup ( self ) : <EOL> utils . init_sahara_context ( self ) <EOL> self . context [ "<STR_LIT>" ] [ "<STR_LIT>" ] = { } <EOL> image_uuid = self . config . get ( "<STR_LIT>" ) <EOL> self . context [ "<STR_LIT>" ] [ "<STR_LIT>" ] = not image_uuid <EOL> if image_uuid : <EOL> user = self . context [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> clients = osclients . Clients ( user [ "<STR_LIT>" ] ) <EOL> image = clients . glance ( ) . images . get ( image_uuid ) <EOL> if not image . is_public : <EOL> raise exceptions . BenchmarkSetupFailure ( <EOL> "<STR_LIT>" ) <EOL> image_id = image_uuid <EOL> for user , tenant_id in rutils . iterate_per_tenants ( <EOL> self . context [ "<STR_LIT>" ] ) : <EOL> self . context [ "<STR_LIT>" ] [ tenant_id ] [ "<STR_LIT>" ] [ "<STR_LIT:image>" ] = ( <EOL> image_id ) <EOL> else : <EOL> for user , tenant_id in rutils . iterate_per_tenants ( <EOL> self . context [ "<STR_LIT>" ] ) : <EOL> image_id = self . _create_image ( <EOL> hadoop_version = self . config [ "<STR_LIT>" ] , <EOL> image_url = self . config [ "<STR_LIT>" ] , <EOL> plugin_name = self . config [ "<STR_LIT>" ] , <EOL> user = user , <EOL> user_name = self . config [ "<STR_LIT:username>" ] ) <EOL> self . context [ "<STR_LIT>" ] [ tenant_id ] [ "<STR_LIT>" ] [ "<STR_LIT:image>" ] = ( <EOL> image_id ) <EOL> @ logging . log_task_wrapper ( LOG . info , _ ( "<STR_LIT>" ) ) <EOL> def cleanup ( self ) : <EOL> if self . context [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> resource_manager . cleanup ( names = [ "<STR_LIT>" ] , <EOL> users = self . context . get ( "<STR_LIT>" , [ ] ) ) </s>
