blob: 55069aadc2a59e2c352630febe42461846d4dbb2 [file] [log] [blame]
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04001import os
Sapan Bhatia511ea5f2014-07-21 22:53:58 -04002import json
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04003import base64
4from django.db.models import F, Q
5from planetstack.config import Config
Sapan Bhatia511ea5f2014-07-21 22:53:58 -04006from ec2_observer.syncstep import SyncStep
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04007from core.models.sliver import Sliver
8from core.models.slice import SlicePrivilege, SliceDeployments
9from core.models.network import Network, NetworkSlice, NetworkDeployments
10from util.logger import Logger, logging
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040011from ec2_observer.awslib import *
12from core.models.site import *
13from core.models.slice import *
14import pdb
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040015
16logger = Logger(level=logging.INFO)
17
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040018class SyncSlivers(SyncStep):
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040019 provides=[Sliver]
20 requested_interval=0
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040021
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040022 def fetch_pending(self, deletion):
23 if deletion:
24 object_source = Sliver.deleted_objects
25 else:
26 object_source = Sliver.objects
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040027
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040028 all_slivers = object_source.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
29 my_slivers = []
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040030
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040031 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 Bhatia26d40bc2014-05-12 15:28:02 -040039
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040040 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 Bhatia26d40bc2014-05-12 15:28:02 -040044
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040045 def sync_record(self, sliver):
46 logger.info("sync'ing sliver:%s deployment:%s " % (sliver, sliver.node.deployment))
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040047
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040048 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 Bhatia511ea5f2014-07-21 22:53:58 -040052
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040053 if sliver.creator.public_key:
54 pubkeys.append(sliver.creator.public_key)
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040055
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040056 if sliver.slice.creator.public_key:
57 pubkeys.append(sliver.slice.creator.public_key)
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040058
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040059 # 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 Bhatia511ea5f2014-07-21 22:53:58 -040079
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040080 instance_type = sliver.node.name.rsplit('.',1)[0]
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040081
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040082 # 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 Bhatia26d40bc2014-05-12 15:28:02 -0400113