Commit 7e3037d9 by Karsa Zoltán István

variables

parent ee30f76a
......@@ -5,6 +5,7 @@ from django.contrib.auth.models import Group, User
from vm.models import Instance, InstanceTemplate, Lease, Interface, Node, InstanceActivity
from firewall.models import Vlan, Rule
from storage.models import Disk, StorageActivity
from vm.models.common import Variable
class RuleSerializer(serializers.ModelSerializer):
class Meta:
......@@ -60,6 +61,12 @@ class LeaseSerializer(serializers.ModelSerializer):
fields = [ 'id', 'name', 'suspend_interval_seconds', 'delete_interval_seconds']
class VariableSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Variable
fields = [ 'id', 'key', 'value', 'url']
class DiskSerializer(serializers.ModelSerializer):
class Meta:
model = Disk
......
......@@ -59,6 +59,7 @@
</div>
{{ form.cloud_init|as_crispy_field }}
{{ form.ci_meta_data|as_crispy_field }}
{{ form.ci_network_config|as_crispy_field }}
{{ form.ci_user_data|as_crispy_field }}
</fieldset>
<fieldset>
......
......@@ -68,6 +68,7 @@ from .views import (
EnableTwoFactorView, DisableTwoFactorView,
AclUserGroupAutocomplete, AclUserAutocomplete,
RescheduleView, GroupImportView, GroupExportView,
VariableREST, GetVariableREST,
)
from .views.node import node_ops, NodeREST, GetNodeREST
from .views.vm import vm_ops, vm_mass_ops
......@@ -83,6 +84,8 @@ urlpatterns = [
path('acpi/group/', GroupREST.as_view()),
path('acpi/group/<int:pk>/', GetGroupREST.as_view()),
path('acpi/vm/', InstanceREST.as_view()),
path('acpi/var/', VariableREST.as_view()),
path('acpi/var/<int:pk>/', GetVariableREST.as_view(), name='variable-detail'),
path('acpi/node/', NodeREST.as_view()),
path('acpi/node/<int:pk>/', GetNodeREST.as_view()),
path('acpi/vm/<int:pk>/', GetInstanceREST.as_view()),
......
......@@ -18,6 +18,7 @@
import json
import logging
from multiprocessing import context
import re
from collections import OrderedDict
from urllib.parse import urljoin
......@@ -56,9 +57,10 @@ from rest_framework.views import APIView
from rest_framework.parsers import JSONParser
from rest_framework.authentication import TokenAuthentication, BasicAuthentication
from rest_framework.permissions import IsAdminUser
from vm.models.common import Variable
from storage.models import StorageActivity
from dashboard.serializers import InstanceActivitySerializer, StorageActivitySerializer
from dashboard.serializers import InstanceActivitySerializer, StorageActivitySerializer, VariableSerializer
from common.models import HumanReadableException, HumanReadableObject
from ..models import GroupProfile, Profile
......@@ -107,6 +109,40 @@ class GetStorageActivityREST(APIView):
return JsonResponse(serializer.data, safe=False)
class VariableREST(APIView):
authentication_classes = [TokenAuthentication,BasicAuthentication]
permission_classes = [IsAdminUser]
def get(self, request, format=None):
if request.query_params.get('key'):
try:
template = Variable.objects.filter(key__istartswith=request.query_params.get('key')).get()
serializer = VariableSerializer(template, many=False, context={'request': request})
return JsonResponse(serializer.data, safe=False)
except:
return JsonResponse({}, status=404)
templates = Variable.objects.all()
serializer = VariableSerializer(templates, many=True, context={'request': request})
return JsonResponse(serializer.data, safe=False)
def post(self, request, format=None):
data = JSONParser().parse(request)
serializer = VariableSerializer(data=data)
if serializer.is_valid():
serializer.save()
return JsonResponse(serializer.data, status=201)
return JsonResponse(serializer.errors, status=400)
class GetVariableREST(APIView):
authentication_classes = [TokenAuthentication,BasicAuthentication]
permission_classes = [IsAdminUser]
def get(self, request, pk, format=None):
act = Variable.objects.get(pk=pk)
serializer = VariableSerializer(act, many=False, context={'request': request})
return JsonResponse(serializer.data, safe=False)
class RedirectToLoginMixin(AccessMixin):
redirect_exception_classes = (PermissionDenied, )
......
......@@ -17,6 +17,8 @@
from django.contrib import admin
from .models.common import Variable
from .models import (Instance, InstanceActivity, InstanceTemplate, Interface,
InterfaceTemplate, Lease, NamedBaseResourceConfig, Node,
NodeActivity, Trait)
......@@ -36,3 +38,4 @@ admin.site.register(NamedBaseResourceConfig)
admin.site.register(Node)
admin.site.register(NodeActivity)
admin.site.register(Trait)
admin.site.register(Variable)
# Generated by Django 3.2.3 on 2022-10-10 14:42
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('vm', '0013_auto_20221006_1509'),
]
operations = [
migrations.CreateModel(
name='Variable',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('key', models.CharField(max_length=100, unique=True, verbose_name='variable name')),
('value', models.CharField(max_length=150, verbose_name='variable value')),
],
),
migrations.AlterField(
model_name='instance',
name='ci_network_config',
field=models.TextField(blank=True, default='network:\n version: 2\n ethernets:\n ens3:\n match:\n macaddress: {{ net.mac }}\n addresses: \n - {{ net.ipv4 }}/24\n gateway4: ipv4\n nameservers:\n addresses:\n - 8.8.8.8', help_text='When cloud-init is active, set network-config(YAML format)', verbose_name='CI Network Data'),
),
migrations.AlterField(
model_name='instance',
name='ci_user_data',
field=models.TextField(blank=True, default="#cloud-config\n\nssh_pwauth: 1\n\nusers:\n - name: {{ sysuser }} \n sudo: ['ALL=(ALL) NOPASSWD:ALL']\n groups: sudo\n shell: /bin/bash\n chpasswd: { expire: False }\n lock-passwd: false\nchpasswd:\n list: |\n {{ sysuser }}:{{ password }}\n expire: False", help_text='When cloud-init is active, set user-data (YAML format)', verbose_name='CI User Data'),
),
migrations.AlterField(
model_name='instancetemplate',
name='ci_network_config',
field=models.TextField(blank=True, default='network:\n version: 2\n ethernets:\n ens3:\n match:\n macaddress: {{ net.mac }}\n addresses: \n - {{ net.ipv4 }}/24\n gateway4: ipv4\n nameservers:\n addresses:\n - 8.8.8.8', help_text='When cloud-init is active, set network-config(YAML format)', verbose_name='CI Network Data'),
),
migrations.AlterField(
model_name='instancetemplate',
name='ci_user_data',
field=models.TextField(blank=True, default="#cloud-config\n\nssh_pwauth: 1\n\nusers:\n - name: {{ sysuser }} \n sudo: ['ALL=(ALL) NOPASSWD:ALL']\n groups: sudo\n shell: /bin/bash\n chpasswd: { expire: False }\n lock-passwd: false\nchpasswd:\n list: |\n {{ sysuser }}:{{ password }}\n expire: False", help_text='When cloud-init is active, set user-data (YAML format)', verbose_name='CI User Data'),
),
]
......@@ -33,6 +33,14 @@ ARCHITECTURES = (('x86_64', 'x86-64 (64 bit)'),
('i686', 'x86 (32 bit)'))
class Variable(Model):
key = CharField(verbose_name='variable name', unique=True, max_length=100)
value = CharField(verbose_name='variable value', max_length=150)
def __str__(self):
return self.key
class BaseResourceConfigModel(Model):
"""Abstract base for models with base resource configuration parameters.
......
......@@ -61,7 +61,7 @@ from common.models import (
from common.operations import OperatedMixin
from ..tasks import agent_tasks
from .activity import (ActivityInProgressError, InstanceActivity)
from .common import BaseResourceConfigModel, Lease
from .common import BaseResourceConfigModel, Lease, Variable
from .network import Interface
from .node import Node, Trait
......@@ -109,9 +109,9 @@ network:
ethernets:
ens3:
match:
macaddress: 'mac'
macaddress: {{ net.mac }}
addresses:
- ipv4/24
- {{ net.ipv4 }}/24
gateway4: ipv4
nameservers:
addresses:
......@@ -323,9 +323,7 @@ class NetTemplate:
self.vlans = list(NetTemplate.Host(net) for net in instance.interface_set.all() if net.host)
self.ipv4 = str(instance.ipv4)
self.ipv6 = str(instance.ipv6)
self.mac = str(instance.mac).lower()
self.mac = str(instance.mac)
class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
......@@ -447,6 +445,10 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
return self.status
def get_ci_data_dict(self):
list = Variable.objects.all()
vars = { }
for var in list:
vars[var.key] = var.value
datas = {
"sysuser": "cloud",
"hostname": self.short_hostname,
......@@ -456,6 +458,7 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
"acl": AclTemplate(self),
"ssh": SSHKeyTemplate(self),
"ci": CITemplate(),
"variables": vars,
}
return datas
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment