Commit 9e4ea7d9 by Karsa Zoltán István

iac api for hotplug

parent 4c1a75d2
...@@ -109,7 +109,7 @@ class InstanceSerializer(serializers.ModelSerializer): ...@@ -109,7 +109,7 @@ class InstanceSerializer(serializers.ModelSerializer):
model = Instance model = Instance
fields = ['id', 'name', 'description', 'status', 'owner', 'access_method', 'boot_menu', 'pw', 'is_base', 'macaddr', fields = ['id', 'name', 'description', 'status', 'owner', 'access_method', 'boot_menu', 'pw', 'is_base', 'macaddr',
'lease', 'raw_data', 'cloud_init', 'ci_meta_data', 'ci_user_data', 'ci_network_config', 'system', 'req_traits', 'interface_set', 'lease', 'raw_data', 'cloud_init', 'ci_meta_data', 'ci_user_data', 'ci_network_config', 'system', 'req_traits', 'interface_set',
'has_agent', 'num_cores', 'ram_size', 'max_ram_size', 'arch', 'priority', 'disks', 'node', 'ipv4addr', 'ipv6addr', 'vlans', 'has_agent', 'num_cores', 'num_cores_max', 'ram_size', 'max_ram_size', 'arch', 'priority', 'disks', 'node', 'ipv4addr', 'ipv6addr', 'vlans',
'hookurl', 'sshportipv4', 'hostipv4'] 'hookurl', 'sshportipv4', 'hostipv4']
extra_kwargs = { extra_kwargs = {
'disks': {'required': False, 'allow_empty': True,}, 'disks': {'required': False, 'allow_empty': True,},
...@@ -122,6 +122,7 @@ class InstanceSerializer(serializers.ModelSerializer): ...@@ -122,6 +122,7 @@ class InstanceSerializer(serializers.ModelSerializer):
'raw_data': {'required': False, }, 'raw_data': {'required': False, },
'sshportipv4': {'required': False, }, 'sshportipv4': {'required': False, },
'hostipv4': {'required': False, }, 'hostipv4': {'required': False, },
'num_cores_max': {'required': False, },
} }
......
...@@ -227,6 +227,8 @@ class InstanceREST(APIView): ...@@ -227,6 +227,8 @@ class InstanceREST(APIView):
data['status'] = 'STOPPED' data['status'] = 'STOPPED'
if 'owner' not in data: if 'owner' not in data:
data['owner'] = request.user.id data['owner'] = request.user.id
if 'num_cores_max' not in data:
data['num_cores_max'] = data['num_cores']
serializer = InstanceSerializer(data=data) serializer = InstanceSerializer(data=data)
if serializer.is_valid(): if serializer.is_valid():
inst = serializer.save() inst = serializer.save()
......
# Generated by Django 3.2.3 on 2022-12-27 19:39
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('vm', '0014_auto_20221010_1442'),
]
operations = [
migrations.AddField(
model_name='instance',
name='num_cores_max',
field=models.IntegerField(default=2, help_text='Number of max virtual CPU cores available to the virtual machine.', validators=[django.core.validators.MinValueValidator(0)], verbose_name='number of cores'),
),
migrations.AddField(
model_name='instancetemplate',
name='num_cores_max',
field=models.IntegerField(default=2, help_text='Number of max virtual CPU cores available to the virtual machine.', validators=[django.core.validators.MinValueValidator(0)], verbose_name='number of cores'),
),
migrations.AddField(
model_name='namedbaseresourceconfig',
name='num_cores_max',
field=models.IntegerField(default=2, help_text='Number of max virtual CPU cores available to the virtual machine.', validators=[django.core.validators.MinValueValidator(0)], verbose_name='number of cores'),
),
migrations.AlterField(
model_name='instance',
name='ci_network_config',
field=models.TextField(blank=True, default="version: 2\nethernets:\n ens3:\n match:\n macaddress: '{{ net.mac }}'\n addresses: \n - {{ net.ipv4 }}/{{ net.mask4 }}\n gateway4: {{ net.gateway4 }}\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_network_config',
field=models.TextField(blank=True, default="version: 2\nethernets:\n ens3:\n match:\n macaddress: '{{ net.mac }}'\n addresses: \n - {{ net.ipv4 }}/{{ net.mask4 }}\n gateway4: {{ net.gateway4 }}\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'),
),
]
...@@ -49,6 +49,10 @@ class BaseResourceConfigModel(Model): ...@@ -49,6 +49,10 @@ class BaseResourceConfigModel(Model):
help_text=_('Number of virtual CPU cores ' help_text=_('Number of virtual CPU cores '
'available to the virtual machine.'), 'available to the virtual machine.'),
validators=[MinValueValidator(0)]) validators=[MinValueValidator(0)])
num_cores_max = IntegerField(verbose_name=_('number of cores'),
help_text=_('Number of max virtual CPU cores '
'available to the virtual machine.'),
validators=[MinValueValidator(0)], default=2)
ram_size = IntegerField(verbose_name=_('RAM size'), ram_size = IntegerField(verbose_name=_('RAM size'),
help_text=_('Mebibytes of memory.'), help_text=_('Mebibytes of memory.'),
validators=[MinValueValidator(0)]) validators=[MinValueValidator(0)])
......
...@@ -545,10 +545,11 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin, ...@@ -545,10 +545,11 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
for network in networks: for network in networks:
if not network.vlan.has_level(params['owner'], 'user'): if not network.vlan.has_level(params['owner'], 'user'):
raise PermissionDenied() raise PermissionDenied()
# create instance and do additional setup # create instance and do additional setup
inst = cls(**params) inst = cls(**params)
if not params["num_cores_max"]:
inst.num_cores_max = inst.num_cores
# save instance # save instance
inst.full_clean() inst.full_clean()
inst.save() inst.save()
...@@ -606,7 +607,7 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin, ...@@ -606,7 +607,7 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
tags = template.tags.all() if tags is None else tags tags = template.tags.all() if tags is None else tags
# prepare parameters # prepare parameters
common_fields = ['name', 'description', 'num_cores', 'ram_size', common_fields = ['name', 'description', 'num_cores', 'num_cores_max','ram_size',
'max_ram_size', 'arch', 'priority', 'boot_menu', 'max_ram_size', 'arch', 'priority', 'boot_menu',
'raw_data', 'lease', 'access_method', 'system', 'raw_data', 'lease', 'access_method', 'system',
'cloud_init', 'ci_meta_data', 'ci_user_data', 'ci_network_config', 'has_agent'] 'cloud_init', 'ci_meta_data', 'ci_user_data', 'ci_network_config', 'has_agent']
...@@ -851,6 +852,7 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin, ...@@ -851,6 +852,7 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
return { return {
'name': self.vm_name, 'name': self.vm_name,
'vcpu': self.num_cores, 'vcpu': self.num_cores,
'vcpu_max': self.num_cores_max,
'memory': int(self.ram_size) * 1024, # convert from MiB to KiB 'memory': int(self.ram_size) * 1024, # convert from MiB to KiB
'memory_max': int(self.max_ram_size) * 1024, # convert MiB to KiB 'memory_max': int(self.max_ram_size) * 1024, # convert MiB to KiB
'cpu_share': self.priority, 'cpu_share': self.priority,
......
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