blob: 030e360a3b31ec8b1d49090c7d754f59107fde44 [file] [log] [blame]
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04001import os
2import base64
Sapan Bhatia511ea5f2014-07-21 22:53:58 -04003import random
4import time
5from datetime import datetime
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04006from django.db.models import F, Q
7from planetstack.config import Config
Sapan Bhatia511ea5f2014-07-21 22:53:58 -04008from ec2_observer.syncstep import SyncStep
9from core.models.user import User
10from core.models.site import *
11from ec2_observer.awslib import *
12import pdb
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040013
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040014class SyncUsers(SyncStep):
15 provides=[User]
16 requested_interval=0
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040017
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040018 def fetch_pending(self, deletion):
Sapan Bhatiae7e4ca12014-07-22 01:27:02 -040019 if (deletion):
20 return []
21
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040022 users = User.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
23 if (users):
24 key_sig = aws_run('ec2 describe-key-pairs')
25 ec2_keys = key_sig['KeyPairs']
26 else:
27 ec2_keys = []
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040028
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040029 for user in users:
30 if (user.public_key):
31 key_name = user.email.lower().replace('@', 'AT').replace('.', '')
32 key_found = False
33
34 for key in ec2_keys:
35 if (key['KeyName']==key_name):
36 key_found = True
37 break
38
39 if (not key_found):
40 aws_run('ec2 import-key-pair --key-name %s --public-key-material "%s"'%(key_name, user.public_key))
41
42 return users
43
44 def sync_record(self, node):
45 node.save()
46