fix/simplify syncvm

This commit is contained in:
Nico Schottelius 2020-02-25 19:23:39 +01:00
commit 446c13b77c
11 changed files with 57 additions and 320 deletions

View file

@ -8,15 +8,10 @@ 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 ungleich_common.ldap.ldap_manager import LdapManager
from opennebula.models import VM as VMModel
def find_user_based_on_email(users, email):
for user in users:
if email in user.mail.values:
return user
from django_auth_ldap.backend import LDAPBackend
class Command(BaseCommand):
@ -26,39 +21,29 @@ class Command(BaseCommand):
pass
def handle(self, *args, **options):
ldap_server_uri = secrets.LDAP_SERVER_URI.split(',')[0]
ldap_manager = LdapManager(
server=ldap_server_uri,
admin_dn=secrets.LDAP_ADMIN_DN,
admin_password=secrets.LDAP_ADMIN_PASSWORD,
)
users = ldap_manager.get('') # Get all users
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_with_email = set()
unknown_user = set()
backend = LDAPBackend()
for vm in vms:
vm_id = vm['ID']
vm_owner_email = vm['UNAME']
vm_owner = vm['UNAME']
user = backend.populate_user(username=vm_owner)
user = find_user_based_on_email(users, vm_owner_email)
if not user:
unknown_user_with_email.add(vm_owner_email)
unknown_user.add(vm_owner)
else:
try:
user_in_db = get_user_model().objects.get(email=vm_owner_email)
except get_user_model().DoesNotExist:
user_in_db = get_user_model().objects.create_user(username=user.uid, email=vm_owner_email)
VMModel.objects.update_or_create(
id=f'opennebula{vm_id}',
defaults={'data': vm, 'owner': user_in_db}
vmid=vm_id,
defaults={'data': vm, 'owner': user}
)
print('User with email but not found in ldap:', unknown_user_with_email)
print('User not found in ldap:', unknown_user)
else:
print(response)
print(secrets.OPENNEBULA_USER_PASS)

View file

@ -0,0 +1,23 @@
# Generated by Django 3.0.3 on 2020-02-25 18:16
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('opennebula', '0003_auto_20200225_1428'),
]
operations = [
migrations.RemoveField(
model_name='vm',
name='id',
),
migrations.AddField(
model_name='vm',
name='vmid',
field=models.IntegerField(default=42, primary_key=True, serialize=False),
preserve_default=False,
),
]

View file

@ -5,7 +5,7 @@ from django.contrib.postgres.fields import JSONField
class VM(models.Model):
id = models.CharField(primary_key=True, editable=True, default=uuid.uuid4, unique=True, max_length=64)
vmid = models.IntegerField(primary_key=True)
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
data = JSONField()
@ -34,7 +34,6 @@ class VM(models.Model):
disks = []
if 'DISK' in self.data['TEMPLATE']:
if type(self.data['TEMPLATE']['DISK']) is dict:
disks = [ self.data['TEMPLATE']['DISK'] ]
else:

View file

@ -3,5 +3,4 @@ djangorestframework
django-auth-ldap
stripe
xmltodict
git+https://code.ungleich.ch/ahmedbilal/ungleich-common/#egg=ungleich-common-ldap&subdirectory=ldap
psycopg2

View file

@ -18,7 +18,7 @@ import ldap
# Uncommitted file with secrets
import uncloud.secrets
from django_auth_ldap.config import LDAPSearch
from django_auth_ldap.config import LDAPSearch, LDAPSearchUnion
# Uncommitted file with local settings i.e logging
try:
@ -129,9 +129,7 @@ AUTH_LDAP_USER_ATTR_MAP = {
AUTH_LDAP_BIND_DN = uncloud.secrets.LDAP_ADMIN_DN
AUTH_LDAP_BIND_PASSWORD = uncloud.secrets.LDAP_ADMIN_PASSWORD
AUTH_LDAP_USER_SEARCH = LDAPSearch(
"dc=ungleich,dc=ch", ldap.SCOPE_SUBTREE, "(uid=%(user)s)"
)
AUTH_LDAP_USER_SEARCH = LDAPSearch("dc=ungleich,dc=ch", ldap.SCOPE_SUBTREE, "(uid=%(user)s)")
################################################################################
@ -174,12 +172,4 @@ USE_TZ = True
STATIC_URL = '/static/'
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': uncloud.secrets.POSTGRESQL_DB_NAME,
}
}
stripe.api_key = uncloud.secrets.STRIPE_KEY

View file

@ -0,0 +1,19 @@
# Generated by Django 3.0.3 on 2020-02-25 18:16
from django.db import migrations, models
import uuid
class Migration(migrations.Migration):
dependencies = [
('uncloud_api', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='vmsnapshotproduct',
name='vm_uuid',
field=models.UUIDField(default=uuid.uuid4, editable=False),
),
]

View file

@ -72,7 +72,7 @@ class VMSnapshotProduct(Product):
gb_ssd = models.FloatField()
gb_hdd = models.FloatField()
vm_uuid = models.UUIDField()
vm_uuid = models.UUIDField(default=uuid.uuid4, editable=False)
# Need to setup recurring_price and one_time_price and recurring period