blob: 496bc79c2aebc6c55a85d17c2e5ef065f02ad2c4 [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):
Sapan Bhatia7b130102014-07-23 16:39:41 -040015 provides=[User]
16 requested_interval=0
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040017
Sapan Bhatia7b130102014-07-23 16:39:41 -040018 def fetch_pending(self, deletion):
Sapan Bhatiae7e4ca12014-07-22 01:27:02 -040019 if (deletion):
20 return []
21
Sapan Bhatia7b130102014-07-23 16:39:41 -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 Bhatia7b130102014-07-23 16:39:41 -040029 for user in users:
30 if (user.public_key):
31 key_name = user.email.lower().replace('@', 'AT').replace('.', '')
32 key_found = False
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040033
Sapan Bhatia7b130102014-07-23 16:39:41 -040034 for key in ec2_keys:
35 if (key['KeyName']==key_name):
36 key_found = True
37 break
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040038
Sapan Bhatia7b130102014-07-23 16:39:41 -040039 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
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040043
Sapan Bhatia7b130102014-07-23 16:39:41 -040044 def sync_record(self, node):
45 node.save()
46