blob: 5fe70c74f9ff0abc8c24a7dcb662bd6b9cd82476 [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
Scott Baker76a840e2015-02-11 21:38:09 -08007from xos.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 *
Sapan Bhatia279e0ca2014-09-08 03:27:37 -040012from ec2_observer.creds import *
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040013import pdb
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040014
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040015class SyncUsers(SyncStep):
Sapan Bhatia7b130102014-07-23 16:39:41 -040016 provides=[User]
17 requested_interval=0
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040018
Sapan Bhatia7b130102014-07-23 16:39:41 -040019 def fetch_pending(self, deletion):
Sapan Bhatiae7e4ca12014-07-22 01:27:02 -040020 if (deletion):
21 return []
22
Sapan Bhatia7b130102014-07-23 16:39:41 -040023 users = User.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Sapan Bhatia279e0ca2014-09-08 03:27:37 -040024
25 keys = []
26 creds = []
27 for u in users:
28 e = get_creds(user=u, site=u.site)
29 key_sig = aws_run('ec2 describe-key-pairs', env=e)
Sapan Bhatia7b130102014-07-23 16:39:41 -040030 ec2_keys = key_sig['KeyPairs']
Sapan Bhatia279e0ca2014-09-08 03:27:37 -040031 creds.append(e)
32 keys.append(ec2_keys)
Sapan Bhatia7b130102014-07-23 16:39:41 -040033 else:
34 ec2_keys = []
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040035
Sapan Bhatia279e0ca2014-09-08 03:27:37 -040036 for user,ec2_keys,e in zip(users,keys,creds):
Sapan Bhatia7b130102014-07-23 16:39:41 -040037 if (user.public_key):
38 key_name = user.email.lower().replace('@', 'AT').replace('.', '')
39 key_found = False
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040040
Sapan Bhatia7b130102014-07-23 16:39:41 -040041 for key in ec2_keys:
42 if (key['KeyName']==key_name):
43 key_found = True
44 break
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040045
Sapan Bhatia7b130102014-07-23 16:39:41 -040046 if (not key_found):
Sapan Bhatia279e0ca2014-09-08 03:27:37 -040047 aws_run('ec2 import-key-pair --key-name %s --public-key-material "%s"'%(key_name, user.public_key),env=e)
Sapan Bhatia7b130102014-07-23 16:39:41 -040048
49 return users
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040050
Sapan Bhatia7b130102014-07-23 16:39:41 -040051 def sync_record(self, node):
52 node.save()