<s> import logging <EOL> import json <EOL> import os <EOL> import yaml <EOL> from django . conf import settings <EOL> from django . contrib . contenttypes . fields import GenericRelation <EOL> from django . db import models <EOL> from django . core . files . storage import FileSystemStorage <EOL> from django . core . files . base import ContentFile <EOL> from django_extensions . db . models import ( <EOL> TimeStampedModel , <EOL> TitleSlugDescriptionModel , <EOL> ) <EOL> from salt . version import __version__ as salt_version <EOL> from stackdio . core . queryset_transform import TransformQuerySet <EOL> from stackdio . core . fields import DeletingFileField <EOL> from stackdio . api . cloud . providers . base import GroupNotFoundException <EOL> from . utils import get_cloud_provider_choices , get_provider_driver_class <EOL> logger = logging . getLogger ( __name__ ) <EOL> FILESYSTEM_CHOICES = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> def get_config_file_path ( instance , filename ) : <EOL> return filename <EOL> def get_global_orch_props_file_path ( instance , filename ) : <EOL> return '<STR_LIT>' . format ( instance . slug , filename ) <EOL> _cloudprovider_model_permissions = ( ) <EOL> _cloudprovider_object_permissions = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class CloudProvider ( models . Model ) : <EOL> model_permissions = _cloudprovider_model_permissions <EOL> object_permissions = _cloudprovider_object_permissions <EOL> class Meta : <EOL> default_permissions = tuple ( set ( _cloudprovider_model_permissions + <EOL> _cloudprovider_object_permissions ) ) <EOL> PROVIDER_CHOICES = get_cloud_provider_choices ( ) <EOL> name = models . CharField ( <EOL> '<STR_LIT:Name>' , <EOL> max_length = <NUM_LIT:32> , <EOL> choices = PROVIDER_CHOICES , <EOL> unique = True ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> def get_driver ( self ) : <EOL> provider_class = get_provider_driver_class ( self ) <EOL> return provider_class ( ) <EOL> _cloudaccount_model_permissions = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> _cloudaccount_object_permissions = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> class CloudAccount ( TimeStampedModel , TitleSlugDescriptionModel ) : <EOL> model_permissions = _cloudaccount_model_permissions <EOL> object_permissions = _cloudaccount_object_permissions <EOL> searchable_fields = ( '<STR_LIT:title>' , '<STR_LIT:description>' ) <EOL> class Meta : <EOL> ordering = ( '<STR_LIT:title>' , ) <EOL> unique_together = ( '<STR_LIT:title>' , '<STR_LIT>' ) <EOL> default_permissions = tuple ( set ( _cloudaccount_model_permissions + <EOL> _cloudaccount_object_permissions ) ) <EOL> provider = models . ForeignKey ( '<STR_LIT>' , verbose_name = '<STR_LIT>' ) <EOL> yaml = models . TextField ( ) <EOL> region = models . ForeignKey ( '<STR_LIT>' , verbose_name = '<STR_LIT>' ) <EOL> vpc_id = models . CharField ( '<STR_LIT>' , max_length = <NUM_LIT:64> , blank = True ) <EOL> account_id = models . CharField ( '<STR_LIT>' , max_length = <NUM_LIT:64> ) <EOL> create_security_groups = models . BooleanField ( '<STR_LIT>' , default = True ) <EOL> formula_components = GenericRelation ( '<STR_LIT>' ) <EOL> formula_versions = GenericRelation ( '<STR_LIT>' ) <EOL> config_file = DeletingFileField ( <EOL> max_length = <NUM_LIT:255> , <EOL> upload_to = get_config_file_path , <EOL> null = True , <EOL> blank = True , <EOL> default = None , <EOL> storage = FileSystemStorage ( <EOL> location = settings . STACKDIO_CONFIG . salt_providers_dir ) ) <EOL> global_orch_props_file = DeletingFileField ( <EOL> max_length = <NUM_LIT:255> , <EOL> upload_to = get_global_orch_props_file_path , <EOL> null = True , <EOL> blank = True , <EOL> default = None , <EOL> storage = FileSystemStorage ( location = settings . FILE_STORAGE_DIRECTORY ) ) <EOL> def __unicode__ ( self ) : <EOL> return self . title <EOL> @ property <EOL> def vpc_enabled ( self ) : <EOL> return len ( self . vpc_id ) > <NUM_LIT:0> <EOL> def get_driver ( self ) : <EOL> provider_class = get_provider_driver_class ( self . provider ) <EOL> return provider_class ( self ) <EOL> def update_config ( self ) : <EOL> """<STR_LIT>""" <EOL> security_groups = [ sg . group_id for sg in self . security_groups . filter ( <EOL> is_default = True <EOL> ) ] <EOL> account_yaml = yaml . safe_load ( self . yaml ) <EOL> account_yaml [ self . slug ] [ '<STR_LIT>' ] = security_groups <EOL> self . yaml = yaml . safe_dump ( account_yaml , default_flow_style = False ) <EOL> self . save ( ) <EOL> if not self . config_file : <EOL> self . config_file . save ( self . slug + '<STR_LIT>' , ContentFile ( self . yaml ) ) <EOL> else : <EOL> with open ( self . config_file . path , '<STR_LIT:w>' ) as f : <EOL> f . write ( self . yaml ) <EOL> @ property <EOL> def global_orchestration_properties ( self ) : <EOL> if not self . global_orch_props_file : <EOL> return { } <EOL> with open ( self . global_orch_props_file . path ) as f : <EOL> return json . loads ( f . read ( ) ) <EOL> @ global_orchestration_properties . setter <EOL> def global_orchestration_properties ( self , props ) : <EOL> props_json = json . dumps ( props , indent = <NUM_LIT:4> ) <EOL> if not self . global_orch_props_file : <EOL> self . global_orch_props_file . save ( '<STR_LIT>' , ContentFile ( props_json ) ) <EOL> else : <EOL> with open ( self . global_orch_props_file . path , '<STR_LIT:w>' ) as f : <EOL> f . write ( props_json ) <EOL> def get_root_directory ( self ) : <EOL> return os . path . join ( settings . FILE_STORAGE_DIRECTORY , '<STR_LIT>' , self . slug ) <EOL> def get_formulas ( self ) : <EOL> formulas = set ( ) <EOL> for component in self . formula_components . all ( ) : <EOL> formulas . add ( component . formula ) <EOL> return list ( formulas ) <EOL> class CloudInstanceSize ( TitleSlugDescriptionModel ) : <EOL> class Meta : <EOL> ordering = ( '<STR_LIT:id>' , ) <EOL> default_permissions = ( ) <EOL> provider = models . ForeignKey ( '<STR_LIT>' , verbose_name = '<STR_LIT>' ) <EOL> instance_id = models . CharField ( '<STR_LIT>' , max_length = <NUM_LIT:64> ) <EOL> def __unicode__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . description , self . instance_id ) <EOL> _cloudimage_model_permissions = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> _cloudimage_object_permissions = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> class CloudImage ( TimeStampedModel , TitleSlugDescriptionModel ) : <EOL> model_permissions = _cloudimage_model_permissions <EOL> object_permissions = _cloudimage_object_permissions <EOL> searchable_fields = ( '<STR_LIT:title>' , '<STR_LIT:description>' ) <EOL> class Meta : <EOL> ordering = ( '<STR_LIT:title>' , ) <EOL> unique_together = ( '<STR_LIT:title>' , '<STR_LIT>' ) <EOL> default_permissions = tuple ( set ( _cloudimage_model_permissions + <EOL> _cloudimage_object_permissions ) ) <EOL> account = models . ForeignKey ( '<STR_LIT>' , related_name = '<STR_LIT>' ) <EOL> image_id = models . CharField ( '<STR_LIT>' , max_length = <NUM_LIT:64> ) <EOL> default_instance_size = models . ForeignKey ( '<STR_LIT>' , <EOL> verbose_name = '<STR_LIT>' ) <EOL> ssh_user = models . CharField ( '<STR_LIT>' , max_length = <NUM_LIT:64> ) <EOL> config_file = DeletingFileField ( <EOL> max_length = <NUM_LIT:255> , <EOL> upload_to = get_config_file_path , <EOL> null = True , <EOL> blank = True , <EOL> default = None , <EOL> storage = FileSystemStorage ( <EOL> location = settings . STACKDIO_CONFIG . salt_profiles_dir <EOL> ) <EOL> ) <EOL> def __unicode__ ( self ) : <EOL> return self . title <EOL> def update_config ( self ) : <EOL> """<STR_LIT>""" <EOL> script = settings . STACKDIO_CONFIG . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> script_args = settings . STACKDIO_CONFIG . get ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> profile_yaml = { <EOL> self . slug : { <EOL> '<STR_LIT>' : self . account . slug , <EOL> '<STR_LIT:image>' : self . image_id , <EOL> '<STR_LIT:size>' : self . default_instance_size . title , <EOL> '<STR_LIT>' : self . ssh_user , <EOL> '<STR_LIT>' : script , <EOL> '<STR_LIT>' : script_args . format ( salt_version = salt_version ) , <EOL> '<STR_LIT>' : '<STR_LIT:all>' , <EOL> '<STR_LIT>' : { } , <EOL> } <EOL> } <EOL> profile_yaml = yaml . safe_dump ( profile_yaml , <EOL> default_flow_style = False ) <EOL> if not self . config_file : <EOL> self . config_file . save ( self . slug + '<STR_LIT>' , ContentFile ( profile_yaml ) ) <EOL> else : <EOL> with open ( self . config_file . path , '<STR_LIT:w>' ) as f : <EOL> f . write ( profile_yaml ) <EOL> def get_driver ( self ) : <EOL> return self . account . get_driver ( ) <EOL> _snapshot_model_permissions = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> _snapshot_object_permissions = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> class Snapshot ( TimeStampedModel , TitleSlugDescriptionModel ) : <EOL> model_permissions = _snapshot_model_permissions <EOL> object_permissions = _snapshot_object_permissions <EOL> class Meta : <EOL> unique_together = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> default_permissions = tuple ( set ( _snapshot_model_permissions + <EOL> _snapshot_object_permissions ) ) <EOL> account = models . ForeignKey ( '<STR_LIT>' , related_name = '<STR_LIT>' ) <EOL> snapshot_id = models . CharField ( max_length = <NUM_LIT:32> ) <EOL> size_in_gb = models . IntegerField ( ) <EOL> filesystem_type = models . CharField ( max_length = <NUM_LIT:16> , <EOL> choices = FILESYSTEM_CHOICES ) <EOL> class CloudRegion ( TitleSlugDescriptionModel ) : <EOL> class Meta : <EOL> unique_together = ( '<STR_LIT:title>' , '<STR_LIT>' ) <EOL> ordering = ( '<STR_LIT>' , '<STR_LIT:title>' ) <EOL> default_permissions = ( ) <EOL> provider = models . ForeignKey ( '<STR_LIT>' , verbose_name = '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return self . title <EOL> class CloudZone ( TitleSlugDescriptionModel ) : <EOL> class Meta : <EOL> unique_together = ( '<STR_LIT:title>' , '<STR_LIT>' ) <EOL> ordering = ( '<STR_LIT>' , '<STR_LIT:title>' ) <EOL> default_permissions = ( ) <EOL> region = models . ForeignKey ( '<STR_LIT>' , related_name = '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return self . title <EOL> class SecurityGroupQuerySet ( TransformQuerySet ) : <EOL> def with_rules ( self ) : <EOL> return self . transform ( self . _inject_rules ) <EOL> def _inject_rules ( self , queryset ) : <EOL> """<STR_LIT>""" <EOL> by_account = { } <EOL> for group in queryset : <EOL> by_account . setdefault ( group . account , [ ] ) . append ( group ) <EOL> for account , groups in by_account . items ( ) : <EOL> group_ids = [ group . group_id for group in groups ] <EOL> driver = account . get_driver ( ) <EOL> account_groups = driver . get_security_groups ( group_ids ) <EOL> for group in groups : <EOL> group . rules = account_groups [ group . name ] [ '<STR_LIT>' ] <EOL> _securitygroup_model_permissions = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> _securitygroup_object_permissions = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> class SecurityGroup ( TimeStampedModel , models . Model ) : <EOL> model_permissions = _securitygroup_model_permissions <EOL> object_permissions = _securitygroup_object_permissions <EOL> class Meta : <EOL> unique_together = ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> default_permissions = tuple ( set ( _securitygroup_model_permissions + <EOL> _snapshot_object_permissions ) ) <EOL> objects = SecurityGroupQuerySet . as_manager ( ) <EOL> name = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> description = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> group_id = models . CharField ( max_length = <NUM_LIT:16> ) <EOL> stack = models . ForeignKey ( <EOL> '<STR_LIT>' , <EOL> null = True , <EOL> related_name = '<STR_LIT>' <EOL> ) <EOL> blueprint_host_definition = models . ForeignKey ( <EOL> '<STR_LIT>' , <EOL> null = True , <EOL> default = None , <EOL> related_name = '<STR_LIT>' <EOL> ) <EOL> account = models . ForeignKey ( '<STR_LIT>' , related_name = '<STR_LIT>' ) <EOL> is_default = models . BooleanField ( default = False ) <EOL> is_managed = models . BooleanField ( default = False ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> def get_active_hosts ( self ) : <EOL> return self . hosts . count ( ) <EOL> def rules ( self ) : <EOL> """<STR_LIT>""" <EOL> driver = self . account . get_driver ( ) <EOL> groups = driver . get_security_groups ( [ self . group_id ] ) <EOL> if len ( groups ) == <NUM_LIT:1> : <EOL> return groups [ <NUM_LIT:0> ] . rules <EOL> else : <EOL> raise GroupNotFoundException ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( self . group_id ) ) </s>
