<s> """<STR_LIT>""" <EOL> from nova import compute <EOL> from nova import utils <EOL> from nova . compute import task_states <EOL> from nova . compute import vm_states <EOL> from nova . compute import flavors <EOL> from nova . openstack . common import log <EOL> from occi import exceptions <EOL> from occi . extensions import infrastructure <EOL> from occi_os_api . extensions import os_mixins <EOL> from occi_os_api . extensions import os_addon <EOL> COMPUTE_API = compute . API ( ) <EOL> LOG = log . getLogger ( __name__ ) <EOL> def create_vm ( entity , context ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in entity . attributes : <EOL> name = entity . attributes [ '<STR_LIT>' ] <EOL> else : <EOL> name = None <EOL> key_name = key_data = None <EOL> password = utils . generate_password ( ) <EOL> access_ip_v4 = None <EOL> access_ip_v6 = None <EOL> user_data = None <EOL> metadata = { } <EOL> injected_files = [ ] <EOL> min_count = max_count = <NUM_LIT:1> <EOL> requested_networks = None <EOL> sg_names = [ ] <EOL> availability_zone = None <EOL> config_drive = None <EOL> block_device_mapping = None <EOL> kernel_id = ramdisk_id = None <EOL> auto_disk_config = None <EOL> scheduler_hints = None <EOL> resource_template = None <EOL> os_template = None <EOL> for mixin in entity . mixins : <EOL> if isinstance ( mixin , os_mixins . ResourceTemplate ) : <EOL> resource_template = mixin <EOL> elif isinstance ( mixin , os_mixins . OsTemplate ) : <EOL> os_template = mixin <EOL> elif mixin == os_addon . OS_KEY_PAIR_EXT : <EOL> attr = '<STR_LIT>' <EOL> key_name = entity . attributes [ attr ] <EOL> attr = '<STR_LIT>' <EOL> key_data = entity . attributes [ attr ] <EOL> elif mixin == os_addon . OS_USER_DATA_EXT : <EOL> attr = '<STR_LIT>' <EOL> user_data = entity . attributes [ attr ] <EOL> if os_addon . SEC_GROUP in mixin . related : <EOL> secgroup = COMPUTE_API . security_group_api . get ( context , <EOL> name = mixin . term ) <EOL> sg_names . append ( secgroup [ "<STR_LIT:name>" ] ) <EOL> if not os_template : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> if resource_template : <EOL> inst_type = flavors . get_flavor_by_flavor_id ( resource_template . res_id ) <EOL> else : <EOL> inst_type = None <EOL> try : <EOL> ( instances , _reservation_id ) = COMPUTE_API . create ( <EOL> context = context , <EOL> instance_type = inst_type , <EOL> image_href = os_template . os_id , <EOL> kernel_id = kernel_id , <EOL> ramdisk_id = ramdisk_id , <EOL> min_count = min_count , <EOL> max_count = max_count , <EOL> display_name = name , <EOL> display_description = name , <EOL> key_name = key_name , <EOL> key_data = key_data , <EOL> security_group = sg_names , <EOL> availability_zone = availability_zone , <EOL> user_data = user_data , <EOL> metadata = metadata , <EOL> injected_files = injected_files , <EOL> admin_password = password , <EOL> block_device_mapping = block_device_mapping , <EOL> access_ip_v4 = access_ip_v4 , <EOL> access_ip_v6 = access_ip_v6 , <EOL> requested_networks = requested_networks , <EOL> config_drive = config_drive , <EOL> auto_disk_config = auto_disk_config , <EOL> scheduler_hints = scheduler_hints ) <EOL> except Exception as e : <EOL> raise AttributeError ( e . message ) <EOL> return instances [ <NUM_LIT:0> ] <EOL> def rebuild_vm ( uid , image_href , context ) : <EOL> """<STR_LIT>""" <EOL> instance = get_vm ( uid , context ) <EOL> admin_password = utils . generate_password ( ) <EOL> kwargs = { } <EOL> try : <EOL> COMPUTE_API . rebuild ( context , instance , image_href , admin_password , <EOL> ** kwargs ) <EOL> except Exception as e : <EOL> raise AttributeError ( e . message ) <EOL> def resize_vm ( uid , flavor_id , context ) : <EOL> """<STR_LIT>""" <EOL> instance = get_vm ( uid , context ) <EOL> kwargs = { } <EOL> try : <EOL> flavor = flavors . get_flavor_by_flavor_id ( flavor_id ) <EOL> COMPUTE_API . resize ( context , instance , flavor_id = flavor [ '<STR_LIT>' ] , <EOL> ** kwargs ) <EOL> ready = False <EOL> i = <NUM_LIT:0> <EOL> while not ready and i < <NUM_LIT:15> : <EOL> i += <NUM_LIT:1> <EOL> state = get_vm ( uid , context ) [ '<STR_LIT>' ] <EOL> if state == '<STR_LIT>' : <EOL> ready = True <EOL> import time <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> instance = get_vm ( uid , context ) <EOL> COMPUTE_API . confirm_resize ( context , instance ) <EOL> except Exception as e : <EOL> raise AttributeError ( str ( e ) ) <EOL> def delete_vm ( uid , context ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> instance = get_vm ( uid , context ) <EOL> COMPUTE_API . delete ( context , instance ) <EOL> except Exception as error : <EOL> raise exceptions . HTTPError ( <NUM_LIT> , str ( error ) ) <EOL> def suspend_vm ( uid , context ) : <EOL> """<STR_LIT>""" <EOL> instance = get_vm ( uid , context ) <EOL> try : <EOL> COMPUTE_API . pause ( context , instance ) <EOL> except Exception as error : <EOL> raise exceptions . HTTPError ( <NUM_LIT> , str ( error ) ) <EOL> def snapshot_vm ( uid , image_name , context ) : <EOL> """<STR_LIT>""" <EOL> instance = get_vm ( uid , context ) <EOL> try : <EOL> COMPUTE_API . snapshot ( context , <EOL> instance , <EOL> image_name ) <EOL> except Exception as e : <EOL> raise AttributeError ( e . message ) <EOL> def start_vm ( uid , context ) : <EOL> """<STR_LIT>""" <EOL> instance = get_vm ( uid , context ) <EOL> try : <EOL> if instance [ '<STR_LIT>' ] in [ vm_states . PAUSED ] : <EOL> COMPUTE_API . unpause ( context , instance ) <EOL> elif instance [ '<STR_LIT>' ] in [ vm_states . SUSPENDED ] : <EOL> COMPUTE_API . resume ( context , instance ) <EOL> elif instance [ '<STR_LIT>' ] in [ vm_states . STOPPED ] : <EOL> COMPUTE_API . start ( context , instance ) <EOL> else : <EOL> raise exceptions . HTTPError ( <NUM_LIT> , "<STR_LIT>" ) <EOL> except exceptions . HTTPError as e : <EOL> raise e <EOL> except Exception as e : <EOL> raise AttributeError ( e . message ) <EOL> def stop_vm ( uid , context ) : <EOL> """<STR_LIT>""" <EOL> instance = get_vm ( uid , context ) <EOL> try : <EOL> COMPUTE_API . suspend ( context , instance ) <EOL> except Exception as e : <EOL> raise AttributeError ( e . message ) <EOL> def restart_vm ( uid , method , context ) : <EOL> """<STR_LIT>""" <EOL> instance = get_vm ( uid , context ) <EOL> if method in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> reboot_type = '<STR_LIT>' <EOL> elif method == '<STR_LIT>' : <EOL> reboot_type = '<STR_LIT>' <EOL> else : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> try : <EOL> COMPUTE_API . reboot ( context , instance , reboot_type ) <EOL> except Exception as e : <EOL> raise AttributeError ( e . message ) <EOL> def attach_volume ( instance_id , volume_id , mount_point , context ) : <EOL> """<STR_LIT>""" <EOL> instance = get_vm ( instance_id , context ) <EOL> try : <EOL> COMPUTE_API . attach_volume ( <EOL> context , <EOL> instance , <EOL> volume_id , <EOL> mount_point ) <EOL> except Exception as e : <EOL> raise AttributeError ( e . message ) <EOL> def detach_volume ( instance_id , volume , context ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> instance = get_vm ( instance_id , context ) <EOL> COMPUTE_API . detach_volume ( context , instance , volume ) <EOL> except Exception as e : <EOL> raise AttributeError ( e ) <EOL> def set_password_for_vm ( uid , password , context ) : <EOL> """<STR_LIT>""" <EOL> instance = get_vm ( uid , context ) <EOL> try : <EOL> COMPUTE_API . set_admin_password ( context , instance , password ) <EOL> except Exception as e : <EOL> raise AttributeError ( e . message ) <EOL> def get_vnc ( uid , context ) : <EOL> """<STR_LIT>""" <EOL> console = None <EOL> instance = get_vm ( uid , context ) <EOL> try : <EOL> console = COMPUTE_API . get_vnc_console ( context , instance , '<STR_LIT>' ) <EOL> except Exception : <EOL> LOG . warn ( '<STR_LIT>' ) <EOL> finally : <EOL> return console <EOL> def get_vm ( uid , context ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> instance = COMPUTE_API . get ( context , uid , want_objects = True ) <EOL> except Exception : <EOL> raise exceptions . HTTPError ( <NUM_LIT> , '<STR_LIT>' ) <EOL> return instance <EOL> def get_vms ( context ) : <EOL> """<STR_LIT>""" <EOL> opts = { '<STR_LIT>' : False } <EOL> tmp = COMPUTE_API . get_all ( context , search_opts = opts ) <EOL> return tmp <EOL> def get_vm_state ( uid , context ) : <EOL> """<STR_LIT>""" <EOL> instance = get_vm ( uid , context ) <EOL> state = '<STR_LIT>' <EOL> actions = [ ] <EOL> if instance [ '<STR_LIT>' ] in [ vm_states . ACTIVE ] : <EOL> state = '<STR_LIT>' <EOL> actions . append ( infrastructure . STOP ) <EOL> actions . append ( infrastructure . SUSPEND ) <EOL> actions . append ( infrastructure . RESTART ) <EOL> elif instance [ '<STR_LIT>' ] in [ vm_states . BUILDING ] : <EOL> state = '<STR_LIT>' <EOL> elif instance [ '<STR_LIT>' ] in [ vm_states . PAUSED , vm_states . SUSPENDED , <EOL> vm_states . STOPPED ] : <EOL> state = '<STR_LIT>' <EOL> actions . append ( infrastructure . START ) <EOL> elif instance [ '<STR_LIT>' ] in [ vm_states . RESCUED , <EOL> vm_states . ERROR , <EOL> vm_states . DELETED ] : <EOL> state = '<STR_LIT>' <EOL> if instance [ '<STR_LIT>' ] in [ task_states . IMAGE_SNAPSHOT ] : <EOL> state = '<STR_LIT>' <EOL> actions = [ ] <EOL> return state , actions <EOL> def retrieve_image ( uid , context ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return COMPUTE_API . image_service . show ( context , uid ) <EOL> except Exception as e : <EOL> raise AttributeError ( e . message ) <EOL> def retrieve_images ( context ) : <EOL> """<STR_LIT>""" <EOL> return COMPUTE_API . image_service . detail ( context ) <EOL> def retrieve_flavors ( ) : <EOL> """<STR_LIT>""" <EOL> return flavors . get_all_flavors ( ) </s>
