forked from uncloud/uncloud
49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
import json
|
|
|
|
import uncloud.secrets as secrets
|
|
|
|
|
|
from xmlrpc.client import ServerProxy as RPCClient
|
|
|
|
from django.core.management.base import BaseCommand
|
|
from django.contrib.auth import get_user_model
|
|
from xmltodict import parse
|
|
|
|
from opennebula.models import VM as VMModel
|
|
|
|
from django_auth_ldap.backend import LDAPBackend
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Syncronize VM information from OpenNebula'
|
|
|
|
def add_arguments(self, parser):
|
|
pass
|
|
|
|
def handle(self, *args, **options):
|
|
with RPCClient(secrets.OPENNEBULA_URL) as rpc_client:
|
|
success, response, *_ = rpc_client.one.vmpool.infoextended(
|
|
secrets.OPENNEBULA_USER_PASS, -2, -1, -1, -1
|
|
)
|
|
if success:
|
|
vms = json.loads(json.dumps(parse(response)))['VM_POOL']['VM']
|
|
unknown_user = set()
|
|
|
|
backend = LDAPBackend()
|
|
|
|
for vm in vms:
|
|
vm_id = vm['ID']
|
|
vm_owner = vm['UNAME']
|
|
|
|
user = backend.populate_user(username=vm_owner)
|
|
|
|
if not user:
|
|
unknown_user.add(vm_owner)
|
|
else:
|
|
VMModel.objects.update_or_create(
|
|
vmid=vm_id,
|
|
defaults={'data': vm, 'owner': user}
|
|
)
|
|
print('User not found in ldap:', unknown_user)
|
|
else:
|
|
print(response)
|