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 * |
| 12 | import pdb |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 13 | |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 14 | class SyncUsers(SyncStep): |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame^] | 15 | provides=[User] |
| 16 | requested_interval=0 |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 17 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame^] | 18 | def fetch_pending(self, deletion): |
Sapan Bhatia | e7e4ca1 | 2014-07-22 01:27:02 -0400 | [diff] [blame] | 19 | if (deletion): |
| 20 | return [] |
| 21 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame^] | 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 = [] |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 28 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame^] | 29 | for user in users: |
| 30 | if (user.public_key): |
| 31 | key_name = user.email.lower().replace('@', 'AT').replace('.', '') |
| 32 | key_found = False |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 33 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame^] | 34 | for key in ec2_keys: |
| 35 | if (key['KeyName']==key_name): |
| 36 | key_found = True |
| 37 | break |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 38 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame^] | 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 |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 43 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame^] | 44 | def sync_record(self, node): |
| 45 | node.save() |
| 46 | |