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