Revision 7e136fd8

b/snf-cyclades-app/synnefo/logic/management/commands/reconcile.py
1
# Copyright 2011 GRNET S.A. All rights reserved.
1
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 2
#
3 3
# Redistribution and use in source and binary forms, with or without
4 4
# modification, are permitted provided that the following conditions
......
41 41

  
42 42
from django.conf import settings
43 43
from django.db.models import Q
44
from django.core.management.base import BaseCommand
44
from django.core.management.base import BaseCommand, CommandError
45 45

  
46
from synnefo.db.models import VirtualMachine
46
from synnefo.db.models import VirtualMachine, Network
47 47
from synnefo.logic import reconciliation, backend
48 48
from synnefo.util.rapi import GanetiRapiClient
49 49

  
50 50

  
51
_valid_users = set()
52
_invalid_users = set()
53

  
54

  
55
def user_exists(user):
56
    from astakos.im.models import AstakosUser
57

  
58
    if user in _valid_users:
59
        return True
60
    elif user in _invalid_users:
61
        return False
62

  
63
    try:
64
        AstakosUser.objects.get(email=user)
65
    except AstakosUser.DoesNotExist:
66
        _invalid_users.add(user)
67
        return False
68
    else:
69
        _valid_users.add(user)
70
        return True
71

  
72

  
51 73
class Command(BaseCommand):
52 74
    can_import_settings = True
53 75

  
......
64 86
                    dest='detect_unsynced',
65 87
                    default=False, help='Detect unsynced operstate between ' +
66 88
                                        'DB and Ganeti'),
89
        make_option('--detect-orphan-servers',
90
                    action='store_true',
91
                    dest='detect_orphan_servers',
92
                    help='Detect VMs with an invalid owner'),
93
        make_option('--detect-orphan-networks',
94
                    action='store_true',
95
                    dest='detect_orphan_networks',
96
                    help='Detect networks with an invalid owner'),
67 97
        make_option('--detect-all', action='store_true',
68 98
                    dest='detect_all',
69 99
                    default=False, help='Enable all --detect-* arguments'),
......
90 120

  
91 121
        if not reduce(lambda x, y: x or y,
92 122
                      map(lambda x: options[x], keys_detect)):
93
            raise Exception("At least one of --detect-* must be specified")
123
            raise CommandError("At least one of --detect-* must be specified")
94 124

  
95 125
        for kf in keys_fix:
96 126
            kd = kf.replace('fix_', 'detect_', 1)
97 127
            if (options[kf] and not options[kd]):
98
                raise Exception("Cannot use --%s without corresponding "
99
                                "--%s argument" % (kf, kd))
128
                raise CommandError("Cannot use --%s without corresponding "
129
                                   "--%s argument" % (kf, kd))
100 130

  
101 131
    def handle(self, **options):
102 132
        verbosity = int(options['verbosity'])
......
138 168
            elif verbosity == 2:
139 169
                print >> sys.stderr, "The operstate of all servers is in sync."
140 170

  
171
        if options['detect_orphan_servers']:
172
            for server in VirtualMachine.objects.filter(deleted=False):
173
                owner = server.userid
174
                if not user_exists(owner):
175
                    msg = "Server %d (%s) has unknown owner %s\n" % (
176
                            server.id, server.name, owner)
177
                    self.stdout.write(msg)
178

  
179
        if options['detect_orphan_networks']:
180
            for network in Network.objects.exclude(state='DELETED'):
181
                owner = network.userid
182
                if owner and not user_exists(owner):
183
                    msg = "Network %d (%s) has unknown owner %s\n" % (
184
                            network.id, network.name, owner)
185
                    self.stdout.write(msg)
186

  
141 187
        #
142 188
        # Then fix them
143 189
        #

Also available in: Unified diff