blob: d7421865e6cde843b540dc8f9bc72cb95e36ea5b [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):
19 users = User.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
20 if (users):
21 key_sig = aws_run('ec2 describe-key-pairs')
22 ec2_keys = key_sig['KeyPairs']
23 else:
24 ec2_keys = []
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040025
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040026 for user in users:
27 if (user.public_key):
28 key_name = user.email.lower().replace('@', 'AT').replace('.', '')
29 key_found = False
30
31 for key in ec2_keys:
32 if (key['KeyName']==key_name):
33 key_found = True
34 break
35
36 if (not key_found):
37 aws_run('ec2 import-key-pair --key-name %s --public-key-material "%s"'%(key_name, user.public_key))
38
39 return users
40
41 def sync_record(self, node):
42 node.save()
43