Revision f5b4f2a3

b/snf-cyclades-app/synnefo/api/servers.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
import random
35

  
34 36
from base64 import b64decode
35 37
from logging import getLogger
36 38

  
......
43 45
from synnefo.api import faults, util
44 46
from synnefo.api.actions import server_actions
45 47
from synnefo.api.common import method_not_allowed
46
from synnefo.db.models import VirtualMachine, VirtualMachineMetadata
48
from synnefo.db.models import Backend, VirtualMachine, VirtualMachineMetadata
47 49
from synnefo.logic.backend import create_instance, delete_instance
48 50
from synnefo.logic.utils import get_rsapi_state
49 51
from synnefo.util.rapi import GanetiApiError
......
251 253
    if count >= vms_limit_for_user:
252 254
        raise faults.OverLimit("Server count limit exceeded for your account.")
253 255

  
256
    backend = random.choice(Backend.objects.all())
254 257
    # We must save the VM instance now, so that it gets a
255 258
    # valid vm.backend_vm_id.
256 259
    vm = VirtualMachine.objects.create(
257 260
        name=name,
261
        backend=backend,
258 262
        userid=request.user_uniq,
259 263
        imageid=image_id,
260 264
        flavor=flavor)
b/snf-cyclades-app/synnefo/db/models.py
309 309

  
310 310
    @property
311 311
    def client(self):
312
        if not self.backend.offline:
312
        if self.backend and not self.backend.offline:
313 313
            return get_client(self.backend_hash, self.backend_id)
314 314
        else:
315 315
            raise ServiceUnavailable
b/snf-cyclades-app/synnefo/logic/backend.py
34 34
import json
35 35

  
36 36
from logging import getLogger
37

  
38 37
from django.conf import settings
39 38
from django.db import transaction
40 39

  
41
from synnefo.db.models import (VirtualMachine, Network, NetworkLink)
40
from synnefo.db.models import (Backend, VirtualMachine, Network, NetworkLink)
42 41
from synnefo.logic import utils
43 42
from synnefo.util.rapi import GanetiRapiClient
44 43

  
45 44

  
45

  
46 46
log = getLogger('synnefo.logic')
47 47

  
48
rapi = GanetiRapiClient(*settings.GANETI_CLUSTER_INFO)
49 48

  
50 49
_firewall_tags = {
51 50
    'ENABLED': settings.GANETI_FIREWALL_ENABLED_TAG,
......
282 281
    # Defined in settings.GANETI_CREATEINSTANCE_KWARGS
283 282
    # kw['hvparams'] = dict(serial_console=False)
284 283

  
285
    return rapi.CreateInstance(**kw)
284
    return vm.client.CreateInstance(**kw)
286 285

  
287 286

  
288 287
def delete_instance(vm):
289 288
    start_action(vm, 'DESTROY')
290
    rapi.DeleteInstance(vm.backend_vm_id, dry_run=settings.TEST)
289
    vm.client.DeleteInstance(vm.backend_vm_id, dry_run=settings.TEST)
291 290

  
292 291

  
293 292
def reboot_instance(vm, reboot_type):
294 293
    assert reboot_type in ('soft', 'hard')
295
    rapi.RebootInstance(vm.backend_vm_id, reboot_type, dry_run=settings.TEST)
294
    vm.client.RebootInstance(vm.backend_vm_id, reboot_type, dry_run=settings.TEST)
296 295
    log.info('Rebooting instance %s', vm.backend_vm_id)
297 296

  
298 297

  
299 298
def startup_instance(vm):
300 299
    start_action(vm, 'START')
301
    rapi.StartupInstance(vm.backend_vm_id, dry_run=settings.TEST)
300
    vm.client.StartupInstance(vm.backend_vm_id, dry_run=settings.TEST)
302 301

  
303 302

  
304 303
def shutdown_instance(vm):
305 304
    start_action(vm, 'STOP')
306
    rapi.ShutdownInstance(vm.backend_vm_id, dry_run=settings.TEST)
305
    vm.client.ShutdownInstance(vm.backend_vm_id, dry_run=settings.TEST)
307 306

  
308 307

  
309 308
def get_instance_console(vm):
......
320 319
    #
321 320
    console = {}
322 321
    console['kind'] = 'vnc'
323
    i = rapi.GetInstance(vm.backend_vm_id)
322
    i = vm.client.GetInstance(vm.backend_vm_id)
324 323
    if i['hvparams']['serial_console']:
325 324
        raise Exception("hv parameter serial_console cannot be true")
326 325
    console['host'] = i['pnode']
......
331 330

  
332 331

  
333 332
def request_status_update(vm):
334
    return rapi.GetInstanceInfo(vm.backend_vm_id)
335

  
336

  
337
def get_job_status(jobid):
338
    return rapi.GetJobStatus(jobid)
333
    return vm.client.GetInstanceInfo(vm.backend_vm_id)
339 334

  
340 335

  
341 336
def update_status(vm, status):
......
395 390

  
396 391
def connect_to_network(vm, net):
397 392
    nic = {'mode': 'bridged', 'link': net.link.name}
398
    rapi.ModifyInstance(vm.backend_vm_id, nics=[('add', -1, nic)],
393
    vm.client.ModifyInstance(vm.backend_vm_id, nics=[('add', -1, nic)],
399 394
                        hotplug=True, dry_run=settings.TEST)
400 395

  
401 396

  
......
404 399
    ops = [('remove', nic.index, {}) for nic in nics if nic.network == net]
405 400
    if not ops:  # Vm not connected to network
406 401
        return
407
    rapi.ModifyInstance(vm.backend_vm_id, nics=ops[::-1],
402
    vm.client.ModifyInstance(vm.backend_vm_id, nics=ops[::-1],
408 403
                        hotplug=True, dry_run=settings.TEST)
409 404

  
410 405

  
......
414 409
    except KeyError:
415 410
        raise ValueError("Unsopported Firewall Profile: %s" % profile)
416 411

  
412
    client = vm.client
417 413
    # Delete all firewall tags
418 414
    for t in _firewall_tags.values():
419
        rapi.DeleteInstanceTags(vm.backend_vm_id, [t], dry_run=settings.TEST)
415
        client.DeleteInstanceTags(vm.backend_vm_id, [t], dry_run=settings.TEST)
420 416

  
421
    rapi.AddInstanceTags(vm.backend_vm_id, [tag], dry_run=settings.TEST)
417
    client.AddInstanceTags(vm.backend_vm_id, [tag], dry_run=settings.TEST)
422 418

  
423 419
    # XXX NOP ModifyInstance call to force process_net_status to run
424 420
    # on the dispatcher
425
    rapi.ModifyInstance(vm.backend_vm_id,
421
    vm.client.ModifyInstance(vm.backend_vm_id,
426 422
                        os_name=settings.GANETI_CREATEINSTANCE_KWARGS['os'])
427 423

  
428 424

  
429
def get_ganeti_instances():
430
    return rapi.GetInstances()
425
def get_ganeti_instances(backend=None, bulk=False):
426
    Instances = [c.client.GetInstances(bulk=bulk) for c in get_backends(backend)]
427
    return reduce(list.__add__, Instances, [])
428

  
429

  
430
def get_ganeti_nodes(backend=None, bulk=False):
431
    Nodes = [c.client.GetNodes(bulk=bulk) for c in get_backends(backend)]
432
    return reduce(list.__add__, Nodes, [])
433

  
434

  
435
def get_ganeti_jobs(backend=None, bulk=False):
436
    Jobs = [c.client.GetJobs(bulk=bulk) for c in get_backends(backend)]
437
    return reduce(list.__add__, Jobs, [])
438

  
439
##
440
##
441
##
442
def get_backends(backend=None):
443
    if backend:
444
        return [backend]
445
    return Backend.objects.all()
446

  
431 447

  
432 448

  
433
def get_ganeti_nodes():
434
    return rapi.GetNodes()
435 449

  
436 450

  
437
def get_ganeti_jobs():
438
    return rapi.GetJobs()
b/snf-cyclades-app/synnefo/logic/management/commands/reconcile.py
44 44

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

  
49 48

  
50 49
class Command(BaseCommand):
......
156 155
                "Issuing OP_INSTANCE_REMOVE for %d Ganeti instances:" % \
157 156
                len(orphans)
158 157
            for id in orphans:
159
                rapi = GanetiRapiClient(*settings.GANETI_CLUSTER_INFO)
160
                rapi.DeleteInstance('%s%s' %
158
                vm = VirtualMachine.objects.get(pk=id)
159
                vm.client.DeleteInstance('%s%s' %
161 160
                                    (settings.BACKEND_PREFIX_ID, str(id)))
162 161
            print >> sys.stderr, "    ...done"
163 162

  
b/snf-cyclades-app/synnefo/logic/reconciliation.py
69 69

  
70 70
from synnefo.db.models import VirtualMachine
71 71
from synnefo.util.dictconfig import dictConfig
72
from synnefo.util.rapi import GanetiRapiClient
72
from synnefo.logic.backend import get_ganeti_instances
73 73

  
74 74

  
75 75
log = logging.getLogger()
......
108 108

  
109 109

  
110 110
def get_instances_from_ganeti():
111
    rapi = GanetiRapiClient(*settings.GANETI_CLUSTER_INFO)
112
    ganeti_instances = rapi.GetInstances(bulk=True)
111
    ganeti_instances = get_ganeti_instances(bulk=True)
113 112
    snf_instances = {}
114 113

  
115 114
    prefix = settings.BACKEND_PREFIX_ID

Also available in: Unified diff