<s> import os <EOL> import json <EOL> import base64 <EOL> from django . db . models import F , Q <EOL> from xos . config import Config <EOL> from ec2_observer . syncstep import SyncStep <EOL> from core . models . instance import Instance <EOL> from core . models . slice import SlicePrivilege , SliceDeployments <EOL> from core . models . network import Network , NetworkSlice , NetworkDeployments <EOL> from xos . logger import Logger , logging <EOL> from ec2_observer . awslib import * <EOL> from core . models . site import * <EOL> from core . models . slice import * <EOL> from ec2_observer . creds import * <EOL> import pdb <EOL> logger = Logger ( level = logging . INFO ) <EOL> class SyncInstances ( SyncStep ) : <EOL> provides = [ Instance ] <EOL> requested_interval = <NUM_LIT:0> <EOL> def fetch_pending ( self , deletion ) : <EOL> if deletion : <EOL> object_source = Instance . deleted_objects <EOL> else : <EOL> object_source = Instance . objects <EOL> all_instances = object_source . filter ( Q ( enacted__lt = F ( '<STR_LIT>' ) ) | Q ( enacted = None ) ) <EOL> my_instances = [ ] <EOL> for instance in all_instances : <EOL> sd = SliceDeployments . objects . filter ( Q ( slice = instance . slice ) ) <EOL> if ( sd ) : <EOL> if ( sd . deployment . name == '<STR_LIT>' ) : <EOL> my_instances . append ( instance ) <EOL> if ( instance . node . deployment . name == '<STR_LIT>' ) : <EOL> my_instances . append ( instance ) <EOL> return my_instances <EOL> def delete_record ( self , instance ) : <EOL> user = instance . creator <EOL> e = get_creds ( user = user , site = user . site ) <EOL> result = aws_run ( '<STR_LIT>' % instance . instance_id , env = e ) <EOL> def sync_record ( self , instance ) : <EOL> logger . info ( "<STR_LIT>" % ( instance , instance . node . deployment ) , extra = instance . tologdict ( ) ) <EOL> if not instance . instance_id : <EOL> slice_memberships = SlicePrivilege . objects . filter ( slice = instance . slice ) <EOL> pubkeys = [ sm . user . public_key for sm in slice_memberships if sm . user . public_key ] <EOL> if instance . creator . public_key : <EOL> pubkeys . append ( instance . creator . public_key ) <EOL> if instance . slice . creator . public_key : <EOL> pubkeys . append ( instance . slice . creator . public_key ) <EOL> instance_type = instance . node . name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> key_name = instance . creator . email . lower ( ) . replace ( '<STR_LIT:@>' , '<STR_LIT>' ) . replace ( '<STR_LIT:.>' , '<STR_LIT>' ) <EOL> u = instance . creator <EOL> s = u . site <EOL> e = get_creds ( user = u , site = s ) <EOL> key_sig = aws_run ( '<STR_LIT>' , env = e ) <EOL> ec2_keys = key_sig [ '<STR_LIT>' ] <EOL> key_found = False <EOL> for key in ec2_keys : <EOL> if ( key [ '<STR_LIT>' ] == key_name ) : <EOL> key_found = True <EOL> break <EOL> if ( not key_found ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> image_id = instance . image . path <EOL> instance_sig = aws_run ( '<STR_LIT>' % ( image_id , instance_type , key_name , instance . node . site . name ) , env = e ) <EOL> instance . instance_id = instance_sig [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> instance . save ( ) <EOL> state = instance_sig [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if ( state == <NUM_LIT:16> ) : <EOL> instance . ip = instance_sig [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> instance . save ( ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> else : <EOL> ret = aws_run ( '<STR_LIT>' % instance . instance_id , env = e ) <EOL> state = ret [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if ( state == <NUM_LIT:16> ) : <EOL> instance . ip = ret [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> instance . save ( ) </s>
