Commit 8b5c4576 by Karsa Zoltán István

ci text processing

parent 6160c35e
...@@ -37,6 +37,8 @@ from django.urls import reverse ...@@ -37,6 +37,8 @@ from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ugettext_lazy as _, ugettext_noop from django.utils.translation import ugettext_lazy as _, ugettext_noop
from passlib.hash import sha512_crypt
from model_utils import Choices from model_utils import Choices
from model_utils.managers import QueryManager from model_utils.managers import QueryManager
from model_utils.models import TimeStampedModel, StatusModel from model_utils.models import TimeStampedModel, StatusModel
...@@ -67,6 +69,25 @@ ACCESS_PROTOCOLS = django.conf.settings.VM_ACCESS_PROTOCOLS ...@@ -67,6 +69,25 @@ ACCESS_PROTOCOLS = django.conf.settings.VM_ACCESS_PROTOCOLS
ACCESS_METHODS = [(key, name) for key, (name, port, transport) ACCESS_METHODS = [(key, name) for key, (name, port, transport)
in list(ACCESS_PROTOCOLS.items())] in list(ACCESS_PROTOCOLS.items())]
CI_META_DATA_DEF = """
instance-id: {{hostname}}
local-hostname: {{hostname}}
""".strip()
CI_USER_DATA_DEF = """
#cloud-config
users:
- default
- name: {{user}}
sudo: ['ALL=(ALL) NOPASSWD:ALL']
groups: sudo
shell: /bin/bash
ssh_pwauth: True
chpasswd: { expire: False }
lock-passwd: false
passwd: "{{password}}"
""".strip()
try: try:
# Python 2: "unicode" is built-in # Python 2: "unicode" is built-in
...@@ -117,9 +138,9 @@ class VirtualMachineDescModel(BaseResourceConfigModel): ...@@ -117,9 +138,9 @@ class VirtualMachineDescModel(BaseResourceConfigModel):
help_text=_( help_text=_(
'VM use cloud-init, set user- and meta-data below')) 'VM use cloud-init, set user- and meta-data below'))
ci_meta_data = TextField(verbose_name=_('ci_meta_data'), blank=True, help_text=_( ci_meta_data = TextField(verbose_name=_('ci_meta_data'), blank=True, help_text=_(
'When cloud-init is active, set meta-data (YAML format)'), default='empty') 'When cloud-init is active, set meta-data (YAML format)'), default=CI_META_DATA_DEF)
ci_user_data = TextField(verbose_name=_('ci_user_data'), blank=True, help_text=_( ci_user_data = TextField(verbose_name=_('ci_user_data'), blank=True, help_text=_(
'When cloud-init is active, set user-data (YAML format)'), default='empty') 'When cloud-init is active, set user-data (YAML format)'), default=CI_USER_DATA_DEF)
req_traits = ManyToManyField(Trait, blank=True, req_traits = ManyToManyField(Trait, blank=True,
help_text=_("A set of traits required for a " help_text=_("A set of traits required for a "
"node to declare to be suitable " "node to declare to be suitable "
...@@ -341,6 +362,19 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin, ...@@ -341,6 +362,19 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
DeprecationWarning) DeprecationWarning)
return self.status return self.status
@property
def get_user_data(self):
passwd = sha512_crypt.hash(self.pw)
user = 'cloud'
data = str(self.ci_user_data).replace('{{user}}', user)
data = data.replace('{{password}}', passwd)
return data
@property
def get_meta_data(self):
data = str(self.ci_user_data).replace('{{hostname}}', self.short_hostname)
return data
def _update_status(self): def _update_status(self):
"""Set the proper status of the instance to Instance.status. """Set the proper status of the instance to Instance.status.
""" """
......
...@@ -500,8 +500,8 @@ class DeployOperation(InstanceOperation): ...@@ -500,8 +500,8 @@ class DeployOperation(InstanceOperation):
def _operation(self, user, activity, name=None): def _operation(self, user, activity, name=None):
logger.info('create ci image') logger.info('create ci image')
disk = Disk.create_ci_disk(meta_data=self.instance.ci_meta_data, disk = Disk.create_ci_disk(meta_data=self.instance.get_meta_data,
user_data=self.instance.ci_user_data) user_data=self.instance.get_user_data)
disk.full_clean() disk.full_clean()
disk.save() disk.save()
self.instance.disks.add(disk) self.instance.disks.add(disk)
......
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