2020-02-21 15:33:37 +00:00
|
|
|
import json
|
|
|
|
|
|
|
|
from xmlrpc.client import ServerProxy as RPCClient
|
2020-03-04 09:44:41 +00:00
|
|
|
from django_auth_ldap.backend import LDAPBackend
|
2020-02-25 06:50:49 +00:00
|
|
|
from django.core.management.base import BaseCommand
|
2020-08-01 16:38:38 +00:00
|
|
|
from django.conf import settings
|
2020-02-21 15:33:37 +00:00
|
|
|
from xmltodict import parse
|
|
|
|
|
|
|
|
from opennebula.models import VM as VMModel
|
|
|
|
|
2020-02-23 18:00:42 +00:00
|
|
|
|
2020-02-21 15:33:37 +00:00
|
|
|
class Command(BaseCommand):
|
|
|
|
help = 'Syncronize VM information from OpenNebula'
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2020-08-01 16:38:38 +00:00
|
|
|
with RPCClient(settings.OPENNEBULA_URL) as rpc_client:
|
2020-02-21 15:33:37 +00:00
|
|
|
success, response, *_ = rpc_client.one.vmpool.infoextended(
|
2020-08-01 16:38:38 +00:00
|
|
|
settings.OPENNEBULA_USER_PASS, -2, -1, -1, -1
|
2020-02-21 15:33:37 +00:00
|
|
|
)
|
|
|
|
if success:
|
|
|
|
vms = json.loads(json.dumps(parse(response)))['VM_POOL']['VM']
|
2020-02-26 10:31:17 +00:00
|
|
|
unknown_user = set()
|
|
|
|
|
|
|
|
backend = LDAPBackend()
|
2020-02-25 06:50:49 +00:00
|
|
|
|
2020-03-04 09:44:41 +00:00
|
|
|
for vm in vms:
|
2020-02-21 15:33:37 +00:00
|
|
|
vm_id = vm['ID']
|
2020-02-26 10:31:17 +00:00
|
|
|
vm_owner = vm['UNAME']
|
|
|
|
|
|
|
|
user = backend.populate_user(username=vm_owner)
|
2020-02-23 16:11:05 +00:00
|
|
|
|
2020-02-25 06:50:49 +00:00
|
|
|
if not user:
|
2020-02-26 10:31:17 +00:00
|
|
|
unknown_user.add(vm_owner)
|
2020-02-25 06:50:49 +00:00
|
|
|
else:
|
|
|
|
VMModel.objects.update_or_create(
|
2020-02-26 10:31:17 +00:00
|
|
|
vmid=vm_id,
|
|
|
|
defaults={'data': vm, 'owner': user}
|
2020-02-25 06:50:49 +00:00
|
|
|
)
|
2020-02-26 10:31:17 +00:00
|
|
|
print('User not found in ldap:', unknown_user)
|
2020-02-21 15:33:37 +00:00
|
|
|
else:
|
|
|
|
print(response)
|