Revision 4161cb41 snf-cyclades-app/synnefo/logic/reconciliation.py
b/snf-cyclades-app/synnefo/logic/reconciliation.py | ||
---|---|---|
67 | 67 |
"the parent directory of the Synnefo Django project.") |
68 | 68 |
setup_environ(settings) |
69 | 69 |
|
70 |
|
|
71 |
from datetime import datetime, timedelta |
|
72 |
|
|
70 | 73 |
from synnefo.db.models import VirtualMachine |
71 | 74 |
from synnefo.util.dictconfig import dictConfig |
75 |
from synnefo.util.rapi import GanetiApiError |
|
72 | 76 |
from synnefo.logic.backend import get_ganeti_instances |
73 | 77 |
|
74 | 78 |
|
... | ... | |
79 | 83 |
idD = set(D.keys()) |
80 | 84 |
idG = set(G.keys()) |
81 | 85 |
|
82 |
return idD - idG |
|
86 |
stale = set() |
|
87 |
for i in idD - idG: |
|
88 |
if D[i] == 'BUILD': |
|
89 |
vm = VirtualMachine.objects.get(id=i) |
|
90 |
# Check time to avoid many rapi calls |
|
91 |
if datetime.now() > vm.backendtime + timedelta(seconds=5): |
|
92 |
try: |
|
93 |
job_status = vm.client.GetJobStatus(vm.backendjobid)['status'] |
|
94 |
if job_status in ('queued', 'waiting', 'running'): |
|
95 |
# Server is still building in Ganeti |
|
96 |
continue |
|
97 |
else: |
|
98 |
new_vm = vm.client.GetInstance('%s%d' % |
|
99 |
(settings.BACKEND_PREFIX_ID, i)) |
|
100 |
# Server has just been created in Ganeti |
|
101 |
continue |
|
102 |
except GanetiApiError: |
|
103 |
stale.add(i) |
|
104 |
else: |
|
105 |
stale.add(i) |
|
106 |
|
|
107 |
return stale |
|
83 | 108 |
|
84 | 109 |
|
85 | 110 |
def orphan_instances_in_ganeti(D, G): |
... | ... | |
98 | 123 |
if (G[i] and D[i] != 'STARTED' or |
99 | 124 |
not G[i] and D[i] not in ('BUILD', 'ERROR', 'STOPPED')): |
100 | 125 |
unsynced.add((i, D[i], G[i])) |
126 |
if not G[i] and D[i] == 'BUILD': |
|
127 |
vm = VirtualMachine.objects.get(id=i) |
|
128 |
# Check time to avoid many rapi calls |
|
129 |
if datetime.now() > vm.backendtime + timedelta(seconds=5): |
|
130 |
try: |
|
131 |
job_info = vm.client.GetJobStatus(job_id = vm.backendjobid) |
|
132 |
if job_info['status'] == 'success': |
|
133 |
unsynced.add((i, D[i], G[i])) |
|
134 |
except GanetiApiError: |
|
135 |
pass |
|
101 | 136 |
|
102 | 137 |
return unsynced |
103 | 138 |
|
104 | 139 |
|
140 |
def instances_with_build_errors(D, G): |
|
141 |
failed = set() |
|
142 |
idD = set(D.keys()) |
|
143 |
idG = set(G.keys()) |
|
144 |
|
|
145 |
for i in idD & idG: |
|
146 |
if not G[i] and D[i] == 'BUILD': |
|
147 |
vm = VirtualMachine.objects.get(id=i) |
|
148 |
# Check time to avoid many rapi calls |
|
149 |
if datetime.now() > vm.backendtime + timedelta(seconds=5): |
|
150 |
try: |
|
151 |
job_info = vm.client.GetJobStatus(job_id = vm.backendjobid) |
|
152 |
if job_info['status'] == 'error': |
|
153 |
failed.add(i) |
|
154 |
except GanetiApiError: |
|
155 |
failed.add(i) |
|
156 |
|
|
157 |
return failed |
|
158 |
|
|
159 |
|
|
160 |
|
|
105 | 161 |
def get_servers_from_db(): |
106 | 162 |
vms = VirtualMachine.objects.filter(deleted=False) |
107 | 163 |
return dict(map(lambda x: (x.id, x.operstate), vms)) |
Also available in: Unified diff