blob: a86b4c49c44ec2d62fddc263a93f5d33763bafc8 [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
Scott Baker76a840e2015-02-11 21:38:09 -08005from xos.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
Sapan Bhatiabf7856b2014-11-19 15:09:38 -05008from core.models.slice import SlicePrivilege, SliceDeployments
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04009from 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 *
Sapan Bhatiaaa2bc9c2014-09-08 03:27:16 -040014from ec2_observer.creds import *
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040015import pdb
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040016
17logger = Logger(level=logging.INFO)
18
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040019class SyncSlivers(SyncStep):
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040020 provides=[Sliver]
21 requested_interval=0
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040022
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040023 def fetch_pending(self, deletion):
24 if deletion:
25 object_source = Sliver.deleted_objects
26 else:
27 object_source = Sliver.objects
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040028
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040029 all_slivers = object_source.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
30 my_slivers = []
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040031
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040032 for sliver in all_slivers:
Sapan Bhatiabf7856b2014-11-19 15:09:38 -050033 sd = SliceDeployments.objects.filter(Q(slice=sliver.slice))
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040034 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 Bhatia26d40bc2014-05-12 15:28:02 -040040
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040041 def delete_record(self, sliver):
Sapan Bhatiaaa2bc9c2014-09-08 03:27:16 -040042 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 Bhatia26d40bc2014-05-12 15:28:02 -040045
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040046 def sync_record(self, sliver):
47 logger.info("sync'ing sliver:%s deployment:%s " % (sliver, sliver.node.deployment))
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040048
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040049 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 Bhatia511ea5f2014-07-21 22:53:58 -040053
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040054 if sliver.creator.public_key:
55 pubkeys.append(sliver.creator.public_key)
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040056
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040057 if sliver.slice.creator.public_key:
58 pubkeys.append(sliver.slice.creator.public_key)
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040059
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040060 # 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 Bhatia511ea5f2014-07-21 22:53:58 -040080
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040081 instance_type = sliver.node.name.rsplit('.',1)[0]
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040082
Sapan Bhatia1fc85c92014-07-22 01:22:15 -040083 # Bail out of we don't have a key
84 key_name = sliver.creator.email.lower().replace('@', 'AT').replace('.', '')
Sapan Bhatiaaa2bc9c2014-09-08 03:27:16 -040085 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 Bhatia1fc85c92014-07-22 01:22:15 -040089 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 Bhatiaaa2bc9c2014-09-08 03:27:16 -0400101 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 Bhatia1fc85c92014-07-22 01:22:15 -0400102 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 Bhatiaaa2bc9c2014-09-08 03:27:16 -0400112 ret = aws_run('ec2 describe-instances --instance-ids %s'%sliver.instance_id, env=e)
Sapan Bhatia1fc85c92014-07-22 01:22:15 -0400113 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 Bhatia26d40bc2014-05-12 15:28:02 -0400117