Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 1 | import os |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 2 | import json |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 3 | import base64 |
| 4 | from django.db.models import F, Q |
| 5 | from planetstack.config import Config |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 6 | from ec2_observer.syncstep import SyncStep |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 7 | from core.models.sliver import Sliver |
| 8 | from core.models.slice import SlicePrivilege, SliceDeployments |
| 9 | from core.models.network import Network, NetworkSlice, NetworkDeployments |
| 10 | from util.logger import Logger, logging |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 11 | from ec2_observer.awslib import * |
| 12 | from core.models.site import * |
| 13 | from core.models.slice import * |
| 14 | import pdb |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 15 | |
| 16 | logger = Logger(level=logging.INFO) |
| 17 | |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 18 | class SyncSlivers(SyncStep): |
Sapan Bhatia | 1fc85c9 | 2014-07-22 01:22:15 -0400 | [diff] [blame] | 19 | provides=[Sliver] |
| 20 | requested_interval=0 |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 21 | |
Sapan Bhatia | 1fc85c9 | 2014-07-22 01:22:15 -0400 | [diff] [blame] | 22 | def fetch_pending(self, deletion): |
| 23 | if deletion: |
| 24 | object_source = Sliver.deleted_objects |
| 25 | else: |
| 26 | object_source = Sliver.objects |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 27 | |
Sapan Bhatia | 1fc85c9 | 2014-07-22 01:22:15 -0400 | [diff] [blame] | 28 | all_slivers = object_source.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 29 | my_slivers = [] |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 30 | |
Sapan Bhatia | 1fc85c9 | 2014-07-22 01:22:15 -0400 | [diff] [blame] | 31 | for sliver in all_slivers: |
| 32 | sd = SliceDeployments.objects.filter(Q(slice=sliver.slice)) |
| 33 | if (sd): |
| 34 | if (sd.deployment.name=='Amazon EC2'): |
| 35 | my_slivers.append(sliver) |
| 36 | if (sliver.node.deployment.name=='Amazon EC2'): |
| 37 | my_slivers.append(sliver) |
| 38 | return my_slivers |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 39 | |
Sapan Bhatia | 1fc85c9 | 2014-07-22 01:22:15 -0400 | [diff] [blame] | 40 | def delete_record(self, sliver): |
| 41 | import pdb |
| 42 | pdb.set_trace() |
| 43 | result = aws_run('ec2 terminate-instances --instance-ids=%s'%sliver.instance_id) |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 44 | |
Sapan Bhatia | 1fc85c9 | 2014-07-22 01:22:15 -0400 | [diff] [blame] | 45 | def sync_record(self, sliver): |
| 46 | logger.info("sync'ing sliver:%s deployment:%s " % (sliver, sliver.node.deployment)) |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 47 | |
Sapan Bhatia | 1fc85c9 | 2014-07-22 01:22:15 -0400 | [diff] [blame] | 48 | if not sliver.instance_id: |
| 49 | # public keys |
| 50 | slice_memberships = SlicePrivilege.objects.filter(slice=sliver.slice) |
| 51 | pubkeys = [sm.user.public_key for sm in slice_memberships if sm.user.public_key] |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 52 | |
Sapan Bhatia | 1fc85c9 | 2014-07-22 01:22:15 -0400 | [diff] [blame] | 53 | if sliver.creator.public_key: |
| 54 | pubkeys.append(sliver.creator.public_key) |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 55 | |
Sapan Bhatia | 1fc85c9 | 2014-07-22 01:22:15 -0400 | [diff] [blame] | 56 | if sliver.slice.creator.public_key: |
| 57 | pubkeys.append(sliver.slice.creator.public_key) |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 58 | |
Sapan Bhatia | 1fc85c9 | 2014-07-22 01:22:15 -0400 | [diff] [blame] | 59 | # netowrks |
| 60 | # include all networks available to the slice and/or associated network templates |
| 61 | #nics = [] |
| 62 | #networks = [ns.network for ns in NetworkSlice.objects.filter(slice=sliver.slice)] |
| 63 | #network_deployments = NetworkDeployments.objects.filter(network__in=networks, |
| 64 | #deployment=sliver.node.deployment) |
| 65 | # Gather private networks first. This includes networks with a template that has |
| 66 | # visibility = private and translation = none |
| 67 | #for network_deployment in network_deployments: |
| 68 | # if network_deployment.network.template.visibility == 'private' and \ |
| 69 | # network_deployment.network.template.translation == 'none': |
| 70 | # nics.append({'net-id': network_deployment.net_id}) |
| 71 | |
| 72 | # now include network template |
| 73 | #network_templates = [network.template.sharedNetworkName for network in networks \ |
| 74 | # if network.template.sharedNetworkName] |
| 75 | #for net in driver.shell.quantum.list_networks()['networks']: |
| 76 | # if net['name'] in network_templates: |
| 77 | # nics.append({'net-id': net['id']}) |
| 78 | # look up image id |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 79 | |
Sapan Bhatia | 1fc85c9 | 2014-07-22 01:22:15 -0400 | [diff] [blame] | 80 | instance_type = sliver.node.name.rsplit('.',1)[0] |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 81 | |
Sapan Bhatia | 1fc85c9 | 2014-07-22 01:22:15 -0400 | [diff] [blame] | 82 | # Bail out of we don't have a key |
| 83 | key_name = sliver.creator.email.lower().replace('@', 'AT').replace('.', '') |
| 84 | key_sig = aws_run('ec2 describe-key-pairs') |
| 85 | ec2_keys = key_sig['KeyPairs'] |
| 86 | key_found = False |
| 87 | for key in ec2_keys: |
| 88 | if (key['KeyName']==key_name): |
| 89 | key_found = True |
| 90 | break |
| 91 | |
| 92 | if (not key_found): |
| 93 | # set backend_status |
| 94 | raise Exception('Will not sync sliver without key') |
| 95 | |
| 96 | image_id = sliver.image.path |
| 97 | instance_sig = aws_run('ec2 run-instances --image-id %s --instance-type %s --count 1 --key-name %s --placement AvailabilityZone=%s'%(image_id,instance_type,key_name,sliver.node.site.name)) |
| 98 | sliver.instance_id = instance_sig['Instances'][0]['InstanceId'] |
| 99 | sliver.save() |
| 100 | state = instance_sig['Instances'][0]['State']['Code'] |
| 101 | if (state==16): |
| 102 | sliver.ip = instance_sig['Instances'][0]['PublicIpAddress'] |
| 103 | sliver.save() |
| 104 | else: |
| 105 | # This status message should go into backend_status |
| 106 | raise Exception('Waiting for instance to start') |
| 107 | else: |
| 108 | ret = aws_run('ec2 describe-instances --instance-ids %s'%sliver.instance_id) |
| 109 | state = ret['Reservations'][0]['Instances'][0]['State']['Code'] |
| 110 | if (state==16): |
| 111 | sliver.ip = ret['Reservations'][0]['Instances'][0]['PublicIpAddress'] |
| 112 | sliver.save() |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 113 | |