2025-08-27 09:55:55 +02:00
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
from django.contrib import admin
|
|
|
|
|
from django.db.models import Q
|
|
|
|
|
|
|
|
|
|
from lib.decorators import readonly
|
|
|
|
|
from lib.proxmox import Proxmox
|
|
|
|
|
from proxmox.models import Lxc, LxcTemplate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@readonly
|
|
|
|
|
def sync_all_lxc_templates_from_proxmox(*args, **kwargs):
|
|
|
|
|
pm = Proxmox()
|
|
|
|
|
for storage in pm.storage_get(enabled=1):
|
|
|
|
|
logging.debug(f'Syncing Templates from storage {storage["storage"]}')
|
|
|
|
|
storage_name = storage['storage']
|
|
|
|
|
for tmpl in pm.storage(f'{storage_name}/content').get(content='vztmpl'):
|
|
|
|
|
try:
|
|
|
|
|
logging.debug(f'Updating {tmpl["volid"]}')
|
|
|
|
|
template = LxcTemplate.objects.get(volid=tmpl['volid'])
|
|
|
|
|
except LxcTemplate.DoesNotExist:
|
|
|
|
|
logging.debug(f'Fail - Creating {tmpl["volid"]}')
|
|
|
|
|
template = LxcTemplate.objects.create(volid=tmpl['volid'])
|
|
|
|
|
|
|
|
|
|
template.write(**tmpl)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@readonly
|
|
|
|
|
def sync_all_lxc_from_proxmox(*args, **kwargs):
|
|
|
|
|
from lib.proxmox import Proxmox
|
|
|
|
|
pm = Proxmox()
|
|
|
|
|
existing_vms = []
|
|
|
|
|
for lxc_data in pm.get_all_lxc(as_dict=True, **kwargs):
|
|
|
|
|
vmid = lxc_data.pop('vmid')
|
|
|
|
|
try:
|
|
|
|
|
lx: Lxc = Lxc.objects.get(vmid=vmid)
|
|
|
|
|
logging.info(f'Updating {vmid}')
|
|
|
|
|
except Lxc.DoesNotExist:
|
|
|
|
|
logging.info(f'Creating {vmid}')
|
|
|
|
|
lx = Lxc.objects.create(**{'vmid': vmid})
|
|
|
|
|
|
|
|
|
|
lx.from_proxmox(**lxc_data)
|
|
|
|
|
|
|
|
|
|
existing_vms.append(vmid)
|
|
|
|
|
|
|
|
|
|
to_delete = Lxc.objects.filter(~Q(vmid__in=existing_vms))
|
|
|
|
|
if to_delete:
|
|
|
|
|
logging.info(f'Deleting {[d.vmid for d in to_delete]}')
|
|
|
|
|
to_delete.delete()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sync_all_lxc_from_proxmox.short_description = 'Sync all LXC from Proxmox'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(Lxc)
|
|
|
|
|
class LxcAdmin(admin.ModelAdmin):
|
|
|
|
|
actions = [sync_all_lxc_from_proxmox, 'sync_selected_from_proxmox']
|
|
|
|
|
search_fields = ('name', 'vmid', 'hostname', 'hwaddr')
|
|
|
|
|
list_display = ('name', 'vmid', 'hwaddr', 'disksize', 'memory', 'cpus', 'status')
|
|
|
|
|
list_filter = ('status', 'disksize',)
|
|
|
|
|
|
|
|
|
|
def get_readonly_fields(self, request, obj=None):
|
|
|
|
|
if obj:
|
|
|
|
|
return [k.name for k in obj._meta.fields if
|
|
|
|
|
k.name not in ['name', 'cores', 'hwaddr', 'size', 'cpus', 'memory', 'description', 'hostname']]
|
|
|
|
|
return self.readonly_fields
|
|
|
|
|
|
|
|
|
|
@admin.action(description='Sync selected from proxmox')
|
|
|
|
|
@readonly
|
|
|
|
|
def sync_selected_from_proxmox(self, request, queryset):
|
|
|
|
|
for lx in queryset:
|
|
|
|
|
lx.sync_from_proxmox()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(LxcTemplate)
|
|
|
|
|
class LxcTemplateAdmin(admin.ModelAdmin):
|
|
|
|
|
actions = [sync_all_lxc_templates_from_proxmox]
|
|
|
|
|
search_fields = ('volid',)
|
2025-08-27 10:13:53 +02:00
|
|
|
list_display = ('volid', 'human_size', 'content', 'is_default_template')
|
|
|
|
|
list_filter = ('is_default_template',)
|