Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 3 | import random |
| 4 | import time |
| 5 | from datetime import datetime |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 6 | from django.db.models import F, Q |
| 7 | from planetstack.config import Config |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 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 * |
Sapan Bhatia | e6bedf5 | 2014-09-08 03:27:37 -0400 | [diff] [blame] | 12 | from ec2_observer.creds import * |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 13 | import pdb |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 14 | |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 15 | class SyncUsers(SyncStep): |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 16 | provides=[User] |
| 17 | requested_interval=0 |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 18 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 19 | def fetch_pending(self, deletion): |
Sapan Bhatia | e7e4ca1 | 2014-07-22 01:27:02 -0400 | [diff] [blame] | 20 | if (deletion): |
| 21 | return [] |
| 22 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 23 | users = User.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Sapan Bhatia | e6bedf5 | 2014-09-08 03:27:37 -0400 | [diff] [blame] | 24 | |
| 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 Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 30 | ec2_keys = key_sig['KeyPairs'] |
Sapan Bhatia | e6bedf5 | 2014-09-08 03:27:37 -0400 | [diff] [blame] | 31 | creds.append(e) |
| 32 | keys.append(ec2_keys) |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 33 | else: |
| 34 | ec2_keys = [] |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 35 | |
Sapan Bhatia | e6bedf5 | 2014-09-08 03:27:37 -0400 | [diff] [blame] | 36 | for user,ec2_keys,e in zip(users,keys,creds): |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 37 | if (user.public_key): |
| 38 | key_name = user.email.lower().replace('@', 'AT').replace('.', '') |
| 39 | key_found = False |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 40 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 41 | for key in ec2_keys: |
| 42 | if (key['KeyName']==key_name): |
| 43 | key_found = True |
| 44 | break |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 45 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 46 | if (not key_found): |
Sapan Bhatia | e6bedf5 | 2014-09-08 03:27:37 -0400 | [diff] [blame] | 47 | aws_run('ec2 import-key-pair --key-name %s --public-key-material "%s"'%(key_name, user.public_key),env=e) |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 48 | |
| 49 | return users |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 50 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 51 | def sync_record(self, node): |
| 52 | node.save() |