Fixed tabulations
[plstackapi.git] / planetstack / ec2_observer / steps / sync_users.py
1 import os
2 import base64
3 import random
4 import time
5 from datetime import datetime 
6 from django.db.models import F, Q
7 from planetstack.config import Config
8 from ec2_observer.syncstep import SyncStep
9 from core.models.user import User
10 from core.models.site import *
11 from ec2_observer.awslib import *
12 import pdb
13
14 class SyncUsers(SyncStep):
15     provides=[User]
16     requested_interval=0
17
18     def fetch_pending(self, deletion):
19         if (deletion):
20             return []
21
22         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 = []
28
29         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