Revision b84092e7 snf-cyclades-app/synnefo/logic/management/commands/reconcile.py

b/snf-cyclades-app/synnefo/logic/management/commands/reconcile.py
41 41
from django.conf import settings
42 42
from django.core.management.base import BaseCommand, CommandError
43 43

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

  
48 48

  
49
_valid_users = set()
50
_invalid_users = set()
51

  
52

  
53
def user_exists(user):
54
    from astakos.im.models import AstakosUser
55

  
56
    if user in _valid_users:
57
        return True
58
    elif user in _invalid_users:
59
        return False
60

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

  
70

  
71 49
class Command(BaseCommand):
72 50
    can_import_settings = True
73 51

  
......
84 62
                    dest='detect_unsynced',
85 63
                    default=False, help='Detect unsynced operstate between ' +
86 64
                                        'DB and Ganeti'),
87
        make_option('--detect-orphan-servers',
88
                    action='store_true',
89
                    dest='detect_orphan_servers',
90
                    help='Detect VMs with an invalid owner'),
91
        make_option('--detect-orphan-networks',
92
                    action='store_true',
93
                    dest='detect_orphan_networks',
94
                    help='Detect networks with an invalid owner'),
95 65
        make_option('--detect-all', action='store_true',
96 66
                    dest='detect_all',
97 67
                    default=False, help='Enable all --detect-* arguments'),
......
166 136
            elif verbosity == 2:
167 137
                print >> sys.stderr, "The operstate of all servers is in sync."
168 138

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

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

  
185 139
        #
186 140
        # Then fix them
187 141
        #

Also available in: Unified diff