From ccc7e527f63d8bb0c2472592f5ad71cb2fb94243 Mon Sep 17 00:00:00 2001 From: Andrew Melton Date: Thu, 25 Apr 2013 13:59:55 -0400 Subject: [PATCH] Starting reconciler --- stacktach/models.py | 17 +++++++++++ stacktach/reconciler.py | 67 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 stacktach/reconciler.py diff --git a/stacktach/models.py b/stacktach/models.py index 6150d6f..6236695 100644 --- a/stacktach/models.py +++ b/stacktach/models.py @@ -99,6 +99,23 @@ class InstanceDeletes(models.Model): raw = models.ForeignKey(RawData, null=True) +class InstanceReconcile(models.Model): + row_created = models.DateTimeField(auto_now_add=True) + row_updated = models.DateTimeField(auto_now=True) + instance = models.CharField(max_length=50, null=True, + blank=True, db_index=True) + launched_at = models.DecimalField(null=True, max_digits=20, + decimal_places=6, db_index=True) + deleted_at = models.DecimalField(null=True, max_digits=20, + decimal_places=6, db_index=True) + instance_type_id = models.CharField(max_length=50, + null=True, + blank=True, + db_index=True) + source = models.CharField(max_length=150, null=True, + blank=True, db_index=True) + + class InstanceExists(models.Model): PENDING = 'pending' VERIFYING = 'verifying' diff --git a/stacktach/reconciler.py b/stacktach/reconciler.py new file mode 100644 index 0000000..c9e18e4 --- /dev/null +++ b/stacktach/reconciler.py @@ -0,0 +1,67 @@ +from novaclient.v1_1 import client + +from stacktach import models + +reconciler_config = { + 'nova':{ + 'DFW':{ + 'username': 'm0lt3n', + 'project_id': '724740', + 'api_key': '', + 'auth_url': 'https://identity.api.rackspacecloud.com/v2.0', + 'auth_system': 'rackspace', + }, + 'ORD':{ + 'username': 'm0lt3n', + 'project_id': '724740', + 'api_key': '', + 'auth_url': 'https://identity.api.rackspacecloud.com/v2.0', + 'auth_system': 'rackspace', + }, + + }, + 'region_mapping_loc': '/etc/stacktach/region_mapping.json' +} + +region_mapping = { + 'x': 'DFW' +} + + +class Reconciler(object): + + def __init__(self, config): + self.config = reconciler_config + self.region_mapping = region_mapping + self.nova_clients = {} + + def _get_nova(self, region): + if region in self.nova_clients: + return self.nova_clients[region] + + region_cfg = self.config['nova'][region] + region_auth_system = region_cfg.get('auth_system', 'keystone') + + nova = client.Client(region_cfg['username'], region_cfg['api_key'], + region_cfg['project_id'], + auth_url=region_cfg['auth_url'], + auth_system=region_auth_system) + + self.nova_clients[region] = nova + return nova + + def _region_for_launch(self, launch): + request = launch.request_id + raws = models.RawData.objects.filter(request_id=request) + if raws.count() == 0: + return False + raw = raws[0] + return self.region_mapping[str(raw.deployment.name)] + + def missing_exists_for_instance(self, launched_id, + period_beginning, + period_ending): + launch = models.InstanceUsage.objects.get(id=launched_id) + region = self._region_for_launch(launch) + nova = self._get_nova(region) + server = nova.servers.get(launch.instance)